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