remove all remnants of DQ::Constants
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract.pm
CommitLineData
96449e8e 1package SQL::Abstract; # see doc at end of file
2
9d9d5bd6 3use Carp ();
312d830b 4use List::Util ();
5use Scalar::Util ();
a82e41dc 6use Module::Runtime qw(use_module);
3a9aca02 7use Moo;
8b9b83ae 8use namespace::clean;
96449e8e 9
64b9e432 10our $VERSION = '1.72';
7479e27e 11
a82e41dc 12$VERSION = eval $VERSION;
96449e8e 13
14sub belch (@) {
15 my($func) = (caller(1))[3];
9d9d5bd6 16 Carp::carp "[$func] Warning: ", @_;
96449e8e 17}
18
19sub puke (@) {
20 my($func) = (caller(1))[3];
9d9d5bd6 21 Carp::croak "[$func] Fatal: ", @_;
96449e8e 22}
23
a82e41dc 24has converter => (is => 'lazy', clearer => 'clear_converter');
96449e8e 25
3a9aca02 26has case => (
27 is => 'ro', coerce => sub { $_[0] eq 'lower' ? 'lower' : undef }
28);
96449e8e 29
3a9aca02 30has logic => (
31 is => 'ro', coerce => sub { uc($_[0]) }, default => sub { 'OR' }
32);
96449e8e 33
3a9aca02 34has bindtype => (
35 is => 'ro', default => sub { 'normal' }
36);
96449e8e 37
3a9aca02 38has cmp => (is => 'ro', default => sub { '=' });
96449e8e 39
3a9aca02 40has sqltrue => (is => 'ro', default => sub { '1=1' });
41has sqlfalse => (is => 'ro', default => sub { '0=1' });
42
43has special_ops => (is => 'ro', default => sub { [] });
44has unary_ops => (is => 'ro', default => sub { [] });
59f23b3d 45
a82e41dc 46# FIXME
47# need to guard against ()'s in column names too, but this will break tons of
48# hacks... ideas anyone?
8f57728a 49
3a9aca02 50has injection_guard => (
51 is => 'ro',
52 default => sub {
53 qr/
54 \;
55 |
56 ^ \s* go \s
57 /xmi;
58 }
59);
60
29a3e5dc 61has renderer => (is => 'lazy', clearer => 'clear_renderer');
3a9aca02 62
29a3e5dc 63has name_sep => (
64 is => 'rw', default => sub { '.' },
a82e41dc 65 trigger => sub {
66 $_[0]->clear_renderer;
67 $_[0]->clear_converter;
68 },
29a3e5dc 69);
3a9aca02 70
29a3e5dc 71has quote_char => (
72 is => 'rw',
a82e41dc 73 trigger => sub {
74 $_[0]->clear_renderer;
75 $_[0]->clear_converter;
76 },
29a3e5dc 77);
a9bb5c4c 78
62d17764 79has collapse_aliases => (
80 is => 'ro',
81 default => sub { 0 }
82);
83
a82e41dc 84has always_quote => (
85 is => 'rw', default => sub { 1 },
86 trigger => sub {
87 $_[0]->clear_renderer;
88 $_[0]->clear_converter;
89 },
90);
3a9aca02 91
92has convert => (is => 'ro');
93
94has array_datatypes => (is => 'ro');
95
a82e41dc 96has converter_class => (
dfaa9684 97 is => 'rw', lazy => 1, builder => '_build_converter_class',
98 trigger => sub { shift->clear_converter },
a82e41dc 99);
100
dfaa9684 101sub _build_converter_class {
102 use_module('SQL::Abstract::Converter')
103}
104
a82e41dc 105has renderer_class => (
dfaa9684 106 is => 'rw', lazy => 1, builder => '_build_renderer_class',
107 trigger => sub { shift->clear_renderer },
a82e41dc 108);
109
dfaa9684 110sub _build_renderer_class {
111 use_module('Data::Query::Renderer::SQL::Naive')
112}
113
a82e41dc 114sub _converter_args {
115 my ($self) = @_;
116 Scalar::Util::weaken($self);
117 +{
118 lower_case => $self->case,
119 default_logic => $self->logic,
120 bind_meta => not($self->bindtype eq 'normal'),
121 identifier_sep => $self->name_sep,
122 (map +($_ => $self->$_), qw(
123 cmp sqltrue sqlfalse injection_guard convert array_datatypes
124 )),
125 special_ops => [
126 map {
127 my $sub = $_->{handler};
128 +{
129 %$_,
130 handler => sub { $self->$sub(@_) }
131 }
132 } @{$self->special_ops}
133 ],
134 renderer_will_quote => (
135 defined($self->quote_char) and $self->always_quote
136 ),
137 }
138}
139
140sub _build_converter {
141 my ($self) = @_;
dfaa9684 142 $self->converter_class->new($self->_converter_args);
a82e41dc 143}
144
145sub _renderer_args {
3a9aca02 146 my ($self) = @_;
3a9aca02 147 my ($chars);
148 for ($self->quote_char) {
149 $chars = defined() ? (ref() ? $_ : [$_]) : ['',''];
150 }
a82e41dc 151 +{
3a9aca02 152 quote_chars => $chars, always_quote => $self->always_quote,
153 identifier_sep => $self->name_sep,
62d17764 154 collapse_aliases => $self->collapse_aliases,
3a9aca02 155 ($self->case ? (lc_keywords => 1) : ()), # always 'lower' if it exists
a82e41dc 156 };
157}
158
159sub _build_renderer {
160 my ($self) = @_;
dfaa9684 161 $self->renderer_class->new($self->_renderer_args);
b6251592 162}
96449e8e 163
8f57728a 164sub _render_dq {
165 my ($self, $dq) = @_;
9057306b 166 if (!$dq) {
167 return '';
168 }
3a9aca02 169 my ($sql, @bind) = @{$self->renderer->render($dq)};
b4951847 170 wantarray ?
171 ($self->{bindtype} eq 'normal'
172 ? ($sql, map $_->{value}, @bind)
a420b11f 173 : ($sql, map [ $_->{value_meta}, $_->{value} ], @bind)
b4951847 174 )
175 : $sql;
176}
177
a9bb5c4c 178sub _render_sqla {
179 my ($self, $type, @args) = @_;
a82e41dc 180 $self->_render_dq($self->converter->${\"_${type}_to_dq"}(@args));
170e6c33 181}
182
a9bb5c4c 183sub insert { shift->_render_sqla(insert => @_) }
fe3ae272 184
a9bb5c4c 185sub update { shift->_render_sqla(update => @_) }
9057306b 186
a9bb5c4c 187sub select { shift->_render_sqla(select => @_) }
9057306b 188
a9bb5c4c 189sub delete { shift->_render_sqla(delete => @_) }
96449e8e 190
96449e8e 191sub where {
192 my ($self, $where, $order) = @_;
193
1d6b8d4d 194 my $sql = '';
195 my @bind;
196
96449e8e 197 # where ?
1d6b8d4d 198 ($sql, @bind) = $self->_recurse_where($where) if defined($where);
96449e8e 199 $sql = $sql ? $self->_sqlcase(' where ') . "( $sql )" : '';
200
201 # order by?
202 if ($order) {
203 $sql .= $self->_order_by($order);
204 }
205
9d48860e 206 return wantarray ? ($sql, @bind) : $sql;
96449e8e 207}
208
a9bb5c4c 209sub _recurse_where { shift->_render_sqla(where => @_) }
d4e889af 210
96449e8e 211sub _order_by {
212 my ($self, $arg) = @_;
a82e41dc 213 if (my $dq = $self->converter->_order_by_to_dq($arg)) {
b4951847 214 # SQLA generates ' ORDER BY foo'. The hilarity.
215 wantarray
216 ? do { my @r = $self->_render_dq($dq); $r[0] = ' '.$r[0]; @r }
217 : ' '.$self->_render_dq($dq);
218 } else {
219 '';
f267b646 220 }
f267b646 221}
222
955e77ca 223# highly optimized, as it's called way too often
96449e8e 224sub _quote {
955e77ca 225 # my ($self, $label) = @_;
96449e8e 226
955e77ca 227 return '' unless defined $_[1];
955e77ca 228 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
96449e8e 229
b6251592 230 unless ($_[0]->{quote_char}) {
170e6c33 231 $_[0]->_assert_pass_injection_guard($_[1]);
b6251592 232 return $_[1];
233 }
96449e8e 234
07d7c35c 235 my $qref = ref $_[0]->{quote_char};
955e77ca 236 my ($l, $r);
07d7c35c 237 if (!$qref) {
238 ($l, $r) = ( $_[0]->{quote_char}, $_[0]->{quote_char} );
955e77ca 239 }
07d7c35c 240 elsif ($qref eq 'ARRAY') {
241 ($l, $r) = @{$_[0]->{quote_char}};
955e77ca 242 }
243 else {
244 puke "Unsupported quote_char format: $_[0]->{quote_char}";
245 }
96449e8e 246
07d7c35c 247 # parts containing * are naturally unquoted
248 return join( $_[0]->{name_sep}||'', map
955e77ca 249 { $_ eq '*' ? $_ : $l . $_ . $r }
250 ( $_[0]->{name_sep} ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) : $_[1] )
251 );
96449e8e 252}
253
a82e41dc 254sub _assert_pass_injection_guard {
255 if ($_[1] =~ $_[0]->{injection_guard}) {
256 my $class = ref $_[0];
257 die "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the
258 "
259 . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own
260"
261 . "{injection_guard} attribute to ${class}->new()"
262 }
263}
96449e8e 264
265# Conversion, if applicable
266sub _convert ($) {
07d7c35c 267 #my ($self, $arg) = @_;
96449e8e 268
269# LDNOTE : modified the previous implementation below because
270# it was not consistent : the first "return" is always an array,
271# the second "return" is context-dependent. Anyway, _convert
9d48860e 272# seems always used with just a single argument, so make it a
96449e8e 273# scalar function.
274# return @_ unless $self->{convert};
275# my $conv = $self->_sqlcase($self->{convert});
276# my @ret = map { $conv.'('.$_.')' } @_;
277# return wantarray ? @ret : $ret[0];
07d7c35c 278 if ($_[0]->{convert}) {
279 return $_[0]->_sqlcase($_[0]->{convert}) .'(' . $_[1] . ')';
96449e8e 280 }
07d7c35c 281 return $_[1];
96449e8e 282}
283
284# And bindtype
285sub _bindtype (@) {
07d7c35c 286 #my ($self, $col, @vals) = @_;
96449e8e 287
9d48860e 288 #LDNOTE : changed original implementation below because it did not make
96449e8e 289 # sense when bindtype eq 'columns' and @vals > 1.
290# return $self->{bindtype} eq 'columns' ? [ $col, @vals ] : @vals;
291
07d7c35c 292 # called often - tighten code
293 return $_[0]->{bindtype} eq 'columns'
294 ? map {[$_[1], $_]} @_[2 .. $#_]
295 : @_[2 .. $#_]
296 ;
96449e8e 297}
298
fe3ae272 299# Dies if any element of @bind is not in [colname => value] format
300# if bindtype is 'columns'.
301sub _assert_bindval_matches_bindtype {
c94a6c93 302# my ($self, @bind) = @_;
303 my $self = shift;
fe3ae272 304 if ($self->{bindtype} eq 'columns') {
c94a6c93 305 for (@_) {
306 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
3a06278c 307 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
fe3ae272 308 }
309 }
310 }
311}
312
96449e8e 313# Fix SQL case, if so requested
314sub _sqlcase {
96449e8e 315 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
316 # don't touch the argument ... crooked logic, but let's not change it!
07d7c35c 317 return $_[0]->{case} ? $_[1] : uc($_[1]);
96449e8e 318}
319
96449e8e 320sub values {
321 my $self = shift;
322 my $data = shift || return;
323 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
324 unless ref $data eq 'HASH';
bab725ce 325
326 my @all_bind;
327 foreach my $k ( sort keys %$data ) {
328 my $v = $data->{$k};
5cf3969e 329 local our $Cur_Col_Meta = $k;
a9bb5c4c 330 my ($sql, @bind) = $self->_render_sqla(
331 mutation_rhs => $v
5cf3969e 332 );
333 push @all_bind, @bind;
bab725ce 334 }
335
336 return @all_bind;
96449e8e 337}
338
339sub generate {
340 my $self = shift;
341
342 my(@sql, @sqlq, @sqlv);
343
344 for (@_) {
345 my $ref = ref $_;
346 if ($ref eq 'HASH') {
347 for my $k (sort keys %$_) {
348 my $v = $_->{$k};
349 my $r = ref $v;
350 my $label = $self->_quote($k);
351 if ($r eq 'ARRAY') {
fe3ae272 352 # literal SQL with bind
353 my ($sql, @bind) = @$v;
354 $self->_assert_bindval_matches_bindtype(@bind);
96449e8e 355 push @sqlq, "$label = $sql";
fe3ae272 356 push @sqlv, @bind;
96449e8e 357 } elsif ($r eq 'SCALAR') {
fe3ae272 358 # literal SQL without bind
96449e8e 359 push @sqlq, "$label = $$v";
9d48860e 360 } else {
96449e8e 361 push @sqlq, "$label = ?";
362 push @sqlv, $self->_bindtype($k, $v);
363 }
364 }
365 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
366 } elsif ($ref eq 'ARRAY') {
367 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
368 for my $v (@$_) {
369 my $r = ref $v;
fe3ae272 370 if ($r eq 'ARRAY') { # literal SQL with bind
371 my ($sql, @bind) = @$v;
372 $self->_assert_bindval_matches_bindtype(@bind);
373 push @sqlq, $sql;
374 push @sqlv, @bind;
375 } elsif ($r eq 'SCALAR') { # literal SQL without bind
96449e8e 376 # embedded literal SQL
377 push @sqlq, $$v;
9d48860e 378 } else {
96449e8e 379 push @sqlq, '?';
380 push @sqlv, $v;
381 }
382 }
383 push @sql, '(' . join(', ', @sqlq) . ')';
384 } elsif ($ref eq 'SCALAR') {
385 # literal SQL
386 push @sql, $$_;
387 } else {
388 # strings get case twiddled
389 push @sql, $self->_sqlcase($_);
390 }
391 }
392
393 my $sql = join ' ', @sql;
394
395 # this is pretty tricky
396 # if ask for an array, return ($stmt, @bind)
397 # otherwise, s/?/shift @sqlv/ to put it inline
398 if (wantarray) {
399 return ($sql, @sqlv);
400 } else {
401 1 while $sql =~ s/\?/my $d = shift(@sqlv);
402 ref $d ? $d->[1] : $d/e;
403 return $sql;
404 }
405}
406
96449e8e 4071;
408
409
96449e8e 410__END__
32eab2da 411
412=head1 NAME
413
414SQL::Abstract - Generate SQL from Perl data structures
415
416=head1 SYNOPSIS
417
418 use SQL::Abstract;
419
420 my $sql = SQL::Abstract->new;
421
422 my($stmt, @bind) = $sql->select($table, \@fields, \%where, \@order);
423
424 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
425
426 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
427
428 my($stmt, @bind) = $sql->delete($table, \%where);
429
430 # Then, use these in your DBI statements
431 my $sth = $dbh->prepare($stmt);
432 $sth->execute(@bind);
433
434 # Just generate the WHERE clause
abe72f94 435 my($stmt, @bind) = $sql->where(\%where, \@order);
32eab2da 436
437 # Return values in the same order, for hashed queries
438 # See PERFORMANCE section for more details
439 my @bind = $sql->values(\%fieldvals);
440
441=head1 DESCRIPTION
442
443This module was inspired by the excellent L<DBIx::Abstract>.
444However, in using that module I found that what I really wanted
445to do was generate SQL, but still retain complete control over my
446statement handles and use the DBI interface. So, I set out to
447create an abstract SQL generation module.
448
449While based on the concepts used by L<DBIx::Abstract>, there are
450several important differences, especially when it comes to WHERE
451clauses. I have modified the concepts used to make the SQL easier
452to generate from Perl data structures and, IMO, more intuitive.
453The underlying idea is for this module to do what you mean, based
454on the data structures you provide it. The big advantage is that
455you don't have to modify your code every time your data changes,
456as this module figures it out.
457
458To begin with, an SQL INSERT is as easy as just specifying a hash
459of C<key=value> pairs:
460
461 my %data = (
462 name => 'Jimbo Bobson',
463 phone => '123-456-7890',
464 address => '42 Sister Lane',
465 city => 'St. Louis',
466 state => 'Louisiana',
467 );
468
469The SQL can then be generated with this:
470
471 my($stmt, @bind) = $sql->insert('people', \%data);
472
473Which would give you something like this:
474
475 $stmt = "INSERT INTO people
476 (address, city, name, phone, state)
477 VALUES (?, ?, ?, ?, ?)";
478 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
479 '123-456-7890', 'Louisiana');
480
481These are then used directly in your DBI code:
482
483 my $sth = $dbh->prepare($stmt);
484 $sth->execute(@bind);
485
96449e8e 486=head2 Inserting and Updating Arrays
487
488If your database has array types (like for example Postgres),
489activate the special option C<< array_datatypes => 1 >>
9d48860e 490when creating the C<SQL::Abstract> object.
96449e8e 491Then you may use an arrayref to insert and update database array types:
492
493 my $sql = SQL::Abstract->new(array_datatypes => 1);
494 my %data = (
495 planets => [qw/Mercury Venus Earth Mars/]
496 );
9d48860e 497
96449e8e 498 my($stmt, @bind) = $sql->insert('solar_system', \%data);
499
500This results in:
501
502 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
503
504 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
505
506
507=head2 Inserting and Updating SQL
508
509In order to apply SQL functions to elements of your C<%data> you may
510specify a reference to an arrayref for the given hash value. For example,
511if you need to execute the Oracle C<to_date> function on a value, you can
512say something like this:
32eab2da 513
514 my %data = (
515 name => 'Bill',
96449e8e 516 date_entered => \["to_date(?,'MM/DD/YYYY')", "03/02/2003"],
9d48860e 517 );
32eab2da 518
519The first value in the array is the actual SQL. Any other values are
520optional and would be included in the bind values array. This gives
521you:
522
523 my($stmt, @bind) = $sql->insert('people', \%data);
524
9d48860e 525 $stmt = "INSERT INTO people (name, date_entered)
32eab2da 526 VALUES (?, to_date(?,'MM/DD/YYYY'))";
527 @bind = ('Bill', '03/02/2003');
528
529An UPDATE is just as easy, all you change is the name of the function:
530
531 my($stmt, @bind) = $sql->update('people', \%data);
532
533Notice that your C<%data> isn't touched; the module will generate
534the appropriately quirky SQL for you automatically. Usually you'll
535want to specify a WHERE clause for your UPDATE, though, which is
536where handling C<%where> hashes comes in handy...
537
96449e8e 538=head2 Complex where statements
539
32eab2da 540This module can generate pretty complicated WHERE statements
541easily. For example, simple C<key=value> pairs are taken to mean
542equality, and if you want to see if a field is within a set
543of values, you can use an arrayref. Let's say we wanted to
544SELECT some data based on this criteria:
545
546 my %where = (
547 requestor => 'inna',
548 worker => ['nwiger', 'rcwe', 'sfz'],
549 status => { '!=', 'completed' }
550 );
551
552 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
553
554The above would give you something like this:
555
556 $stmt = "SELECT * FROM tickets WHERE
557 ( requestor = ? ) AND ( status != ? )
558 AND ( worker = ? OR worker = ? OR worker = ? )";
559 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
560
561Which you could then use in DBI code like so:
562
563 my $sth = $dbh->prepare($stmt);
564 $sth->execute(@bind);
565
566Easy, eh?
567
568=head1 FUNCTIONS
569
570The functions are simple. There's one for each major SQL operation,
571and a constructor you use first. The arguments are specified in a
9d48860e 572similar order to each function (table, then fields, then a where
32eab2da 573clause) to try and simplify things.
574
83cab70b 575
83cab70b 576
32eab2da 577
578=head2 new(option => 'value')
579
580The C<new()> function takes a list of options and values, and returns
581a new B<SQL::Abstract> object which can then be used to generate SQL
582through the methods below. The options accepted are:
583
584=over
585
586=item case
587
588If set to 'lower', then SQL will be generated in all lowercase. By
589default SQL is generated in "textbook" case meaning something like:
590
591 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
592
96449e8e 593Any setting other than 'lower' is ignored.
594
32eab2da 595=item cmp
596
597This determines what the default comparison operator is. By default
598it is C<=>, meaning that a hash like this:
599
600 %where = (name => 'nwiger', email => 'nate@wiger.org');
601
602Will generate SQL like this:
603
604 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
605
606However, you may want loose comparisons by default, so if you set
607C<cmp> to C<like> you would get SQL such as:
608
609 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
610
611You can also override the comparsion on an individual basis - see
612the huge section on L</"WHERE CLAUSES"> at the bottom.
613
96449e8e 614=item sqltrue, sqlfalse
615
616Expressions for inserting boolean values within SQL statements.
6e0c6552 617By default these are C<1=1> and C<1=0>. They are used
618by the special operators C<-in> and C<-not_in> for generating
619correct SQL even when the argument is an empty array (see below).
96449e8e 620
32eab2da 621=item logic
622
623This determines the default logical operator for multiple WHERE
7cac25e6 624statements in arrays or hashes. If absent, the default logic is "or"
625for arrays, and "and" for hashes. This means that a WHERE
32eab2da 626array of the form:
627
628 @where = (
9d48860e 629 event_date => {'>=', '2/13/99'},
630 event_date => {'<=', '4/24/03'},
32eab2da 631 );
632
7cac25e6 633will generate SQL like this:
32eab2da 634
635 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
636
637This is probably not what you want given this query, though (look
638at the dates). To change the "OR" to an "AND", simply specify:
639
640 my $sql = SQL::Abstract->new(logic => 'and');
641
642Which will change the above C<WHERE> to:
643
644 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
645
96449e8e 646The logic can also be changed locally by inserting
7cac25e6 647a modifier in front of an arrayref :
96449e8e 648
9d48860e 649 @where = (-and => [event_date => {'>=', '2/13/99'},
7cac25e6 650 event_date => {'<=', '4/24/03'} ]);
96449e8e 651
652See the L</"WHERE CLAUSES"> section for explanations.
653
32eab2da 654=item convert
655
656This will automatically convert comparisons using the specified SQL
657function for both column and value. This is mostly used with an argument
658of C<upper> or C<lower>, so that the SQL will have the effect of
659case-insensitive "searches". For example, this:
660
661 $sql = SQL::Abstract->new(convert => 'upper');
662 %where = (keywords => 'MaKe iT CAse inSeNSItive');
663
664Will turn out the following SQL:
665
666 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
667
668The conversion can be C<upper()>, C<lower()>, or any other SQL function
669that can be applied symmetrically to fields (actually B<SQL::Abstract> does
670not validate this option; it will just pass through what you specify verbatim).
671
672=item bindtype
673
674This is a kludge because many databases suck. For example, you can't
675just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
676Instead, you have to use C<bind_param()>:
677
678 $sth->bind_param(1, 'reg data');
679 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
680
681The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
682which loses track of which field each slot refers to. Fear not.
683
684If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
685Currently, you can specify either C<normal> (default) or C<columns>. If you
686specify C<columns>, you will get an array that looks like this:
687
688 my $sql = SQL::Abstract->new(bindtype => 'columns');
689 my($stmt, @bind) = $sql->insert(...);
690
691 @bind = (
692 [ 'column1', 'value1' ],
693 [ 'column2', 'value2' ],
694 [ 'column3', 'value3' ],
695 );
696
697You can then iterate through this manually, using DBI's C<bind_param()>.
e3f9dff4 698
32eab2da 699 $sth->prepare($stmt);
700 my $i = 1;
701 for (@bind) {
702 my($col, $data) = @$_;
703 if ($col eq 'details' || $col eq 'comments') {
704 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
705 } elsif ($col eq 'image') {
706 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
707 } else {
708 $sth->bind_param($i, $data);
709 }
710 $i++;
711 }
712 $sth->execute; # execute without @bind now
713
714Now, why would you still use B<SQL::Abstract> if you have to do this crap?
715Basically, the advantage is still that you don't have to care which fields
716are or are not included. You could wrap that above C<for> loop in a simple
717sub called C<bind_fields()> or something and reuse it repeatedly. You still
718get a layer of abstraction over manual SQL specification.
719
deb148a2 720Note that if you set L</bindtype> to C<columns>, the C<\[$sql, @bind]>
721construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
722will expect the bind values in this format.
723
32eab2da 724=item quote_char
725
726This is the character that a table or column name will be quoted
9d48860e 727with. By default this is an empty string, but you could set it to
32eab2da 728the character C<`>, to generate SQL like this:
729
730 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
731
96449e8e 732Alternatively, you can supply an array ref of two items, the first being the left
733hand quote character, and the second the right hand quote character. For
734example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
735that generates SQL like this:
736
737 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
738
9d48860e 739Quoting is useful if you have tables or columns names that are reserved
96449e8e 740words in your database's SQL dialect.
32eab2da 741
742=item name_sep
743
744This is the character that separates a table and column name. It is
745necessary to specify this when the C<quote_char> option is selected,
746so that tables and column names can be individually quoted like this:
747
748 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
749
b6251592 750=item injection_guard
751
752A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
753column name specified in a query structure. This is a safety mechanism to avoid
754injection attacks when mishandling user input e.g.:
755
756 my %condition_as_column_value_pairs = get_values_from_user();
757 $sqla->select( ... , \%condition_as_column_value_pairs );
758
759If the expression matches an exception is thrown. Note that literal SQL
760supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
761
762Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
763
96449e8e 764=item array_datatypes
32eab2da 765
9d48860e 766When this option is true, arrayrefs in INSERT or UPDATE are
767interpreted as array datatypes and are passed directly
96449e8e 768to the DBI layer.
769When this option is false, arrayrefs are interpreted
770as literal SQL, just like refs to arrayrefs
771(but this behavior is for backwards compatibility; when writing
772new queries, use the "reference to arrayref" syntax
773for literal SQL).
32eab2da 774
32eab2da 775
96449e8e 776=item special_ops
32eab2da 777
9d48860e 778Takes a reference to a list of "special operators"
96449e8e 779to extend the syntax understood by L<SQL::Abstract>.
780See section L</"SPECIAL OPERATORS"> for details.
32eab2da 781
59f23b3d 782=item unary_ops
783
9d48860e 784Takes a reference to a list of "unary operators"
59f23b3d 785to extend the syntax understood by L<SQL::Abstract>.
786See section L</"UNARY OPERATORS"> for details.
787
32eab2da 788
32eab2da 789
96449e8e 790=back
32eab2da 791
02288357 792=head2 insert($table, \@values || \%fieldvals, \%options)
32eab2da 793
794This is the simplest function. You simply give it a table name
795and either an arrayref of values or hashref of field/value pairs.
796It returns an SQL INSERT statement and a list of bind values.
96449e8e 797See the sections on L</"Inserting and Updating Arrays"> and
798L</"Inserting and Updating SQL"> for information on how to insert
799with those data types.
32eab2da 800
02288357 801The optional C<\%options> hash reference may contain additional
802options to generate the insert SQL. Currently supported options
803are:
804
805=over 4
806
807=item returning
808
809Takes either a scalar of raw SQL fields, or an array reference of
810field names, and adds on an SQL C<RETURNING> statement at the end.
811This allows you to return data generated by the insert statement
812(such as row IDs) without performing another C<SELECT> statement.
813Note, however, this is not part of the SQL standard and may not
814be supported by all database engines.
815
816=back
817
32eab2da 818=head2 update($table, \%fieldvals, \%where)
819
820This takes a table, hashref of field/value pairs, and an optional
86298391 821hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
32eab2da 822of bind values.
96449e8e 823See the sections on L</"Inserting and Updating Arrays"> and
824L</"Inserting and Updating SQL"> for information on how to insert
825with those data types.
32eab2da 826
96449e8e 827=head2 select($source, $fields, $where, $order)
32eab2da 828
9d48860e 829This returns a SQL SELECT statement and associated list of bind values, as
96449e8e 830specified by the arguments :
32eab2da 831
96449e8e 832=over
32eab2da 833
96449e8e 834=item $source
32eab2da 835
9d48860e 836Specification of the 'FROM' part of the statement.
96449e8e 837The argument can be either a plain scalar (interpreted as a table
838name, will be quoted), or an arrayref (interpreted as a list
839of table names, joined by commas, quoted), or a scalarref
840(literal table name, not quoted), or a ref to an arrayref
841(list of literal table names, joined by commas, not quoted).
32eab2da 842
96449e8e 843=item $fields
32eab2da 844
9d48860e 845Specification of the list of fields to retrieve from
96449e8e 846the source.
847The argument can be either an arrayref (interpreted as a list
9d48860e 848of field names, will be joined by commas and quoted), or a
96449e8e 849plain scalar (literal SQL, not quoted).
850Please observe that this API is not as flexible as for
e3f9dff4 851the first argument C<$table>, for backwards compatibility reasons.
32eab2da 852
96449e8e 853=item $where
32eab2da 854
96449e8e 855Optional argument to specify the WHERE part of the query.
856The argument is most often a hashref, but can also be
9d48860e 857an arrayref or plain scalar --
96449e8e 858see section L<WHERE clause|/"WHERE CLAUSES"> for details.
32eab2da 859
96449e8e 860=item $order
32eab2da 861
96449e8e 862Optional argument to specify the ORDER BY part of the query.
9d48860e 863The argument can be a scalar, a hashref or an arrayref
96449e8e 864-- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
865for details.
32eab2da 866
96449e8e 867=back
32eab2da 868
32eab2da 869
870=head2 delete($table, \%where)
871
86298391 872This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
32eab2da 873It returns an SQL DELETE statement and list of bind values.
874
32eab2da 875=head2 where(\%where, \@order)
876
877This is used to generate just the WHERE clause. For example,
878if you have an arbitrary data structure and know what the
879rest of your SQL is going to look like, but want an easy way
880to produce a WHERE clause, use this. It returns an SQL WHERE
881clause and list of bind values.
882
32eab2da 883
884=head2 values(\%data)
885
886This just returns the values from the hash C<%data>, in the same
887order that would be returned from any of the other above queries.
888Using this allows you to markedly speed up your queries if you
889are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
890
32eab2da 891=head2 generate($any, 'number', $of, \@data, $struct, \%types)
892
893Warning: This is an experimental method and subject to change.
894
895This returns arbitrarily generated SQL. It's a really basic shortcut.
896It will return two different things, depending on return context:
897
898 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
899 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
900
901These would return the following:
902
903 # First calling form
904 $stmt = "CREATE TABLE test (?, ?)";
905 @bind = (field1, field2);
906
907 # Second calling form
908 $stmt_and_val = "CREATE TABLE test (field1, field2)";
909
910Depending on what you're trying to do, it's up to you to choose the correct
911format. In this example, the second form is what you would want.
912
913By the same token:
914
915 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
916
917Might give you:
918
919 ALTER SESSION SET nls_date_format = 'MM/YY'
920
921You get the idea. Strings get their case twiddled, but everything
922else remains verbatim.
923
32eab2da 924=head1 WHERE CLAUSES
925
96449e8e 926=head2 Introduction
927
32eab2da 928This module uses a variation on the idea from L<DBIx::Abstract>. It
929is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
930module is that things in arrays are OR'ed, and things in hashes
931are AND'ed.>
932
933The easiest way to explain is to show lots of examples. After
934each C<%where> hash shown, it is assumed you used:
935
936 my($stmt, @bind) = $sql->where(\%where);
937
938However, note that the C<%where> hash can be used directly in any
939of the other functions as well, as described above.
940
96449e8e 941=head2 Key-value pairs
942
32eab2da 943So, let's get started. To begin, a simple hash:
944
945 my %where = (
946 user => 'nwiger',
947 status => 'completed'
948 );
949
950Is converted to SQL C<key = val> statements:
951
952 $stmt = "WHERE user = ? AND status = ?";
953 @bind = ('nwiger', 'completed');
954
955One common thing I end up doing is having a list of values that
956a field can be in. To do this, simply specify a list inside of
957an arrayref:
958
959 my %where = (
960 user => 'nwiger',
961 status => ['assigned', 'in-progress', 'pending'];
962 );
963
964This simple code will create the following:
9d48860e 965
32eab2da 966 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
967 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
968
9d48860e 969A field associated to an empty arrayref will be considered a
7cac25e6 970logical false and will generate 0=1.
8a68b5be 971
b864ba9b 972=head2 Tests for NULL values
973
974If the value part is C<undef> then this is converted to SQL <IS NULL>
975
976 my %where = (
977 user => 'nwiger',
978 status => undef,
979 );
980
981becomes:
982
983 $stmt = "WHERE user = ? AND status IS NULL";
984 @bind = ('nwiger');
985
e9614080 986To test if a column IS NOT NULL:
987
988 my %where = (
989 user => 'nwiger',
990 status => { '!=', undef },
991 );
cc422895 992
6e0c6552 993=head2 Specific comparison operators
96449e8e 994
32eab2da 995If you want to specify a different type of operator for your comparison,
996you can use a hashref for a given column:
997
998 my %where = (
999 user => 'nwiger',
1000 status => { '!=', 'completed' }
1001 );
1002
1003Which would generate:
1004
1005 $stmt = "WHERE user = ? AND status != ?";
1006 @bind = ('nwiger', 'completed');
1007
1008To test against multiple values, just enclose the values in an arrayref:
1009
96449e8e 1010 status => { '=', ['assigned', 'in-progress', 'pending'] };
1011
f2d5020d 1012Which would give you:
96449e8e 1013
1014 "WHERE status = ? OR status = ? OR status = ?"
1015
1016
1017The hashref can also contain multiple pairs, in which case it is expanded
32eab2da 1018into an C<AND> of its elements:
1019
1020 my %where = (
1021 user => 'nwiger',
1022 status => { '!=', 'completed', -not_like => 'pending%' }
1023 );
1024
1025 # Or more dynamically, like from a form
1026 $where{user} = 'nwiger';
1027 $where{status}{'!='} = 'completed';
1028 $where{status}{'-not_like'} = 'pending%';
1029
1030 # Both generate this
1031 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
1032 @bind = ('nwiger', 'completed', 'pending%');
1033
96449e8e 1034
32eab2da 1035To get an OR instead, you can combine it with the arrayref idea:
1036
1037 my %where => (
1038 user => 'nwiger',
1a6f2a03 1039 priority => [ { '=', 2 }, { '>', 5 } ]
32eab2da 1040 );
1041
1042Which would generate:
1043
1a6f2a03 1044 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
1045 @bind = ('2', '5', 'nwiger');
32eab2da 1046
44b9e502 1047If you want to include literal SQL (with or without bind values), just use a
1048scalar reference or array reference as the value:
1049
1050 my %where = (
1051 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
1052 date_expires => { '<' => \"now()" }
1053 );
1054
1055Which would generate:
1056
1057 $stmt = "WHERE date_entered > "to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
1058 @bind = ('11/26/2008');
1059
96449e8e 1060
1061=head2 Logic and nesting operators
1062
1063In the example above,
1064there is a subtle trap if you want to say something like
32eab2da 1065this (notice the C<AND>):
1066
1067 WHERE priority != ? AND priority != ?
1068
1069Because, in Perl you I<can't> do this:
1070
1071 priority => { '!=', 2, '!=', 1 }
1072
1073As the second C<!=> key will obliterate the first. The solution
1074is to use the special C<-modifier> form inside an arrayref:
1075
9d48860e 1076 priority => [ -and => {'!=', 2},
96449e8e 1077 {'!=', 1} ]
1078
32eab2da 1079
1080Normally, these would be joined by C<OR>, but the modifier tells it
1081to use C<AND> instead. (Hint: You can use this in conjunction with the
1082C<logic> option to C<new()> in order to change the way your queries
1083work by default.) B<Important:> Note that the C<-modifier> goes
1084B<INSIDE> the arrayref, as an extra first element. This will
1085B<NOT> do what you think it might:
1086
1087 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
1088
1089Here is a quick list of equivalencies, since there is some overlap:
1090
1091 # Same
1092 status => {'!=', 'completed', 'not like', 'pending%' }
1093 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
1094
1095 # Same
1096 status => {'=', ['assigned', 'in-progress']}
1097 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
1098 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
1099
e3f9dff4 1100
1101
96449e8e 1102=head2 Special operators : IN, BETWEEN, etc.
1103
32eab2da 1104You can also use the hashref format to compare a list of fields using the
1105C<IN> comparison operator, by specifying the list as an arrayref:
1106
1107 my %where = (
1108 status => 'completed',
1109 reportid => { -in => [567, 2335, 2] }
1110 );
1111
1112Which would generate:
1113
1114 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
1115 @bind = ('completed', '567', '2335', '2');
1116
9d48860e 1117The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
96449e8e 1118the same way.
1119
6e0c6552 1120If the argument to C<-in> is an empty array, 'sqlfalse' is generated
1121(by default : C<1=0>). Similarly, C<< -not_in => [] >> generates
1122'sqltrue' (by default : C<1=1>).
1123
e41c3bdd 1124In addition to the array you can supply a chunk of literal sql or
1125literal sql with bind:
6e0c6552 1126
e41c3bdd 1127 my %where = {
1128 customer => { -in => \[
1129 'SELECT cust_id FROM cust WHERE balance > ?',
1130 2000,
1131 ],
1132 status => { -in => \'SELECT status_codes FROM states' },
1133 };
6e0c6552 1134
e41c3bdd 1135would generate:
1136
1137 $stmt = "WHERE (
1138 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
1139 AND status IN ( SELECT status_codes FROM states )
1140 )";
1141 @bind = ('2000');
1142
1143
1144
1145Another pair of operators is C<-between> and C<-not_between>,
96449e8e 1146used with an arrayref of two values:
32eab2da 1147
1148 my %where = (
1149 user => 'nwiger',
1150 completion_date => {
1151 -not_between => ['2002-10-01', '2003-02-06']
1152 }
1153 );
1154
1155Would give you:
1156
1157 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
1158
e41c3bdd 1159Just like with C<-in> all plausible combinations of literal SQL
1160are possible:
1161
1162 my %where = {
1163 start0 => { -between => [ 1, 2 ] },
1164 start1 => { -between => \["? AND ?", 1, 2] },
1165 start2 => { -between => \"lower(x) AND upper(y)" },
9d48860e 1166 start3 => { -between => [
e41c3bdd 1167 \"lower(x)",
1168 \["upper(?)", 'stuff' ],
1169 ] },
1170 };
1171
1172Would give you:
1173
1174 $stmt = "WHERE (
1175 ( start0 BETWEEN ? AND ? )
1176 AND ( start1 BETWEEN ? AND ? )
1177 AND ( start2 BETWEEN lower(x) AND upper(y) )
1178 AND ( start3 BETWEEN lower(x) AND upper(?) )
1179 )";
1180 @bind = (1, 2, 1, 2, 'stuff');
1181
1182
9d48860e 1183These are the two builtin "special operators"; but the
96449e8e 1184list can be expanded : see section L</"SPECIAL OPERATORS"> below.
1185
59f23b3d 1186=head2 Unary operators: bool
97a920ef 1187
1188If you wish to test against boolean columns or functions within your
1189database you can use the C<-bool> and C<-not_bool> operators. For
1190example to test the column C<is_user> being true and the column
827bb0eb 1191C<is_enabled> being false you would use:-
97a920ef 1192
1193 my %where = (
1194 -bool => 'is_user',
1195 -not_bool => 'is_enabled',
1196 );
1197
1198Would give you:
1199
277b5d3f 1200 WHERE is_user AND NOT is_enabled
97a920ef 1201
0b604e9d 1202If a more complex combination is required, testing more conditions,
1203then you should use the and/or operators:-
1204
1205 my %where = (
1206 -and => [
1207 -bool => 'one',
1208 -bool => 'two',
1209 -bool => 'three',
1210 -not_bool => 'four',
1211 ],
1212 );
1213
1214Would give you:
1215
1216 WHERE one AND two AND three AND NOT four
97a920ef 1217
1218
107b72f1 1219=head2 Nested conditions, -and/-or prefixes
96449e8e 1220
32eab2da 1221So far, we've seen how multiple conditions are joined with a top-level
1222C<AND>. We can change this by putting the different conditions we want in
1223hashes and then putting those hashes in an array. For example:
1224
1225 my @where = (
1226 {
1227 user => 'nwiger',
1228 status => { -like => ['pending%', 'dispatched'] },
1229 },
1230 {
1231 user => 'robot',
1232 status => 'unassigned',
1233 }
1234 );
1235
1236This data structure would create the following:
1237
1238 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
1239 OR ( user = ? AND status = ? ) )";
1240 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
1241
107b72f1 1242
48d9f5f8 1243Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
1244to change the logic inside :
32eab2da 1245
1246 my @where = (
1247 -and => [
1248 user => 'nwiger',
48d9f5f8 1249 [
1250 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
1251 -or => { workhrs => {'<', 50}, geo => 'EURO' },
32eab2da 1252 ],
1253 ],
1254 );
1255
1256That would yield:
1257
48d9f5f8 1258 WHERE ( user = ? AND (
1259 ( workhrs > ? AND geo = ? )
1260 OR ( workhrs < ? OR geo = ? )
1261 ) )
107b72f1 1262
cc422895 1263=head3 Algebraic inconsistency, for historical reasons
107b72f1 1264
7cac25e6 1265C<Important note>: when connecting several conditions, the C<-and->|C<-or>
1266operator goes C<outside> of the nested structure; whereas when connecting
1267several constraints on one column, the C<-and> operator goes
1268C<inside> the arrayref. Here is an example combining both features :
1269
1270 my @where = (
1271 -and => [a => 1, b => 2],
1272 -or => [c => 3, d => 4],
1273 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
1274 )
1275
1276yielding
1277
9d48860e 1278 WHERE ( ( ( a = ? AND b = ? )
1279 OR ( c = ? OR d = ? )
7cac25e6 1280 OR ( e LIKE ? AND e LIKE ? ) ) )
1281
107b72f1 1282This difference in syntax is unfortunate but must be preserved for
1283historical reasons. So be careful : the two examples below would
1284seem algebraically equivalent, but they are not
1285
9d48860e 1286 {col => [-and => {-like => 'foo%'}, {-like => '%bar'}]}
107b72f1 1287 # yields : WHERE ( ( col LIKE ? AND col LIKE ? ) )
1288
9d48860e 1289 [-and => {col => {-like => 'foo%'}, {col => {-like => '%bar'}}]]
107b72f1 1290 # yields : WHERE ( ( col LIKE ? OR col LIKE ? ) )
1291
7cac25e6 1292
cc422895 1293=head2 Literal SQL and value type operators
96449e8e 1294
cc422895 1295The basic premise of SQL::Abstract is that in WHERE specifications the "left
1296side" is a column name and the "right side" is a value (normally rendered as
1297a placeholder). This holds true for both hashrefs and arrayref pairs as you
1298see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
1299alter this behavior. There are several ways of doing so.
e9614080 1300
cc422895 1301=head3 -ident
1302
1303This is a virtual operator that signals the string to its right side is an
1304identifier (a column name) and not a value. For example to compare two
1305columns you would write:
32eab2da 1306
e9614080 1307 my %where = (
1308 priority => { '<', 2 },
cc422895 1309 requestor => { -ident => 'submitter' },
e9614080 1310 );
1311
1312which creates:
1313
1314 $stmt = "WHERE priority < ? AND requestor = submitter";
1315 @bind = ('2');
1316
cc422895 1317If you are maintaining legacy code you may see a different construct as
1318described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
1319code.
1320
1321=head3 -value
e9614080 1322
cc422895 1323This is a virtual operator that signals that the construct to its right side
1324is a value to be passed to DBI. This is for example necessary when you want
1325to write a where clause against an array (for RDBMS that support such
1326datatypes). For example:
e9614080 1327
32eab2da 1328 my %where = (
cc422895 1329 array => { -value => [1, 2, 3] }
32eab2da 1330 );
1331
cc422895 1332will result in:
32eab2da 1333
cc422895 1334 $stmt = 'WHERE array = ?';
1335 @bind = ([1, 2, 3]);
32eab2da 1336
cc422895 1337Note that if you were to simply say:
32eab2da 1338
1339 my %where = (
cc422895 1340 array => [1, 2, 3]
32eab2da 1341 );
1342
cc422895 1343the result would porbably be not what you wanted:
1344
1345 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
1346 @bind = (1, 2, 3);
1347
1348=head3 Literal SQL
96449e8e 1349
cc422895 1350Finally, sometimes only literal SQL will do. To include a random snippet
1351of SQL verbatim, you specify it as a scalar reference. Consider this only
1352as a last resort. Usually there is a better way. For example:
96449e8e 1353
1354 my %where = (
cc422895 1355 priority => { '<', 2 },
1356 requestor => { -in => \'(SELECT name FROM hitmen)' },
96449e8e 1357 );
1358
cc422895 1359Would create:
96449e8e 1360
cc422895 1361 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
1362 @bind = (2);
1363
1364Note that in this example, you only get one bind parameter back, since
1365the verbatim SQL is passed as part of the statement.
1366
1367=head4 CAVEAT
1368
1369 Never use untrusted input as a literal SQL argument - this is a massive
1370 security risk (there is no way to check literal snippets for SQL
1371 injections and other nastyness). If you need to deal with untrusted input
1372 use literal SQL with placeholders as described next.
96449e8e 1373
cc422895 1374=head3 Literal SQL with placeholders and bind values (subqueries)
96449e8e 1375
1376If the literal SQL to be inserted has placeholders and bind values,
1377use a reference to an arrayref (yes this is a double reference --
1378not so common, but perfectly legal Perl). For example, to find a date
1379in Postgres you can use something like this:
1380
1381 my %where = (
1382 date_column => \[q/= date '2008-09-30' - ?::integer/, 10/]
1383 )
1384
1385This would create:
1386
d2a8fe1a 1387 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
96449e8e 1388 @bind = ('10');
1389
deb148a2 1390Note that you must pass the bind values in the same format as they are returned
62552e7d 1391by L</where>. That means that if you set L</bindtype> to C<columns>, you must
26f2dca5 1392provide the bind values in the C<< [ column_meta => value ] >> format, where
1393C<column_meta> is an opaque scalar value; most commonly the column name, but
62552e7d 1394you can use any scalar value (including references and blessed references),
1395L<SQL::Abstract> will simply pass it through intact. So if C<bindtype> is set
1396to C<columns> the above example will look like:
deb148a2 1397
1398 my %where = (
1399 date_column => \[q/= date '2008-09-30' - ?::integer/, [ dummy => 10 ]/]
1400 )
96449e8e 1401
1402Literal SQL is especially useful for nesting parenthesized clauses in the
1403main SQL query. Here is a first example :
1404
1405 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
1406 100, "foo%");
1407 my %where = (
1408 foo => 1234,
1409 bar => \["IN ($sub_stmt)" => @sub_bind],
1410 );
1411
1412This yields :
1413
9d48860e 1414 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
96449e8e 1415 WHERE c2 < ? AND c3 LIKE ?))";
1416 @bind = (1234, 100, "foo%");
1417
9d48860e 1418Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
96449e8e 1419are expressed in the same way. Of course the C<$sub_stmt> and
9d48860e 1420its associated bind values can be generated through a former call
96449e8e 1421to C<select()> :
1422
1423 my ($sub_stmt, @sub_bind)
9d48860e 1424 = $sql->select("t1", "c1", {c2 => {"<" => 100},
96449e8e 1425 c3 => {-like => "foo%"}});
1426 my %where = (
1427 foo => 1234,
1428 bar => \["> ALL ($sub_stmt)" => @sub_bind],
1429 );
1430
1431In the examples above, the subquery was used as an operator on a column;
9d48860e 1432but the same principle also applies for a clause within the main C<%where>
96449e8e 1433hash, like an EXISTS subquery :
1434
9d48860e 1435 my ($sub_stmt, @sub_bind)
96449e8e 1436 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
48d9f5f8 1437 my %where = ( -and => [
96449e8e 1438 foo => 1234,
48d9f5f8 1439 \["EXISTS ($sub_stmt)" => @sub_bind],
1440 ]);
96449e8e 1441
1442which yields
1443
9d48860e 1444 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
96449e8e 1445 WHERE c1 = ? AND c2 > t0.c0))";
1446 @bind = (1234, 1);
1447
1448
9d48860e 1449Observe that the condition on C<c2> in the subquery refers to
1450column C<t0.c0> of the main query : this is I<not> a bind
1451value, so we have to express it through a scalar ref.
96449e8e 1452Writing C<< c2 => {">" => "t0.c0"} >> would have generated
1453C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
1454what we wanted here.
1455
96449e8e 1456Finally, here is an example where a subquery is used
1457for expressing unary negation:
1458
9d48860e 1459 my ($sub_stmt, @sub_bind)
96449e8e 1460 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
1461 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
1462 my %where = (
1463 lname => {like => '%son%'},
48d9f5f8 1464 \["NOT ($sub_stmt)" => @sub_bind],
96449e8e 1465 );
1466
1467This yields
1468
1469 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
1470 @bind = ('%son%', 10, 20)
1471
cc422895 1472=head3 Deprecated usage of Literal SQL
1473
1474Below are some examples of archaic use of literal SQL. It is shown only as
1475reference for those who deal with legacy code. Each example has a much
1476better, cleaner and safer alternative that users should opt for in new code.
1477
1478=over
1479
1480=item *
1481
1482 my %where = ( requestor => \'IS NOT NULL' )
1483
1484 $stmt = "WHERE requestor IS NOT NULL"
1485
1486This used to be the way of generating NULL comparisons, before the handling
1487of C<undef> got formalized. For new code please use the superior syntax as
1488described in L</Tests for NULL values>.
96449e8e 1489
cc422895 1490=item *
1491
1492 my %where = ( requestor => \'= submitter' )
1493
1494 $stmt = "WHERE requestor = submitter"
1495
1496This used to be the only way to compare columns. Use the superior L</-ident>
1497method for all new code. For example an identifier declared in such a way
1498will be properly quoted if L</quote_char> is properly set, while the legacy
1499form will remain as supplied.
1500
1501=item *
1502
1503 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
1504
1505 $stmt = "WHERE completed > ? AND is_ready"
1506 @bind = ('2012-12-21')
1507
1508Using an empty string literal used to be the only way to express a boolean.
1509For all new code please use the much more readable
1510L<-bool|/Unary operators: bool> operator.
1511
1512=back
96449e8e 1513
1514=head2 Conclusion
1515
32eab2da 1516These pages could go on for a while, since the nesting of the data
1517structures this module can handle are pretty much unlimited (the
1518module implements the C<WHERE> expansion as a recursive function
1519internally). Your best bet is to "play around" with the module a
1520little to see how the data structures behave, and choose the best
1521format for your data based on that.
1522
1523And of course, all the values above will probably be replaced with
1524variables gotten from forms or the command line. After all, if you
1525knew everything ahead of time, you wouldn't have to worry about
1526dynamically-generating SQL and could just hardwire it into your
1527script.
1528
86298391 1529=head1 ORDER BY CLAUSES
1530
9d48860e 1531Some functions take an order by clause. This can either be a scalar (just a
86298391 1532column name,) a hash of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } >>,
1cfa1db3 1533or an array of either of the two previous forms. Examples:
1534
952f9e2d 1535 Given | Will Generate
1cfa1db3 1536 ----------------------------------------------------------
952f9e2d 1537 |
1538 \'colA DESC' | ORDER BY colA DESC
1539 |
1540 'colA' | ORDER BY colA
1541 |
1542 [qw/colA colB/] | ORDER BY colA, colB
1543 |
1544 {-asc => 'colA'} | ORDER BY colA ASC
1545 |
1546 {-desc => 'colB'} | ORDER BY colB DESC
1547 |
1548 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
1549 |
855e6047 1550 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
952f9e2d 1551 |
1552 [ |
1553 { -asc => 'colA' }, | ORDER BY colA ASC, colB DESC,
1554 { -desc => [qw/colB/], | colC ASC, colD ASC
1555 { -asc => [qw/colC colD/],|
1556 ] |
1557 ===========================================================
86298391 1558
96449e8e 1559
1560
1561=head1 SPECIAL OPERATORS
1562
e3f9dff4 1563 my $sqlmaker = SQL::Abstract->new(special_ops => [
3a2e1a5e 1564 {
1565 regex => qr/.../,
e3f9dff4 1566 handler => sub {
1567 my ($self, $field, $op, $arg) = @_;
1568 ...
3a2e1a5e 1569 },
1570 },
1571 {
1572 regex => qr/.../,
1573 handler => 'method_name',
e3f9dff4 1574 },
1575 ]);
1576
9d48860e 1577A "special operator" is a SQL syntactic clause that can be
e3f9dff4 1578applied to a field, instead of a usual binary operator.
9d48860e 1579For example :
e3f9dff4 1580
1581 WHERE field IN (?, ?, ?)
1582 WHERE field BETWEEN ? AND ?
1583 WHERE MATCH(field) AGAINST (?, ?)
96449e8e 1584
e3f9dff4 1585Special operators IN and BETWEEN are fairly standard and therefore
3a2e1a5e 1586are builtin within C<SQL::Abstract> (as the overridable methods
1587C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
1588like the MATCH .. AGAINST example above which is specific to MySQL,
1589you can write your own operator handlers - supply a C<special_ops>
1590argument to the C<new> method. That argument takes an arrayref of
1591operator definitions; each operator definition is a hashref with two
1592entries:
96449e8e 1593
e3f9dff4 1594=over
1595
1596=item regex
1597
1598the regular expression to match the operator
96449e8e 1599
e3f9dff4 1600=item handler
1601
3a2e1a5e 1602Either a coderef or a plain scalar method name. In both cases
1603the expected return is C<< ($sql, @bind) >>.
1604
1605When supplied with a method name, it is simply called on the
1606L<SQL::Abstract/> object as:
1607
1608 $self->$method_name ($field, $op, $arg)
1609
1610 Where:
1611
1612 $op is the part that matched the handler regex
1613 $field is the LHS of the operator
1614 $arg is the RHS
1615
1616When supplied with a coderef, it is called as:
1617
1618 $coderef->($self, $field, $op, $arg)
1619
e3f9dff4 1620
1621=back
1622
9d48860e 1623For example, here is an implementation
e3f9dff4 1624of the MATCH .. AGAINST syntax for MySQL
1625
1626 my $sqlmaker = SQL::Abstract->new(special_ops => [
9d48860e 1627
e3f9dff4 1628 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
9d48860e 1629 {regex => qr/^match$/i,
e3f9dff4 1630 handler => sub {
1631 my ($self, $field, $op, $arg) = @_;
1632 $arg = [$arg] if not ref $arg;
1633 my $label = $self->_quote($field);
1634 my ($placeholder) = $self->_convert('?');
1635 my $placeholders = join ", ", (($placeholder) x @$arg);
1636 my $sql = $self->_sqlcase('match') . " ($label) "
1637 . $self->_sqlcase('against') . " ($placeholders) ";
1638 my @bind = $self->_bindtype($field, @$arg);
1639 return ($sql, @bind);
1640 }
1641 },
9d48860e 1642
e3f9dff4 1643 ]);
96449e8e 1644
1645
59f23b3d 1646=head1 UNARY OPERATORS
1647
112b5232 1648 my $sqlmaker = SQL::Abstract->new(unary_ops => [
59f23b3d 1649 {
1650 regex => qr/.../,
1651 handler => sub {
1652 my ($self, $op, $arg) = @_;
1653 ...
1654 },
1655 },
1656 {
1657 regex => qr/.../,
1658 handler => 'method_name',
1659 },
1660 ]);
1661
9d48860e 1662A "unary operator" is a SQL syntactic clause that can be
59f23b3d 1663applied to a field - the operator goes before the field
1664
1665You can write your own operator handlers - supply a C<unary_ops>
1666argument to the C<new> method. That argument takes an arrayref of
1667operator definitions; each operator definition is a hashref with two
1668entries:
1669
1670=over
1671
1672=item regex
1673
1674the regular expression to match the operator
1675
1676=item handler
1677
1678Either a coderef or a plain scalar method name. In both cases
1679the expected return is C<< $sql >>.
1680
1681When supplied with a method name, it is simply called on the
1682L<SQL::Abstract/> object as:
1683
1684 $self->$method_name ($op, $arg)
1685
1686 Where:
1687
1688 $op is the part that matched the handler regex
1689 $arg is the RHS or argument of the operator
1690
1691When supplied with a coderef, it is called as:
1692
1693 $coderef->($self, $op, $arg)
1694
1695
1696=back
1697
1698
32eab2da 1699=head1 PERFORMANCE
1700
1701Thanks to some benchmarking by Mark Stosberg, it turns out that
1702this module is many orders of magnitude faster than using C<DBIx::Abstract>.
1703I must admit this wasn't an intentional design issue, but it's a
1704byproduct of the fact that you get to control your C<DBI> handles
1705yourself.
1706
1707To maximize performance, use a code snippet like the following:
1708
1709 # prepare a statement handle using the first row
1710 # and then reuse it for the rest of the rows
1711 my($sth, $stmt);
1712 for my $href (@array_of_hashrefs) {
1713 $stmt ||= $sql->insert('table', $href);
1714 $sth ||= $dbh->prepare($stmt);
1715 $sth->execute($sql->values($href));
1716 }
1717
1718The reason this works is because the keys in your C<$href> are sorted
1719internally by B<SQL::Abstract>. Thus, as long as your data retains
1720the same structure, you only have to generate the SQL the first time
1721around. On subsequent queries, simply use the C<values> function provided
1722by this module to return your values in the correct order.
1723
b864ba9b 1724However this depends on the values having the same type - if, for
1725example, the values of a where clause may either have values
1726(resulting in sql of the form C<column = ?> with a single bind
1727value), or alternatively the values might be C<undef> (resulting in
1728sql of the form C<column IS NULL> with no bind value) then the
1729caching technique suggested will not work.
96449e8e 1730
32eab2da 1731=head1 FORMBUILDER
1732
1733If you use my C<CGI::FormBuilder> module at all, you'll hopefully
1734really like this part (I do, at least). Building up a complex query
1735can be as simple as the following:
1736
1737 #!/usr/bin/perl
1738
1739 use CGI::FormBuilder;
1740 use SQL::Abstract;
1741
1742 my $form = CGI::FormBuilder->new(...);
1743 my $sql = SQL::Abstract->new;
1744
1745 if ($form->submitted) {
1746 my $field = $form->field;
1747 my $id = delete $field->{id};
1748 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
1749 }
1750
1751Of course, you would still have to connect using C<DBI> to run the
1752query, but the point is that if you make your form look like your
1753table, the actual query script can be extremely simplistic.
1754
1755If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
9d48860e 1756a fast interface to returning and formatting data. I frequently
32eab2da 1757use these three modules together to write complex database query
1758apps in under 50 lines.
1759
d8cc1792 1760=head1 REPO
1761
1762=over
1763
6d19fbf9 1764=item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
d8cc1792 1765
6d19fbf9 1766=item * git: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
d8cc1792 1767
1768=back
32eab2da 1769
96449e8e 1770=head1 CHANGES
1771
1772Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
1773Great care has been taken to preserve the I<published> behavior
1774documented in previous versions in the 1.* family; however,
9d48860e 1775some features that were previously undocumented, or behaved
96449e8e 1776differently from the documentation, had to be changed in order
1777to clarify the semantics. Hence, client code that was relying
9d48860e 1778on some dark areas of C<SQL::Abstract> v1.*
96449e8e 1779B<might behave differently> in v1.50.
32eab2da 1780
d2a8fe1a 1781The main changes are :
1782
96449e8e 1783=over
32eab2da 1784
9d48860e 1785=item *
32eab2da 1786
96449e8e 1787support for literal SQL through the C<< \ [$sql, bind] >> syntax.
1788
1789=item *
1790
145fbfc8 1791support for the { operator => \"..." } construct (to embed literal SQL)
1792
1793=item *
1794
9c37b9c0 1795support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
1796
1797=item *
1798
96449e8e 1799optional support for L<array datatypes|/"Inserting and Updating Arrays">
1800
9d48860e 1801=item *
96449e8e 1802
1803defensive programming : check arguments
1804
1805=item *
1806
1807fixed bug with global logic, which was previously implemented
7cac25e6 1808through global variables yielding side-effects. Prior versions would
96449e8e 1809interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
1810as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
1811Now this is interpreted
1812as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
1813
96449e8e 1814
1815=item *
1816
1817fixed semantics of _bindtype on array args
1818
9d48860e 1819=item *
96449e8e 1820
1821dropped the C<_anoncopy> of the %where tree. No longer necessary,
1822we just avoid shifting arrays within that tree.
1823
1824=item *
1825
1826dropped the C<_modlogic> function
1827
1828=back
32eab2da 1829
32eab2da 1830=head1 ACKNOWLEDGEMENTS
1831
1832There are a number of individuals that have really helped out with
1833this module. Unfortunately, most of them submitted bugs via CPAN
1834so I have no idea who they are! But the people I do know are:
1835
9d48860e 1836 Ash Berlin (order_by hash term support)
b643abe1 1837 Matt Trout (DBIx::Class support)
32eab2da 1838 Mark Stosberg (benchmarking)
1839 Chas Owens (initial "IN" operator support)
1840 Philip Collins (per-field SQL functions)
1841 Eric Kolve (hashref "AND" support)
1842 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
1843 Dan Kubb (support for "quote_char" and "name_sep")
f5aab26e 1844 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
48d9f5f8 1845 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
dbdf7648 1846 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
e96c510a 1847 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
02288357 1848 Oliver Charles (support for "RETURNING" after "INSERT")
32eab2da 1849
1850Thanks!
1851
32eab2da 1852=head1 SEE ALSO
1853
86298391 1854L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
32eab2da 1855
32eab2da 1856=head1 AUTHOR
1857
b643abe1 1858Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
1859
1860This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
32eab2da 1861
abe72f94 1862For support, your best bet is to try the C<DBIx::Class> users mailing list.
1863While not an official support venue, C<DBIx::Class> makes heavy use of
1864C<SQL::Abstract>, and as such list members there are very familiar with
1865how to create queries.
1866
0d067ded 1867=head1 LICENSE
1868
d988ab87 1869This module is free software; you may copy this under the same
1870terms as perl itself (either the GNU General Public License or
1871the Artistic License)
32eab2da 1872
1873=cut
1874