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