1 package SQL::Abstract; # see doc at end of file
3 use SQL::Abstract::_TempExtlib;
8 use Module::Runtime qw(use_module);
12 # DO NOT INCREMENT TO 2.0 WITHOUT COORDINATING WITH mst OR ribasushi
13 our $VERSION = '1.99_01';
14 # DO NOT INCREMENT TO 2.0 WITHOUT COORDINATING WITH mst OR ribasushi
16 # This would confuse some packagers
17 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
20 my($func) = (caller(1))[3];
21 Carp::carp "[$func] Warning: ", @_;
25 my($func) = (caller(1))[3];
26 Carp::croak "[$func] Fatal: ", @_;
29 has converter => (is => 'lazy', clearer => 'clear_converter');
32 is => 'ro', coerce => sub { $_[0] eq 'lower' ? 'lower' : undef }
36 is => 'ro', coerce => sub { uc($_[0]) }, default => sub { 'OR' }
40 is => 'ro', default => sub { 'normal' }
43 has cmp => (is => 'ro', default => sub { '=' });
45 has sqltrue => (is => 'ro', default => sub { '1=1' });
46 has sqlfalse => (is => 'ro', default => sub { '0=1' });
48 has special_ops => (is => 'ro', default => sub { [] });
49 has unary_ops => (is => 'ro', default => sub { [] });
52 # need to guard against ()'s in column names too, but this will break tons of
53 # hacks... ideas anyone?
55 has injection_guard => (
66 has renderer => (is => 'lazy', clearer => 'clear_renderer');
69 is => 'rw', default => sub { '.' },
71 $_[0]->clear_renderer;
72 $_[0]->clear_converter;
79 $_[0]->clear_renderer;
80 $_[0]->clear_converter;
84 has collapse_aliases => (
90 is => 'rw', default => sub { 1 },
92 $_[0]->clear_renderer;
93 $_[0]->clear_converter;
97 has convert => (is => 'ro');
99 has array_datatypes => (is => 'ro');
101 has converter_class => (
102 is => 'rw', lazy => 1, builder => '_build_converter_class',
103 trigger => sub { shift->clear_converter },
106 sub _build_converter_class {
107 use_module('SQL::Abstract::Converter')
110 has renderer_class => (
111 is => 'rw', lazy => 1, clearer => 1, builder => 1,
112 trigger => sub { shift->clear_renderer },
115 after clear_renderer_class => sub { shift->clear_renderer };
117 sub _build_renderer_class {
119 my ($class, @roles) = (
120 $self->_build_base_renderer_class, $self->_build_renderer_roles
122 return $class unless @roles;
123 return use_module('Moo::Role')->create_class_with_roles($class, @roles);
126 sub _build_base_renderer_class {
127 use_module('Data::Query::Renderer::SQL::Naive')
130 sub _build_renderer_roles { () }
132 sub _converter_args {
134 Scalar::Util::weaken($self);
136 lower_case => $self->case,
137 default_logic => $self->logic,
138 bind_meta => not($self->bindtype eq 'normal'),
139 identifier_sep => $self->name_sep,
140 (map +($_ => $self->$_), qw(
141 cmp sqltrue sqlfalse injection_guard convert array_datatypes
145 my $sub = $_->{handler};
148 handler => sub { $self->$sub(@_) }
150 } @{$self->special_ops}
152 renderer_will_quote => (
153 defined($self->quote_char) and $self->always_quote
158 sub _build_converter {
160 $self->converter_class->new($self->_converter_args);
166 for ($self->quote_char) {
167 $chars = defined() ? (ref() ? $_ : [$_]) : ['',''];
170 quote_chars => $chars, always_quote => $self->always_quote,
171 identifier_sep => $self->name_sep,
172 collapse_aliases => $self->collapse_aliases,
173 ($self->case ? (lc_keywords => 1) : ()), # always 'lower' if it exists
177 sub _build_renderer {
179 $self->renderer_class->new($self->_renderer_args);
183 my ($self, $dq) = @_;
187 my ($sql, @bind) = @{$self->renderer->render($dq)};
189 ($self->{bindtype} eq 'normal'
190 ? ($sql, map $_->{value}, @bind)
191 : ($sql, map [ $_->{value_meta}, $_->{value} ], @bind)
197 my ($self, $type, @args) = @_;
198 $self->_render_dq($self->converter->${\"_${type}_to_dq"}(@args));
201 sub insert { shift->_render_sqla(insert => @_) }
203 sub update { shift->_render_sqla(update => @_) }
205 sub select { shift->_render_sqla(select => @_) }
207 sub delete { shift->_render_sqla(delete => @_) }
210 my ($self, $where, $order) = @_;
216 ($sql, @bind) = $self->_recurse_where($where) if defined($where);
217 $sql = $sql ? $self->_sqlcase(' where ') . "( $sql )" : '';
221 $sql .= $self->_order_by($order);
224 return wantarray ? ($sql, @bind) : $sql;
227 sub _recurse_where { shift->_render_sqla(where => @_) }
230 my ($self, $arg) = @_;
231 if (my $dq = $self->converter->_order_by_to_dq($arg)) {
232 # SQLA generates ' ORDER BY foo'. The hilarity.
234 ? do { my @r = $self->_render_dq($dq); $r[0] = ' '.$r[0]; @r }
235 : ' '.$self->_render_dq($dq);
241 # highly optimized, as it's called way too often
243 # my ($self, $label) = @_;
245 return '' unless defined $_[1];
246 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
248 unless ($_[0]->{quote_char}) {
249 $_[0]->_assert_pass_injection_guard($_[1]);
253 my $qref = ref $_[0]->{quote_char};
256 ($l, $r) = ( $_[0]->{quote_char}, $_[0]->{quote_char} );
258 elsif ($qref eq 'ARRAY') {
259 ($l, $r) = @{$_[0]->{quote_char}};
262 puke "Unsupported quote_char format: $_[0]->{quote_char}";
265 # parts containing * are naturally unquoted
266 return join( $_[0]->{name_sep}||'', map
267 { $_ eq '*' ? $_ : $l . $_ . $r }
268 ( $_[0]->{name_sep} ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) : $_[1] )
272 sub _assert_pass_injection_guard {
273 if ($_[1] =~ $_[0]->{injection_guard}) {
274 my $class = ref $_[0];
275 die "Possible SQL injection attempt '$_[1]'. If this is indeed a part of "
276 . "the desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply "
277 . "your own {injection_guard} attribute to ${class}->new()"
281 # Conversion, if applicable
283 #my ($self, $arg) = @_;
284 if ($_[0]->{convert}) {
285 return $_[0]->_sqlcase($_[0]->{convert}) .'(' . $_[1] . ')';
292 #my ($self, $col, @vals) = @_;
293 # called often - tighten code
294 return $_[0]->{bindtype} eq 'columns'
295 ? map {[$_[1], $_]} @_[2 .. $#_]
300 # Dies if any element of @bind is not in [colname => value] format
301 # if bindtype is 'columns'.
302 sub _assert_bindval_matches_bindtype {
303 # my ($self, @bind) = @_;
305 if ($self->{bindtype} eq 'columns') {
307 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
308 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
314 # Fix SQL case, if so requested
316 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
317 # don't touch the argument ... crooked logic, but let's not change it!
318 return $_[0]->{case} ? $_[1] : uc($_[1]);
323 my $data = shift || return;
324 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
325 unless ref $data eq 'HASH';
328 foreach my $k ( sort keys %$data ) {
330 local our $Cur_Col_Meta = $k;
331 my ($sql, @bind) = $self->_render_sqla(
334 push @all_bind, @bind;
343 my(@sql, @sqlq, @sqlv);
347 if ($ref eq 'HASH') {
348 for my $k (sort keys %$_) {
351 my $label = $self->_quote($k);
353 # literal SQL with bind
354 my ($sql, @bind) = @$v;
355 $self->_assert_bindval_matches_bindtype(@bind);
356 push @sqlq, "$label = $sql";
358 } elsif ($r eq 'SCALAR') {
359 # literal SQL without bind
360 push @sqlq, "$label = $$v";
362 push @sqlq, "$label = ?";
363 push @sqlv, $self->_bindtype($k, $v);
366 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
367 } elsif ($ref eq 'ARRAY') {
368 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
371 if ($r eq 'ARRAY') { # literal SQL with bind
372 my ($sql, @bind) = @$v;
373 $self->_assert_bindval_matches_bindtype(@bind);
376 } elsif ($r eq 'SCALAR') { # literal SQL without bind
377 # embedded literal SQL
384 push @sql, '(' . join(', ', @sqlq) . ')';
385 } elsif ($ref eq 'SCALAR') {
389 # strings get case twiddled
390 push @sql, $self->_sqlcase($_);
394 my $sql = join ' ', @sql;
396 # this is pretty tricky
397 # if ask for an array, return ($stmt, @bind)
398 # otherwise, s/?/shift @sqlv/ to put it inline
400 return ($sql, @sqlv);
402 1 while $sql =~ s/\?/my $d = shift(@sqlv);
403 ref $d ? $d->[1] : $d/e;
415 SQL::Abstract - Generate SQL from Perl data structures
421 my $sql = SQL::Abstract->new;
423 my($stmt, @bind) = $sql->select($source, \@fields, \%where, \@order);
425 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
427 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
429 my($stmt, @bind) = $sql->delete($table, \%where);
431 # Then, use these in your DBI statements
432 my $sth = $dbh->prepare($stmt);
433 $sth->execute(@bind);
435 # Just generate the WHERE clause
436 my($stmt, @bind) = $sql->where(\%where, \@order);
438 # Return values in the same order, for hashed queries
439 # See PERFORMANCE section for more details
440 my @bind = $sql->values(\%fieldvals);
444 This module was inspired by the excellent L<DBIx::Abstract>.
445 However, in using that module I found that what I really wanted
446 to do was generate SQL, but still retain complete control over my
447 statement handles and use the DBI interface. So, I set out to
448 create an abstract SQL generation module.
450 While based on the concepts used by L<DBIx::Abstract>, there are
451 several important differences, especially when it comes to WHERE
452 clauses. I have modified the concepts used to make the SQL easier
453 to generate from Perl data structures and, IMO, more intuitive.
454 The underlying idea is for this module to do what you mean, based
455 on the data structures you provide it. The big advantage is that
456 you don't have to modify your code every time your data changes,
457 as this module figures it out.
459 To begin with, an SQL INSERT is as easy as just specifying a hash
460 of C<key=value> pairs:
463 name => 'Jimbo Bobson',
464 phone => '123-456-7890',
465 address => '42 Sister Lane',
467 state => 'Louisiana',
470 The SQL can then be generated with this:
472 my($stmt, @bind) = $sql->insert('people', \%data);
474 Which would give you something like this:
476 $stmt = "INSERT INTO people
477 (address, city, name, phone, state)
478 VALUES (?, ?, ?, ?, ?)";
479 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
480 '123-456-7890', 'Louisiana');
482 These are then used directly in your DBI code:
484 my $sth = $dbh->prepare($stmt);
485 $sth->execute(@bind);
487 =head2 Inserting and Updating Arrays
489 If your database has array types (like for example Postgres),
490 activate the special option C<< array_datatypes => 1 >>
491 when creating the C<SQL::Abstract> object.
492 Then you may use an arrayref to insert and update database array types:
494 my $sql = SQL::Abstract->new(array_datatypes => 1);
496 planets => [qw/Mercury Venus Earth Mars/]
499 my($stmt, @bind) = $sql->insert('solar_system', \%data);
503 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
505 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
508 =head2 Inserting and Updating SQL
510 In order to apply SQL functions to elements of your C<%data> you may
511 specify a reference to an arrayref for the given hash value. For example,
512 if you need to execute the Oracle C<to_date> function on a value, you can
513 say something like this:
517 date_entered => \["to_date(?,'MM/DD/YYYY')", "03/02/2003"],
520 The first value in the array is the actual SQL. Any other values are
521 optional and would be included in the bind values array. This gives
524 my($stmt, @bind) = $sql->insert('people', \%data);
526 $stmt = "INSERT INTO people (name, date_entered)
527 VALUES (?, to_date(?,'MM/DD/YYYY'))";
528 @bind = ('Bill', '03/02/2003');
530 An UPDATE is just as easy, all you change is the name of the function:
532 my($stmt, @bind) = $sql->update('people', \%data);
534 Notice that your C<%data> isn't touched; the module will generate
535 the appropriately quirky SQL for you automatically. Usually you'll
536 want to specify a WHERE clause for your UPDATE, though, which is
537 where handling C<%where> hashes comes in handy...
539 =head2 Complex where statements
541 This module can generate pretty complicated WHERE statements
542 easily. For example, simple C<key=value> pairs are taken to mean
543 equality, and if you want to see if a field is within a set
544 of values, you can use an arrayref. Let's say we wanted to
545 SELECT some data based on this criteria:
549 worker => ['nwiger', 'rcwe', 'sfz'],
550 status => { '!=', 'completed' }
553 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
555 The above would give you something like this:
557 $stmt = "SELECT * FROM tickets WHERE
558 ( requestor = ? ) AND ( status != ? )
559 AND ( worker = ? OR worker = ? OR worker = ? )";
560 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
562 Which you could then use in DBI code like so:
564 my $sth = $dbh->prepare($stmt);
565 $sth->execute(@bind);
571 The functions are simple. There's one for each major SQL operation,
572 and a constructor you use first. The arguments are specified in a
573 similar order to each function (table, then fields, then a where
574 clause) to try and simplify things.
579 =head2 new(option => 'value')
581 The C<new()> function takes a list of options and values, and returns
582 a new B<SQL::Abstract> object which can then be used to generate SQL
583 through the methods below. The options accepted are:
589 If set to 'lower', then SQL will be generated in all lowercase. By
590 default SQL is generated in "textbook" case meaning something like:
592 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
594 Any setting other than 'lower' is ignored.
598 This determines what the default comparison operator is. By default
599 it is C<=>, meaning that a hash like this:
601 %where = (name => 'nwiger', email => 'nate@wiger.org');
603 Will generate SQL like this:
605 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
607 However, you may want loose comparisons by default, so if you set
608 C<cmp> to C<like> you would get SQL such as:
610 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
612 You can also override the comparison on an individual basis - see
613 the huge section on L</"WHERE CLAUSES"> at the bottom.
615 =item sqltrue, sqlfalse
617 Expressions for inserting boolean values within SQL statements.
618 By default these are C<1=1> and C<1=0>. They are used
619 by the special operators C<-in> and C<-not_in> for generating
620 correct SQL even when the argument is an empty array (see below).
624 This determines the default logical operator for multiple WHERE
625 statements in arrays or hashes. If absent, the default logic is "or"
626 for arrays, and "and" for hashes. This means that a WHERE
630 event_date => {'>=', '2/13/99'},
631 event_date => {'<=', '4/24/03'},
634 will generate SQL like this:
636 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
638 This is probably not what you want given this query, though (look
639 at the dates). To change the "OR" to an "AND", simply specify:
641 my $sql = SQL::Abstract->new(logic => 'and');
643 Which will change the above C<WHERE> to:
645 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
647 The logic can also be changed locally by inserting
648 a modifier in front of an arrayref :
650 @where = (-and => [event_date => {'>=', '2/13/99'},
651 event_date => {'<=', '4/24/03'} ]);
653 See the L</"WHERE CLAUSES"> section for explanations.
657 This will automatically convert comparisons using the specified SQL
658 function for both column and value. This is mostly used with an argument
659 of C<upper> or C<lower>, so that the SQL will have the effect of
660 case-insensitive "searches". For example, this:
662 $sql = SQL::Abstract->new(convert => 'upper');
663 %where = (keywords => 'MaKe iT CAse inSeNSItive');
665 Will turn out the following SQL:
667 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
669 The conversion can be C<upper()>, C<lower()>, or any other SQL function
670 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
671 not validate this option; it will just pass through what you specify verbatim).
675 This is a kludge because many databases suck. For example, you can't
676 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
677 Instead, you have to use C<bind_param()>:
679 $sth->bind_param(1, 'reg data');
680 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
682 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
683 which loses track of which field each slot refers to. Fear not.
685 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
686 Currently, you can specify either C<normal> (default) or C<columns>. If you
687 specify C<columns>, you will get an array that looks like this:
689 my $sql = SQL::Abstract->new(bindtype => 'columns');
690 my($stmt, @bind) = $sql->insert(...);
693 [ 'column1', 'value1' ],
694 [ 'column2', 'value2' ],
695 [ 'column3', 'value3' ],
698 You can then iterate through this manually, using DBI's C<bind_param()>.
700 $sth->prepare($stmt);
703 my($col, $data) = @$_;
704 if ($col eq 'details' || $col eq 'comments') {
705 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
706 } elsif ($col eq 'image') {
707 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
709 $sth->bind_param($i, $data);
713 $sth->execute; # execute without @bind now
715 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
716 Basically, the advantage is still that you don't have to care which fields
717 are or are not included. You could wrap that above C<for> loop in a simple
718 sub called C<bind_fields()> or something and reuse it repeatedly. You still
719 get a layer of abstraction over manual SQL specification.
721 Note that if you set L</bindtype> to C<columns>, the C<\[$sql, @bind]>
722 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
723 will expect the bind values in this format.
727 This is the character that a table or column name will be quoted
728 with. By default this is an empty string, but you could set it to
729 the character C<`>, to generate SQL like this:
731 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
733 Alternatively, you can supply an array ref of two items, the first being the left
734 hand quote character, and the second the right hand quote character. For
735 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
736 that generates SQL like this:
738 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
740 Quoting is useful if you have tables or columns names that are reserved
741 words in your database's SQL dialect.
745 This is the character that separates a table and column name. It is
746 necessary to specify this when the C<quote_char> option is selected,
747 so that tables and column names can be individually quoted like this:
749 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
751 =item injection_guard
753 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
754 column name specified in a query structure. This is a safety mechanism to avoid
755 injection attacks when mishandling user input e.g.:
757 my %condition_as_column_value_pairs = get_values_from_user();
758 $sqla->select( ... , \%condition_as_column_value_pairs );
760 If the expression matches an exception is thrown. Note that literal SQL
761 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
763 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
765 =item array_datatypes
767 When this option is true, arrayrefs in INSERT or UPDATE are
768 interpreted as array datatypes and are passed directly
770 When this option is false, arrayrefs are interpreted
771 as literal SQL, just like refs to arrayrefs
772 (but this behavior is for backwards compatibility; when writing
773 new queries, use the "reference to arrayref" syntax
779 Takes a reference to a list of "special operators"
780 to extend the syntax understood by L<SQL::Abstract>.
781 See section L</"SPECIAL OPERATORS"> for details.
785 Takes a reference to a list of "unary operators"
786 to extend the syntax understood by L<SQL::Abstract>.
787 See section L</"UNARY OPERATORS"> for details.
793 =head2 insert($table, \@values || \%fieldvals, \%options)
795 This is the simplest function. You simply give it a table name
796 and either an arrayref of values or hashref of field/value pairs.
797 It returns an SQL INSERT statement and a list of bind values.
798 See the sections on L</"Inserting and Updating Arrays"> and
799 L</"Inserting and Updating SQL"> for information on how to insert
800 with those data types.
802 The optional C<\%options> hash reference may contain additional
803 options to generate the insert SQL. Currently supported options
810 Takes either a scalar of raw SQL fields, or an array reference of
811 field names, and adds on an SQL C<RETURNING> statement at the end.
812 This allows you to return data generated by the insert statement
813 (such as row IDs) without performing another C<SELECT> statement.
814 Note, however, this is not part of the SQL standard and may not
815 be supported by all database engines.
819 =head2 update($table, \%fieldvals, \%where)
821 This takes a table, hashref of field/value pairs, and an optional
822 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
824 See the sections on L</"Inserting and Updating Arrays"> and
825 L</"Inserting and Updating SQL"> for information on how to insert
826 with those data types.
828 =head2 select($source, $fields, $where, $order)
830 This returns a SQL SELECT statement and associated list of bind values, as
831 specified by the arguments :
837 Specification of the 'FROM' part of the statement.
838 The argument can be either a plain scalar (interpreted as a table
839 name, will be quoted), or an arrayref (interpreted as a list
840 of table names, joined by commas, quoted), or a scalarref
841 (literal table name, not quoted), or a ref to an arrayref
842 (list of literal table names, joined by commas, not quoted).
846 Specification of the list of fields to retrieve from
848 The argument can be either an arrayref (interpreted as a list
849 of field names, will be joined by commas and quoted), or a
850 plain scalar (literal SQL, not quoted).
851 Please observe that this API is not as flexible as that of
852 the first argument C<$source>, for backwards compatibility reasons.
856 Optional argument to specify the WHERE part of the query.
857 The argument is most often a hashref, but can also be
858 an arrayref or plain scalar --
859 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
863 Optional argument to specify the ORDER BY part of the query.
864 The argument can be a scalar, a hashref or an arrayref
865 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
871 =head2 delete($table, \%where)
873 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
874 It returns an SQL DELETE statement and list of bind values.
876 =head2 where(\%where, \@order)
878 This is used to generate just the WHERE clause. For example,
879 if you have an arbitrary data structure and know what the
880 rest of your SQL is going to look like, but want an easy way
881 to produce a WHERE clause, use this. It returns an SQL WHERE
882 clause and list of bind values.
885 =head2 values(\%data)
887 This just returns the values from the hash C<%data>, in the same
888 order that would be returned from any of the other above queries.
889 Using this allows you to markedly speed up your queries if you
890 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
892 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
894 Warning: This is an experimental method and subject to change.
896 This returns arbitrarily generated SQL. It's a really basic shortcut.
897 It will return two different things, depending on return context:
899 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
900 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
902 These would return the following:
905 $stmt = "CREATE TABLE test (?, ?)";
906 @bind = (field1, field2);
908 # Second calling form
909 $stmt_and_val = "CREATE TABLE test (field1, field2)";
911 Depending on what you're trying to do, it's up to you to choose the correct
912 format. In this example, the second form is what you would want.
916 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
920 ALTER SESSION SET nls_date_format = 'MM/YY'
922 You get the idea. Strings get their case twiddled, but everything
923 else remains verbatim.
929 This module uses a variation on the idea from L<DBIx::Abstract>. It
930 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
931 module is that things in arrays are OR'ed, and things in hashes
934 The easiest way to explain is to show lots of examples. After
935 each C<%where> hash shown, it is assumed you used:
937 my($stmt, @bind) = $sql->where(\%where);
939 However, note that the C<%where> hash can be used directly in any
940 of the other functions as well, as described above.
942 =head2 Key-value pairs
944 So, let's get started. To begin, a simple hash:
948 status => 'completed'
951 Is converted to SQL C<key = val> statements:
953 $stmt = "WHERE user = ? AND status = ?";
954 @bind = ('nwiger', 'completed');
956 One common thing I end up doing is having a list of values that
957 a field can be in. To do this, simply specify a list inside of
962 status => ['assigned', 'in-progress', 'pending'];
965 This simple code will create the following:
967 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
968 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
970 A field associated to an empty arrayref will be considered a
971 logical false and will generate 0=1.
973 =head2 Tests for NULL values
975 If the value part is C<undef> then this is converted to SQL <IS NULL>
984 $stmt = "WHERE user = ? AND status IS NULL";
987 To test if a column IS NOT NULL:
991 status => { '!=', undef },
994 =head2 Specific comparison operators
996 If you want to specify a different type of operator for your comparison,
997 you can use a hashref for a given column:
1001 status => { '!=', 'completed' }
1004 Which would generate:
1006 $stmt = "WHERE user = ? AND status != ?";
1007 @bind = ('nwiger', 'completed');
1009 To test against multiple values, just enclose the values in an arrayref:
1011 status => { '=', ['assigned', 'in-progress', 'pending'] };
1013 Which would give you:
1015 "WHERE status = ? OR status = ? OR status = ?"
1018 The hashref can also contain multiple pairs, in which case it is expanded
1019 into an C<AND> of its elements:
1023 status => { '!=', 'completed', -not_like => 'pending%' }
1026 # Or more dynamically, like from a form
1027 $where{user} = 'nwiger';
1028 $where{status}{'!='} = 'completed';
1029 $where{status}{'-not_like'} = 'pending%';
1031 # Both generate this
1032 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
1033 @bind = ('nwiger', 'completed', 'pending%');
1036 To get an OR instead, you can combine it with the arrayref idea:
1040 priority => [ { '=', 2 }, { '>', 5 } ]
1043 Which would generate:
1045 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
1046 @bind = ('2', '5', 'nwiger');
1048 If you want to include literal SQL (with or without bind values), just use a
1049 scalar reference or array reference as the value:
1052 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
1053 date_expires => { '<' => \"now()" }
1056 Which would generate:
1058 $stmt = "WHERE date_entered > "to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
1059 @bind = ('11/26/2008');
1062 =head2 Logic and nesting operators
1064 In the example above,
1065 there is a subtle trap if you want to say something like
1066 this (notice the C<AND>):
1068 WHERE priority != ? AND priority != ?
1070 Because, in Perl you I<can't> do this:
1072 priority => { '!=', 2, '!=', 1 }
1074 As the second C<!=> key will obliterate the first. The solution
1075 is to use the special C<-modifier> form inside an arrayref:
1077 priority => [ -and => {'!=', 2},
1081 Normally, these would be joined by C<OR>, but the modifier tells it
1082 to use C<AND> instead. (Hint: You can use this in conjunction with the
1083 C<logic> option to C<new()> in order to change the way your queries
1084 work by default.) B<Important:> Note that the C<-modifier> goes
1085 B<INSIDE> the arrayref, as an extra first element. This will
1086 B<NOT> do what you think it might:
1088 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
1090 Here is a quick list of equivalencies, since there is some overlap:
1093 status => {'!=', 'completed', 'not like', 'pending%' }
1094 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
1097 status => {'=', ['assigned', 'in-progress']}
1098 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
1099 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
1103 =head2 Special operators : IN, BETWEEN, etc.
1105 You can also use the hashref format to compare a list of fields using the
1106 C<IN> comparison operator, by specifying the list as an arrayref:
1109 status => 'completed',
1110 reportid => { -in => [567, 2335, 2] }
1113 Which would generate:
1115 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
1116 @bind = ('completed', '567', '2335', '2');
1118 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
1121 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
1122 (by default : C<1=0>). Similarly, C<< -not_in => [] >> generates
1123 'sqltrue' (by default : C<1=1>).
1125 In addition to the array you can supply a chunk of literal sql or
1126 literal sql with bind:
1129 customer => { -in => \[
1130 'SELECT cust_id FROM cust WHERE balance > ?',
1133 status => { -in => \'SELECT status_codes FROM states' },
1139 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
1140 AND status IN ( SELECT status_codes FROM states )
1144 Finally, if the argument to C<-in> is not a reference, it will be
1145 treated as a single-element array.
1147 Another pair of operators is C<-between> and C<-not_between>,
1148 used with an arrayref of two values:
1152 completion_date => {
1153 -not_between => ['2002-10-01', '2003-02-06']
1159 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
1161 Just like with C<-in> all plausible combinations of literal SQL
1165 start0 => { -between => [ 1, 2 ] },
1166 start1 => { -between => \["? AND ?", 1, 2] },
1167 start2 => { -between => \"lower(x) AND upper(y)" },
1168 start3 => { -between => [
1170 \["upper(?)", 'stuff' ],
1177 ( start0 BETWEEN ? AND ? )
1178 AND ( start1 BETWEEN ? AND ? )
1179 AND ( start2 BETWEEN lower(x) AND upper(y) )
1180 AND ( start3 BETWEEN lower(x) AND upper(?) )
1182 @bind = (1, 2, 1, 2, 'stuff');
1185 These are the two builtin "special operators"; but the
1186 list can be expanded : see section L</"SPECIAL OPERATORS"> below.
1188 =head2 Unary operators: bool
1190 If you wish to test against boolean columns or functions within your
1191 database you can use the C<-bool> and C<-not_bool> operators. For
1192 example to test the column C<is_user> being true and the column
1193 C<is_enabled> being false you would use:-
1197 -not_bool => 'is_enabled',
1202 WHERE is_user AND NOT is_enabled
1204 If a more complex combination is required, testing more conditions,
1205 then you should use the and/or operators:-
1210 -not_bool => { two=> { -rlike => 'bar' } },
1211 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
1222 (NOT ( three = ? OR three > ? ))
1225 =head2 Nested conditions, -and/-or prefixes
1227 So far, we've seen how multiple conditions are joined with a top-level
1228 C<AND>. We can change this by putting the different conditions we want in
1229 hashes and then putting those hashes in an array. For example:
1234 status => { -like => ['pending%', 'dispatched'] },
1238 status => 'unassigned',
1242 This data structure would create the following:
1244 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
1245 OR ( user = ? AND status = ? ) )";
1246 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
1249 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
1250 to change the logic inside :
1256 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
1257 -or => { workhrs => {'<', 50}, geo => 'EURO' },
1264 WHERE ( user = ? AND (
1265 ( workhrs > ? AND geo = ? )
1266 OR ( workhrs < ? OR geo = ? )
1269 =head3 Algebraic inconsistency, for historical reasons
1271 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
1272 operator goes C<outside> of the nested structure; whereas when connecting
1273 several constraints on one column, the C<-and> operator goes
1274 C<inside> the arrayref. Here is an example combining both features :
1277 -and => [a => 1, b => 2],
1278 -or => [c => 3, d => 4],
1279 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
1284 WHERE ( ( ( a = ? AND b = ? )
1285 OR ( c = ? OR d = ? )
1286 OR ( e LIKE ? AND e LIKE ? ) ) )
1288 This difference in syntax is unfortunate but must be preserved for
1289 historical reasons. So be careful : the two examples below would
1290 seem algebraically equivalent, but they are not
1292 {col => [-and => {-like => 'foo%'}, {-like => '%bar'}]}
1293 # yields : WHERE ( ( col LIKE ? AND col LIKE ? ) )
1295 [-and => {col => {-like => 'foo%'}, {col => {-like => '%bar'}}]]
1296 # yields : WHERE ( ( col LIKE ? OR col LIKE ? ) )
1299 =head2 Literal SQL and value type operators
1301 The basic premise of SQL::Abstract is that in WHERE specifications the "left
1302 side" is a column name and the "right side" is a value (normally rendered as
1303 a placeholder). This holds true for both hashrefs and arrayref pairs as you
1304 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
1305 alter this behavior. There are several ways of doing so.
1309 This is a virtual operator that signals the string to its right side is an
1310 identifier (a column name) and not a value. For example to compare two
1311 columns you would write:
1314 priority => { '<', 2 },
1315 requestor => { -ident => 'submitter' },
1320 $stmt = "WHERE priority < ? AND requestor = submitter";
1323 If you are maintaining legacy code you may see a different construct as
1324 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
1329 This is a virtual operator that signals that the construct to its right side
1330 is a value to be passed to DBI. This is for example necessary when you want
1331 to write a where clause against an array (for RDBMS that support such
1332 datatypes). For example:
1335 array => { -value => [1, 2, 3] }
1340 $stmt = 'WHERE array = ?';
1341 @bind = ([1, 2, 3]);
1343 Note that if you were to simply say:
1349 the result would probably not be what you wanted:
1351 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
1356 Finally, sometimes only literal SQL will do. To include a random snippet
1357 of SQL verbatim, you specify it as a scalar reference. Consider this only
1358 as a last resort. Usually there is a better way. For example:
1361 priority => { '<', 2 },
1362 requestor => { -in => \'(SELECT name FROM hitmen)' },
1367 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
1370 Note that in this example, you only get one bind parameter back, since
1371 the verbatim SQL is passed as part of the statement.
1375 Never use untrusted input as a literal SQL argument - this is a massive
1376 security risk (there is no way to check literal snippets for SQL
1377 injections and other nastyness). If you need to deal with untrusted input
1378 use literal SQL with placeholders as described next.
1380 =head3 Literal SQL with placeholders and bind values (subqueries)
1382 If the literal SQL to be inserted has placeholders and bind values,
1383 use a reference to an arrayref (yes this is a double reference --
1384 not so common, but perfectly legal Perl). For example, to find a date
1385 in Postgres you can use something like this:
1388 date_column => \[q/= date '2008-09-30' - ?::integer/, 10/]
1393 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
1396 Note that you must pass the bind values in the same format as they are returned
1397 by L</where>. That means that if you set L</bindtype> to C<columns>, you must
1398 provide the bind values in the C<< [ column_meta => value ] >> format, where
1399 C<column_meta> is an opaque scalar value; most commonly the column name, but
1400 you can use any scalar value (including references and blessed references),
1401 L<SQL::Abstract> will simply pass it through intact. So if C<bindtype> is set
1402 to C<columns> the above example will look like:
1405 date_column => \[q/= date '2008-09-30' - ?::integer/, [ dummy => 10 ]/]
1408 Literal SQL is especially useful for nesting parenthesized clauses in the
1409 main SQL query. Here is a first example :
1411 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
1415 bar => \["IN ($sub_stmt)" => @sub_bind],
1420 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
1421 WHERE c2 < ? AND c3 LIKE ?))";
1422 @bind = (1234, 100, "foo%");
1424 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
1425 are expressed in the same way. Of course the C<$sub_stmt> and
1426 its associated bind values can be generated through a former call
1429 my ($sub_stmt, @sub_bind)
1430 = $sql->select("t1", "c1", {c2 => {"<" => 100},
1431 c3 => {-like => "foo%"}});
1434 bar => \["> ALL ($sub_stmt)" => @sub_bind],
1437 In the examples above, the subquery was used as an operator on a column;
1438 but the same principle also applies for a clause within the main C<%where>
1439 hash, like an EXISTS subquery :
1441 my ($sub_stmt, @sub_bind)
1442 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
1443 my %where = ( -and => [
1445 \["EXISTS ($sub_stmt)" => @sub_bind],
1450 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
1451 WHERE c1 = ? AND c2 > t0.c0))";
1455 Observe that the condition on C<c2> in the subquery refers to
1456 column C<t0.c0> of the main query : this is I<not> a bind
1457 value, so we have to express it through a scalar ref.
1458 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
1459 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
1460 what we wanted here.
1462 Finally, here is an example where a subquery is used
1463 for expressing unary negation:
1465 my ($sub_stmt, @sub_bind)
1466 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
1467 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
1469 lname => {like => '%son%'},
1470 \["NOT ($sub_stmt)" => @sub_bind],
1475 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
1476 @bind = ('%son%', 10, 20)
1478 =head3 Deprecated usage of Literal SQL
1480 Below are some examples of archaic use of literal SQL. It is shown only as
1481 reference for those who deal with legacy code. Each example has a much
1482 better, cleaner and safer alternative that users should opt for in new code.
1488 my %where = ( requestor => \'IS NOT NULL' )
1490 $stmt = "WHERE requestor IS NOT NULL"
1492 This used to be the way of generating NULL comparisons, before the handling
1493 of C<undef> got formalized. For new code please use the superior syntax as
1494 described in L</Tests for NULL values>.
1498 my %where = ( requestor => \'= submitter' )
1500 $stmt = "WHERE requestor = submitter"
1502 This used to be the only way to compare columns. Use the superior L</-ident>
1503 method for all new code. For example an identifier declared in such a way
1504 will be properly quoted if L</quote_char> is properly set, while the legacy
1505 form will remain as supplied.
1509 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
1511 $stmt = "WHERE completed > ? AND is_ready"
1512 @bind = ('2012-12-21')
1514 Using an empty string literal used to be the only way to express a boolean.
1515 For all new code please use the much more readable
1516 L<-bool|/Unary operators: bool> operator.
1522 These pages could go on for a while, since the nesting of the data
1523 structures this module can handle are pretty much unlimited (the
1524 module implements the C<WHERE> expansion as a recursive function
1525 internally). Your best bet is to "play around" with the module a
1526 little to see how the data structures behave, and choose the best
1527 format for your data based on that.
1529 And of course, all the values above will probably be replaced with
1530 variables gotten from forms or the command line. After all, if you
1531 knew everything ahead of time, you wouldn't have to worry about
1532 dynamically-generating SQL and could just hardwire it into your
1535 =head1 ORDER BY CLAUSES
1537 Some functions take an order by clause. This can either be a scalar (just a
1538 column name,) a hash of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } >>,
1539 or an array of either of the two previous forms. Examples:
1541 Given | Will Generate
1542 ----------------------------------------------------------
1544 \'colA DESC' | ORDER BY colA DESC
1546 'colA' | ORDER BY colA
1548 [qw/colA colB/] | ORDER BY colA, colB
1550 {-asc => 'colA'} | ORDER BY colA ASC
1552 {-desc => 'colB'} | ORDER BY colB DESC
1554 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
1556 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
1559 { -asc => 'colA' }, | ORDER BY colA ASC, colB DESC,
1560 { -desc => [qw/colB/], | colC ASC, colD ASC
1561 { -asc => [qw/colC colD/],|
1563 ===========================================================
1567 =head1 SPECIAL OPERATORS
1569 my $sqlmaker = SQL::Abstract->new(special_ops => [
1573 my ($self, $field, $op, $arg) = @_;
1579 handler => 'method_name',
1583 A "special operator" is a SQL syntactic clause that can be
1584 applied to a field, instead of a usual binary operator.
1587 WHERE field IN (?, ?, ?)
1588 WHERE field BETWEEN ? AND ?
1589 WHERE MATCH(field) AGAINST (?, ?)
1591 Special operators IN and BETWEEN are fairly standard and therefore
1592 are builtin within C<SQL::Abstract> (as the overridable methods
1593 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
1594 like the MATCH .. AGAINST example above which is specific to MySQL,
1595 you can write your own operator handlers - supply a C<special_ops>
1596 argument to the C<new> method. That argument takes an arrayref of
1597 operator definitions; each operator definition is a hashref with two
1604 the regular expression to match the operator
1608 Either a coderef or a plain scalar method name. In both cases
1609 the expected return is C<< ($sql, @bind) >>.
1611 When supplied with a method name, it is simply called on the
1612 L<SQL::Abstract/> object as:
1614 $self->$method_name ($field, $op, $arg)
1618 $op is the part that matched the handler regex
1619 $field is the LHS of the operator
1622 When supplied with a coderef, it is called as:
1624 $coderef->($self, $field, $op, $arg)
1629 For example, here is an implementation
1630 of the MATCH .. AGAINST syntax for MySQL
1632 my $sqlmaker = SQL::Abstract->new(special_ops => [
1634 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
1635 {regex => qr/^match$/i,
1637 my ($self, $field, $op, $arg) = @_;
1638 $arg = [$arg] if not ref $arg;
1639 my $label = $self->_quote($field);
1640 my ($placeholder) = $self->_convert('?');
1641 my $placeholders = join ", ", (($placeholder) x @$arg);
1642 my $sql = $self->_sqlcase('match') . " ($label) "
1643 . $self->_sqlcase('against') . " ($placeholders) ";
1644 my @bind = $self->_bindtype($field, @$arg);
1645 return ($sql, @bind);
1652 =head1 UNARY OPERATORS
1654 my $sqlmaker = SQL::Abstract->new(unary_ops => [
1658 my ($self, $op, $arg) = @_;
1664 handler => 'method_name',
1668 A "unary operator" is a SQL syntactic clause that can be
1669 applied to a field - the operator goes before the field
1671 You can write your own operator handlers - supply a C<unary_ops>
1672 argument to the C<new> method. That argument takes an arrayref of
1673 operator definitions; each operator definition is a hashref with two
1680 the regular expression to match the operator
1684 Either a coderef or a plain scalar method name. In both cases
1685 the expected return is C<< $sql >>.
1687 When supplied with a method name, it is simply called on the
1688 L<SQL::Abstract/> object as:
1690 $self->$method_name ($op, $arg)
1694 $op is the part that matched the handler regex
1695 $arg is the RHS or argument of the operator
1697 When supplied with a coderef, it is called as:
1699 $coderef->($self, $op, $arg)
1707 Thanks to some benchmarking by Mark Stosberg, it turns out that
1708 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
1709 I must admit this wasn't an intentional design issue, but it's a
1710 byproduct of the fact that you get to control your C<DBI> handles
1713 To maximize performance, use a code snippet like the following:
1715 # prepare a statement handle using the first row
1716 # and then reuse it for the rest of the rows
1718 for my $href (@array_of_hashrefs) {
1719 $stmt ||= $sql->insert('table', $href);
1720 $sth ||= $dbh->prepare($stmt);
1721 $sth->execute($sql->values($href));
1724 The reason this works is because the keys in your C<$href> are sorted
1725 internally by B<SQL::Abstract>. Thus, as long as your data retains
1726 the same structure, you only have to generate the SQL the first time
1727 around. On subsequent queries, simply use the C<values> function provided
1728 by this module to return your values in the correct order.
1730 However this depends on the values having the same type - if, for
1731 example, the values of a where clause may either have values
1732 (resulting in sql of the form C<column = ?> with a single bind
1733 value), or alternatively the values might be C<undef> (resulting in
1734 sql of the form C<column IS NULL> with no bind value) then the
1735 caching technique suggested will not work.
1739 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
1740 really like this part (I do, at least). Building up a complex query
1741 can be as simple as the following:
1748 use CGI::FormBuilder;
1751 my $form = CGI::FormBuilder->new(...);
1752 my $sql = SQL::Abstract->new;
1754 if ($form->submitted) {
1755 my $field = $form->field;
1756 my $id = delete $field->{id};
1757 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
1760 Of course, you would still have to connect using C<DBI> to run the
1761 query, but the point is that if you make your form look like your
1762 table, the actual query script can be extremely simplistic.
1764 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
1765 a fast interface to returning and formatting data. I frequently
1766 use these three modules together to write complex database query
1767 apps in under 50 lines.
1773 =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
1775 =item * git: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
1781 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
1782 Great care has been taken to preserve the I<published> behavior
1783 documented in previous versions in the 1.* family; however,
1784 some features that were previously undocumented, or behaved
1785 differently from the documentation, had to be changed in order
1786 to clarify the semantics. Hence, client code that was relying
1787 on some dark areas of C<SQL::Abstract> v1.*
1788 B<might behave differently> in v1.50.
1790 The main changes are :
1796 support for literal SQL through the C<< \ [$sql, bind] >> syntax.
1800 support for the { operator => \"..." } construct (to embed literal SQL)
1804 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
1808 optional support for L<array datatypes|/"Inserting and Updating Arrays">
1812 defensive programming : check arguments
1816 fixed bug with global logic, which was previously implemented
1817 through global variables yielding side-effects. Prior versions would
1818 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
1819 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
1820 Now this is interpreted
1821 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
1826 fixed semantics of _bindtype on array args
1830 dropped the C<_anoncopy> of the %where tree. No longer necessary,
1831 we just avoid shifting arrays within that tree.
1835 dropped the C<_modlogic> function
1839 =head1 ACKNOWLEDGEMENTS
1841 There are a number of individuals that have really helped out with
1842 this module. Unfortunately, most of them submitted bugs via CPAN
1843 so I have no idea who they are! But the people I do know are:
1845 Ash Berlin (order_by hash term support)
1846 Matt Trout (DBIx::Class support)
1847 Mark Stosberg (benchmarking)
1848 Chas Owens (initial "IN" operator support)
1849 Philip Collins (per-field SQL functions)
1850 Eric Kolve (hashref "AND" support)
1851 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
1852 Dan Kubb (support for "quote_char" and "name_sep")
1853 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
1854 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
1855 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
1856 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
1857 Oliver Charles (support for "RETURNING" after "INSERT")
1863 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
1867 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
1869 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
1871 For support, your best bet is to try the C<DBIx::Class> users mailing list.
1872 While not an official support venue, C<DBIx::Class> makes heavy use of
1873 C<SQL::Abstract>, and as such list members there are very familiar with
1874 how to create queries.
1878 This module is free software; you may copy this under the same
1879 terms as perl itself (either the GNU General Public License or
1880 the Artistic License)