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