don't gensym for unqualified versions of already selected columns
[dbsrgits/Data-Query.git] / t / sql.t
diff --git a/t/sql.t b/t/sql.t
index 64e2f2c..a8cc66d 100644 (file)
--- a/t/sql.t
+++ b/t/sql.t
@@ -3,16 +3,19 @@ use Test::More qw(no_plan);
 
 use Devel::Dwarn;
 use Data::Query::Renderer::SQL::Naive;
+use Data::Query::ExprHelpers qw(perl_scalar_value);
 
 BEGIN { require 't/expr.include' }
 
 my $rend = Data::Query::Renderer::SQL::Naive->new({ quote_chars => [ "'" ] });
 
+sub binding { map perl_scalar_value($_), @_ }
+
 sub expr_sql_is (&;@) {
   my $sub = shift;
   @_
-    ? is_deeply($rend->render(_mk_expr($sub)), @_)
-    : ::Dwarn($rend->render(_mk_expr($sub)));
+    ? is_deeply($rend->render(_run_expr($sub)->{expr}), @_)
+    : ::Dwarn($rend->render(_run_expr($sub)->{expr}));
 }
 
 expr_sql_is { $_->foo }
@@ -28,66 +31,36 @@ expr_sql_is { $_->foo->group }
   "Complex identifier -> SQL";
 
 expr_sql_is { $_->foo == 1 }
-  [
-    "foo = ?",
-    {
-      subtype => {
-        Perl => "Scalar"
-      },
-      type => "Value",
-      value => 1
-    }
-  ],
+  [ "foo = ?", binding(1) ],
   "Simple expression -> SQL";
 
 expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") }
-  [
-    "( foo = ? AND bar = ? )",
-    {
-      subtype => {
-        Perl => "Scalar"
-      },
-      type => "Value",
-      value => 1
-    },
-    {
-      subtype => {
-        Perl => "Scalar"
-      },
-      type => "Value",
-      value => "foo"
-    }
-  ],
+  [ "( foo = ? AND bar = ? )", binding(1, "foo") ],
   "Compound expression -> SQL";
 
 
 expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") & ($_->baz > 3) }
-  [
-    "( foo = ? AND bar = ? AND baz > ? )",
-    {
-      subtype => {
-        Perl => "Scalar"
-      },
-      type => "Value",
-      value => 1
-    },
-    {
-      subtype => {
-        Perl => "Scalar"
-      },
-      type => "Value",
-      value => "foo"
-    },
-    {
-      subtype => {
-        Perl => "Scalar"
-      },
-      type => "Value",
-      value => 3
-    }
-  ],
+  [ "( foo = ? AND bar = ? AND baz > ? )", binding(1, "foo", 3) ],
   "Flatten expression ok";
 
-expr_sql_is { !$_->foo }
-  [ "NOT foo" ],
+expr_sql_is { !$_->foo } # XXX revisit this why are the parens here
+  [ "( NOT foo )" ],
   "Unary expression ok";
+
+expr_sql_is { SELECT { $_->foo } }
+  [ "SELECT foo" ],
+  "Simple identifier";
+
+expr_sql_is { SELECT { $_->foo, 1 } }
+  # the extra space here is a little icky but Naive's _flatten_structure
+  # will need rewriting to fix it - commit bits available if you do it first
+  [ "SELECT foo, ?", binding(1) ],
+  "Identifier and literal";
+
+expr_sql_is { SELECT { $_->foo => AS("foom"), 1 } }
+  [ "SELECT foo AS foom, ?", binding(1) ],
+  "AS with parens";
+
+expr_sql_is { SELECT { $_->foo => AS "foom", 1 } }
+  [ "SELECT foo AS foom, ?", binding(1) ],
+  "AS without parens";