[Bast-commits] r7553 - in SQL-Abstract/1.x/trunk: lib/SQL t

nigel at dev.catalyst.perl.org nigel at dev.catalyst.perl.org
Fri Sep 4 14:34:56 GMT 2009


Author: nigel
Date: 2009-09-04 14:34:55 +0000 (Fri, 04 Sep 2009)
New Revision: 7553

Modified:
   SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm
   SQL-Abstract/1.x/trunk/t/02where.t
Log:
Extended -bool handling to handle more types of parameters

Modified: SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm
===================================================================
--- SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm	2009-09-04 09:56:00 UTC (rev 7552)
+++ SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm	2009-09-04 14:34:55 UTC (rev 7553)
@@ -553,15 +553,36 @@
 sub _where_op_BOOL {
   my ($self, $op, $v) = @_; 
 
-  my $prefix = ($op =~ /\bnot\b/i) ? 'NOT ' : '';
+  my ( $prefix, $suffix ) = ( $op =~ /\bnot\b/i ) 
+    ? ( '(NOT ', ')' ) 
+    : ( '', '' );
   $self->_SWITCH_refkind($v, {
+    ARRAYREF => sub {
+      my ( $sql, @bind ) = $self->_where_ARRAYREF($v, '');
+      return ( ($prefix . $sql . $suffix), @bind );
+    },
+
+    ARRAYREFREF => sub {
+      my ( $sql, @bind ) = @{ ${$v} };
+      return ( ($prefix . $sql . $suffix), @bind );
+    },
+
+    HASHREF => sub {
+      my ( $sql, @bind ) = $self->_where_HASHREF($v);
+      return ( ($prefix . $sql . $suffix), @bind );
+    },
+
     SCALARREF  => sub {         # literal SQL
-      return ($prefix . $$v); 
+      return ($prefix . $$v . $suffix); 
     },
 
     SCALAR => sub { # interpreted as SQL column
-      return ($prefix . $self->_convert($self->_quote($v))); 
+      return ($prefix . $self->_convert($self->_quote($v)) . $suffix); 
     },
+
+    UNDEF => sub {
+      puke "-$op => undef not supported";
+    },
    });
 }
 

Modified: SQL-Abstract/1.x/trunk/t/02where.t
===================================================================
--- SQL-Abstract/1.x/trunk/t/02where.t	2009-09-04 09:56:00 UTC (rev 7552)
+++ SQL-Abstract/1.x/trunk/t/02where.t	2009-09-04 14:34:55 UTC (rev 7553)
@@ -255,16 +255,52 @@
 
    {
        where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
-       stmt => " WHERE NOT foo AND NOT bar",
+       stmt => " WHERE (NOT foo) AND (NOT bar)",
        bind => [],
    },
 
    {
        where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
-       stmt => " WHERE NOT foo OR NOT bar",
+       stmt => " WHERE (NOT foo) OR (NOT bar)",
        bind => [],
    },
 
+   {
+       where => { -bool => \['function(?)', 20]  },
+       stmt => " WHERE function(?)",
+       bind => [20],
+   },
+
+   {
+       where => { -not_bool => \['function(?)', 20]  },
+       stmt => " WHERE NOT function(?)",
+       bind => [20],
+   },
+
+   {
+       where => { -bool => { a => 1, b => 2}  },
+       stmt => " WHERE a = ? AND b = ?",
+       bind => [1, 2],
+   },
+
+   {
+       where => { -bool => [ a => 1, b => 2] },
+       stmt => " WHERE a = ? OR b = ?",
+       bind => [1, 2],
+   },
+
+   {
+       where => { -not_bool => { a => 1, b => 2}  },
+       stmt => " WHERE NOT (a = ? AND b = ?)",
+       bind => [1, 2],
+   },
+
+   {
+       where => { -not_bool => [ a => 1, b => 2] },
+       stmt => " WHERE NOT ( a = ? OR b = ? )",
+       bind => [1, 2],
+   },
+
 );
 
 plan tests => ( @handle_tests * 2 ) + 1;
@@ -275,7 +311,7 @@
     my($stmt, @bind);
     lives_ok (sub { 
       ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
-      is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
+      is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind}, $stmt)
         || diag "Search term:\n" . Dumper $case->{where};
     });
 }




More information about the Bast-commits mailing list