1 package SQL::Abstract; # see doc at end of file
3 use SQL::Abstract::_TempExtlib;
8 use Module::Runtime qw(use_module);
9 use Sub::Quote 'quote_sub';
13 # DO NOT INCREMENT TO 2.0 WITHOUT COORDINATING WITH mst OR ribasushi
14 our $VERSION = '1.99_01';
15 # DO NOT INCREMENT TO 2.0 WITHOUT COORDINATING WITH mst OR ribasushi
17 # This would confuse some packagers
18 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
21 my($func) = (caller(1))[3];
22 Carp::carp "[$func] Warning: ", @_;
26 my($func) = (caller(1))[3];
27 Carp::croak "[$func] Fatal: ", @_;
30 has converter => (is => 'lazy', clearer => 'clear_converter');
33 is => 'ro', coerce => quote_sub( q{ $_[0] eq 'lower' ? 'lower' : undef } ),
37 is => 'ro', coerce => quote_sub( q{ uc($_[0]) } ), default => 'OR',
41 is => 'ro', default => 'normal'
44 has cmp => (is => 'ro', default => '=');
46 has sqltrue => (is => 'ro', default => '1=1' );
47 has sqlfalse => (is => 'ro', default => '0=1' );
49 has special_ops => (is => 'ro', default => quote_sub( q{ [] } ));
50 has unary_ops => (is => 'ro', default => quote_sub( q{ [] } ));
53 # need to guard against ()'s in column names too, but this will break tons of
54 # hacks... ideas anyone?
56 has injection_guard => (
58 default => quote_sub( q{
67 has renderer => (is => 'lazy', clearer => 'clear_renderer');
70 is => 'rw', default => '.',
71 trigger => quote_sub( q{
72 $_[0]->clear_renderer;
73 $_[0]->clear_converter;
79 trigger => quote_sub( q{
80 $_[0]->clear_renderer;
81 $_[0]->clear_converter;
85 has collapse_aliases => (
91 is => 'rw', default => 1,
92 trigger => quote_sub( q{
93 $_[0]->clear_renderer;
94 $_[0]->clear_converter;
98 has convert => (is => 'ro');
100 has array_datatypes => (is => 'ro');
102 has converter_class => (
103 is => 'rw', lazy => 1, builder => '_build_converter_class',
104 trigger => quote_sub( q{ $_[0]->clear_converter } ),
107 sub _build_converter_class {
108 use_module('SQL::Abstract::Converter')
111 has renderer_class => (
112 is => 'rw', lazy => 1, clearer => 1, builder => 1,
113 trigger => quote_sub( q{ $_[0]->clear_renderer } ),
116 after clear_renderer_class => sub { $_[0]->clear_renderer };
118 sub _build_renderer_class {
120 my ($class, @roles) = (
121 $self->_build_base_renderer_class, $self->_build_renderer_roles
123 return $class unless @roles;
124 return use_module('Moo::Role')->create_class_with_roles($class, @roles);
127 sub _build_base_renderer_class {
128 use_module('Data::Query::Renderer::SQL::Naive')
131 sub _build_renderer_roles { () }
133 sub _converter_args {
135 Scalar::Util::weaken($self);
137 lower_case => $self->case,
138 default_logic => $self->logic,
139 bind_meta => not($self->bindtype eq 'normal'),
140 identifier_sep => $self->name_sep,
141 (map +($_ => $self->$_), qw(
142 cmp sqltrue sqlfalse injection_guard convert array_datatypes
146 my $sub = $_->{handler};
149 handler => sub { $self->$sub(@_) }
151 } @{$self->special_ops}
153 renderer_will_quote => (
154 defined($self->quote_char) and $self->always_quote
159 sub _build_converter {
161 $self->converter_class->new($self->_converter_args);
167 for ($self->quote_char) {
168 $chars = defined() ? (ref() ? $_ : [$_]) : ['',''];
171 quote_chars => $chars, always_quote => $self->always_quote,
172 identifier_sep => $self->name_sep,
173 collapse_aliases => $self->collapse_aliases,
174 ($self->case ? (lc_keywords => 1) : ()), # always 'lower' if it exists
178 sub _build_renderer {
180 $self->renderer_class->new($self->_renderer_args);
184 my ($self, $dq) = @_;
188 my ($sql, @bind) = @{$self->renderer->render($dq)};
190 ($self->{bindtype} eq 'normal'
191 ? ($sql, map $_->{value}, @bind)
192 : ($sql, map [ $_->{value_meta}, $_->{value} ], @bind)
198 my ($self, $type, @args) = @_;
199 $self->_render_dq($self->converter->${\"_${type}_to_dq"}(@args));
202 sub insert { shift->_render_sqla(insert => @_) }
204 sub update { shift->_render_sqla(update => @_) }
206 sub select { shift->_render_sqla(select => @_) }
208 sub delete { shift->_render_sqla(delete => @_) }
211 my ($self, $where, $order) = @_;
217 ($sql, @bind) = $self->_recurse_where($where) if defined($where);
218 $sql = $sql ? $self->_sqlcase(' where ') . "( $sql )" : '';
222 $sql .= $self->_order_by($order);
225 return wantarray ? ($sql, @bind) : $sql;
228 sub _recurse_where { shift->_render_sqla(where => @_) }
231 my ($self, $arg) = @_;
232 if (my $dq = $self->converter->_order_by_to_dq($arg)) {
233 # SQLA generates ' ORDER BY foo'. The hilarity.
235 ? do { my @r = $self->_render_dq($dq); $r[0] = ' '.$r[0]; @r }
236 : ' '.$self->_render_dq($dq);
242 # highly optimized, as it's called way too often
244 # my ($self, $label) = @_;
246 return '' unless defined $_[1];
247 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
249 unless ($_[0]->{quote_char}) {
250 $_[0]->_assert_pass_injection_guard($_[1]);
254 my $qref = ref $_[0]->{quote_char};
257 ($l, $r) = ( $_[0]->{quote_char}, $_[0]->{quote_char} );
259 elsif ($qref eq 'ARRAY') {
260 ($l, $r) = @{$_[0]->{quote_char}};
263 puke "Unsupported quote_char format: $_[0]->{quote_char}";
266 # parts containing * are naturally unquoted
267 return join( $_[0]->{name_sep}||'', map
268 { $_ eq '*' ? $_ : $l . $_ . $r }
269 ( $_[0]->{name_sep} ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) : $_[1] )
273 sub _assert_pass_injection_guard {
274 if ($_[1] =~ $_[0]->{injection_guard}) {
275 my $class = ref $_[0];
276 die "Possible SQL injection attempt '$_[1]'. If this is indeed a part of "
277 . "the desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply "
278 . "your own {injection_guard} attribute to ${class}->new()"
282 # Conversion, if applicable
284 #my ($self, $arg) = @_;
285 if ($_[0]->{convert}) {
286 return $_[0]->_sqlcase($_[0]->{convert}) .'(' . $_[1] . ')';
293 #my ($self, $col, @vals) = @_;
294 # called often - tighten code
295 return $_[0]->{bindtype} eq 'columns'
296 ? map {[$_[1], $_]} @_[2 .. $#_]
301 # Dies if any element of @bind is not in [colname => value] format
302 # if bindtype is 'columns'.
303 sub _assert_bindval_matches_bindtype {
304 # my ($self, @bind) = @_;
306 if ($self->{bindtype} eq 'columns') {
308 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
309 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
315 # Fix SQL case, if so requested
317 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
318 # don't touch the argument ... crooked logic, but let's not change it!
319 return $_[0]->{case} ? $_[1] : uc($_[1]);
324 my $data = shift || return;
325 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
326 unless ref $data eq 'HASH';
329 foreach my $k ( sort keys %$data ) {
331 local our $Cur_Col_Meta = $k;
332 my ($sql, @bind) = $self->_render_sqla(
335 push @all_bind, @bind;
344 my(@sql, @sqlq, @sqlv);
348 if ($ref eq 'HASH') {
349 for my $k (sort keys %$_) {
352 my $label = $self->_quote($k);
354 # literal SQL with bind
355 my ($sql, @bind) = @$v;
356 $self->_assert_bindval_matches_bindtype(@bind);
357 push @sqlq, "$label = $sql";
359 } elsif ($r eq 'SCALAR') {
360 # literal SQL without bind
361 push @sqlq, "$label = $$v";
363 push @sqlq, "$label = ?";
364 push @sqlv, $self->_bindtype($k, $v);
367 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
368 } elsif ($ref eq 'ARRAY') {
369 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
372 if ($r eq 'ARRAY') { # literal SQL with bind
373 my ($sql, @bind) = @$v;
374 $self->_assert_bindval_matches_bindtype(@bind);
377 } elsif ($r eq 'SCALAR') { # literal SQL without bind
378 # embedded literal SQL
385 push @sql, '(' . join(', ', @sqlq) . ')';
386 } elsif ($ref eq 'SCALAR') {
390 # strings get case twiddled
391 push @sql, $self->_sqlcase($_);
395 my $sql = join ' ', @sql;
397 # this is pretty tricky
398 # if ask for an array, return ($stmt, @bind)
399 # otherwise, s/?/shift @sqlv/ to put it inline
401 return ($sql, @sqlv);
403 1 while $sql =~ s/\?/my $d = shift(@sqlv);
404 ref $d ? $d->[1] : $d/e;
416 SQL::Abstract - Generate SQL from Perl data structures
422 my $sql = SQL::Abstract->new;
424 my($stmt, @bind) = $sql->select($source, \@fields, \%where, \@order);
426 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
428 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
430 my($stmt, @bind) = $sql->delete($table, \%where);
432 # Then, use these in your DBI statements
433 my $sth = $dbh->prepare($stmt);
434 $sth->execute(@bind);
436 # Just generate the WHERE clause
437 my($stmt, @bind) = $sql->where(\%where, \@order);
439 # Return values in the same order, for hashed queries
440 # See PERFORMANCE section for more details
441 my @bind = $sql->values(\%fieldvals);
445 This module was inspired by the excellent L<DBIx::Abstract>.
446 However, in using that module I found that what I really wanted
447 to do was generate SQL, but still retain complete control over my
448 statement handles and use the DBI interface. So, I set out to
449 create an abstract SQL generation module.
451 While based on the concepts used by L<DBIx::Abstract>, there are
452 several important differences, especially when it comes to WHERE
453 clauses. I have modified the concepts used to make the SQL easier
454 to generate from Perl data structures and, IMO, more intuitive.
455 The underlying idea is for this module to do what you mean, based
456 on the data structures you provide it. The big advantage is that
457 you don't have to modify your code every time your data changes,
458 as this module figures it out.
460 To begin with, an SQL INSERT is as easy as just specifying a hash
461 of C<key=value> pairs:
464 name => 'Jimbo Bobson',
465 phone => '123-456-7890',
466 address => '42 Sister Lane',
468 state => 'Louisiana',
471 The SQL can then be generated with this:
473 my($stmt, @bind) = $sql->insert('people', \%data);
475 Which would give you something like this:
477 $stmt = "INSERT INTO people
478 (address, city, name, phone, state)
479 VALUES (?, ?, ?, ?, ?)";
480 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
481 '123-456-7890', 'Louisiana');
483 These are then used directly in your DBI code:
485 my $sth = $dbh->prepare($stmt);
486 $sth->execute(@bind);
488 =head2 Inserting and Updating Arrays
490 If your database has array types (like for example Postgres),
491 activate the special option C<< array_datatypes => 1 >>
492 when creating the C<SQL::Abstract> object.
493 Then you may use an arrayref to insert and update database array types:
495 my $sql = SQL::Abstract->new(array_datatypes => 1);
497 planets => [qw/Mercury Venus Earth Mars/]
500 my($stmt, @bind) = $sql->insert('solar_system', \%data);
504 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
506 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
509 =head2 Inserting and Updating SQL
511 In order to apply SQL functions to elements of your C<%data> you may
512 specify a reference to an arrayref for the given hash value. For example,
513 if you need to execute the Oracle C<to_date> function on a value, you can
514 say something like this:
518 date_entered => \["to_date(?,'MM/DD/YYYY')", "03/02/2003"],
521 The first value in the array is the actual SQL. Any other values are
522 optional and would be included in the bind values array. This gives
525 my($stmt, @bind) = $sql->insert('people', \%data);
527 $stmt = "INSERT INTO people (name, date_entered)
528 VALUES (?, to_date(?,'MM/DD/YYYY'))";
529 @bind = ('Bill', '03/02/2003');
531 An UPDATE is just as easy, all you change is the name of the function:
533 my($stmt, @bind) = $sql->update('people', \%data);
535 Notice that your C<%data> isn't touched; the module will generate
536 the appropriately quirky SQL for you automatically. Usually you'll
537 want to specify a WHERE clause for your UPDATE, though, which is
538 where handling C<%where> hashes comes in handy...
540 =head2 Complex where statements
542 This module can generate pretty complicated WHERE statements
543 easily. For example, simple C<key=value> pairs are taken to mean
544 equality, and if you want to see if a field is within a set
545 of values, you can use an arrayref. Let's say we wanted to
546 SELECT some data based on this criteria:
550 worker => ['nwiger', 'rcwe', 'sfz'],
551 status => { '!=', 'completed' }
554 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
556 The above would give you something like this:
558 $stmt = "SELECT * FROM tickets WHERE
559 ( requestor = ? ) AND ( status != ? )
560 AND ( worker = ? OR worker = ? OR worker = ? )";
561 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
563 Which you could then use in DBI code like so:
565 my $sth = $dbh->prepare($stmt);
566 $sth->execute(@bind);
572 The functions are simple. There's one for each major SQL operation,
573 and a constructor you use first. The arguments are specified in a
574 similar order to each function (table, then fields, then a where
575 clause) to try and simplify things.
580 =head2 new(option => 'value')
582 The C<new()> function takes a list of options and values, and returns
583 a new B<SQL::Abstract> object which can then be used to generate SQL
584 through the methods below. The options accepted are:
590 If set to 'lower', then SQL will be generated in all lowercase. By
591 default SQL is generated in "textbook" case meaning something like:
593 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
595 Any setting other than 'lower' is ignored.
599 This determines what the default comparison operator is. By default
600 it is C<=>, meaning that a hash like this:
602 %where = (name => 'nwiger', email => 'nate@wiger.org');
604 Will generate SQL like this:
606 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
608 However, you may want loose comparisons by default, so if you set
609 C<cmp> to C<like> you would get SQL such as:
611 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
613 You can also override the comparison on an individual basis - see
614 the huge section on L</"WHERE CLAUSES"> at the bottom.
616 =item sqltrue, sqlfalse
618 Expressions for inserting boolean values within SQL statements.
619 By default these are C<1=1> and C<1=0>. They are used
620 by the special operators C<-in> and C<-not_in> for generating
621 correct SQL even when the argument is an empty array (see below).
625 This determines the default logical operator for multiple WHERE
626 statements in arrays or hashes. If absent, the default logic is "or"
627 for arrays, and "and" for hashes. This means that a WHERE
631 event_date => {'>=', '2/13/99'},
632 event_date => {'<=', '4/24/03'},
635 will generate SQL like this:
637 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
639 This is probably not what you want given this query, though (look
640 at the dates). To change the "OR" to an "AND", simply specify:
642 my $sql = SQL::Abstract->new(logic => 'and');
644 Which will change the above C<WHERE> to:
646 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
648 The logic can also be changed locally by inserting
649 a modifier in front of an arrayref :
651 @where = (-and => [event_date => {'>=', '2/13/99'},
652 event_date => {'<=', '4/24/03'} ]);
654 See the L</"WHERE CLAUSES"> section for explanations.
658 This will automatically convert comparisons using the specified SQL
659 function for both column and value. This is mostly used with an argument
660 of C<upper> or C<lower>, so that the SQL will have the effect of
661 case-insensitive "searches". For example, this:
663 $sql = SQL::Abstract->new(convert => 'upper');
664 %where = (keywords => 'MaKe iT CAse inSeNSItive');
666 Will turn out the following SQL:
668 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
670 The conversion can be C<upper()>, C<lower()>, or any other SQL function
671 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
672 not validate this option; it will just pass through what you specify verbatim).
676 This is a kludge because many databases suck. For example, you can't
677 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
678 Instead, you have to use C<bind_param()>:
680 $sth->bind_param(1, 'reg data');
681 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
683 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
684 which loses track of which field each slot refers to. Fear not.
686 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
687 Currently, you can specify either C<normal> (default) or C<columns>. If you
688 specify C<columns>, you will get an array that looks like this:
690 my $sql = SQL::Abstract->new(bindtype => 'columns');
691 my($stmt, @bind) = $sql->insert(...);
694 [ 'column1', 'value1' ],
695 [ 'column2', 'value2' ],
696 [ 'column3', 'value3' ],
699 You can then iterate through this manually, using DBI's C<bind_param()>.
701 $sth->prepare($stmt);
704 my($col, $data) = @$_;
705 if ($col eq 'details' || $col eq 'comments') {
706 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
707 } elsif ($col eq 'image') {
708 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
710 $sth->bind_param($i, $data);
714 $sth->execute; # execute without @bind now
716 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
717 Basically, the advantage is still that you don't have to care which fields
718 are or are not included. You could wrap that above C<for> loop in a simple
719 sub called C<bind_fields()> or something and reuse it repeatedly. You still
720 get a layer of abstraction over manual SQL specification.
722 Note that if you set L</bindtype> to C<columns>, the C<\[$sql, @bind]>
723 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
724 will expect the bind values in this format.
728 This is the character that a table or column name will be quoted
729 with. By default this is an empty string, but you could set it to
730 the character C<`>, to generate SQL like this:
732 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
734 Alternatively, you can supply an array ref of two items, the first being the left
735 hand quote character, and the second the right hand quote character. For
736 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
737 that generates SQL like this:
739 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
741 Quoting is useful if you have tables or columns names that are reserved
742 words in your database's SQL dialect.
746 This is the character that separates a table and column name. It is
747 necessary to specify this when the C<quote_char> option is selected,
748 so that tables and column names can be individually quoted like this:
750 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
752 =item injection_guard
754 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
755 column name specified in a query structure. This is a safety mechanism to avoid
756 injection attacks when mishandling user input e.g.:
758 my %condition_as_column_value_pairs = get_values_from_user();
759 $sqla->select( ... , \%condition_as_column_value_pairs );
761 If the expression matches an exception is thrown. Note that literal SQL
762 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
764 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
766 =item array_datatypes
768 When this option is true, arrayrefs in INSERT or UPDATE are
769 interpreted as array datatypes and are passed directly
771 When this option is false, arrayrefs are interpreted
772 as literal SQL, just like refs to arrayrefs
773 (but this behavior is for backwards compatibility; when writing
774 new queries, use the "reference to arrayref" syntax
780 Takes a reference to a list of "special operators"
781 to extend the syntax understood by L<SQL::Abstract>.
782 See section L</"SPECIAL OPERATORS"> for details.
786 Takes a reference to a list of "unary operators"
787 to extend the syntax understood by L<SQL::Abstract>.
788 See section L</"UNARY OPERATORS"> for details.
794 =head2 insert($table, \@values || \%fieldvals, \%options)
796 This is the simplest function. You simply give it a table name
797 and either an arrayref of values or hashref of field/value pairs.
798 It returns an SQL INSERT statement and a list of bind values.
799 See the sections on L</"Inserting and Updating Arrays"> and
800 L</"Inserting and Updating SQL"> for information on how to insert
801 with those data types.
803 The optional C<\%options> hash reference may contain additional
804 options to generate the insert SQL. Currently supported options
811 Takes either a scalar of raw SQL fields, or an array reference of
812 field names, and adds on an SQL C<RETURNING> statement at the end.
813 This allows you to return data generated by the insert statement
814 (such as row IDs) without performing another C<SELECT> statement.
815 Note, however, this is not part of the SQL standard and may not
816 be supported by all database engines.
820 =head2 update($table, \%fieldvals, \%where)
822 This takes a table, hashref of field/value pairs, and an optional
823 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
825 See the sections on L</"Inserting and Updating Arrays"> and
826 L</"Inserting and Updating SQL"> for information on how to insert
827 with those data types.
829 =head2 select($source, $fields, $where, $order)
831 This returns a SQL SELECT statement and associated list of bind values, as
832 specified by the arguments :
838 Specification of the 'FROM' part of the statement.
839 The argument can be either a plain scalar (interpreted as a table
840 name, will be quoted), or an arrayref (interpreted as a list
841 of table names, joined by commas, quoted), or a scalarref
842 (literal table name, not quoted), or a ref to an arrayref
843 (list of literal table names, joined by commas, not quoted).
847 Specification of the list of fields to retrieve from
849 The argument can be either an arrayref (interpreted as a list
850 of field names, will be joined by commas and quoted), or a
851 plain scalar (literal SQL, not quoted).
852 Please observe that this API is not as flexible as that of
853 the first argument C<$source>, for backwards compatibility reasons.
857 Optional argument to specify the WHERE part of the query.
858 The argument is most often a hashref, but can also be
859 an arrayref or plain scalar --
860 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
864 Optional argument to specify the ORDER BY part of the query.
865 The argument can be a scalar, a hashref or an arrayref
866 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
872 =head2 delete($table, \%where)
874 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
875 It returns an SQL DELETE statement and list of bind values.
877 =head2 where(\%where, \@order)
879 This is used to generate just the WHERE clause. For example,
880 if you have an arbitrary data structure and know what the
881 rest of your SQL is going to look like, but want an easy way
882 to produce a WHERE clause, use this. It returns an SQL WHERE
883 clause and list of bind values.
886 =head2 values(\%data)
888 This just returns the values from the hash C<%data>, in the same
889 order that would be returned from any of the other above queries.
890 Using this allows you to markedly speed up your queries if you
891 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
893 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
895 Warning: This is an experimental method and subject to change.
897 This returns arbitrarily generated SQL. It's a really basic shortcut.
898 It will return two different things, depending on return context:
900 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
901 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
903 These would return the following:
906 $stmt = "CREATE TABLE test (?, ?)";
907 @bind = (field1, field2);
909 # Second calling form
910 $stmt_and_val = "CREATE TABLE test (field1, field2)";
912 Depending on what you're trying to do, it's up to you to choose the correct
913 format. In this example, the second form is what you would want.
917 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
921 ALTER SESSION SET nls_date_format = 'MM/YY'
923 You get the idea. Strings get their case twiddled, but everything
924 else remains verbatim.
930 This module uses a variation on the idea from L<DBIx::Abstract>. It
931 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
932 module is that things in arrays are OR'ed, and things in hashes
935 The easiest way to explain is to show lots of examples. After
936 each C<%where> hash shown, it is assumed you used:
938 my($stmt, @bind) = $sql->where(\%where);
940 However, note that the C<%where> hash can be used directly in any
941 of the other functions as well, as described above.
943 =head2 Key-value pairs
945 So, let's get started. To begin, a simple hash:
949 status => 'completed'
952 Is converted to SQL C<key = val> statements:
954 $stmt = "WHERE user = ? AND status = ?";
955 @bind = ('nwiger', 'completed');
957 One common thing I end up doing is having a list of values that
958 a field can be in. To do this, simply specify a list inside of
963 status => ['assigned', 'in-progress', 'pending'];
966 This simple code will create the following:
968 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
969 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
971 A field associated to an empty arrayref will be considered a
972 logical false and will generate 0=1.
974 =head2 Tests for NULL values
976 If the value part is C<undef> then this is converted to SQL <IS NULL>
985 $stmt = "WHERE user = ? AND status IS NULL";
988 To test if a column IS NOT NULL:
992 status => { '!=', undef },
995 =head2 Specific comparison operators
997 If you want to specify a different type of operator for your comparison,
998 you can use a hashref for a given column:
1002 status => { '!=', 'completed' }
1005 Which would generate:
1007 $stmt = "WHERE user = ? AND status != ?";
1008 @bind = ('nwiger', 'completed');
1010 To test against multiple values, just enclose the values in an arrayref:
1012 status => { '=', ['assigned', 'in-progress', 'pending'] };
1014 Which would give you:
1016 "WHERE status = ? OR status = ? OR status = ?"
1019 The hashref can also contain multiple pairs, in which case it is expanded
1020 into an C<AND> of its elements:
1024 status => { '!=', 'completed', -not_like => 'pending%' }
1027 # Or more dynamically, like from a form
1028 $where{user} = 'nwiger';
1029 $where{status}{'!='} = 'completed';
1030 $where{status}{'-not_like'} = 'pending%';
1032 # Both generate this
1033 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
1034 @bind = ('nwiger', 'completed', 'pending%');
1037 To get an OR instead, you can combine it with the arrayref idea:
1041 priority => [ { '=', 2 }, { '>', 5 } ]
1044 Which would generate:
1046 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
1047 @bind = ('2', '5', 'nwiger');
1049 If you want to include literal SQL (with or without bind values), just use a
1050 scalar reference or array reference as the value:
1053 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
1054 date_expires => { '<' => \"now()" }
1057 Which would generate:
1059 $stmt = "WHERE date_entered > "to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
1060 @bind = ('11/26/2008');
1063 =head2 Logic and nesting operators
1065 In the example above,
1066 there is a subtle trap if you want to say something like
1067 this (notice the C<AND>):
1069 WHERE priority != ? AND priority != ?
1071 Because, in Perl you I<can't> do this:
1073 priority => { '!=', 2, '!=', 1 }
1075 As the second C<!=> key will obliterate the first. The solution
1076 is to use the special C<-modifier> form inside an arrayref:
1078 priority => [ -and => {'!=', 2},
1082 Normally, these would be joined by C<OR>, but the modifier tells it
1083 to use C<AND> instead. (Hint: You can use this in conjunction with the
1084 C<logic> option to C<new()> in order to change the way your queries
1085 work by default.) B<Important:> Note that the C<-modifier> goes
1086 B<INSIDE> the arrayref, as an extra first element. This will
1087 B<NOT> do what you think it might:
1089 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
1091 Here is a quick list of equivalencies, since there is some overlap:
1094 status => {'!=', 'completed', 'not like', 'pending%' }
1095 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
1098 status => {'=', ['assigned', 'in-progress']}
1099 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
1100 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
1104 =head2 Special operators : IN, BETWEEN, etc.
1106 You can also use the hashref format to compare a list of fields using the
1107 C<IN> comparison operator, by specifying the list as an arrayref:
1110 status => 'completed',
1111 reportid => { -in => [567, 2335, 2] }
1114 Which would generate:
1116 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
1117 @bind = ('completed', '567', '2335', '2');
1119 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
1122 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
1123 (by default : C<1=0>). Similarly, C<< -not_in => [] >> generates
1124 'sqltrue' (by default : C<1=1>).
1126 In addition to the array you can supply a chunk of literal sql or
1127 literal sql with bind:
1130 customer => { -in => \[
1131 'SELECT cust_id FROM cust WHERE balance > ?',
1134 status => { -in => \'SELECT status_codes FROM states' },
1140 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
1141 AND status IN ( SELECT status_codes FROM states )
1145 Finally, if the argument to C<-in> is not a reference, it will be
1146 treated as a single-element array.
1148 Another pair of operators is C<-between> and C<-not_between>,
1149 used with an arrayref of two values:
1153 completion_date => {
1154 -not_between => ['2002-10-01', '2003-02-06']
1160 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
1162 Just like with C<-in> all plausible combinations of literal SQL
1166 start0 => { -between => [ 1, 2 ] },
1167 start1 => { -between => \["? AND ?", 1, 2] },
1168 start2 => { -between => \"lower(x) AND upper(y)" },
1169 start3 => { -between => [
1171 \["upper(?)", 'stuff' ],
1178 ( start0 BETWEEN ? AND ? )
1179 AND ( start1 BETWEEN ? AND ? )
1180 AND ( start2 BETWEEN lower(x) AND upper(y) )
1181 AND ( start3 BETWEEN lower(x) AND upper(?) )
1183 @bind = (1, 2, 1, 2, 'stuff');
1186 These are the two builtin "special operators"; but the
1187 list can be expanded : see section L</"SPECIAL OPERATORS"> below.
1189 =head2 Unary operators: bool
1191 If you wish to test against boolean columns or functions within your
1192 database you can use the C<-bool> and C<-not_bool> operators. For
1193 example to test the column C<is_user> being true and the column
1194 C<is_enabled> being false you would use:-
1198 -not_bool => 'is_enabled',
1203 WHERE is_user AND NOT is_enabled
1205 If a more complex combination is required, testing more conditions,
1206 then you should use the and/or operators:-
1211 -not_bool => { two=> { -rlike => 'bar' } },
1212 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
1223 (NOT ( three = ? OR three > ? ))
1226 =head2 Nested conditions, -and/-or prefixes
1228 So far, we've seen how multiple conditions are joined with a top-level
1229 C<AND>. We can change this by putting the different conditions we want in
1230 hashes and then putting those hashes in an array. For example:
1235 status => { -like => ['pending%', 'dispatched'] },
1239 status => 'unassigned',
1243 This data structure would create the following:
1245 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
1246 OR ( user = ? AND status = ? ) )";
1247 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
1250 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
1251 to change the logic inside :
1257 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
1258 -or => { workhrs => {'<', 50}, geo => 'EURO' },
1265 WHERE ( user = ? AND (
1266 ( workhrs > ? AND geo = ? )
1267 OR ( workhrs < ? OR geo = ? )
1270 =head3 Algebraic inconsistency, for historical reasons
1272 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
1273 operator goes C<outside> of the nested structure; whereas when connecting
1274 several constraints on one column, the C<-and> operator goes
1275 C<inside> the arrayref. Here is an example combining both features :
1278 -and => [a => 1, b => 2],
1279 -or => [c => 3, d => 4],
1280 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
1285 WHERE ( ( ( a = ? AND b = ? )
1286 OR ( c = ? OR d = ? )
1287 OR ( e LIKE ? AND e LIKE ? ) ) )
1289 This difference in syntax is unfortunate but must be preserved for
1290 historical reasons. So be careful : the two examples below would
1291 seem algebraically equivalent, but they are not
1293 {col => [-and => {-like => 'foo%'}, {-like => '%bar'}]}
1294 # yields : WHERE ( ( col LIKE ? AND col LIKE ? ) )
1296 [-and => {col => {-like => 'foo%'}, {col => {-like => '%bar'}}]]
1297 # yields : WHERE ( ( col LIKE ? OR col LIKE ? ) )
1300 =head2 Literal SQL and value type operators
1302 The basic premise of SQL::Abstract is that in WHERE specifications the "left
1303 side" is a column name and the "right side" is a value (normally rendered as
1304 a placeholder). This holds true for both hashrefs and arrayref pairs as you
1305 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
1306 alter this behavior. There are several ways of doing so.
1310 This is a virtual operator that signals the string to its right side is an
1311 identifier (a column name) and not a value. For example to compare two
1312 columns you would write:
1315 priority => { '<', 2 },
1316 requestor => { -ident => 'submitter' },
1321 $stmt = "WHERE priority < ? AND requestor = submitter";
1324 If you are maintaining legacy code you may see a different construct as
1325 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
1330 This is a virtual operator that signals that the construct to its right side
1331 is a value to be passed to DBI. This is for example necessary when you want
1332 to write a where clause against an array (for RDBMS that support such
1333 datatypes). For example:
1336 array => { -value => [1, 2, 3] }
1341 $stmt = 'WHERE array = ?';
1342 @bind = ([1, 2, 3]);
1344 Note that if you were to simply say:
1350 the result would probably not be what you wanted:
1352 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
1357 Finally, sometimes only literal SQL will do. To include a random snippet
1358 of SQL verbatim, you specify it as a scalar reference. Consider this only
1359 as a last resort. Usually there is a better way. For example:
1362 priority => { '<', 2 },
1363 requestor => { -in => \'(SELECT name FROM hitmen)' },
1368 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
1371 Note that in this example, you only get one bind parameter back, since
1372 the verbatim SQL is passed as part of the statement.
1376 Never use untrusted input as a literal SQL argument - this is a massive
1377 security risk (there is no way to check literal snippets for SQL
1378 injections and other nastyness). If you need to deal with untrusted input
1379 use literal SQL with placeholders as described next.
1381 =head3 Literal SQL with placeholders and bind values (subqueries)
1383 If the literal SQL to be inserted has placeholders and bind values,
1384 use a reference to an arrayref (yes this is a double reference --
1385 not so common, but perfectly legal Perl). For example, to find a date
1386 in Postgres you can use something like this:
1389 date_column => \[q/= date '2008-09-30' - ?::integer/, 10/]
1394 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
1397 Note that you must pass the bind values in the same format as they are returned
1398 by L</where>. That means that if you set L</bindtype> to C<columns>, you must
1399 provide the bind values in the C<< [ column_meta => value ] >> format, where
1400 C<column_meta> is an opaque scalar value; most commonly the column name, but
1401 you can use any scalar value (including references and blessed references),
1402 L<SQL::Abstract> will simply pass it through intact. So if C<bindtype> is set
1403 to C<columns> the above example will look like:
1406 date_column => \[q/= date '2008-09-30' - ?::integer/, [ dummy => 10 ]/]
1409 Literal SQL is especially useful for nesting parenthesized clauses in the
1410 main SQL query. Here is a first example :
1412 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
1416 bar => \["IN ($sub_stmt)" => @sub_bind],
1421 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
1422 WHERE c2 < ? AND c3 LIKE ?))";
1423 @bind = (1234, 100, "foo%");
1425 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
1426 are expressed in the same way. Of course the C<$sub_stmt> and
1427 its associated bind values can be generated through a former call
1430 my ($sub_stmt, @sub_bind)
1431 = $sql->select("t1", "c1", {c2 => {"<" => 100},
1432 c3 => {-like => "foo%"}});
1435 bar => \["> ALL ($sub_stmt)" => @sub_bind],
1438 In the examples above, the subquery was used as an operator on a column;
1439 but the same principle also applies for a clause within the main C<%where>
1440 hash, like an EXISTS subquery :
1442 my ($sub_stmt, @sub_bind)
1443 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
1444 my %where = ( -and => [
1446 \["EXISTS ($sub_stmt)" => @sub_bind],
1451 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
1452 WHERE c1 = ? AND c2 > t0.c0))";
1456 Observe that the condition on C<c2> in the subquery refers to
1457 column C<t0.c0> of the main query : this is I<not> a bind
1458 value, so we have to express it through a scalar ref.
1459 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
1460 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
1461 what we wanted here.
1463 Finally, here is an example where a subquery is used
1464 for expressing unary negation:
1466 my ($sub_stmt, @sub_bind)
1467 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
1468 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
1470 lname => {like => '%son%'},
1471 \["NOT ($sub_stmt)" => @sub_bind],
1476 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
1477 @bind = ('%son%', 10, 20)
1479 =head3 Deprecated usage of Literal SQL
1481 Below are some examples of archaic use of literal SQL. It is shown only as
1482 reference for those who deal with legacy code. Each example has a much
1483 better, cleaner and safer alternative that users should opt for in new code.
1489 my %where = ( requestor => \'IS NOT NULL' )
1491 $stmt = "WHERE requestor IS NOT NULL"
1493 This used to be the way of generating NULL comparisons, before the handling
1494 of C<undef> got formalized. For new code please use the superior syntax as
1495 described in L</Tests for NULL values>.
1499 my %where = ( requestor => \'= submitter' )
1501 $stmt = "WHERE requestor = submitter"
1503 This used to be the only way to compare columns. Use the superior L</-ident>
1504 method for all new code. For example an identifier declared in such a way
1505 will be properly quoted if L</quote_char> is properly set, while the legacy
1506 form will remain as supplied.
1510 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
1512 $stmt = "WHERE completed > ? AND is_ready"
1513 @bind = ('2012-12-21')
1515 Using an empty string literal used to be the only way to express a boolean.
1516 For all new code please use the much more readable
1517 L<-bool|/Unary operators: bool> operator.
1523 These pages could go on for a while, since the nesting of the data
1524 structures this module can handle are pretty much unlimited (the
1525 module implements the C<WHERE> expansion as a recursive function
1526 internally). Your best bet is to "play around" with the module a
1527 little to see how the data structures behave, and choose the best
1528 format for your data based on that.
1530 And of course, all the values above will probably be replaced with
1531 variables gotten from forms or the command line. After all, if you
1532 knew everything ahead of time, you wouldn't have to worry about
1533 dynamically-generating SQL and could just hardwire it into your
1536 =head1 ORDER BY CLAUSES
1538 Some functions take an order by clause. This can either be a scalar (just a
1539 column name,) a hash of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } >>,
1540 or an array of either of the two previous forms. Examples:
1542 Given | Will Generate
1543 ----------------------------------------------------------
1545 \'colA DESC' | ORDER BY colA DESC
1547 'colA' | ORDER BY colA
1549 [qw/colA colB/] | ORDER BY colA, colB
1551 {-asc => 'colA'} | ORDER BY colA ASC
1553 {-desc => 'colB'} | ORDER BY colB DESC
1555 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
1557 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
1560 { -asc => 'colA' }, | ORDER BY colA ASC, colB DESC,
1561 { -desc => [qw/colB/], | colC ASC, colD ASC
1562 { -asc => [qw/colC colD/],|
1564 ===========================================================
1568 =head1 SPECIAL OPERATORS
1570 my $sqlmaker = SQL::Abstract->new(special_ops => [
1574 my ($self, $field, $op, $arg) = @_;
1580 handler => 'method_name',
1584 A "special operator" is a SQL syntactic clause that can be
1585 applied to a field, instead of a usual binary operator.
1588 WHERE field IN (?, ?, ?)
1589 WHERE field BETWEEN ? AND ?
1590 WHERE MATCH(field) AGAINST (?, ?)
1592 Special operators IN and BETWEEN are fairly standard and therefore
1593 are builtin within C<SQL::Abstract> (as the overridable methods
1594 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
1595 like the MATCH .. AGAINST example above which is specific to MySQL,
1596 you can write your own operator handlers - supply a C<special_ops>
1597 argument to the C<new> method. That argument takes an arrayref of
1598 operator definitions; each operator definition is a hashref with two
1605 the regular expression to match the operator
1609 Either a coderef or a plain scalar method name. In both cases
1610 the expected return is C<< ($sql, @bind) >>.
1612 When supplied with a method name, it is simply called on the
1613 L<SQL::Abstract/> object as:
1615 $self->$method_name ($field, $op, $arg)
1619 $op is the part that matched the handler regex
1620 $field is the LHS of the operator
1623 When supplied with a coderef, it is called as:
1625 $coderef->($self, $field, $op, $arg)
1630 For example, here is an implementation
1631 of the MATCH .. AGAINST syntax for MySQL
1633 my $sqlmaker = SQL::Abstract->new(special_ops => [
1635 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
1636 {regex => qr/^match$/i,
1638 my ($self, $field, $op, $arg) = @_;
1639 $arg = [$arg] if not ref $arg;
1640 my $label = $self->_quote($field);
1641 my ($placeholder) = $self->_convert('?');
1642 my $placeholders = join ", ", (($placeholder) x @$arg);
1643 my $sql = $self->_sqlcase('match') . " ($label) "
1644 . $self->_sqlcase('against') . " ($placeholders) ";
1645 my @bind = $self->_bindtype($field, @$arg);
1646 return ($sql, @bind);
1653 =head1 UNARY OPERATORS
1655 my $sqlmaker = SQL::Abstract->new(unary_ops => [
1659 my ($self, $op, $arg) = @_;
1665 handler => 'method_name',
1669 A "unary operator" is a SQL syntactic clause that can be
1670 applied to a field - the operator goes before the field
1672 You can write your own operator handlers - supply a C<unary_ops>
1673 argument to the C<new> method. That argument takes an arrayref of
1674 operator definitions; each operator definition is a hashref with two
1681 the regular expression to match the operator
1685 Either a coderef or a plain scalar method name. In both cases
1686 the expected return is C<< $sql >>.
1688 When supplied with a method name, it is simply called on the
1689 L<SQL::Abstract/> object as:
1691 $self->$method_name ($op, $arg)
1695 $op is the part that matched the handler regex
1696 $arg is the RHS or argument of the operator
1698 When supplied with a coderef, it is called as:
1700 $coderef->($self, $op, $arg)
1708 Thanks to some benchmarking by Mark Stosberg, it turns out that
1709 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
1710 I must admit this wasn't an intentional design issue, but it's a
1711 byproduct of the fact that you get to control your C<DBI> handles
1714 To maximize performance, use a code snippet like the following:
1716 # prepare a statement handle using the first row
1717 # and then reuse it for the rest of the rows
1719 for my $href (@array_of_hashrefs) {
1720 $stmt ||= $sql->insert('table', $href);
1721 $sth ||= $dbh->prepare($stmt);
1722 $sth->execute($sql->values($href));
1725 The reason this works is because the keys in your C<$href> are sorted
1726 internally by B<SQL::Abstract>. Thus, as long as your data retains
1727 the same structure, you only have to generate the SQL the first time
1728 around. On subsequent queries, simply use the C<values> function provided
1729 by this module to return your values in the correct order.
1731 However this depends on the values having the same type - if, for
1732 example, the values of a where clause may either have values
1733 (resulting in sql of the form C<column = ?> with a single bind
1734 value), or alternatively the values might be C<undef> (resulting in
1735 sql of the form C<column IS NULL> with no bind value) then the
1736 caching technique suggested will not work.
1740 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
1741 really like this part (I do, at least). Building up a complex query
1742 can be as simple as the following:
1749 use CGI::FormBuilder;
1752 my $form = CGI::FormBuilder->new(...);
1753 my $sql = SQL::Abstract->new;
1755 if ($form->submitted) {
1756 my $field = $form->field;
1757 my $id = delete $field->{id};
1758 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
1761 Of course, you would still have to connect using C<DBI> to run the
1762 query, but the point is that if you make your form look like your
1763 table, the actual query script can be extremely simplistic.
1765 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
1766 a fast interface to returning and formatting data. I frequently
1767 use these three modules together to write complex database query
1768 apps in under 50 lines.
1774 =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
1776 =item * git: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
1782 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
1783 Great care has been taken to preserve the I<published> behavior
1784 documented in previous versions in the 1.* family; however,
1785 some features that were previously undocumented, or behaved
1786 differently from the documentation, had to be changed in order
1787 to clarify the semantics. Hence, client code that was relying
1788 on some dark areas of C<SQL::Abstract> v1.*
1789 B<might behave differently> in v1.50.
1791 The main changes are :
1797 support for literal SQL through the C<< \ [$sql, bind] >> syntax.
1801 support for the { operator => \"..." } construct (to embed literal SQL)
1805 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
1809 optional support for L<array datatypes|/"Inserting and Updating Arrays">
1813 defensive programming : check arguments
1817 fixed bug with global logic, which was previously implemented
1818 through global variables yielding side-effects. Prior versions would
1819 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
1820 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
1821 Now this is interpreted
1822 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
1827 fixed semantics of _bindtype on array args
1831 dropped the C<_anoncopy> of the %where tree. No longer necessary,
1832 we just avoid shifting arrays within that tree.
1836 dropped the C<_modlogic> function
1840 =head1 ACKNOWLEDGEMENTS
1842 There are a number of individuals that have really helped out with
1843 this module. Unfortunately, most of them submitted bugs via CPAN
1844 so I have no idea who they are! But the people I do know are:
1846 Ash Berlin (order_by hash term support)
1847 Matt Trout (DBIx::Class support)
1848 Mark Stosberg (benchmarking)
1849 Chas Owens (initial "IN" operator support)
1850 Philip Collins (per-field SQL functions)
1851 Eric Kolve (hashref "AND" support)
1852 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
1853 Dan Kubb (support for "quote_char" and "name_sep")
1854 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
1855 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
1856 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
1857 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
1858 Oliver Charles (support for "RETURNING" after "INSERT")
1864 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
1868 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
1870 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
1872 For support, your best bet is to try the C<DBIx::Class> users mailing list.
1873 While not an official support venue, C<DBIx::Class> makes heavy use of
1874 C<SQL::Abstract>, and as such list members there are very familiar with
1875 how to create queries.
1879 This module is free software; you may copy this under the same
1880 terms as perl itself (either the GNU General Public License or
1881 the Artistic License)