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

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Tue Sep 22 08:10:35 GMT 2009


Author: ribasushi
Date: 2009-09-22 08:10:34 +0000 (Tue, 22 Sep 2009)
New Revision: 7718

Added:
   SQL-Abstract/1.x/trunk/t/05in_between.t
Removed:
   SQL-Abstract/1.x/trunk/t/05between.t
Modified:
   SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm
Log:
Allow scalarref in IN and open up non-grouping parenthesis around IN arguments (saves sqlite from brain damage)

Modified: SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm
===================================================================
--- SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm	2009-09-22 07:55:36 UTC (rev 7717)
+++ SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm	2009-09-22 08:10:34 UTC (rev 7718)
@@ -893,21 +893,35 @@
       }
     },
 
+    SCALARREF => sub {  # literal SQL
+      my $sql = $self->_open_outer_paren ($$vals);
+      return ("$label $op ( $sql )");
+    },
     ARRAYREFREF => sub {  # literal SQL with bind
       my ($sql, @bind) = @$$vals;
       $self->_assert_bindval_matches_bindtype(@bind);
+      $sql = $self->_open_outer_paren ($sql);
       return ("$label $op ( $sql )", @bind);
     },
 
     FALLBACK => sub {
-      puke "special op 'in' requires an arrayref (or arrayref-ref)";
+      puke "special op 'in' requires an arrayref (or scalarref/arrayref-ref)";
     },
   });
 
   return ($sql, @bind);
 }
 
+# Some databases (SQLite) treat col IN (1, 2) different from
+# col IN ( (1, 2) ). Use this to strip all outer parens while
+# adding them back in the corresponding method
+sub _open_outer_paren {
+  my ($self, $sql) = @_;
+  $sql = $1 while $sql =~ /^ \s* \( (.*) \) \s* $/x;
+  return $sql;
+}
 
+
 #======================================================================
 # ORDER BY
 #======================================================================

Deleted: SQL-Abstract/1.x/trunk/t/05between.t
===================================================================
--- SQL-Abstract/1.x/trunk/t/05between.t	2009-09-22 07:55:36 UTC (rev 7717)
+++ SQL-Abstract/1.x/trunk/t/05between.t	2009-09-22 08:10:34 UTC (rev 7718)
@@ -1,110 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-use Test::More;
-use Test::Exception;
-use SQL::Abstract::Test import => ['is_same_sql_bind'];
-
-use Data::Dumper;
-use SQL::Abstract;
-
-=begin
-Test -between and -in 
-  * between
-    * [scalar, scalar]
-    * [scalarref, scalar]
-    * [scalar, scalarref]
-    * [scalarref, scalarref]
-    * \[]
-      * \["? AND ?", scalar, scalar]
-      * \["1 AND ?", scalar]
-      * \["? AND 2", scalar]
-      * \["1 AND 2"]
-=cut
-
-my @in_between_tests = (
-  {
-    where => { x => { -between => [1, 2] } },
-    stmt => 'WHERE (x BETWEEN ? AND ?)',
-    bind => [qw/1 2/],
-    test => '-between with two placeholders',
-  },
-  {
-    where => { x => { -between => [\"1", 2] } },
-    stmt => 'WHERE (x BETWEEN 1 AND ?)',
-    bind => [qw/2/],
-    test => '-between with one literal sql arg and one placeholder',
-  },
-  {
-    where => { x => { -between => [1, \"2"] } },
-    stmt => 'WHERE (x BETWEEN ? AND 2)',
-    bind => [qw/1/],
-    test => '-between with one placeholder and one literal sql arg',
-  },
-  {
-    where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
-    stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
-    bind => [],
-    test => '-between with two literal sql arguments',
-  },
-  {
-    where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
-    stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
-    bind => [1, 0],
-    test => '-between with two literal sql arguments with bind',
-  },
-  {
-    where => { x => { -between => \['? AND ?', 1, 2] } },
-    stmt => 'WHERE (x BETWEEN ? AND ?)',
-    bind => [1,2],
-    test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
-  },
-  {
-    where => { x => { -between => \["'something' AND ?", 2] } },
-    stmt => "WHERE (x BETWEEN 'something' AND ?)",
-    bind => [2],
-    test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
-  },
-  {
-    where => { x => { -between => \["? AND 'something'", 1] } },
-    stmt => "WHERE (x BETWEEN ? AND 'something')",
-    bind => [1],
-    test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
-  },
-  {
-    where => { x => { -between => \"'this' AND 'that'" } },
-    stmt => "WHERE (x BETWEEN 'this' AND 'that')",
-    bind => [],
-    test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
-  },
-);
-
-plan tests => @in_between_tests*4;
-
-for my $case (@in_between_tests) {
-  TODO: {
-    local $TODO = $case->{todo} if $case->{todo};
-
-    local $Data::Dumper::Terse = 1;
-
-    lives_ok (sub {
-
-      my @w;
-      local $SIG{__WARN__} = sub { push @w, @_ };
-      my $sql = SQL::Abstract->new ($case->{args} || {});
-      lives_ok (sub { 
-        my ($stmt, @bind) = $sql->where($case->{where});
-        is_same_sql_bind(
-          $stmt,
-          \@bind,
-          $case->{stmt},
-          $case->{bind},
-        )
-          || diag "Search term:\n" . Dumper $case->{where};
-      });
-      is (@w, 0, $case->{test} || 'No warnings within in-between tests')
-        || diag join "\n", 'Emitted warnings:', @w;
-    }, "$case->{test} doesn't die");
-  }
-}

Copied: SQL-Abstract/1.x/trunk/t/05in_between.t (from rev 7716, SQL-Abstract/1.x/trunk/t/05between.t)
===================================================================
--- SQL-Abstract/1.x/trunk/t/05in_between.t	                        (rev 0)
+++ SQL-Abstract/1.x/trunk/t/05in_between.t	2009-09-22 08:10:34 UTC (rev 7718)
@@ -0,0 +1,120 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+use SQL::Abstract::Test import => ['is_same_sql_bind'];
+
+use Data::Dumper;
+use SQL::Abstract;
+
+my @in_between_tests = (
+  {
+    where => { x => { -between => [1, 2] } },
+    stmt => 'WHERE (x BETWEEN ? AND ?)',
+    bind => [qw/1 2/],
+    test => '-between with two placeholders',
+  },
+  {
+    where => { x => { -between => [\"1", 2] } },
+    stmt => 'WHERE (x BETWEEN 1 AND ?)',
+    bind => [qw/2/],
+    test => '-between with one literal sql arg and one placeholder',
+  },
+  {
+    where => { x => { -between => [1, \"2"] } },
+    stmt => 'WHERE (x BETWEEN ? AND 2)',
+    bind => [qw/1/],
+    test => '-between with one placeholder and one literal sql arg',
+  },
+  {
+    where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
+    stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
+    bind => [],
+    test => '-between with two literal sql arguments',
+  },
+  {
+    where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
+    stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
+    bind => [1, 0],
+    test => '-between with two literal sql arguments with bind',
+  },
+  {
+    where => { x => { -between => \['? AND ?', 1, 2] } },
+    stmt => 'WHERE (x BETWEEN ? AND ?)',
+    bind => [1,2],
+    test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
+  },
+  {
+    where => { x => { -between => \["'something' AND ?", 2] } },
+    stmt => "WHERE (x BETWEEN 'something' AND ?)",
+    bind => [2],
+    test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
+  },
+  {
+    where => { x => { -between => \["? AND 'something'", 1] } },
+    stmt => "WHERE (x BETWEEN ? AND 'something')",
+    bind => [1],
+    test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
+  },
+  {
+    where => { x => { -between => \"'this' AND 'that'" } },
+    stmt => "WHERE (x BETWEEN 'this' AND 'that')",
+    bind => [],
+    test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
+  },
+
+
+  {
+    parenthesis_significant => 1,
+    where => { x => { -in => [ 1 .. 3] } },
+    stmt => "WHERE ( x IN (?, ?, ?) )",
+    bind => [ 1 .. 3],
+    test => '-in with an array of scalars',
+  },
+  {
+    parenthesis_significant => 1,
+    where => { x => { -in => \'( 1,2,lower(y) )' } },
+    stmt => "WHERE ( x IN (1, 2, lower(y) ) )",
+    bind => [],
+    test => '-in with a literal scalarref',
+  },
+  {
+    parenthesis_significant => 1,
+    where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
+    stmt => "WHERE ( x IN (?, ?, lower(y) ) )",
+    bind => [1, 2],
+    test => '-in with a literal arrayrefref',
+  },
+);
+
+plan tests => @in_between_tests*4;
+
+for my $case (@in_between_tests) {
+  TODO: {
+    local $TODO = $case->{todo} if $case->{todo};
+    local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
+
+    local $Data::Dumper::Terse = 1;
+
+    lives_ok (sub {
+
+      my @w;
+      local $SIG{__WARN__} = sub { push @w, @_ };
+      my $sql = SQL::Abstract->new ($case->{args} || {});
+      lives_ok (sub { 
+        my ($stmt, @bind) = $sql->where($case->{where});
+        is_same_sql_bind(
+          $stmt,
+          \@bind,
+          $case->{stmt},
+          $case->{bind},
+        )
+          || diag "Search term:\n" . Dumper $case->{where};
+      });
+      is (@w, 0, $case->{test} || 'No warnings within in-between tests')
+        || diag join "\n", 'Emitted warnings:', @w;
+    }, "$case->{test} doesn't die");
+  }
+}




More information about the Bast-commits mailing list