1 package SQL::Abstract; # see doc at end of file
3 use SQL::Abstract::_TempExtlib;
8 use Module::Runtime qw(use_module);
12 our $VERSION = '1.77';
14 # This would confuse some packagers
15 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
18 my($func) = (caller(1))[3];
19 Carp::carp "[$func] Warning: ", @_;
23 my($func) = (caller(1))[3];
24 Carp::croak "[$func] Fatal: ", @_;
27 has converter => (is => 'lazy', clearer => 'clear_converter');
30 is => 'ro', coerce => sub { $_[0] eq 'lower' ? 'lower' : undef }
34 is => 'ro', coerce => sub { uc($_[0]) }, default => sub { 'OR' }
38 is => 'ro', default => sub { 'normal' }
41 has cmp => (is => 'ro', default => sub { '=' });
43 has sqltrue => (is => 'ro', default => sub { '1=1' });
44 has sqlfalse => (is => 'ro', default => sub { '0=1' });
46 has special_ops => (is => 'ro', default => sub { [] });
47 has unary_ops => (is => 'ro', default => sub { [] });
50 # need to guard against ()'s in column names too, but this will break tons of
51 # hacks... ideas anyone?
53 has injection_guard => (
64 has renderer => (is => 'lazy', clearer => 'clear_renderer');
67 is => 'rw', default => sub { '.' },
69 $_[0]->clear_renderer;
70 $_[0]->clear_converter;
77 $_[0]->clear_renderer;
78 $_[0]->clear_converter;
82 has collapse_aliases => (
88 is => 'rw', default => sub { 1 },
90 $_[0]->clear_renderer;
91 $_[0]->clear_converter;
95 has convert => (is => 'ro');
97 has array_datatypes => (is => 'ro');
99 has converter_class => (
100 is => 'rw', lazy => 1, builder => '_build_converter_class',
101 trigger => sub { shift->clear_converter },
104 sub _build_converter_class {
105 use_module('SQL::Abstract::Converter')
108 has renderer_class => (
109 is => 'rw', lazy => 1, clearer => 1, builder => 1,
110 trigger => sub { shift->clear_renderer },
113 after clear_renderer_class => sub { shift->clear_renderer };
115 sub _build_renderer_class {
117 my ($class, @roles) = (
118 $self->_build_base_renderer_class, $self->_build_renderer_roles
120 return $class unless @roles;
121 return use_module('Moo::Role')->create_class_with_roles($class, @roles);
124 sub _build_base_renderer_class {
125 use_module('Data::Query::Renderer::SQL::Naive')
128 sub _build_renderer_roles { () }
130 sub _converter_args {
132 Scalar::Util::weaken($self);
134 lower_case => $self->case,
135 default_logic => $self->logic,
136 bind_meta => not($self->bindtype eq 'normal'),
137 identifier_sep => $self->name_sep,
138 (map +($_ => $self->$_), qw(
139 cmp sqltrue sqlfalse injection_guard convert array_datatypes
143 my $sub = $_->{handler};
146 handler => sub { $self->$sub(@_) }
148 } @{$self->special_ops}
150 renderer_will_quote => (
151 defined($self->quote_char) and $self->always_quote
156 sub _build_converter {
158 $self->converter_class->new($self->_converter_args);
164 for ($self->quote_char) {
165 $chars = defined() ? (ref() ? $_ : [$_]) : ['',''];
168 quote_chars => $chars, always_quote => $self->always_quote,
169 identifier_sep => $self->name_sep,
170 collapse_aliases => $self->collapse_aliases,
171 ($self->case ? (lc_keywords => 1) : ()), # always 'lower' if it exists
175 sub _build_renderer {
177 $self->renderer_class->new($self->_renderer_args);
181 my ($self, $dq) = @_;
185 my ($sql, @bind) = @{$self->renderer->render($dq)};
187 ($self->{bindtype} eq 'normal'
188 ? ($sql, map $_->{value}, @bind)
189 : ($sql, map [ $_->{value_meta}, $_->{value} ], @bind)
195 my ($self, $type, @args) = @_;
196 $self->_render_dq($self->converter->${\"_${type}_to_dq"}(@args));
199 sub insert { shift->_render_sqla(insert => @_) }
201 sub update { shift->_render_sqla(update => @_) }
203 sub select { shift->_render_sqla(select => @_) }
205 sub delete { shift->_render_sqla(delete => @_) }
208 my ($self, $where, $order) = @_;
214 ($sql, @bind) = $self->_recurse_where($where) if defined($where);
215 $sql = $sql ? $self->_sqlcase(' where ') . "( $sql )" : '';
219 $sql .= $self->_order_by($order);
222 return wantarray ? ($sql, @bind) : $sql;
225 sub _recurse_where { shift->_render_sqla(where => @_) }
228 my ($self, $arg) = @_;
229 if (my $dq = $self->converter->_order_by_to_dq($arg)) {
230 # SQLA generates ' ORDER BY foo'. The hilarity.
232 ? do { my @r = $self->_render_dq($dq); $r[0] = ' '.$r[0]; @r }
233 : ' '.$self->_render_dq($dq);
239 # highly optimized, as it's called way too often
241 # my ($self, $label) = @_;
243 return '' unless defined $_[1];
244 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
246 unless ($_[0]->{quote_char}) {
247 $_[0]->_assert_pass_injection_guard($_[1]);
251 my $qref = ref $_[0]->{quote_char};
254 ($l, $r) = ( $_[0]->{quote_char}, $_[0]->{quote_char} );
256 elsif ($qref eq 'ARRAY') {
257 ($l, $r) = @{$_[0]->{quote_char}};
260 puke "Unsupported quote_char format: $_[0]->{quote_char}";
263 # parts containing * are naturally unquoted
264 return join( $_[0]->{name_sep}||'', map
265 { $_ eq '*' ? $_ : $l . $_ . $r }
266 ( $_[0]->{name_sep} ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) : $_[1] )
270 sub _assert_pass_injection_guard {
271 if ($_[1] =~ $_[0]->{injection_guard}) {
272 my $class = ref $_[0];
273 die "Possible SQL injection attempt '$_[1]'. If this is indeed a part of "
274 . "the desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply "
275 . "your own {injection_guard} attribute to ${class}->new()"
279 # Conversion, if applicable
281 #my ($self, $arg) = @_;
282 if ($_[0]->{convert}) {
283 return $_[0]->_sqlcase($_[0]->{convert}) .'(' . $_[1] . ')';
290 #my ($self, $col, @vals) = @_;
291 # called often - tighten code
292 return $_[0]->{bindtype} eq 'columns'
293 ? map {[$_[1], $_]} @_[2 .. $#_]
298 # Dies if any element of @bind is not in [colname => value] format
299 # if bindtype is 'columns'.
300 sub _assert_bindval_matches_bindtype {
301 # my ($self, @bind) = @_;
303 if ($self->{bindtype} eq 'columns') {
305 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
306 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
312 # Fix SQL case, if so requested
314 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
315 # don't touch the argument ... crooked logic, but let's not change it!
316 return $_[0]->{case} ? $_[1] : uc($_[1]);
321 my $data = shift || return;
322 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
323 unless ref $data eq 'HASH';
326 foreach my $k ( sort keys %$data ) {
328 local our $Cur_Col_Meta = $k;
329 my ($sql, @bind) = $self->_render_sqla(
332 push @all_bind, @bind;
341 my(@sql, @sqlq, @sqlv);
345 if ($ref eq 'HASH') {
346 for my $k (sort keys %$_) {
349 my $label = $self->_quote($k);
351 # literal SQL with bind
352 my ($sql, @bind) = @$v;
353 $self->_assert_bindval_matches_bindtype(@bind);
354 push @sqlq, "$label = $sql";
356 } elsif ($r eq 'SCALAR') {
357 # literal SQL without bind
358 push @sqlq, "$label = $$v";
360 push @sqlq, "$label = ?";
361 push @sqlv, $self->_bindtype($k, $v);
364 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
365 } elsif ($ref eq 'ARRAY') {
366 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
369 if ($r eq 'ARRAY') { # literal SQL with bind
370 my ($sql, @bind) = @$v;
371 $self->_assert_bindval_matches_bindtype(@bind);
374 } elsif ($r eq 'SCALAR') { # literal SQL without bind
375 # embedded literal SQL
382 push @sql, '(' . join(', ', @sqlq) . ')';
383 } elsif ($ref eq 'SCALAR') {
387 # strings get case twiddled
388 push @sql, $self->_sqlcase($_);
392 my $sql = join ' ', @sql;
394 # this is pretty tricky
395 # if ask for an array, return ($stmt, @bind)
396 # otherwise, s/?/shift @sqlv/ to put it inline
398 return ($sql, @sqlv);
400 1 while $sql =~ s/\?/my $d = shift(@sqlv);
401 ref $d ? $d->[1] : $d/e;
413 SQL::Abstract - Generate SQL from Perl data structures
419 my $sql = SQL::Abstract->new;
421 my($stmt, @bind) = $sql->select($source, \@fields, \%where, \@order);
423 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
425 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
427 my($stmt, @bind) = $sql->delete($table, \%where);
429 # Then, use these in your DBI statements
430 my $sth = $dbh->prepare($stmt);
431 $sth->execute(@bind);
433 # Just generate the WHERE clause
434 my($stmt, @bind) = $sql->where(\%where, \@order);
436 # Return values in the same order, for hashed queries
437 # See PERFORMANCE section for more details
438 my @bind = $sql->values(\%fieldvals);
442 This module was inspired by the excellent L<DBIx::Abstract>.
443 However, in using that module I found that what I really wanted
444 to do was generate SQL, but still retain complete control over my
445 statement handles and use the DBI interface. So, I set out to
446 create an abstract SQL generation module.
448 While based on the concepts used by L<DBIx::Abstract>, there are
449 several important differences, especially when it comes to WHERE
450 clauses. I have modified the concepts used to make the SQL easier
451 to generate from Perl data structures and, IMO, more intuitive.
452 The underlying idea is for this module to do what you mean, based
453 on the data structures you provide it. The big advantage is that
454 you don't have to modify your code every time your data changes,
455 as this module figures it out.
457 To begin with, an SQL INSERT is as easy as just specifying a hash
458 of C<key=value> pairs:
461 name => 'Jimbo Bobson',
462 phone => '123-456-7890',
463 address => '42 Sister Lane',
465 state => 'Louisiana',
468 The SQL can then be generated with this:
470 my($stmt, @bind) = $sql->insert('people', \%data);
472 Which would give you something like this:
474 $stmt = "INSERT INTO people
475 (address, city, name, phone, state)
476 VALUES (?, ?, ?, ?, ?)";
477 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
478 '123-456-7890', 'Louisiana');
480 These are then used directly in your DBI code:
482 my $sth = $dbh->prepare($stmt);
483 $sth->execute(@bind);
485 =head2 Inserting and Updating Arrays
487 If your database has array types (like for example Postgres),
488 activate the special option C<< array_datatypes => 1 >>
489 when creating the C<SQL::Abstract> object.
490 Then you may use an arrayref to insert and update database array types:
492 my $sql = SQL::Abstract->new(array_datatypes => 1);
494 planets => [qw/Mercury Venus Earth Mars/]
497 my($stmt, @bind) = $sql->insert('solar_system', \%data);
501 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
503 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
506 =head2 Inserting and Updating SQL
508 In order to apply SQL functions to elements of your C<%data> you may
509 specify a reference to an arrayref for the given hash value. For example,
510 if you need to execute the Oracle C<to_date> function on a value, you can
511 say something like this:
515 date_entered => \["to_date(?,'MM/DD/YYYY')", "03/02/2003"],
518 The first value in the array is the actual SQL. Any other values are
519 optional and would be included in the bind values array. This gives
522 my($stmt, @bind) = $sql->insert('people', \%data);
524 $stmt = "INSERT INTO people (name, date_entered)
525 VALUES (?, to_date(?,'MM/DD/YYYY'))";
526 @bind = ('Bill', '03/02/2003');
528 An UPDATE is just as easy, all you change is the name of the function:
530 my($stmt, @bind) = $sql->update('people', \%data);
532 Notice that your C<%data> isn't touched; the module will generate
533 the appropriately quirky SQL for you automatically. Usually you'll
534 want to specify a WHERE clause for your UPDATE, though, which is
535 where handling C<%where> hashes comes in handy...
537 =head2 Complex where statements
539 This module can generate pretty complicated WHERE statements
540 easily. For example, simple C<key=value> pairs are taken to mean
541 equality, and if you want to see if a field is within a set
542 of values, you can use an arrayref. Let's say we wanted to
543 SELECT some data based on this criteria:
547 worker => ['nwiger', 'rcwe', 'sfz'],
548 status => { '!=', 'completed' }
551 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
553 The above would give you something like this:
555 $stmt = "SELECT * FROM tickets WHERE
556 ( requestor = ? ) AND ( status != ? )
557 AND ( worker = ? OR worker = ? OR worker = ? )";
558 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
560 Which you could then use in DBI code like so:
562 my $sth = $dbh->prepare($stmt);
563 $sth->execute(@bind);
569 The functions are simple. There's one for each major SQL operation,
570 and a constructor you use first. The arguments are specified in a
571 similar order to each function (table, then fields, then a where
572 clause) to try and simplify things.
577 =head2 new(option => 'value')
579 The C<new()> function takes a list of options and values, and returns
580 a new B<SQL::Abstract> object which can then be used to generate SQL
581 through the methods below. The options accepted are:
587 If set to 'lower', then SQL will be generated in all lowercase. By
588 default SQL is generated in "textbook" case meaning something like:
590 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
592 Any setting other than 'lower' is ignored.
596 This determines what the default comparison operator is. By default
597 it is C<=>, meaning that a hash like this:
599 %where = (name => 'nwiger', email => 'nate@wiger.org');
601 Will generate SQL like this:
603 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
605 However, you may want loose comparisons by default, so if you set
606 C<cmp> to C<like> you would get SQL such as:
608 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
610 You can also override the comparison on an individual basis - see
611 the huge section on L</"WHERE CLAUSES"> at the bottom.
613 =item sqltrue, sqlfalse
615 Expressions for inserting boolean values within SQL statements.
616 By default these are C<1=1> and C<1=0>. They are used
617 by the special operators C<-in> and C<-not_in> for generating
618 correct SQL even when the argument is an empty array (see below).
622 This determines the default logical operator for multiple WHERE
623 statements in arrays or hashes. If absent, the default logic is "or"
624 for arrays, and "and" for hashes. This means that a WHERE
628 event_date => {'>=', '2/13/99'},
629 event_date => {'<=', '4/24/03'},
632 will generate SQL like this:
634 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
636 This is probably not what you want given this query, though (look
637 at the dates). To change the "OR" to an "AND", simply specify:
639 my $sql = SQL::Abstract->new(logic => 'and');
641 Which will change the above C<WHERE> to:
643 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
645 The logic can also be changed locally by inserting
646 a modifier in front of an arrayref :
648 @where = (-and => [event_date => {'>=', '2/13/99'},
649 event_date => {'<=', '4/24/03'} ]);
651 See the L</"WHERE CLAUSES"> section for explanations.
655 This will automatically convert comparisons using the specified SQL
656 function for both column and value. This is mostly used with an argument
657 of C<upper> or C<lower>, so that the SQL will have the effect of
658 case-insensitive "searches". For example, this:
660 $sql = SQL::Abstract->new(convert => 'upper');
661 %where = (keywords => 'MaKe iT CAse inSeNSItive');
663 Will turn out the following SQL:
665 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
667 The conversion can be C<upper()>, C<lower()>, or any other SQL function
668 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
669 not validate this option; it will just pass through what you specify verbatim).
673 This is a kludge because many databases suck. For example, you can't
674 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
675 Instead, you have to use C<bind_param()>:
677 $sth->bind_param(1, 'reg data');
678 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
680 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
681 which loses track of which field each slot refers to. Fear not.
683 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
684 Currently, you can specify either C<normal> (default) or C<columns>. If you
685 specify C<columns>, you will get an array that looks like this:
687 my $sql = SQL::Abstract->new(bindtype => 'columns');
688 my($stmt, @bind) = $sql->insert(...);
691 [ 'column1', 'value1' ],
692 [ 'column2', 'value2' ],
693 [ 'column3', 'value3' ],
696 You can then iterate through this manually, using DBI's C<bind_param()>.
698 $sth->prepare($stmt);
701 my($col, $data) = @$_;
702 if ($col eq 'details' || $col eq 'comments') {
703 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
704 } elsif ($col eq 'image') {
705 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
707 $sth->bind_param($i, $data);
711 $sth->execute; # execute without @bind now
713 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
714 Basically, the advantage is still that you don't have to care which fields
715 are or are not included. You could wrap that above C<for> loop in a simple
716 sub called C<bind_fields()> or something and reuse it repeatedly. You still
717 get a layer of abstraction over manual SQL specification.
719 Note that if you set L</bindtype> to C<columns>, the C<\[$sql, @bind]>
720 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
721 will expect the bind values in this format.
725 This is the character that a table or column name will be quoted
726 with. By default this is an empty string, but you could set it to
727 the character C<`>, to generate SQL like this:
729 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
731 Alternatively, you can supply an array ref of two items, the first being the left
732 hand quote character, and the second the right hand quote character. For
733 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
734 that generates SQL like this:
736 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
738 Quoting is useful if you have tables or columns names that are reserved
739 words in your database's SQL dialect.
743 This is the character that separates a table and column name. It is
744 necessary to specify this when the C<quote_char> option is selected,
745 so that tables and column names can be individually quoted like this:
747 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
749 =item injection_guard
751 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
752 column name specified in a query structure. This is a safety mechanism to avoid
753 injection attacks when mishandling user input e.g.:
755 my %condition_as_column_value_pairs = get_values_from_user();
756 $sqla->select( ... , \%condition_as_column_value_pairs );
758 If the expression matches an exception is thrown. Note that literal SQL
759 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
761 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
763 =item array_datatypes
765 When this option is true, arrayrefs in INSERT or UPDATE are
766 interpreted as array datatypes and are passed directly
768 When this option is false, arrayrefs are interpreted
769 as literal SQL, just like refs to arrayrefs
770 (but this behavior is for backwards compatibility; when writing
771 new queries, use the "reference to arrayref" syntax
777 Takes a reference to a list of "special operators"
778 to extend the syntax understood by L<SQL::Abstract>.
779 See section L</"SPECIAL OPERATORS"> for details.
783 Takes a reference to a list of "unary operators"
784 to extend the syntax understood by L<SQL::Abstract>.
785 See section L</"UNARY OPERATORS"> for details.
791 =head2 insert($table, \@values || \%fieldvals, \%options)
793 This is the simplest function. You simply give it a table name
794 and either an arrayref of values or hashref of field/value pairs.
795 It returns an SQL INSERT statement and a list of bind values.
796 See the sections on L</"Inserting and Updating Arrays"> and
797 L</"Inserting and Updating SQL"> for information on how to insert
798 with those data types.
800 The optional C<\%options> hash reference may contain additional
801 options to generate the insert SQL. Currently supported options
808 Takes either a scalar of raw SQL fields, or an array reference of
809 field names, and adds on an SQL C<RETURNING> statement at the end.
810 This allows you to return data generated by the insert statement
811 (such as row IDs) without performing another C<SELECT> statement.
812 Note, however, this is not part of the SQL standard and may not
813 be supported by all database engines.
817 =head2 update($table, \%fieldvals, \%where)
819 This takes a table, hashref of field/value pairs, and an optional
820 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
822 See the sections on L</"Inserting and Updating Arrays"> and
823 L</"Inserting and Updating SQL"> for information on how to insert
824 with those data types.
826 =head2 select($source, $fields, $where, $order)
828 This returns a SQL SELECT statement and associated list of bind values, as
829 specified by the arguments :
835 Specification of the 'FROM' part of the statement.
836 The argument can be either a plain scalar (interpreted as a table
837 name, will be quoted), or an arrayref (interpreted as a list
838 of table names, joined by commas, quoted), or a scalarref
839 (literal table name, not quoted), or a ref to an arrayref
840 (list of literal table names, joined by commas, not quoted).
844 Specification of the list of fields to retrieve from
846 The argument can be either an arrayref (interpreted as a list
847 of field names, will be joined by commas and quoted), or a
848 plain scalar (literal SQL, not quoted).
849 Please observe that this API is not as flexible as that of
850 the first argument C<$source>, for backwards compatibility reasons.
854 Optional argument to specify the WHERE part of the query.
855 The argument is most often a hashref, but can also be
856 an arrayref or plain scalar --
857 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
861 Optional argument to specify the ORDER BY part of the query.
862 The argument can be a scalar, a hashref or an arrayref
863 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
869 =head2 delete($table, \%where)
871 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
872 It returns an SQL DELETE statement and list of bind values.
874 =head2 where(\%where, \@order)
876 This is used to generate just the WHERE clause. For example,
877 if you have an arbitrary data structure and know what the
878 rest of your SQL is going to look like, but want an easy way
879 to produce a WHERE clause, use this. It returns an SQL WHERE
880 clause and list of bind values.
883 =head2 values(\%data)
885 This just returns the values from the hash C<%data>, in the same
886 order that would be returned from any of the other above queries.
887 Using this allows you to markedly speed up your queries if you
888 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
890 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
892 Warning: This is an experimental method and subject to change.
894 This returns arbitrarily generated SQL. It's a really basic shortcut.
895 It will return two different things, depending on return context:
897 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
898 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
900 These would return the following:
903 $stmt = "CREATE TABLE test (?, ?)";
904 @bind = (field1, field2);
906 # Second calling form
907 $stmt_and_val = "CREATE TABLE test (field1, field2)";
909 Depending on what you're trying to do, it's up to you to choose the correct
910 format. In this example, the second form is what you would want.
914 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
918 ALTER SESSION SET nls_date_format = 'MM/YY'
920 You get the idea. Strings get their case twiddled, but everything
921 else remains verbatim.
927 This module uses a variation on the idea from L<DBIx::Abstract>. It
928 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
929 module is that things in arrays are OR'ed, and things in hashes
932 The easiest way to explain is to show lots of examples. After
933 each C<%where> hash shown, it is assumed you used:
935 my($stmt, @bind) = $sql->where(\%where);
937 However, note that the C<%where> hash can be used directly in any
938 of the other functions as well, as described above.
940 =head2 Key-value pairs
942 So, let's get started. To begin, a simple hash:
946 status => 'completed'
949 Is converted to SQL C<key = val> statements:
951 $stmt = "WHERE user = ? AND status = ?";
952 @bind = ('nwiger', 'completed');
954 One common thing I end up doing is having a list of values that
955 a field can be in. To do this, simply specify a list inside of
960 status => ['assigned', 'in-progress', 'pending'];
963 This simple code will create the following:
965 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
966 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
968 A field associated to an empty arrayref will be considered a
969 logical false and will generate 0=1.
971 =head2 Tests for NULL values
973 If the value part is C<undef> then this is converted to SQL <IS NULL>
982 $stmt = "WHERE user = ? AND status IS NULL";
985 To test if a column IS NOT NULL:
989 status => { '!=', undef },
992 =head2 Specific comparison operators
994 If you want to specify a different type of operator for your comparison,
995 you can use a hashref for a given column:
999 status => { '!=', 'completed' }
1002 Which would generate:
1004 $stmt = "WHERE user = ? AND status != ?";
1005 @bind = ('nwiger', 'completed');
1007 To test against multiple values, just enclose the values in an arrayref:
1009 status => { '=', ['assigned', 'in-progress', 'pending'] };
1011 Which would give you:
1013 "WHERE status = ? OR status = ? OR status = ?"
1016 The hashref can also contain multiple pairs, in which case it is expanded
1017 into an C<AND> of its elements:
1021 status => { '!=', 'completed', -not_like => 'pending%' }
1024 # Or more dynamically, like from a form
1025 $where{user} = 'nwiger';
1026 $where{status}{'!='} = 'completed';
1027 $where{status}{'-not_like'} = 'pending%';
1029 # Both generate this
1030 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
1031 @bind = ('nwiger', 'completed', 'pending%');
1034 To get an OR instead, you can combine it with the arrayref idea:
1038 priority => [ { '=', 2 }, { '>', 5 } ]
1041 Which would generate:
1043 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
1044 @bind = ('2', '5', 'nwiger');
1046 If you want to include literal SQL (with or without bind values), just use a
1047 scalar reference or array reference as the value:
1050 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
1051 date_expires => { '<' => \"now()" }
1054 Which would generate:
1056 $stmt = "WHERE date_entered > "to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
1057 @bind = ('11/26/2008');
1060 =head2 Logic and nesting operators
1062 In the example above,
1063 there is a subtle trap if you want to say something like
1064 this (notice the C<AND>):
1066 WHERE priority != ? AND priority != ?
1068 Because, in Perl you I<can't> do this:
1070 priority => { '!=', 2, '!=', 1 }
1072 As the second C<!=> key will obliterate the first. The solution
1073 is to use the special C<-modifier> form inside an arrayref:
1075 priority => [ -and => {'!=', 2},
1079 Normally, these would be joined by C<OR>, but the modifier tells it
1080 to use C<AND> instead. (Hint: You can use this in conjunction with the
1081 C<logic> option to C<new()> in order to change the way your queries
1082 work by default.) B<Important:> Note that the C<-modifier> goes
1083 B<INSIDE> the arrayref, as an extra first element. This will
1084 B<NOT> do what you think it might:
1086 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
1088 Here is a quick list of equivalencies, since there is some overlap:
1091 status => {'!=', 'completed', 'not like', 'pending%' }
1092 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
1095 status => {'=', ['assigned', 'in-progress']}
1096 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
1097 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
1101 =head2 Special operators : IN, BETWEEN, etc.
1103 You can also use the hashref format to compare a list of fields using the
1104 C<IN> comparison operator, by specifying the list as an arrayref:
1107 status => 'completed',
1108 reportid => { -in => [567, 2335, 2] }
1111 Which would generate:
1113 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
1114 @bind = ('completed', '567', '2335', '2');
1116 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
1119 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
1120 (by default : C<1=0>). Similarly, C<< -not_in => [] >> generates
1121 'sqltrue' (by default : C<1=1>).
1123 In addition to the array you can supply a chunk of literal sql or
1124 literal sql with bind:
1127 customer => { -in => \[
1128 'SELECT cust_id FROM cust WHERE balance > ?',
1131 status => { -in => \'SELECT status_codes FROM states' },
1137 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
1138 AND status IN ( SELECT status_codes FROM states )
1142 Finally, if the argument to C<-in> is not a reference, it will be
1143 treated as a single-element array.
1145 Another pair of operators is C<-between> and C<-not_between>,
1146 used with an arrayref of two values:
1150 completion_date => {
1151 -not_between => ['2002-10-01', '2003-02-06']
1157 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
1159 Just like with C<-in> all plausible combinations of literal SQL
1163 start0 => { -between => [ 1, 2 ] },
1164 start1 => { -between => \["? AND ?", 1, 2] },
1165 start2 => { -between => \"lower(x) AND upper(y)" },
1166 start3 => { -between => [
1168 \["upper(?)", 'stuff' ],
1175 ( start0 BETWEEN ? AND ? )
1176 AND ( start1 BETWEEN ? AND ? )
1177 AND ( start2 BETWEEN lower(x) AND upper(y) )
1178 AND ( start3 BETWEEN lower(x) AND upper(?) )
1180 @bind = (1, 2, 1, 2, 'stuff');
1183 These are the two builtin "special operators"; but the
1184 list can be expanded : see section L</"SPECIAL OPERATORS"> below.
1186 =head2 Unary operators: bool
1188 If you wish to test against boolean columns or functions within your
1189 database you can use the C<-bool> and C<-not_bool> operators. For
1190 example to test the column C<is_user> being true and the column
1191 C<is_enabled> being false you would use:-
1195 -not_bool => 'is_enabled',
1200 WHERE is_user AND NOT is_enabled
1202 If a more complex combination is required, testing more conditions,
1203 then you should use the and/or operators:-
1208 -not_bool => { two=> { -rlike => 'bar' } },
1209 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
1220 (NOT ( three = ? OR three > ? ))
1223 =head2 Nested conditions, -and/-or prefixes
1225 So far, we've seen how multiple conditions are joined with a top-level
1226 C<AND>. We can change this by putting the different conditions we want in
1227 hashes and then putting those hashes in an array. For example:
1232 status => { -like => ['pending%', 'dispatched'] },
1236 status => 'unassigned',
1240 This data structure would create the following:
1242 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
1243 OR ( user = ? AND status = ? ) )";
1244 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
1247 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
1248 to change the logic inside :
1254 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
1255 -or => { workhrs => {'<', 50}, geo => 'EURO' },
1262 WHERE ( user = ? AND (
1263 ( workhrs > ? AND geo = ? )
1264 OR ( workhrs < ? OR geo = ? )
1267 =head3 Algebraic inconsistency, for historical reasons
1269 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
1270 operator goes C<outside> of the nested structure; whereas when connecting
1271 several constraints on one column, the C<-and> operator goes
1272 C<inside> the arrayref. Here is an example combining both features :
1275 -and => [a => 1, b => 2],
1276 -or => [c => 3, d => 4],
1277 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
1282 WHERE ( ( ( a = ? AND b = ? )
1283 OR ( c = ? OR d = ? )
1284 OR ( e LIKE ? AND e LIKE ? ) ) )
1286 This difference in syntax is unfortunate but must be preserved for
1287 historical reasons. So be careful : the two examples below would
1288 seem algebraically equivalent, but they are not
1290 {col => [-and => {-like => 'foo%'}, {-like => '%bar'}]}
1291 # yields : WHERE ( ( col LIKE ? AND col LIKE ? ) )
1293 [-and => {col => {-like => 'foo%'}, {col => {-like => '%bar'}}]]
1294 # yields : WHERE ( ( col LIKE ? OR col LIKE ? ) )
1297 =head2 Literal SQL and value type operators
1299 The basic premise of SQL::Abstract is that in WHERE specifications the "left
1300 side" is a column name and the "right side" is a value (normally rendered as
1301 a placeholder). This holds true for both hashrefs and arrayref pairs as you
1302 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
1303 alter this behavior. There are several ways of doing so.
1307 This is a virtual operator that signals the string to its right side is an
1308 identifier (a column name) and not a value. For example to compare two
1309 columns you would write:
1312 priority => { '<', 2 },
1313 requestor => { -ident => 'submitter' },
1318 $stmt = "WHERE priority < ? AND requestor = submitter";
1321 If you are maintaining legacy code you may see a different construct as
1322 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
1327 This is a virtual operator that signals that the construct to its right side
1328 is a value to be passed to DBI. This is for example necessary when you want
1329 to write a where clause against an array (for RDBMS that support such
1330 datatypes). For example:
1333 array => { -value => [1, 2, 3] }
1338 $stmt = 'WHERE array = ?';
1339 @bind = ([1, 2, 3]);
1341 Note that if you were to simply say:
1347 the result would probably not be what you wanted:
1349 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
1354 Finally, sometimes only literal SQL will do. To include a random snippet
1355 of SQL verbatim, you specify it as a scalar reference. Consider this only
1356 as a last resort. Usually there is a better way. For example:
1359 priority => { '<', 2 },
1360 requestor => { -in => \'(SELECT name FROM hitmen)' },
1365 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
1368 Note that in this example, you only get one bind parameter back, since
1369 the verbatim SQL is passed as part of the statement.
1373 Never use untrusted input as a literal SQL argument - this is a massive
1374 security risk (there is no way to check literal snippets for SQL
1375 injections and other nastyness). If you need to deal with untrusted input
1376 use literal SQL with placeholders as described next.
1378 =head3 Literal SQL with placeholders and bind values (subqueries)
1380 If the literal SQL to be inserted has placeholders and bind values,
1381 use a reference to an arrayref (yes this is a double reference --
1382 not so common, but perfectly legal Perl). For example, to find a date
1383 in Postgres you can use something like this:
1386 date_column => \[q/= date '2008-09-30' - ?::integer/, 10/]
1391 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
1394 Note that you must pass the bind values in the same format as they are returned
1395 by L</where>. That means that if you set L</bindtype> to C<columns>, you must
1396 provide the bind values in the C<< [ column_meta => value ] >> format, where
1397 C<column_meta> is an opaque scalar value; most commonly the column name, but
1398 you can use any scalar value (including references and blessed references),
1399 L<SQL::Abstract> will simply pass it through intact. So if C<bindtype> is set
1400 to C<columns> the above example will look like:
1403 date_column => \[q/= date '2008-09-30' - ?::integer/, [ dummy => 10 ]/]
1406 Literal SQL is especially useful for nesting parenthesized clauses in the
1407 main SQL query. Here is a first example :
1409 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
1413 bar => \["IN ($sub_stmt)" => @sub_bind],
1418 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
1419 WHERE c2 < ? AND c3 LIKE ?))";
1420 @bind = (1234, 100, "foo%");
1422 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
1423 are expressed in the same way. Of course the C<$sub_stmt> and
1424 its associated bind values can be generated through a former call
1427 my ($sub_stmt, @sub_bind)
1428 = $sql->select("t1", "c1", {c2 => {"<" => 100},
1429 c3 => {-like => "foo%"}});
1432 bar => \["> ALL ($sub_stmt)" => @sub_bind],
1435 In the examples above, the subquery was used as an operator on a column;
1436 but the same principle also applies for a clause within the main C<%where>
1437 hash, like an EXISTS subquery :
1439 my ($sub_stmt, @sub_bind)
1440 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
1441 my %where = ( -and => [
1443 \["EXISTS ($sub_stmt)" => @sub_bind],
1448 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
1449 WHERE c1 = ? AND c2 > t0.c0))";
1453 Observe that the condition on C<c2> in the subquery refers to
1454 column C<t0.c0> of the main query : this is I<not> a bind
1455 value, so we have to express it through a scalar ref.
1456 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
1457 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
1458 what we wanted here.
1460 Finally, here is an example where a subquery is used
1461 for expressing unary negation:
1463 my ($sub_stmt, @sub_bind)
1464 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
1465 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
1467 lname => {like => '%son%'},
1468 \["NOT ($sub_stmt)" => @sub_bind],
1473 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
1474 @bind = ('%son%', 10, 20)
1476 =head3 Deprecated usage of Literal SQL
1478 Below are some examples of archaic use of literal SQL. It is shown only as
1479 reference for those who deal with legacy code. Each example has a much
1480 better, cleaner and safer alternative that users should opt for in new code.
1486 my %where = ( requestor => \'IS NOT NULL' )
1488 $stmt = "WHERE requestor IS NOT NULL"
1490 This used to be the way of generating NULL comparisons, before the handling
1491 of C<undef> got formalized. For new code please use the superior syntax as
1492 described in L</Tests for NULL values>.
1496 my %where = ( requestor => \'= submitter' )
1498 $stmt = "WHERE requestor = submitter"
1500 This used to be the only way to compare columns. Use the superior L</-ident>
1501 method for all new code. For example an identifier declared in such a way
1502 will be properly quoted if L</quote_char> is properly set, while the legacy
1503 form will remain as supplied.
1507 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
1509 $stmt = "WHERE completed > ? AND is_ready"
1510 @bind = ('2012-12-21')
1512 Using an empty string literal used to be the only way to express a boolean.
1513 For all new code please use the much more readable
1514 L<-bool|/Unary operators: bool> operator.
1520 These pages could go on for a while, since the nesting of the data
1521 structures this module can handle are pretty much unlimited (the
1522 module implements the C<WHERE> expansion as a recursive function
1523 internally). Your best bet is to "play around" with the module a
1524 little to see how the data structures behave, and choose the best
1525 format for your data based on that.
1527 And of course, all the values above will probably be replaced with
1528 variables gotten from forms or the command line. After all, if you
1529 knew everything ahead of time, you wouldn't have to worry about
1530 dynamically-generating SQL and could just hardwire it into your
1533 =head1 ORDER BY CLAUSES
1535 Some functions take an order by clause. This can either be a scalar (just a
1536 column name,) a hash of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } >>,
1537 or an array of either of the two previous forms. Examples:
1539 Given | Will Generate
1540 ----------------------------------------------------------
1542 \'colA DESC' | ORDER BY colA DESC
1544 'colA' | ORDER BY colA
1546 [qw/colA colB/] | ORDER BY colA, colB
1548 {-asc => 'colA'} | ORDER BY colA ASC
1550 {-desc => 'colB'} | ORDER BY colB DESC
1552 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
1554 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
1557 { -asc => 'colA' }, | ORDER BY colA ASC, colB DESC,
1558 { -desc => [qw/colB/], | colC ASC, colD ASC
1559 { -asc => [qw/colC colD/],|
1561 ===========================================================
1565 =head1 SPECIAL OPERATORS
1567 my $sqlmaker = SQL::Abstract->new(special_ops => [
1571 my ($self, $field, $op, $arg) = @_;
1577 handler => 'method_name',
1581 A "special operator" is a SQL syntactic clause that can be
1582 applied to a field, instead of a usual binary operator.
1585 WHERE field IN (?, ?, ?)
1586 WHERE field BETWEEN ? AND ?
1587 WHERE MATCH(field) AGAINST (?, ?)
1589 Special operators IN and BETWEEN are fairly standard and therefore
1590 are builtin within C<SQL::Abstract> (as the overridable methods
1591 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
1592 like the MATCH .. AGAINST example above which is specific to MySQL,
1593 you can write your own operator handlers - supply a C<special_ops>
1594 argument to the C<new> method. That argument takes an arrayref of
1595 operator definitions; each operator definition is a hashref with two
1602 the regular expression to match the operator
1606 Either a coderef or a plain scalar method name. In both cases
1607 the expected return is C<< ($sql, @bind) >>.
1609 When supplied with a method name, it is simply called on the
1610 L<SQL::Abstract/> object as:
1612 $self->$method_name ($field, $op, $arg)
1616 $op is the part that matched the handler regex
1617 $field is the LHS of the operator
1620 When supplied with a coderef, it is called as:
1622 $coderef->($self, $field, $op, $arg)
1627 For example, here is an implementation
1628 of the MATCH .. AGAINST syntax for MySQL
1630 my $sqlmaker = SQL::Abstract->new(special_ops => [
1632 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
1633 {regex => qr/^match$/i,
1635 my ($self, $field, $op, $arg) = @_;
1636 $arg = [$arg] if not ref $arg;
1637 my $label = $self->_quote($field);
1638 my ($placeholder) = $self->_convert('?');
1639 my $placeholders = join ", ", (($placeholder) x @$arg);
1640 my $sql = $self->_sqlcase('match') . " ($label) "
1641 . $self->_sqlcase('against') . " ($placeholders) ";
1642 my @bind = $self->_bindtype($field, @$arg);
1643 return ($sql, @bind);
1650 =head1 UNARY OPERATORS
1652 my $sqlmaker = SQL::Abstract->new(unary_ops => [
1656 my ($self, $op, $arg) = @_;
1662 handler => 'method_name',
1666 A "unary operator" is a SQL syntactic clause that can be
1667 applied to a field - the operator goes before the field
1669 You can write your own operator handlers - supply a C<unary_ops>
1670 argument to the C<new> method. That argument takes an arrayref of
1671 operator definitions; each operator definition is a hashref with two
1678 the regular expression to match the operator
1682 Either a coderef or a plain scalar method name. In both cases
1683 the expected return is C<< $sql >>.
1685 When supplied with a method name, it is simply called on the
1686 L<SQL::Abstract/> object as:
1688 $self->$method_name ($op, $arg)
1692 $op is the part that matched the handler regex
1693 $arg is the RHS or argument of the operator
1695 When supplied with a coderef, it is called as:
1697 $coderef->($self, $op, $arg)
1705 Thanks to some benchmarking by Mark Stosberg, it turns out that
1706 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
1707 I must admit this wasn't an intentional design issue, but it's a
1708 byproduct of the fact that you get to control your C<DBI> handles
1711 To maximize performance, use a code snippet like the following:
1713 # prepare a statement handle using the first row
1714 # and then reuse it for the rest of the rows
1716 for my $href (@array_of_hashrefs) {
1717 $stmt ||= $sql->insert('table', $href);
1718 $sth ||= $dbh->prepare($stmt);
1719 $sth->execute($sql->values($href));
1722 The reason this works is because the keys in your C<$href> are sorted
1723 internally by B<SQL::Abstract>. Thus, as long as your data retains
1724 the same structure, you only have to generate the SQL the first time
1725 around. On subsequent queries, simply use the C<values> function provided
1726 by this module to return your values in the correct order.
1728 However this depends on the values having the same type - if, for
1729 example, the values of a where clause may either have values
1730 (resulting in sql of the form C<column = ?> with a single bind
1731 value), or alternatively the values might be C<undef> (resulting in
1732 sql of the form C<column IS NULL> with no bind value) then the
1733 caching technique suggested will not work.
1737 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
1738 really like this part (I do, at least). Building up a complex query
1739 can be as simple as the following:
1746 use CGI::FormBuilder;
1749 my $form = CGI::FormBuilder->new(...);
1750 my $sql = SQL::Abstract->new;
1752 if ($form->submitted) {
1753 my $field = $form->field;
1754 my $id = delete $field->{id};
1755 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
1758 Of course, you would still have to connect using C<DBI> to run the
1759 query, but the point is that if you make your form look like your
1760 table, the actual query script can be extremely simplistic.
1762 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
1763 a fast interface to returning and formatting data. I frequently
1764 use these three modules together to write complex database query
1765 apps in under 50 lines.
1771 =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
1773 =item * git: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
1779 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
1780 Great care has been taken to preserve the I<published> behavior
1781 documented in previous versions in the 1.* family; however,
1782 some features that were previously undocumented, or behaved
1783 differently from the documentation, had to be changed in order
1784 to clarify the semantics. Hence, client code that was relying
1785 on some dark areas of C<SQL::Abstract> v1.*
1786 B<might behave differently> in v1.50.
1788 The main changes are :
1794 support for literal SQL through the C<< \ [$sql, bind] >> syntax.
1798 support for the { operator => \"..." } construct (to embed literal SQL)
1802 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
1806 optional support for L<array datatypes|/"Inserting and Updating Arrays">
1810 defensive programming : check arguments
1814 fixed bug with global logic, which was previously implemented
1815 through global variables yielding side-effects. Prior versions would
1816 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
1817 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
1818 Now this is interpreted
1819 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
1824 fixed semantics of _bindtype on array args
1828 dropped the C<_anoncopy> of the %where tree. No longer necessary,
1829 we just avoid shifting arrays within that tree.
1833 dropped the C<_modlogic> function
1837 =head1 ACKNOWLEDGEMENTS
1839 There are a number of individuals that have really helped out with
1840 this module. Unfortunately, most of them submitted bugs via CPAN
1841 so I have no idea who they are! But the people I do know are:
1843 Ash Berlin (order_by hash term support)
1844 Matt Trout (DBIx::Class support)
1845 Mark Stosberg (benchmarking)
1846 Chas Owens (initial "IN" operator support)
1847 Philip Collins (per-field SQL functions)
1848 Eric Kolve (hashref "AND" support)
1849 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
1850 Dan Kubb (support for "quote_char" and "name_sep")
1851 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
1852 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
1853 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
1854 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
1855 Oliver Charles (support for "RETURNING" after "INSERT")
1861 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
1865 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
1867 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
1869 For support, your best bet is to try the C<DBIx::Class> users mailing list.
1870 While not an official support venue, C<DBIx::Class> makes heavy use of
1871 C<SQL::Abstract>, and as such list members there are very familiar with
1872 how to create queries.
1876 This module is free software; you may copy this under the same
1877 terms as perl itself (either the GNU General Public License or
1878 the Artistic License)