1 package SQL::Abstract; # see doc at end of file
3 # LDNOTE : this code is heavy refactoring from original SQLA.
4 # Several design decisions will need discussion during
5 # the test / diffusion / acceptance phase; those are marked with flag
6 # 'LDNOTE' (note by laurent.dami AT free.fr)
11 use List::Util qw/first/;
12 use Scalar::Util qw/blessed/;
14 #======================================================================
16 #======================================================================
18 our $VERSION = '1.61';
20 # This would confuse some packagers
21 #$VERSION = eval $VERSION; # numify for warning-free dev releases
25 # special operators (-in, -between). May be extended/overridden by user.
26 # See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation
27 my @BUILTIN_SPECIAL_OPS = (
28 {regex => qr/^(not )?between$/i, handler => '_where_field_BETWEEN'},
29 {regex => qr/^(not )?in$/i, handler => '_where_field_IN'},
32 # unaryish operators - key maps to handler
33 my @BUILTIN_UNARY_OPS = (
34 # the digits are backcompat stuff
35 { regex => qr/^and (?: \s? \d+ )? $/xi, handler => '_where_op_ANDOR' },
36 { regex => qr/^or (?: \s? \d+ )? $/xi, handler => '_where_op_ANDOR' },
37 { regex => qr/^nest (?: \s? \d+ )? $/xi, handler => '_where_op_NEST' },
38 { regex => qr/^ (?: not \s )? bool $/xi, handler => '_where_op_BOOL' },
41 #======================================================================
42 # DEBUGGING AND ERROR REPORTING
43 #======================================================================
46 return unless $_[0]->{debug}; shift; # a little faster
47 my $func = (caller(1))[3];
48 warn "[$func] ", @_, "\n";
52 my($func) = (caller(1))[3];
53 carp "[$func] Warning: ", @_;
57 my($func) = (caller(1))[3];
58 croak "[$func] Fatal: ", @_;
62 #======================================================================
64 #======================================================================
68 my $class = ref($self) || $self;
69 my %opt = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
71 # choose our case by keeping an option around
72 delete $opt{case} if $opt{case} && $opt{case} ne 'lower';
74 # default logic for interpreting arrayrefs
75 $opt{logic} = $opt{logic} ? uc $opt{logic} : 'OR';
77 # how to return bind vars
78 # LDNOTE: changed nwiger code : why this 'delete' ??
79 # $opt{bindtype} ||= delete($opt{bind_type}) || 'normal';
80 $opt{bindtype} ||= 'normal';
82 # default comparison is "=", but can be overridden
85 # generic SQL comparison operators
86 my $anchored_cmp_ops = join ('|', map { '^' . $_ . '$' } (
87 '(?:is \s+)? (?:not \s+)? like',
89 (map { quotemeta($_) } (qw/ < > != <> = <= >= /) ),
91 $opt{cmp_ops} = qr/$anchored_cmp_ops/ix;
93 # try to recognize which are the 'equality' and 'unequality' ops
94 # (temporary quickfix, should go through a more seasoned API)
95 $opt{equality_op} = qr/^(\Q$opt{cmp}\E|is|(is\s+)?like)$/i;
96 $opt{inequality_op} = qr/^(!=|<>|(is\s+)?not(\s+like)?)$/i;
99 $opt{sqltrue} ||= '1=1';
100 $opt{sqlfalse} ||= '0=1';
103 $opt{special_ops} ||= [];
104 push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS;
107 $opt{unary_ops} ||= [];
108 push @{$opt{unary_ops}}, @BUILTIN_UNARY_OPS;
110 return bless \%opt, $class;
115 #======================================================================
117 #======================================================================
121 my $table = $self->_table(shift);
122 my $data = shift || return;
125 my $method = $self->_METHOD_FOR_refkind("_insert", $data);
126 my ($sql, @bind) = $self->$method($data);
127 $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
129 if (my $ret = $options->{returning}) {
130 $sql .= $self->_insert_returning ($ret);
133 return wantarray ? ($sql, @bind) : $sql;
136 sub _insert_returning {
137 my ($self, $fields) = @_;
139 my $f = $self->_SWITCH_refkind($fields, {
140 ARRAYREF => sub {join ', ', map { $self->_quote($_) } @$fields;},
141 SCALAR => sub {$self->_quote($fields)},
142 SCALARREF => sub {$$fields},
144 return join (' ', $self->_sqlcase(' returning'), $f);
147 sub _insert_HASHREF { # explicit list of fields and then values
148 my ($self, $data) = @_;
150 my @fields = sort keys %$data;
152 my ($sql, @bind) = $self->_insert_values($data);
155 $_ = $self->_quote($_) foreach @fields;
156 $sql = "( ".join(", ", @fields).") ".$sql;
158 return ($sql, @bind);
161 sub _insert_ARRAYREF { # just generate values(?,?) part (no list of fields)
162 my ($self, $data) = @_;
164 # no names (arrayref) so can't generate bindtype
165 $self->{bindtype} ne 'columns'
166 or belch "can't do 'columns' bindtype when called with arrayref";
168 # fold the list of values into a hash of column name - value pairs
169 # (where the column names are artificially generated, and their
170 # lexicographical ordering keep the ordering of the original list)
171 my $i = "a"; # incremented values will be in lexicographical order
172 my $data_in_hash = { map { ($i++ => $_) } @$data };
174 return $self->_insert_values($data_in_hash);
177 sub _insert_ARRAYREFREF { # literal SQL with bind
178 my ($self, $data) = @_;
180 my ($sql, @bind) = @${$data};
181 $self->_assert_bindval_matches_bindtype(@bind);
183 return ($sql, @bind);
187 sub _insert_SCALARREF { # literal SQL without bind
188 my ($self, $data) = @_;
194 my ($self, $data) = @_;
196 my (@values, @all_bind);
197 foreach my $column (sort keys %$data) {
198 my $v = $data->{$column};
200 $self->_SWITCH_refkind($v, {
203 if ($self->{array_datatypes}) { # if array datatype are activated
205 push @all_bind, $self->_bindtype($column, $v);
207 else { # else literal SQL with bind
208 my ($sql, @bind) = @$v;
209 $self->_assert_bindval_matches_bindtype(@bind);
211 push @all_bind, @bind;
215 ARRAYREFREF => sub { # literal SQL with bind
216 my ($sql, @bind) = @${$v};
217 $self->_assert_bindval_matches_bindtype(@bind);
219 push @all_bind, @bind;
222 # THINK : anything useful to do with a HASHREF ?
223 HASHREF => sub { # (nothing, but old SQLA passed it through)
224 #TODO in SQLA >= 2.0 it will die instead
225 belch "HASH ref as bind value in insert is not supported";
227 push @all_bind, $self->_bindtype($column, $v);
230 SCALARREF => sub { # literal SQL without bind
234 SCALAR_or_UNDEF => sub {
236 push @all_bind, $self->_bindtype($column, $v);
243 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
244 return ($sql, @all_bind);
249 #======================================================================
251 #======================================================================
256 my $table = $self->_table(shift);
257 my $data = shift || return;
260 # first build the 'SET' part of the sql statement
261 my (@set, @all_bind);
262 puke "Unsupported data type specified to \$sql->update"
263 unless ref $data eq 'HASH';
265 for my $k (sort keys %$data) {
268 my $label = $self->_quote($k);
270 $self->_SWITCH_refkind($v, {
272 if ($self->{array_datatypes}) { # array datatype
273 push @set, "$label = ?";
274 push @all_bind, $self->_bindtype($k, $v);
276 else { # literal SQL with bind
277 my ($sql, @bind) = @$v;
278 $self->_assert_bindval_matches_bindtype(@bind);
279 push @set, "$label = $sql";
280 push @all_bind, @bind;
283 ARRAYREFREF => sub { # literal SQL with bind
284 my ($sql, @bind) = @${$v};
285 $self->_assert_bindval_matches_bindtype(@bind);
286 push @set, "$label = $sql";
287 push @all_bind, @bind;
289 SCALARREF => sub { # literal SQL without bind
290 push @set, "$label = $$v";
292 SCALAR_or_UNDEF => sub {
293 push @set, "$label = ?";
294 push @all_bind, $self->_bindtype($k, $v);
300 my $sql = $self->_sqlcase('update') . " $table " . $self->_sqlcase('set ')
304 my($where_sql, @where_bind) = $self->where($where);
306 push @all_bind, @where_bind;
309 return wantarray ? ($sql, @all_bind) : $sql;
315 #======================================================================
317 #======================================================================
322 my $table = $self->_table(shift);
323 my $fields = shift || '*';
327 my($where_sql, @bind) = $self->where($where, $order);
329 my $f = (ref $fields eq 'ARRAY') ? join ', ', map { $self->_quote($_) } @$fields
331 my $sql = join(' ', $self->_sqlcase('select'), $f,
332 $self->_sqlcase('from'), $table)
335 return wantarray ? ($sql, @bind) : $sql;
338 #======================================================================
340 #======================================================================
345 my $table = $self->_table(shift);
349 my($where_sql, @bind) = $self->where($where);
350 my $sql = $self->_sqlcase('delete from') . " $table" . $where_sql;
352 return wantarray ? ($sql, @bind) : $sql;
356 #======================================================================
358 #======================================================================
362 # Finally, a separate routine just to handle WHERE clauses
364 my ($self, $where, $order) = @_;
367 my ($sql, @bind) = $self->_recurse_where($where);
368 $sql = $sql ? $self->_sqlcase(' where ') . "( $sql )" : '';
372 $sql .= $self->_order_by($order);
375 return wantarray ? ($sql, @bind) : $sql;
380 my ($self, $where, $logic) = @_;
382 # dispatch on appropriate method according to refkind of $where
383 my $method = $self->_METHOD_FOR_refkind("_where", $where);
386 my ($sql, @bind) = $self->$method($where, $logic);
388 # DBIx::Class directly calls _recurse_where in scalar context, so
389 # we must implement it, even if not in the official API
390 return wantarray ? ($sql, @bind) : $sql;
395 #======================================================================
396 # WHERE: top-level ARRAYREF
397 #======================================================================
400 sub _where_ARRAYREF {
401 my ($self, $where, $logic) = @_;
403 $logic = uc($logic || $self->{logic});
404 $logic eq 'AND' or $logic eq 'OR' or puke "unknown logic: $logic";
406 my @clauses = @$where;
408 my (@sql_clauses, @all_bind);
409 # need to use while() so can shift() for pairs
410 while (my $el = shift @clauses) {
412 # switch according to kind of $el and get corresponding ($sql, @bind)
413 my ($sql, @bind) = $self->_SWITCH_refkind($el, {
415 # skip empty elements, otherwise get invalid trailing AND stuff
416 ARRAYREF => sub {$self->_recurse_where($el) if @$el},
418 ARRAYREFREF => sub { @{${$el}} if @{${$el}}},
420 HASHREF => sub {$self->_recurse_where($el, 'and') if %$el},
421 # LDNOTE : previous SQLA code for hashrefs was creating a dirty
422 # side-effect: the first hashref within an array would change
423 # the global logic to 'AND'. So [ {cond1, cond2}, [cond3, cond4] ]
424 # was interpreted as "(cond1 AND cond2) OR (cond3 AND cond4)",
425 # whereas it should be "(cond1 AND cond2) OR (cond3 OR cond4)".
427 SCALARREF => sub { ($$el); },
429 SCALAR => sub {# top-level arrayref with scalars, recurse in pairs
430 $self->_recurse_where({$el => shift(@clauses)})},
432 UNDEF => sub {puke "not supported : UNDEF in arrayref" },
436 push @sql_clauses, $sql;
437 push @all_bind, @bind;
441 return $self->_join_sql_clauses($logic, \@sql_clauses, \@all_bind);
444 #======================================================================
445 # WHERE: top-level ARRAYREFREF
446 #======================================================================
448 sub _where_ARRAYREFREF {
449 my ($self, $where) = @_;
450 my ($sql, @bind) = @{${$where}};
452 return ($sql, @bind);
455 #======================================================================
456 # WHERE: top-level HASHREF
457 #======================================================================
460 my ($self, $where) = @_;
461 my (@sql_clauses, @all_bind);
463 for my $k (sort keys %$where) {
464 my $v = $where->{$k};
466 # ($k => $v) is either a special unary op or a regular hashpair
467 my ($sql, @bind) = do {
469 # put the operator in canonical form
471 $op =~ s/^-//; # remove initial dash
472 $op =~ s/[_\t ]+/ /g; # underscores and whitespace become single spaces
473 $op =~ s/^\s+|\s+$//g;# remove leading/trailing space
475 $self->_debug("Unary OP(-$op) within hashref, recursing...");
477 my $op_entry = first {$op =~ $_->{regex}} @{$self->{unary_ops}};
478 if (my $handler = $op_entry->{handler}) {
479 if (not ref $handler) {
480 if ($op =~ s/\s?\d+$//) {
481 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
482 . "You probably wanted ...-and => [ -$op => COND1, -$op => COND2 ... ]";
484 $self->$handler ($op, $v);
486 elsif (ref $handler eq 'CODE') {
487 $handler->($self, $op, $v);
490 puke "Illegal handler for operator $k - expecting a method name or a coderef";
494 $self->debug("Generic unary OP: $k - recursing as function");
495 $self->_where_func_generic ($op, $v);
499 my $method = $self->_METHOD_FOR_refkind("_where_hashpair", $v);
500 $self->$method($k, $v);
504 push @sql_clauses, $sql;
505 push @all_bind, @bind;
508 return $self->_join_sql_clauses('and', \@sql_clauses, \@all_bind);
511 sub _where_func_generic {
512 my ($self, $op, $rhs) = @_;
514 my ($sql, @bind) = $self->_SWITCH_refkind ($rhs, {
516 ($self->_convert('?'), $self->_bindtype('xxx', $rhs) );
519 $self->_recurse_where ($rhs)
523 $sql = sprintf ('%s%s',
524 $self->_sqlcase($op),
525 ($op =~ $self->{cmp_ops}) ? " $sql" : "( $sql )",
528 return ($sql, @bind);
531 sub _where_op_ANDOR {
532 my ($self, $op, $v) = @_;
534 $self->_SWITCH_refkind($v, {
536 return $self->_where_ARRAYREF($v, $op);
540 return ( $op =~ /^or/i )
541 ? $self->_where_ARRAYREF( [ map { $_ => $v->{$_} } ( sort keys %$v ) ], $op )
542 : $self->_where_HASHREF($v);
546 puke "-$op => \\\$scalar not supported, use -nest => ...";
550 puke "-$op => \\[..] not supported, use -nest => ...";
553 SCALAR => sub { # permissively interpreted as SQL
554 puke "-$op => 'scalar' not supported, use -nest => \\'scalar'";
558 puke "-$op => undef not supported";
564 my ($self, $op, $v) = @_;
566 $self->_SWITCH_refkind($v, {
569 return $self->_where_ARRAYREF($v, '');
573 return $self->_where_HASHREF($v);
576 SCALARREF => sub { # literal SQL
580 ARRAYREFREF => sub { # literal SQL
584 SCALAR => sub { # permissively interpreted as SQL
585 belch "literal SQL should be -nest => \\'scalar' "
586 . "instead of -nest => 'scalar' ";
591 puke "-$op => undef not supported";
598 my ($self, $op, $v) = @_;
600 my ( $prefix, $suffix ) = ( $op =~ /\bnot\b/i )
604 my ($sql, @bind) = do {
605 $self->_SWITCH_refkind($v, {
606 SCALAR => sub { # interpreted as SQL column
607 $self->_convert($self->_quote($v));
611 puke "-$op => undef not supported";
615 $self->_recurse_where ($v);
621 join ('', $prefix, $sql, $suffix),
627 sub _where_hashpair_ARRAYREF {
628 my ($self, $k, $v) = @_;
631 my @v = @$v; # need copy because of shift below
632 $self->_debug("ARRAY($k) means distribute over elements");
634 # put apart first element if it is an operator (-and, -or)
636 (defined $v[0] && $v[0] =~ /^ - (?: AND|OR ) $/ix)
640 my @distributed = map { {$k => $_} } @v;
643 $self->_debug("OP($op) reinjected into the distributed array");
644 unshift @distributed, $op;
647 my $logic = $op ? substr($op, 1) : '';
649 return $self->_recurse_where(\@distributed, $logic);
652 # LDNOTE : not sure of this one. What does "distribute over nothing" mean?
653 $self->_debug("empty ARRAY($k) means 0=1");
654 return ($self->{sqlfalse});
658 sub _where_hashpair_HASHREF {
659 my ($self, $k, $v, $logic) = @_;
662 my ($all_sql, @all_bind);
664 for my $orig_op (sort keys %$v) {
665 my $val = $v->{$orig_op};
667 # put the operator in canonical form
669 $op =~ s/^-//; # remove initial dash
670 $op =~ s/[_\t ]+/ /g; # underscores and whitespace become single spaces
671 $op =~ s/^\s+|\s+$//g;# remove leading/trailing space
675 # CASE: col-value logic modifiers
676 if ( $orig_op =~ /^ \- (and|or) $/xi ) {
677 ($sql, @bind) = $self->_where_hashpair_HASHREF($k, $val, $1);
679 # CASE: special operators like -in or -between
680 elsif ( my $special_op = first {$op =~ $_->{regex}} @{$self->{special_ops}} ) {
681 my $handler = $special_op->{handler};
683 puke "No handler supplied for special operator $orig_op";
685 elsif (not ref $handler) {
686 ($sql, @bind) = $self->$handler ($k, $op, $val);
688 elsif (ref $handler eq 'CODE') {
689 ($sql, @bind) = $handler->($self, $k, $op, $val);
692 puke "Illegal handler for special operator $orig_op - expecting a method name or a coderef";
696 $self->_SWITCH_refkind($val, {
698 ARRAYREF => sub { # CASE: col => {op => \@vals}
699 ($sql, @bind) = $self->_where_field_op_ARRAYREF($k, $op, $val);
702 ARRAYREFREF => sub { # CASE: col => {op => \[$sql, @bind]} (literal SQL with bind)
703 my ($sub_sql, @sub_bind) = @$$val;
704 $self->_assert_bindval_matches_bindtype(@sub_bind);
705 $sql = join ' ', $self->_convert($self->_quote($k)),
706 $self->_sqlcase($op),
711 UNDEF => sub { # CASE: col => {op => undef} : sql "IS (NOT)? NULL"
712 my $is = ($op =~ $self->{equality_op}) ? 'is' :
713 ($op =~ $self->{inequality_op}) ? 'is not' :
714 puke "unexpected operator '$orig_op' with undef operand";
715 $sql = $self->_quote($k) . $self->_sqlcase(" $is null");
718 FALLBACK => sub { # CASE: col => {op/func => $stuff}
719 ($sql, @bind) = $self->_where_func_generic ($op, $val);
720 $sql = join ' ', $self->_convert($self->_quote($k)), $sql;
725 ($all_sql) = (defined $all_sql and $all_sql) ? $self->_join_sql_clauses($logic, [$all_sql, $sql], []) : $sql;
726 push @all_bind, @bind;
728 return ($all_sql, @all_bind);
733 sub _where_field_op_ARRAYREF {
734 my ($self, $k, $op, $vals) = @_;
736 my @vals = @$vals; #always work on a copy
739 $self->_debug(sprintf '%s means multiple elements: [ %s ]',
741 join (', ', map { defined $_ ? "'$_'" : 'NULL' } @vals ),
744 # see if the first element is an -and/-or op
746 if (defined $vals[0] && $vals[0] =~ /^ - ( AND|OR ) $/ix) {
751 # distribute $op over each remaining member of @vals, append logic if exists
752 return $self->_recurse_where([map { {$k => {$op, $_}} } @vals], $logic);
754 # LDNOTE : had planned to change the distribution logic when
755 # $op =~ $self->{inequality_op}, because of Morgan laws :
756 # with {field => {'!=' => [22, 33]}}, it would be ridiculous to generate
757 # WHERE field != 22 OR field != 33 : the user probably means
758 # WHERE field != 22 AND field != 33.
759 # To do this, replace the above to roughly :
760 # my $logic = ($op =~ $self->{inequality_op}) ? 'AND' : 'OR';
761 # return $self->_recurse_where([map { {$k => {$op, $_}} } @vals], $logic);
765 # try to DWIM on equality operators
766 # LDNOTE : not 100% sure this is the correct thing to do ...
767 return ($self->{sqlfalse}) if $op =~ $self->{equality_op};
768 return ($self->{sqltrue}) if $op =~ $self->{inequality_op};
771 puke "operator '$op' applied on an empty array (field '$k')";
776 sub _where_hashpair_SCALARREF {
777 my ($self, $k, $v) = @_;
778 $self->_debug("SCALAR($k) means literal SQL: $$v");
779 my $sql = $self->_quote($k) . " " . $$v;
783 # literal SQL with bind
784 sub _where_hashpair_ARRAYREFREF {
785 my ($self, $k, $v) = @_;
786 $self->_debug("REF($k) means literal SQL: @${$v}");
787 my ($sql, @bind) = @${$v};
788 $self->_assert_bindval_matches_bindtype(@bind);
789 $sql = $self->_quote($k) . " " . $sql;
790 return ($sql, @bind );
793 # literal SQL without bind
794 sub _where_hashpair_SCALAR {
795 my ($self, $k, $v) = @_;
796 $self->_debug("NOREF($k) means simple key=val: $k $self->{cmp} $v");
797 my $sql = join ' ', $self->_convert($self->_quote($k)),
798 $self->_sqlcase($self->{cmp}),
799 $self->_convert('?');
800 my @bind = $self->_bindtype($k, $v);
801 return ( $sql, @bind);
805 sub _where_hashpair_UNDEF {
806 my ($self, $k, $v) = @_;
807 $self->_debug("UNDEF($k) means IS NULL");
808 my $sql = $self->_quote($k) . $self->_sqlcase(' is null');
812 #======================================================================
813 # WHERE: TOP-LEVEL OTHERS (SCALARREF, SCALAR, UNDEF)
814 #======================================================================
817 sub _where_SCALARREF {
818 my ($self, $where) = @_;
821 $self->_debug("SCALAR(*top) means literal SQL: $$where");
827 my ($self, $where) = @_;
830 $self->_debug("NOREF(*top) means literal SQL: $where");
841 #======================================================================
842 # WHERE: BUILTIN SPECIAL OPERATORS (-in, -between)
843 #======================================================================
846 sub _where_field_BETWEEN {
847 my ($self, $k, $op, $vals) = @_;
849 my ($label, $and, $placeholder);
850 $label = $self->_convert($self->_quote($k));
851 $and = ' ' . $self->_sqlcase('and') . ' ';
852 $placeholder = $self->_convert('?');
853 $op = $self->_sqlcase($op);
855 my ($clause, @bind) = $self->_SWITCH_refkind($vals, {
863 puke "special op 'between' accepts an arrayref with exactly two values"
866 my (@all_sql, @all_bind);
867 foreach my $val (@$vals) {
868 my ($sql, @bind) = $self->_SWITCH_refkind($val, {
870 return ($placeholder, ($val));
873 return ($self->_convert($$val), ());
876 my ($sql, @bind) = @$$val;
877 return ($self->_convert($sql), @bind);
881 push @all_bind, @bind;
885 (join $and, @all_sql),
886 $self->_bindtype($k, @all_bind),
890 puke "special op 'between' accepts an arrayref with two values, or a single literal scalarref/arrayref-ref";
894 my $sql = "( $label $op $clause )";
899 sub _where_field_IN {
900 my ($self, $k, $op, $vals) = @_;
902 # backwards compatibility : if scalar, force into an arrayref
903 $vals = [$vals] if defined $vals && ! ref $vals;
905 my ($label) = $self->_convert($self->_quote($k));
906 my ($placeholder) = $self->_convert('?');
907 $op = $self->_sqlcase($op);
909 my ($sql, @bind) = $self->_SWITCH_refkind($vals, {
910 ARRAYREF => sub { # list of choices
911 if (@$vals) { # nonempty list
912 my $placeholders = join ", ", (($placeholder) x @$vals);
913 my $sql = "$label $op ( $placeholders )";
914 my @bind = $self->_bindtype($k, @$vals);
916 return ($sql, @bind);
918 else { # empty list : some databases won't understand "IN ()", so DWIM
919 my $sql = ($op =~ /\bnot\b/i) ? $self->{sqltrue} : $self->{sqlfalse};
924 SCALARREF => sub { # literal SQL
925 my $sql = $self->_open_outer_paren ($$vals);
926 return ("$label $op ( $sql )");
928 ARRAYREFREF => sub { # literal SQL with bind
929 my ($sql, @bind) = @$$vals;
930 $self->_assert_bindval_matches_bindtype(@bind);
931 $sql = $self->_open_outer_paren ($sql);
932 return ("$label $op ( $sql )", @bind);
936 puke "special op 'in' requires an arrayref (or scalarref/arrayref-ref)";
940 return ($sql, @bind);
943 # Some databases (SQLite) treat col IN (1, 2) different from
944 # col IN ( (1, 2) ). Use this to strip all outer parens while
945 # adding them back in the corresponding method
946 sub _open_outer_paren {
947 my ($self, $sql) = @_;
948 $sql = $1 while $sql =~ /^ \s* \( (.*) \) \s* $/xs;
953 #======================================================================
955 #======================================================================
958 my ($self, $arg) = @_;
961 for my $c ($self->_order_by_chunks ($arg) ) {
962 $self->_SWITCH_refkind ($c, {
963 SCALAR => sub { push @sql, $c },
964 ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
970 $self->_sqlcase(' order by'),
976 return wantarray ? ($sql, @bind) : $sql;
979 sub _order_by_chunks {
980 my ($self, $arg) = @_;
982 return $self->_SWITCH_refkind($arg, {
985 map { $self->_order_by_chunks ($_ ) } @$arg;
988 ARRAYREFREF => sub { [ @$$arg ] },
990 SCALAR => sub {$self->_quote($arg)},
992 UNDEF => sub {return () },
994 SCALARREF => sub {$$arg}, # literal SQL, no quoting
997 # get first pair in hash
998 my ($key, $val) = each %$arg;
1000 return () unless $key;
1002 if ( (keys %$arg) > 1 or not $key =~ /^-(desc|asc)/i ) {
1003 puke "hash passed to _order_by must have exactly one key (-desc or -asc)";
1009 for my $c ($self->_order_by_chunks ($val)) {
1012 $self->_SWITCH_refkind ($c, {
1017 ($sql, @bind) = @$c;
1021 $sql = $sql . ' ' . $self->_sqlcase($direction);
1023 push @ret, [ $sql, @bind];
1032 #======================================================================
1033 # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
1034 #======================================================================
1039 $self->_SWITCH_refkind($from, {
1040 ARRAYREF => sub {join ', ', map { $self->_quote($_) } @$from;},
1041 SCALAR => sub {$self->_quote($from)},
1042 SCALARREF => sub {$$from},
1043 ARRAYREFREF => sub {join ', ', @$from;},
1048 #======================================================================
1050 #======================================================================
1056 $label or puke "can't quote an empty label";
1058 # left and right quote characters
1059 my ($ql, $qr, @other) = $self->_SWITCH_refkind($self->{quote_char}, {
1060 SCALAR => sub {($self->{quote_char}, $self->{quote_char})},
1061 ARRAYREF => sub {@{$self->{quote_char}}},
1065 or puke "quote_char must be an arrayref of 2 values";
1067 # no quoting if no quoting chars
1068 $ql or return $label;
1070 # no quoting for literal SQL
1071 return $$label if ref($label) eq 'SCALAR';
1073 # separate table / column (if applicable)
1074 my $sep = $self->{name_sep} || '';
1075 my @to_quote = $sep ? split /\Q$sep\E/, $label : ($label);
1077 # do the quoting, except for "*" or for `table`.*
1078 my @quoted = map { $_ eq '*' ? $_: $ql.$_.$qr} @to_quote;
1080 # reassemble and return.
1081 return join $sep, @quoted;
1085 # Conversion, if applicable
1087 my ($self, $arg) = @_;
1089 # LDNOTE : modified the previous implementation below because
1090 # it was not consistent : the first "return" is always an array,
1091 # the second "return" is context-dependent. Anyway, _convert
1092 # seems always used with just a single argument, so make it a
1094 # return @_ unless $self->{convert};
1095 # my $conv = $self->_sqlcase($self->{convert});
1096 # my @ret = map { $conv.'('.$_.')' } @_;
1097 # return wantarray ? @ret : $ret[0];
1098 if ($self->{convert}) {
1099 my $conv = $self->_sqlcase($self->{convert});
1100 $arg = $conv.'('.$arg.')';
1108 my($col, @vals) = @_;
1110 #LDNOTE : changed original implementation below because it did not make
1111 # sense when bindtype eq 'columns' and @vals > 1.
1112 # return $self->{bindtype} eq 'columns' ? [ $col, @vals ] : @vals;
1114 return $self->{bindtype} eq 'columns' ? map {[$col, $_]} @vals : @vals;
1117 # Dies if any element of @bind is not in [colname => value] format
1118 # if bindtype is 'columns'.
1119 sub _assert_bindval_matches_bindtype {
1120 my ($self, @bind) = @_;
1122 if ($self->{bindtype} eq 'columns') {
1123 foreach my $val (@bind) {
1124 if (!defined $val || ref($val) ne 'ARRAY' || @$val != 2) {
1125 die "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
1131 sub _join_sql_clauses {
1132 my ($self, $logic, $clauses_aref, $bind_aref) = @_;
1134 if (@$clauses_aref > 1) {
1135 my $join = " " . $self->_sqlcase($logic) . " ";
1136 my $sql = '( ' . join($join, @$clauses_aref) . ' )';
1137 return ($sql, @$bind_aref);
1139 elsif (@$clauses_aref) {
1140 return ($clauses_aref->[0], @$bind_aref); # no parentheses
1143 return (); # if no SQL, ignore @$bind_aref
1148 # Fix SQL case, if so requested
1152 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
1153 # don't touch the argument ... crooked logic, but let's not change it!
1154 return $self->{case} ? $_[0] : uc($_[0]);
1158 #======================================================================
1159 # DISPATCHING FROM REFKIND
1160 #======================================================================
1163 my ($self, $data) = @_;
1169 # blessed objects are treated like scalars
1170 $ref = (blessed $data) ? '' : ref $data;
1171 $n_steps += 1 if $ref;
1172 last if $ref ne 'REF';
1176 my $base = $ref || (defined $data ? 'SCALAR' : 'UNDEF');
1178 return $base . ('REF' x $n_steps);
1184 my ($self, $data) = @_;
1185 my @try = ($self->_refkind($data));
1186 push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF';
1187 push @try, 'FALLBACK';
1191 sub _METHOD_FOR_refkind {
1192 my ($self, $meth_prefix, $data) = @_;
1193 my $method = first {$_} map {$self->can($meth_prefix."_".$_)}
1194 $self->_try_refkind($data)
1195 or puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data);
1200 sub _SWITCH_refkind {
1201 my ($self, $data, $dispatch_table) = @_;
1203 my $coderef = first {$_} map {$dispatch_table->{$_}}
1204 $self->_try_refkind($data)
1205 or puke "no dispatch entry for ".$self->_refkind($data);
1212 #======================================================================
1213 # VALUES, GENERATE, AUTOLOAD
1214 #======================================================================
1216 # LDNOTE: original code from nwiger, didn't touch code in that section
1217 # I feel the AUTOLOAD stuff should not be the default, it should
1218 # only be activated on explicit demand by user.
1222 my $data = shift || return;
1223 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
1224 unless ref $data eq 'HASH';
1227 foreach my $k ( sort keys %$data ) {
1228 my $v = $data->{$k};
1229 $self->_SWITCH_refkind($v, {
1231 if ($self->{array_datatypes}) { # array datatype
1232 push @all_bind, $self->_bindtype($k, $v);
1234 else { # literal SQL with bind
1235 my ($sql, @bind) = @$v;
1236 $self->_assert_bindval_matches_bindtype(@bind);
1237 push @all_bind, @bind;
1240 ARRAYREFREF => sub { # literal SQL with bind
1241 my ($sql, @bind) = @${$v};
1242 $self->_assert_bindval_matches_bindtype(@bind);
1243 push @all_bind, @bind;
1245 SCALARREF => sub { # literal SQL without bind
1247 SCALAR_or_UNDEF => sub {
1248 push @all_bind, $self->_bindtype($k, $v);
1259 my(@sql, @sqlq, @sqlv);
1263 if ($ref eq 'HASH') {
1264 for my $k (sort keys %$_) {
1267 my $label = $self->_quote($k);
1268 if ($r eq 'ARRAY') {
1269 # literal SQL with bind
1270 my ($sql, @bind) = @$v;
1271 $self->_assert_bindval_matches_bindtype(@bind);
1272 push @sqlq, "$label = $sql";
1274 } elsif ($r eq 'SCALAR') {
1275 # literal SQL without bind
1276 push @sqlq, "$label = $$v";
1278 push @sqlq, "$label = ?";
1279 push @sqlv, $self->_bindtype($k, $v);
1282 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
1283 } elsif ($ref eq 'ARRAY') {
1284 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
1287 if ($r eq 'ARRAY') { # literal SQL with bind
1288 my ($sql, @bind) = @$v;
1289 $self->_assert_bindval_matches_bindtype(@bind);
1292 } elsif ($r eq 'SCALAR') { # literal SQL without bind
1293 # embedded literal SQL
1300 push @sql, '(' . join(', ', @sqlq) . ')';
1301 } elsif ($ref eq 'SCALAR') {
1305 # strings get case twiddled
1306 push @sql, $self->_sqlcase($_);
1310 my $sql = join ' ', @sql;
1312 # this is pretty tricky
1313 # if ask for an array, return ($stmt, @bind)
1314 # otherwise, s/?/shift @sqlv/ to put it inline
1316 return ($sql, @sqlv);
1318 1 while $sql =~ s/\?/my $d = shift(@sqlv);
1319 ref $d ? $d->[1] : $d/e;
1328 # This allows us to check for a local, then _form, attr
1330 my($name) = $AUTOLOAD =~ /.*::(.+)/;
1331 return $self->generate($name, @_);
1342 SQL::Abstract - Generate SQL from Perl data structures
1348 my $sql = SQL::Abstract->new;
1350 my($stmt, @bind) = $sql->select($table, \@fields, \%where, \@order);
1352 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
1354 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
1356 my($stmt, @bind) = $sql->delete($table, \%where);
1358 # Then, use these in your DBI statements
1359 my $sth = $dbh->prepare($stmt);
1360 $sth->execute(@bind);
1362 # Just generate the WHERE clause
1363 my($stmt, @bind) = $sql->where(\%where, \@order);
1365 # Return values in the same order, for hashed queries
1366 # See PERFORMANCE section for more details
1367 my @bind = $sql->values(\%fieldvals);
1371 This module was inspired by the excellent L<DBIx::Abstract>.
1372 However, in using that module I found that what I really wanted
1373 to do was generate SQL, but still retain complete control over my
1374 statement handles and use the DBI interface. So, I set out to
1375 create an abstract SQL generation module.
1377 While based on the concepts used by L<DBIx::Abstract>, there are
1378 several important differences, especially when it comes to WHERE
1379 clauses. I have modified the concepts used to make the SQL easier
1380 to generate from Perl data structures and, IMO, more intuitive.
1381 The underlying idea is for this module to do what you mean, based
1382 on the data structures you provide it. The big advantage is that
1383 you don't have to modify your code every time your data changes,
1384 as this module figures it out.
1386 To begin with, an SQL INSERT is as easy as just specifying a hash
1387 of C<key=value> pairs:
1390 name => 'Jimbo Bobson',
1391 phone => '123-456-7890',
1392 address => '42 Sister Lane',
1393 city => 'St. Louis',
1394 state => 'Louisiana',
1397 The SQL can then be generated with this:
1399 my($stmt, @bind) = $sql->insert('people', \%data);
1401 Which would give you something like this:
1403 $stmt = "INSERT INTO people
1404 (address, city, name, phone, state)
1405 VALUES (?, ?, ?, ?, ?)";
1406 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
1407 '123-456-7890', 'Louisiana');
1409 These are then used directly in your DBI code:
1411 my $sth = $dbh->prepare($stmt);
1412 $sth->execute(@bind);
1414 =head2 Inserting and Updating Arrays
1416 If your database has array types (like for example Postgres),
1417 activate the special option C<< array_datatypes => 1 >>
1418 when creating the C<SQL::Abstract> object.
1419 Then you may use an arrayref to insert and update database array types:
1421 my $sql = SQL::Abstract->new(array_datatypes => 1);
1423 planets => [qw/Mercury Venus Earth Mars/]
1426 my($stmt, @bind) = $sql->insert('solar_system', \%data);
1430 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
1432 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
1435 =head2 Inserting and Updating SQL
1437 In order to apply SQL functions to elements of your C<%data> you may
1438 specify a reference to an arrayref for the given hash value. For example,
1439 if you need to execute the Oracle C<to_date> function on a value, you can
1440 say something like this:
1444 date_entered => \["to_date(?,'MM/DD/YYYY')", "03/02/2003"],
1447 The first value in the array is the actual SQL. Any other values are
1448 optional and would be included in the bind values array. This gives
1451 my($stmt, @bind) = $sql->insert('people', \%data);
1453 $stmt = "INSERT INTO people (name, date_entered)
1454 VALUES (?, to_date(?,'MM/DD/YYYY'))";
1455 @bind = ('Bill', '03/02/2003');
1457 An UPDATE is just as easy, all you change is the name of the function:
1459 my($stmt, @bind) = $sql->update('people', \%data);
1461 Notice that your C<%data> isn't touched; the module will generate
1462 the appropriately quirky SQL for you automatically. Usually you'll
1463 want to specify a WHERE clause for your UPDATE, though, which is
1464 where handling C<%where> hashes comes in handy...
1466 =head2 Complex where statements
1468 This module can generate pretty complicated WHERE statements
1469 easily. For example, simple C<key=value> pairs are taken to mean
1470 equality, and if you want to see if a field is within a set
1471 of values, you can use an arrayref. Let's say we wanted to
1472 SELECT some data based on this criteria:
1475 requestor => 'inna',
1476 worker => ['nwiger', 'rcwe', 'sfz'],
1477 status => { '!=', 'completed' }
1480 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
1482 The above would give you something like this:
1484 $stmt = "SELECT * FROM tickets WHERE
1485 ( requestor = ? ) AND ( status != ? )
1486 AND ( worker = ? OR worker = ? OR worker = ? )";
1487 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
1489 Which you could then use in DBI code like so:
1491 my $sth = $dbh->prepare($stmt);
1492 $sth->execute(@bind);
1498 The functions are simple. There's one for each major SQL operation,
1499 and a constructor you use first. The arguments are specified in a
1500 similar order to each function (table, then fields, then a where
1501 clause) to try and simplify things.
1506 =head2 new(option => 'value')
1508 The C<new()> function takes a list of options and values, and returns
1509 a new B<SQL::Abstract> object which can then be used to generate SQL
1510 through the methods below. The options accepted are:
1516 If set to 'lower', then SQL will be generated in all lowercase. By
1517 default SQL is generated in "textbook" case meaning something like:
1519 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
1521 Any setting other than 'lower' is ignored.
1525 This determines what the default comparison operator is. By default
1526 it is C<=>, meaning that a hash like this:
1528 %where = (name => 'nwiger', email => 'nate@wiger.org');
1530 Will generate SQL like this:
1532 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
1534 However, you may want loose comparisons by default, so if you set
1535 C<cmp> to C<like> you would get SQL such as:
1537 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
1539 You can also override the comparsion on an individual basis - see
1540 the huge section on L</"WHERE CLAUSES"> at the bottom.
1542 =item sqltrue, sqlfalse
1544 Expressions for inserting boolean values within SQL statements.
1545 By default these are C<1=1> and C<1=0>. They are used
1546 by the special operators C<-in> and C<-not_in> for generating
1547 correct SQL even when the argument is an empty array (see below).
1551 This determines the default logical operator for multiple WHERE
1552 statements in arrays or hashes. If absent, the default logic is "or"
1553 for arrays, and "and" for hashes. This means that a WHERE
1557 event_date => {'>=', '2/13/99'},
1558 event_date => {'<=', '4/24/03'},
1561 will generate SQL like this:
1563 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
1565 This is probably not what you want given this query, though (look
1566 at the dates). To change the "OR" to an "AND", simply specify:
1568 my $sql = SQL::Abstract->new(logic => 'and');
1570 Which will change the above C<WHERE> to:
1572 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
1574 The logic can also be changed locally by inserting
1575 a modifier in front of an arrayref :
1577 @where = (-and => [event_date => {'>=', '2/13/99'},
1578 event_date => {'<=', '4/24/03'} ]);
1580 See the L</"WHERE CLAUSES"> section for explanations.
1584 This will automatically convert comparisons using the specified SQL
1585 function for both column and value. This is mostly used with an argument
1586 of C<upper> or C<lower>, so that the SQL will have the effect of
1587 case-insensitive "searches". For example, this:
1589 $sql = SQL::Abstract->new(convert => 'upper');
1590 %where = (keywords => 'MaKe iT CAse inSeNSItive');
1592 Will turn out the following SQL:
1594 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
1596 The conversion can be C<upper()>, C<lower()>, or any other SQL function
1597 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
1598 not validate this option; it will just pass through what you specify verbatim).
1602 This is a kludge because many databases suck. For example, you can't
1603 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
1604 Instead, you have to use C<bind_param()>:
1606 $sth->bind_param(1, 'reg data');
1607 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
1609 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
1610 which loses track of which field each slot refers to. Fear not.
1612 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
1613 Currently, you can specify either C<normal> (default) or C<columns>. If you
1614 specify C<columns>, you will get an array that looks like this:
1616 my $sql = SQL::Abstract->new(bindtype => 'columns');
1617 my($stmt, @bind) = $sql->insert(...);
1620 [ 'column1', 'value1' ],
1621 [ 'column2', 'value2' ],
1622 [ 'column3', 'value3' ],
1625 You can then iterate through this manually, using DBI's C<bind_param()>.
1627 $sth->prepare($stmt);
1630 my($col, $data) = @$_;
1631 if ($col eq 'details' || $col eq 'comments') {
1632 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
1633 } elsif ($col eq 'image') {
1634 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
1636 $sth->bind_param($i, $data);
1640 $sth->execute; # execute without @bind now
1642 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
1643 Basically, the advantage is still that you don't have to care which fields
1644 are or are not included. You could wrap that above C<for> loop in a simple
1645 sub called C<bind_fields()> or something and reuse it repeatedly. You still
1646 get a layer of abstraction over manual SQL specification.
1648 Note that if you set L</bindtype> to C<columns>, the C<\[$sql, @bind]>
1649 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
1650 will expect the bind values in this format.
1654 This is the character that a table or column name will be quoted
1655 with. By default this is an empty string, but you could set it to
1656 the character C<`>, to generate SQL like this:
1658 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
1660 Alternatively, you can supply an array ref of two items, the first being the left
1661 hand quote character, and the second the right hand quote character. For
1662 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
1663 that generates SQL like this:
1665 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
1667 Quoting is useful if you have tables or columns names that are reserved
1668 words in your database's SQL dialect.
1672 This is the character that separates a table and column name. It is
1673 necessary to specify this when the C<quote_char> option is selected,
1674 so that tables and column names can be individually quoted like this:
1676 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
1678 =item array_datatypes
1680 When this option is true, arrayrefs in INSERT or UPDATE are
1681 interpreted as array datatypes and are passed directly
1683 When this option is false, arrayrefs are interpreted
1684 as literal SQL, just like refs to arrayrefs
1685 (but this behavior is for backwards compatibility; when writing
1686 new queries, use the "reference to arrayref" syntax
1692 Takes a reference to a list of "special operators"
1693 to extend the syntax understood by L<SQL::Abstract>.
1694 See section L</"SPECIAL OPERATORS"> for details.
1698 Takes a reference to a list of "unary operators"
1699 to extend the syntax understood by L<SQL::Abstract>.
1700 See section L</"UNARY OPERATORS"> for details.
1706 =head2 insert($table, \@values || \%fieldvals, \%options)
1708 This is the simplest function. You simply give it a table name
1709 and either an arrayref of values or hashref of field/value pairs.
1710 It returns an SQL INSERT statement and a list of bind values.
1711 See the sections on L</"Inserting and Updating Arrays"> and
1712 L</"Inserting and Updating SQL"> for information on how to insert
1713 with those data types.
1715 The optional C<\%options> hash reference may contain additional
1716 options to generate the insert SQL. Currently supported options
1723 Takes either a scalar of raw SQL fields, or an array reference of
1724 field names, and adds on an SQL C<RETURNING> statement at the end.
1725 This allows you to return data generated by the insert statement
1726 (such as row IDs) without performing another C<SELECT> statement.
1727 Note, however, this is not part of the SQL standard and may not
1728 be supported by all database engines.
1732 =head2 update($table, \%fieldvals, \%where)
1734 This takes a table, hashref of field/value pairs, and an optional
1735 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
1737 See the sections on L</"Inserting and Updating Arrays"> and
1738 L</"Inserting and Updating SQL"> for information on how to insert
1739 with those data types.
1741 =head2 select($source, $fields, $where, $order)
1743 This returns a SQL SELECT statement and associated list of bind values, as
1744 specified by the arguments :
1750 Specification of the 'FROM' part of the statement.
1751 The argument can be either a plain scalar (interpreted as a table
1752 name, will be quoted), or an arrayref (interpreted as a list
1753 of table names, joined by commas, quoted), or a scalarref
1754 (literal table name, not quoted), or a ref to an arrayref
1755 (list of literal table names, joined by commas, not quoted).
1759 Specification of the list of fields to retrieve from
1761 The argument can be either an arrayref (interpreted as a list
1762 of field names, will be joined by commas and quoted), or a
1763 plain scalar (literal SQL, not quoted).
1764 Please observe that this API is not as flexible as for
1765 the first argument C<$table>, for backwards compatibility reasons.
1769 Optional argument to specify the WHERE part of the query.
1770 The argument is most often a hashref, but can also be
1771 an arrayref or plain scalar --
1772 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
1776 Optional argument to specify the ORDER BY part of the query.
1777 The argument can be a scalar, a hashref or an arrayref
1778 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
1784 =head2 delete($table, \%where)
1786 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
1787 It returns an SQL DELETE statement and list of bind values.
1789 =head2 where(\%where, \@order)
1791 This is used to generate just the WHERE clause. For example,
1792 if you have an arbitrary data structure and know what the
1793 rest of your SQL is going to look like, but want an easy way
1794 to produce a WHERE clause, use this. It returns an SQL WHERE
1795 clause and list of bind values.
1798 =head2 values(\%data)
1800 This just returns the values from the hash C<%data>, in the same
1801 order that would be returned from any of the other above queries.
1802 Using this allows you to markedly speed up your queries if you
1803 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
1805 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
1807 Warning: This is an experimental method and subject to change.
1809 This returns arbitrarily generated SQL. It's a really basic shortcut.
1810 It will return two different things, depending on return context:
1812 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
1813 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
1815 These would return the following:
1817 # First calling form
1818 $stmt = "CREATE TABLE test (?, ?)";
1819 @bind = (field1, field2);
1821 # Second calling form
1822 $stmt_and_val = "CREATE TABLE test (field1, field2)";
1824 Depending on what you're trying to do, it's up to you to choose the correct
1825 format. In this example, the second form is what you would want.
1829 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
1833 ALTER SESSION SET nls_date_format = 'MM/YY'
1835 You get the idea. Strings get their case twiddled, but everything
1836 else remains verbatim.
1841 =head1 WHERE CLAUSES
1845 This module uses a variation on the idea from L<DBIx::Abstract>. It
1846 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
1847 module is that things in arrays are OR'ed, and things in hashes
1850 The easiest way to explain is to show lots of examples. After
1851 each C<%where> hash shown, it is assumed you used:
1853 my($stmt, @bind) = $sql->where(\%where);
1855 However, note that the C<%where> hash can be used directly in any
1856 of the other functions as well, as described above.
1858 =head2 Key-value pairs
1860 So, let's get started. To begin, a simple hash:
1864 status => 'completed'
1867 Is converted to SQL C<key = val> statements:
1869 $stmt = "WHERE user = ? AND status = ?";
1870 @bind = ('nwiger', 'completed');
1872 One common thing I end up doing is having a list of values that
1873 a field can be in. To do this, simply specify a list inside of
1878 status => ['assigned', 'in-progress', 'pending'];
1881 This simple code will create the following:
1883 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
1884 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
1886 A field associated to an empty arrayref will be considered a
1887 logical false and will generate 0=1.
1889 =head2 Specific comparison operators
1891 If you want to specify a different type of operator for your comparison,
1892 you can use a hashref for a given column:
1896 status => { '!=', 'completed' }
1899 Which would generate:
1901 $stmt = "WHERE user = ? AND status != ?";
1902 @bind = ('nwiger', 'completed');
1904 To test against multiple values, just enclose the values in an arrayref:
1906 status => { '=', ['assigned', 'in-progress', 'pending'] };
1908 Which would give you:
1910 "WHERE status = ? OR status = ? OR status = ?"
1913 The hashref can also contain multiple pairs, in which case it is expanded
1914 into an C<AND> of its elements:
1918 status => { '!=', 'completed', -not_like => 'pending%' }
1921 # Or more dynamically, like from a form
1922 $where{user} = 'nwiger';
1923 $where{status}{'!='} = 'completed';
1924 $where{status}{'-not_like'} = 'pending%';
1926 # Both generate this
1927 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
1928 @bind = ('nwiger', 'completed', 'pending%');
1931 To get an OR instead, you can combine it with the arrayref idea:
1935 priority => [ {'=', 2}, {'!=', 1} ]
1938 Which would generate:
1940 $stmt = "WHERE user = ? AND priority = ? OR priority != ?";
1941 @bind = ('nwiger', '2', '1');
1943 If you want to include literal SQL (with or without bind values), just use a
1944 scalar reference or array reference as the value:
1947 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
1948 date_expires => { '<' => \"now()" }
1951 Which would generate:
1953 $stmt = "WHERE date_entered > "to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
1954 @bind = ('11/26/2008');
1957 =head2 Logic and nesting operators
1959 In the example above,
1960 there is a subtle trap if you want to say something like
1961 this (notice the C<AND>):
1963 WHERE priority != ? AND priority != ?
1965 Because, in Perl you I<can't> do this:
1967 priority => { '!=', 2, '!=', 1 }
1969 As the second C<!=> key will obliterate the first. The solution
1970 is to use the special C<-modifier> form inside an arrayref:
1972 priority => [ -and => {'!=', 2},
1976 Normally, these would be joined by C<OR>, but the modifier tells it
1977 to use C<AND> instead. (Hint: You can use this in conjunction with the
1978 C<logic> option to C<new()> in order to change the way your queries
1979 work by default.) B<Important:> Note that the C<-modifier> goes
1980 B<INSIDE> the arrayref, as an extra first element. This will
1981 B<NOT> do what you think it might:
1983 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
1985 Here is a quick list of equivalencies, since there is some overlap:
1988 status => {'!=', 'completed', 'not like', 'pending%' }
1989 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
1992 status => {'=', ['assigned', 'in-progress']}
1993 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
1994 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
1998 =head2 Special operators : IN, BETWEEN, etc.
2000 You can also use the hashref format to compare a list of fields using the
2001 C<IN> comparison operator, by specifying the list as an arrayref:
2004 status => 'completed',
2005 reportid => { -in => [567, 2335, 2] }
2008 Which would generate:
2010 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
2011 @bind = ('completed', '567', '2335', '2');
2013 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
2016 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
2017 (by default : C<1=0>). Similarly, C<< -not_in => [] >> generates
2018 'sqltrue' (by default : C<1=1>).
2020 In addition to the array you can supply a chunk of literal sql or
2021 literal sql with bind:
2024 customer => { -in => \[
2025 'SELECT cust_id FROM cust WHERE balance > ?',
2028 status => { -in => \'SELECT status_codes FROM states' },
2034 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
2035 AND status IN ( SELECT status_codes FROM states )
2041 Another pair of operators is C<-between> and C<-not_between>,
2042 used with an arrayref of two values:
2046 completion_date => {
2047 -not_between => ['2002-10-01', '2003-02-06']
2053 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
2055 Just like with C<-in> all plausible combinations of literal SQL
2059 start0 => { -between => [ 1, 2 ] },
2060 start1 => { -between => \["? AND ?", 1, 2] },
2061 start2 => { -between => \"lower(x) AND upper(y)" },
2062 start3 => { -between => [
2064 \["upper(?)", 'stuff' ],
2071 ( start0 BETWEEN ? AND ? )
2072 AND ( start1 BETWEEN ? AND ? )
2073 AND ( start2 BETWEEN lower(x) AND upper(y) )
2074 AND ( start3 BETWEEN lower(x) AND upper(?) )
2076 @bind = (1, 2, 1, 2, 'stuff');
2079 These are the two builtin "special operators"; but the
2080 list can be expanded : see section L</"SPECIAL OPERATORS"> below.
2082 =head2 Unary operators: bool
2084 If you wish to test against boolean columns or functions within your
2085 database you can use the C<-bool> and C<-not_bool> operators. For
2086 example to test the column C<is_user> being true and the column
2087 <is_enabled> being false you would use:-
2091 -not_bool => 'is_enabled',
2096 WHERE is_user AND NOT is_enabled
2098 If a more complex combination is required, testing more conditions,
2099 then you should use the and/or operators:-
2106 -not_bool => 'four',
2112 WHERE one AND two AND three AND NOT four
2115 =head2 Nested conditions, -and/-or prefixes
2117 So far, we've seen how multiple conditions are joined with a top-level
2118 C<AND>. We can change this by putting the different conditions we want in
2119 hashes and then putting those hashes in an array. For example:
2124 status => { -like => ['pending%', 'dispatched'] },
2128 status => 'unassigned',
2132 This data structure would create the following:
2134 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
2135 OR ( user = ? AND status = ? ) )";
2136 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
2139 There is also a special C<-nest>
2140 operator which adds an additional set of parens, to create a subquery.
2141 For example, to get something like this:
2143 $stmt = "WHERE user = ? AND ( workhrs > ? OR geo = ? )";
2144 @bind = ('nwiger', '20', 'ASIA');
2150 -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ],
2154 Finally, clauses in hashrefs or arrayrefs can be
2155 prefixed with an C<-and> or C<-or> to change the logic
2162 -and => [workhrs => {'>', 20}, geo => 'ASIA' ],
2163 -and => [workhrs => {'<', 50}, geo => 'EURO' ]
2170 WHERE ( user = ? AND
2171 ( ( workhrs > ? AND geo = ? )
2172 OR ( workhrs < ? AND geo = ? ) ) )
2175 =head2 Algebraic inconsistency, for historical reasons
2177 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
2178 operator goes C<outside> of the nested structure; whereas when connecting
2179 several constraints on one column, the C<-and> operator goes
2180 C<inside> the arrayref. Here is an example combining both features :
2183 -and => [a => 1, b => 2],
2184 -or => [c => 3, d => 4],
2185 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
2190 WHERE ( ( ( a = ? AND b = ? )
2191 OR ( c = ? OR d = ? )
2192 OR ( e LIKE ? AND e LIKE ? ) ) )
2194 This difference in syntax is unfortunate but must be preserved for
2195 historical reasons. So be careful : the two examples below would
2196 seem algebraically equivalent, but they are not
2198 {col => [-and => {-like => 'foo%'}, {-like => '%bar'}]}
2199 # yields : WHERE ( ( col LIKE ? AND col LIKE ? ) )
2201 [-and => {col => {-like => 'foo%'}, {col => {-like => '%bar'}}]]
2202 # yields : WHERE ( ( col LIKE ? OR col LIKE ? ) )
2207 Finally, sometimes only literal SQL will do. If you want to include
2208 literal SQL verbatim, you can specify it as a scalar reference, namely:
2210 my $inn = 'is Not Null';
2212 priority => { '<', 2 },
2218 $stmt = "WHERE priority < ? AND requestor is Not Null";
2221 Note that in this example, you only get one bind parameter back, since
2222 the verbatim SQL is passed as part of the statement.
2224 Of course, just to prove a point, the above can also be accomplished
2228 priority => { '<', 2 },
2229 requestor => { '!=', undef },
2235 Conditions on boolean columns can be expressed in the same way, passing
2236 a reference to an empty string, however using liternal SQL in this way
2237 is deprecated - the preferred method is to use the boolean operators -
2238 see L</"Unary operators: bool"> :
2241 priority => { '<', 2 },
2247 $stmt = "WHERE priority < ? AND is_ready";
2251 =head2 Literal SQL with placeholders and bind values (subqueries)
2253 If the literal SQL to be inserted has placeholders and bind values,
2254 use a reference to an arrayref (yes this is a double reference --
2255 not so common, but perfectly legal Perl). For example, to find a date
2256 in Postgres you can use something like this:
2259 date_column => \[q/= date '2008-09-30' - ?::integer/, 10/]
2264 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
2267 Note that you must pass the bind values in the same format as they are returned
2268 by L</where>. That means that if you set L</bindtype> to C<columns>, you must
2269 provide the bind values in the C<< [ column_meta => value ] >> format, where
2270 C<column_meta> is an opaque scalar value; most commonly the column name, but
2271 you can use any scalar value (including references and blessed references),
2272 L<SQL::Abstract> will simply pass it through intact. So if C<bindtype> is set
2273 to C<columns> the above example will look like:
2276 date_column => \[q/= date '2008-09-30' - ?::integer/, [ dummy => 10 ]/]
2279 Literal SQL is especially useful for nesting parenthesized clauses in the
2280 main SQL query. Here is a first example :
2282 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
2286 bar => \["IN ($sub_stmt)" => @sub_bind],
2291 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
2292 WHERE c2 < ? AND c3 LIKE ?))";
2293 @bind = (1234, 100, "foo%");
2295 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
2296 are expressed in the same way. Of course the C<$sub_stmt> and
2297 its associated bind values can be generated through a former call
2300 my ($sub_stmt, @sub_bind)
2301 = $sql->select("t1", "c1", {c2 => {"<" => 100},
2302 c3 => {-like => "foo%"}});
2305 bar => \["> ALL ($sub_stmt)" => @sub_bind],
2308 In the examples above, the subquery was used as an operator on a column;
2309 but the same principle also applies for a clause within the main C<%where>
2310 hash, like an EXISTS subquery :
2312 my ($sub_stmt, @sub_bind)
2313 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
2316 -nest => \["EXISTS ($sub_stmt)" => @sub_bind],
2321 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
2322 WHERE c1 = ? AND c2 > t0.c0))";
2326 Observe that the condition on C<c2> in the subquery refers to
2327 column C<t0.c0> of the main query : this is I<not> a bind
2328 value, so we have to express it through a scalar ref.
2329 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
2330 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
2331 what we wanted here.
2333 Another use of the subquery technique is when some SQL clauses need
2334 parentheses, as it often occurs with some proprietary SQL extensions
2335 like for example fulltext expressions, geospatial expressions,
2336 NATIVE clauses, etc. Here is an example of a fulltext query in MySQL :
2339 -nest => \["MATCH (col1, col2) AGAINST (?)" => qw/apples/]
2342 Finally, here is an example where a subquery is used
2343 for expressing unary negation:
2345 my ($sub_stmt, @sub_bind)
2346 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
2347 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
2349 lname => {like => '%son%'},
2350 -nest => \["NOT ($sub_stmt)" => @sub_bind],
2355 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
2356 @bind = ('%son%', 10, 20)
2362 These pages could go on for a while, since the nesting of the data
2363 structures this module can handle are pretty much unlimited (the
2364 module implements the C<WHERE> expansion as a recursive function
2365 internally). Your best bet is to "play around" with the module a
2366 little to see how the data structures behave, and choose the best
2367 format for your data based on that.
2369 And of course, all the values above will probably be replaced with
2370 variables gotten from forms or the command line. After all, if you
2371 knew everything ahead of time, you wouldn't have to worry about
2372 dynamically-generating SQL and could just hardwire it into your
2378 =head1 ORDER BY CLAUSES
2380 Some functions take an order by clause. This can either be a scalar (just a
2381 column name,) a hash of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } >>,
2382 or an array of either of the two previous forms. Examples:
2384 Given | Will Generate
2385 ----------------------------------------------------------
2387 \'colA DESC' | ORDER BY colA DESC
2389 'colA' | ORDER BY colA
2391 [qw/colA colB/] | ORDER BY colA, colB
2393 {-asc => 'colA'} | ORDER BY colA ASC
2395 {-desc => 'colB'} | ORDER BY colB DESC
2397 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
2399 { -asc => [qw/colA colB] } | ORDER BY colA ASC, colB ASC
2402 { -asc => 'colA' }, | ORDER BY colA ASC, colB DESC,
2403 { -desc => [qw/colB/], | colC ASC, colD ASC
2404 { -asc => [qw/colC colD/],|
2406 ===========================================================
2410 =head1 SPECIAL OPERATORS
2412 my $sqlmaker = SQL::Abstract->new(special_ops => [
2416 my ($self, $field, $op, $arg) = @_;
2422 handler => 'method_name',
2426 A "special operator" is a SQL syntactic clause that can be
2427 applied to a field, instead of a usual binary operator.
2430 WHERE field IN (?, ?, ?)
2431 WHERE field BETWEEN ? AND ?
2432 WHERE MATCH(field) AGAINST (?, ?)
2434 Special operators IN and BETWEEN are fairly standard and therefore
2435 are builtin within C<SQL::Abstract> (as the overridable methods
2436 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
2437 like the MATCH .. AGAINST example above which is specific to MySQL,
2438 you can write your own operator handlers - supply a C<special_ops>
2439 argument to the C<new> method. That argument takes an arrayref of
2440 operator definitions; each operator definition is a hashref with two
2447 the regular expression to match the operator
2451 Either a coderef or a plain scalar method name. In both cases
2452 the expected return is C<< ($sql, @bind) >>.
2454 When supplied with a method name, it is simply called on the
2455 L<SQL::Abstract/> object as:
2457 $self->$method_name ($field, $op, $arg)
2461 $op is the part that matched the handler regex
2462 $field is the LHS of the operator
2465 When supplied with a coderef, it is called as:
2467 $coderef->($self, $field, $op, $arg)
2472 For example, here is an implementation
2473 of the MATCH .. AGAINST syntax for MySQL
2475 my $sqlmaker = SQL::Abstract->new(special_ops => [
2477 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
2478 {regex => qr/^match$/i,
2480 my ($self, $field, $op, $arg) = @_;
2481 $arg = [$arg] if not ref $arg;
2482 my $label = $self->_quote($field);
2483 my ($placeholder) = $self->_convert('?');
2484 my $placeholders = join ", ", (($placeholder) x @$arg);
2485 my $sql = $self->_sqlcase('match') . " ($label) "
2486 . $self->_sqlcase('against') . " ($placeholders) ";
2487 my @bind = $self->_bindtype($field, @$arg);
2488 return ($sql, @bind);
2495 =head1 UNARY OPERATORS
2497 my $sqlmaker = SQL::Abstract->new(unary_ops => [
2501 my ($self, $op, $arg) = @_;
2507 handler => 'method_name',
2511 A "unary operator" is a SQL syntactic clause that can be
2512 applied to a field - the operator goes before the field
2514 You can write your own operator handlers - supply a C<unary_ops>
2515 argument to the C<new> method. That argument takes an arrayref of
2516 operator definitions; each operator definition is a hashref with two
2523 the regular expression to match the operator
2527 Either a coderef or a plain scalar method name. In both cases
2528 the expected return is C<< $sql >>.
2530 When supplied with a method name, it is simply called on the
2531 L<SQL::Abstract/> object as:
2533 $self->$method_name ($op, $arg)
2537 $op is the part that matched the handler regex
2538 $arg is the RHS or argument of the operator
2540 When supplied with a coderef, it is called as:
2542 $coderef->($self, $op, $arg)
2550 Thanks to some benchmarking by Mark Stosberg, it turns out that
2551 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
2552 I must admit this wasn't an intentional design issue, but it's a
2553 byproduct of the fact that you get to control your C<DBI> handles
2556 To maximize performance, use a code snippet like the following:
2558 # prepare a statement handle using the first row
2559 # and then reuse it for the rest of the rows
2561 for my $href (@array_of_hashrefs) {
2562 $stmt ||= $sql->insert('table', $href);
2563 $sth ||= $dbh->prepare($stmt);
2564 $sth->execute($sql->values($href));
2567 The reason this works is because the keys in your C<$href> are sorted
2568 internally by B<SQL::Abstract>. Thus, as long as your data retains
2569 the same structure, you only have to generate the SQL the first time
2570 around. On subsequent queries, simply use the C<values> function provided
2571 by this module to return your values in the correct order.
2576 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
2577 really like this part (I do, at least). Building up a complex query
2578 can be as simple as the following:
2582 use CGI::FormBuilder;
2585 my $form = CGI::FormBuilder->new(...);
2586 my $sql = SQL::Abstract->new;
2588 if ($form->submitted) {
2589 my $field = $form->field;
2590 my $id = delete $field->{id};
2591 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
2594 Of course, you would still have to connect using C<DBI> to run the
2595 query, but the point is that if you make your form look like your
2596 table, the actual query script can be extremely simplistic.
2598 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
2599 a fast interface to returning and formatting data. I frequently
2600 use these three modules together to write complex database query
2601 apps in under 50 lines.
2606 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
2607 Great care has been taken to preserve the I<published> behavior
2608 documented in previous versions in the 1.* family; however,
2609 some features that were previously undocumented, or behaved
2610 differently from the documentation, had to be changed in order
2611 to clarify the semantics. Hence, client code that was relying
2612 on some dark areas of C<SQL::Abstract> v1.*
2613 B<might behave differently> in v1.50.
2615 The main changes are :
2621 support for literal SQL through the C<< \ [$sql, bind] >> syntax.
2625 support for the { operator => \"..." } construct (to embed literal SQL)
2629 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
2633 optional support for L<array datatypes|/"Inserting and Updating Arrays">
2637 defensive programming : check arguments
2641 fixed bug with global logic, which was previously implemented
2642 through global variables yielding side-effects. Prior versions would
2643 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
2644 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
2645 Now this is interpreted
2646 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
2651 fixed semantics of _bindtype on array args
2655 dropped the C<_anoncopy> of the %where tree. No longer necessary,
2656 we just avoid shifting arrays within that tree.
2660 dropped the C<_modlogic> function
2666 =head1 ACKNOWLEDGEMENTS
2668 There are a number of individuals that have really helped out with
2669 this module. Unfortunately, most of them submitted bugs via CPAN
2670 so I have no idea who they are! But the people I do know are:
2672 Ash Berlin (order_by hash term support)
2673 Matt Trout (DBIx::Class support)
2674 Mark Stosberg (benchmarking)
2675 Chas Owens (initial "IN" operator support)
2676 Philip Collins (per-field SQL functions)
2677 Eric Kolve (hashref "AND" support)
2678 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
2679 Dan Kubb (support for "quote_char" and "name_sep")
2680 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
2681 Laurent Dami (internal refactoring, multiple -nest, extensible list of special operators, literal SQL)
2682 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
2683 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
2684 Oliver Charles (support for "RETURNING" after "INSERT")
2690 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
2694 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
2696 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
2698 For support, your best bet is to try the C<DBIx::Class> users mailing list.
2699 While not an official support venue, C<DBIx::Class> makes heavy use of
2700 C<SQL::Abstract>, and as such list members there are very familiar with
2701 how to create queries.
2705 This module is free software; you may copy this under the same
2706 terms as perl itself (either the GNU General Public License or
2707 the Artistic License)