perl5ever has asked for the wisdom of the Perl Monks concerning the following question:

Update: modified example in #3 to make it non-destructive of its arguments.

I'm writing an "SQL fragment" class which helps with building SQL that uses place holders. Typical usage would be:

my $stmt = new SQL::Fragment("SELECT *, PRICE+? FROM ITEMS", $fees); my @conds = (); push(@conds, new SQL::Fragment("color = ?", "blue")); push(@conds, [ "size = ?", "XXL" ]); # can also just use array refs if (@conds) { $stmt->append(" WHERE ")->append_join(" AND ", @conds); } my ($sql, @params) = $stmt->sql_and_params;
So here's what I'm looking for:

1. An existing CPAN module which does this.

2. Help with naming some of the following methods:

sub append - append another SQL Fragment to $self sub append_join - append a join-ed list of SQL Fragments to $self sub append_join_parens - do the equivalent of: join( $sep, map { "($_)" } @list ) For instance, this is useful to ensure each condition is in it's own s +et of parens.

However, I'm a little unhappy with the name append_join_parens - it's a little long and very specific. Any ideas on a better name (or protocol)? Maybe I should drop the append_ from append_join and append_join_parens - joining would implicitly imply appending.

3. I'm sure someone will want the even more general append_join_map method. In that case, append_join_parens could be written:

sub append_join_parens { my $self = shift; $self->append_join_map( sub { [ "(".$_->sql.")", $_->params ] }); $self; }
Any ideas for improvements?

4. A better name for this class?

Replies are listed 'Best First'.
Re: need help with method names
by perrin (Chancellor) on Mar 23, 2009 at 21:14 UTC
    Here are a few that I like: SQL::Abstract (no joins last I looked), SQL::Interpolate (can do anything, but a bit confusing), Rose::DB::Object::QueryBuilder (can do most things and has a nice interface). Incidentally, there's no need to use Rose::DB::Object just to use the QueryBuilder. I've used it on its own.
Re: need help with method names
by EvanCarroll (Chaplain) on Mar 23, 2009 at 21:37 UTC