Explicitly use default names for builders and clearers
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract.pm
index 3a2bead..8a0bae9 100644 (file)
@@ -1,15 +1,21 @@
 package SQL::Abstract; # see doc at end of file
 
+use SQL::Abstract::_TempExtlib;
+
 use Carp ();
 use List::Util ();
 use Scalar::Util ();
 use Module::Runtime qw(use_module);
+use Sub::Quote 'quote_sub';
 use Moo;
 use namespace::clean;
 
-our $VERSION  = '1.72';
+# DO NOT INCREMENT TO 2.0 WITHOUT COORDINATING WITH mst OR ribasushi
+      our $VERSION  = '1.99_01';
+# DO NOT INCREMENT TO 2.0 WITHOUT COORDINATING WITH mst OR ribasushi
 
-$VERSION = eval $VERSION;
+# This would confuse some packagers
+$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
 
 sub belch (@) {
   my($func) = (caller(1))[3];
@@ -21,27 +27,27 @@ sub puke (@) {
   Carp::croak "[$func] Fatal: ", @_;
 }
 
-has converter => (is => 'lazy', clearer => 'clear_converter');
+has converter => (is => 'lazy', clearer => 1);
 
 has case => (
-  is => 'ro', coerce => sub { $_[0] eq 'lower' ? 'lower' : undef }
+  is => 'ro', coerce => quote_sub( q{ $_[0] eq 'lower' ? 'lower' : undef } ),
 );
 
 has logic => (
-  is => 'ro', coerce => sub { uc($_[0]) }, default => sub { 'OR' }
+  is => 'ro', coerce => quote_sub( q{ uc($_[0]) } ), default => 'OR',
 );
 
 has bindtype => (
-  is => 'ro', default => sub { 'normal' }
+  is => 'ro', default => 'normal'
 );
 
-has cmp => (is => 'ro', default => sub { '=' });
+has cmp => (is => 'ro', default => '=');
 
-has sqltrue => (is => 'ro', default => sub { '1=1' });
-has sqlfalse => (is => 'ro', default => sub { '0=1' });
+has sqltrue => (is => 'ro', default => '1=1' );
+has sqlfalse => (is => 'ro', default => '0=1' );
 
-has special_ops => (is => 'ro', default => sub { [] });
-has unary_ops => (is => 'ro', default => sub { [] });
+has special_ops => (is => 'ro', default => quote_sub( q{ [] } ));
+has unary_ops => (is => 'ro', default => quote_sub( q{ [] } ));
 
 # FIXME
 # need to guard against ()'s in column names too, but this will break tons of
@@ -49,44 +55,44 @@ has unary_ops => (is => 'ro', default => sub { [] });
 
 has injection_guard => (
   is => 'ro',
-  default => sub {
+  default => quote_sub( q{
     qr/
       \;
         |
       ^ \s* go \s
     /xmi;
-  }
+  })
 );
 
-has renderer => (is => 'lazy', clearer => 'clear_renderer');
+has renderer => (is => 'lazy', clearer => 1);
 
 has name_sep => (
-  is => 'rw', default => sub { '.' },
-  trigger => sub {
+  is => 'rw', default => '.',
+  trigger => quote_sub( q{
     $_[0]->clear_renderer;
     $_[0]->clear_converter;
-  },
+  }),
 );
 
 has quote_char => (
   is => 'rw',
-  trigger => sub {
+  trigger => quote_sub( q{
     $_[0]->clear_renderer;
     $_[0]->clear_converter;
-  },
+  }),
 );
 
 has collapse_aliases => (
   is => 'ro',
-  default => sub { 0 }
+  default => 0,
 );
 
 has always_quote => (
-  is => 'rw', default => sub { 1 },
-  trigger => sub {
+  is => 'rw', default => 1,
+  trigger => quote_sub( q{
     $_[0]->clear_renderer;
     $_[0]->clear_converter;
-  },
+  }),
 );
 
 has convert => (is => 'ro');
@@ -94,8 +100,8 @@ has convert => (is => 'ro');
 has array_datatypes => (is => 'ro');
 
 has converter_class => (
-  is => 'rw', lazy => 1, builder => '_build_converter_class',
-  trigger => sub { shift->clear_converter },
+  is => 'rw', lazy => 1, builder => 1,
+  trigger => quote_sub( q{ $_[0]->clear_converter } ),
 );
 
 sub _build_converter_class {
@@ -104,10 +110,10 @@ sub _build_converter_class {
 
 has renderer_class => (
   is => 'rw', lazy => 1, clearer => 1, builder => 1,
-  trigger => sub { shift->clear_renderer },
+  trigger => quote_sub( q{ $_[0]->clear_renderer } ),
 );
 
-after clear_renderer_class => sub { shift->clear_renderer };
+after clear_renderer_class => sub { $_[0]->clear_renderer };
 
 sub _build_renderer_class {
   my ($self) = @_;
@@ -267,27 +273,15 @@ sub _quote {
 sub _assert_pass_injection_guard {
   if ($_[1] =~ $_[0]->{injection_guard}) {
     my $class = ref $_[0];
-    die "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the
- "
-     . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own 
-"
-     . "{injection_guard} attribute to ${class}->new()"
+    die "Possible SQL injection attempt '$_[1]'. If this is indeed a part of "
+      . "the desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply "
+      . "your own {injection_guard} attribute to ${class}->new()"
   }
 }
 
 # Conversion, if applicable
 sub _convert ($) {
   #my ($self, $arg) = @_;
-
-# LDNOTE : modified the previous implementation below because
-# it was not consistent : the first "return" is always an array,
-# the second "return" is context-dependent. Anyway, _convert
-# seems always used with just a single argument, so make it a
-# scalar function.
-#     return @_ unless $self->{convert};
-#     my $conv = $self->_sqlcase($self->{convert});
-#     my @ret = map { $conv.'('.$_.')' } @_;
-#     return wantarray ? @ret : $ret[0];
   if ($_[0]->{convert}) {
     return $_[0]->_sqlcase($_[0]->{convert}) .'(' . $_[1] . ')';
   }
@@ -297,11 +291,6 @@ sub _convert ($) {
 # And bindtype
 sub _bindtype (@) {
   #my ($self, $col, @vals) = @_;
-
-  #LDNOTE : changed original implementation below because it did not make
-  # sense when bindtype eq 'columns' and @vals > 1.
-#  return $self->{bindtype} eq 'columns' ? [ $col, @vals ] : @vals;
-
   # called often - tighten code
   return $_[0]->{bindtype} eq 'columns'
     ? map {[$_[1], $_]} @_[2 .. $#_]
@@ -432,7 +421,7 @@ SQL::Abstract - Generate SQL from Perl data structures
 
     my $sql = SQL::Abstract->new;
 
-    my($stmt, @bind) = $sql->select($table, \@fields, \%where, \@order);
+    my($stmt, @bind) = $sql->select($source, \@fields, \%where, \@order);
 
     my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
 
@@ -621,7 +610,7 @@ C<cmp> to C<like> you would get SQL such as:
 
     WHERE name like 'nwiger' AND email like 'nate@wiger.org'
 
-You can also override the comparsion on an individual basis - see
+You can also override the comparison on an individual basis - see
 the huge section on L</"WHERE CLAUSES"> at the bottom.
 
 =item sqltrue, sqlfalse
@@ -860,8 +849,8 @@ the source.
 The argument can be either an arrayref (interpreted as a list
 of field names, will be joined by commas and quoted), or a
 plain scalar (literal SQL, not quoted).
-Please observe that this API is not as flexible as for
-the first argument C<$table>, for backwards compatibility reasons.
+Please observe that this API is not as flexible as that of
+the first argument C<$source>, for backwards compatibility reasons.
 
 =item $where
 
@@ -1153,7 +1142,8 @@ would generate:
     )";
     @bind = ('2000');
 
-
+Finally, if the argument to C<-in> is not a reference, it will be
+treated as a single-element array.
 
 Another pair of operators is C<-between> and C<-not_between>,
 used with an arrayref of two values:
@@ -1218,15 +1208,19 @@ then you should use the and/or operators:-
     my %where  = (
         -and           => [
             -bool      => 'one',
-            -bool      => 'two',
-            -bool      => 'three',
-            -not_bool  => 'four',
+            -not_bool  => { two=> { -rlike => 'bar' } },
+            -not_bool  => { three => [ { '=', 2 }, { '>', 5 } ] },
         ],
     );
 
 Would give you:
 
-    WHERE one AND two AND three AND NOT four
+    WHERE
+      one
+        AND
+      (NOT two RLIKE ?)
+        AND
+      (NOT ( three = ? OR three > ? ))
 
 
 =head2 Nested conditions, -and/-or prefixes
@@ -1353,7 +1347,7 @@ Note that if you were to simply say:
         array => [1, 2, 3]
     );
 
-the result would porbably be not what you wanted:
+the result would probably not be what you wanted:
 
     $stmt = 'WHERE array = ? OR array = ? OR array = ?';
     @bind = (1, 2, 3);
@@ -1749,6 +1743,9 @@ can be as simple as the following:
 
     #!/usr/bin/perl
 
+    use warnings;
+    use strict;
+
     use CGI::FormBuilder;
     use SQL::Abstract;