Escape closing quote character in table and column names
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
index 7e641f7..2746b60 100644 (file)
@@ -113,6 +113,13 @@ my @tests = (
               bind   => ['nwiger']
       },
       {
+              func   => 'select',
+              args   => [[\'test1', 'test2'], '*', { 'test1.a' => 'boom' } ],
+              stmt   => 'SELECT * FROM test1, test2 WHERE ( test1.a = ? )',
+              stmt_q => 'SELECT * FROM test1, `test2` WHERE ( `test1`.`a` = ? )',
+              bind   => ['boom']
+      },
+      {
               func   => 'insert',
               args   => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
               stmt   => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
@@ -552,19 +559,40 @@ my @tests = (
               bind => [],
               warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
       },
+      {
+              func => 'select',
+              args => ['`test``table`', ['`test``column`']],
+              stmt => 'SELECT `test``column` FROM `test``table`',
+              stmt_q => 'SELECT ```test````column``` FROM ```test````table```',
+              bind => [],
+      },
+      {
+              func => 'select',
+              args => ['`test\\`table`', ['`test`\\column`']],
+              stmt => 'SELECT `test`\column` FROM `test\`table`',
+              stmt_q => 'SELECT `\`test\`\\\\column\`` FROM `\`test\\\\\`table\``',
+              esc  => '\\',
+              bind => [],
+      },
 );
 
 # check is( not) => undef
-for my $op (qw( is is_not), 'is not' ) {
+for my $op ( qw(not is is_not), 'is not' ) {
   (my $sop = uc $op) =~ s/_/ /gi;
 
-  push @tests, {
-    func => 'where',
-    args => [{ a => { "$_$op" => undef } }],
-    stmt => "WHERE a $sop NULL",
-    stmt_q => "WHERE `a` $sop NULL",
-    bind => [],
-  } for ('', '-');  # with and without -
+  $sop = 'IS NOT' if $sop eq 'NOT';
+
+  for my $uc (0, 1) {
+    for my $prefix ('', '-') {
+      push @tests, {
+        func => 'where',
+        args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
+        stmt => "WHERE a $sop NULL",
+        stmt_q => "WHERE `a` $sop NULL",
+        bind => [],
+      };
+    }
+  }
 }
 
 # check single-element inequality ops for no warnings
@@ -636,9 +664,15 @@ for my $t (@tests) {
 
   for my $quoted (0, 1) {
 
-    my $maker = SQL::Abstract->new(%$new, $quoted
-      ? (quote_char => '`', name_sep => '.')
-      : ()
+    my $maker = SQL::Abstract->new(
+      %$new,
+      ($quoted ? (
+        quote_char => '`',
+        name_sep => '.',
+        ( $t->{esc} ? (
+          escape_char => $t->{esc},
+        ) : ())
+      ) : ())
     );
 
     my($stmt, @bind);