[Bast-commits] r9678 - in SQL-Abstract/1.x/branches/sqla-tree: lib/SQL/Abstract t

frew at dev.catalyst.perl.org frew at dev.catalyst.perl.org
Sat Sep 4 02:05:47 GMT 2010


Author: frew
Date: 2010-09-04 03:05:47 +0100 (Sat, 04 Sep 2010)
New Revision: 9678

Modified:
   SQL-Abstract/1.x/branches/sqla-tree/lib/SQL/Abstract/Tree.pm
   SQL-Abstract/1.x/branches/sqla-tree/t/11unparse.t
Log:
add html profile

Modified: SQL-Abstract/1.x/branches/sqla-tree/lib/SQL/Abstract/Tree.pm
===================================================================
--- SQL-Abstract/1.x/branches/sqla-tree/lib/SQL/Abstract/Tree.pm	2010-09-04 01:22:34 UTC (rev 9677)
+++ SQL-Abstract/1.x/branches/sqla-tree/lib/SQL/Abstract/Tree.pm	2010-09-04 02:05:47 UTC (rev 9678)
@@ -77,28 +77,37 @@
 
 sub _binary_op_keywords { @binary_op_keywords }
 
+my %indents = (
+   select => 0,
+   where  => 1,
+   from   => 1,
+);
+
 my %profiles = (
    console => {
       indent_string => ' ',
       indent_amount => 2,
       newline       => "\n",
       colormap      => {},
-      indentmap     => {
-         select => 0,
-         where  => 1,
-         from   => 1,
-      },
+      indentmap     => { %indents },
    },
    console_monochrome => {
       indent_string => ' ',
       indent_amount => 2,
       newline       => "\n",
       colormap      => {},
-      indentmap     => {
-         select => 0,
-         where  => 1,
-         from   => 1,
+      indentmap     => { %indents },
+   },
+   html => {
+      indent_string => ' ',
+      indent_amount => 2,
+      newline       => "<br />\n",
+      colormap      => {
+         select => ['<span class="select">', '</span>'],
+         where  => ['<span class="where">', '</span>'],
+         from   => ['<span class="from">', '</span>'],
       },
+      indentmap     => { %indents },
    },
    none => {
       colormap      => {},

Modified: SQL-Abstract/1.x/branches/sqla-tree/t/11unparse.t
===================================================================
--- SQL-Abstract/1.x/branches/sqla-tree/t/11unparse.t	2010-09-04 01:22:34 UTC (rev 9677)
+++ SQL-Abstract/1.x/branches/sqla-tree/t/11unparse.t	2010-09-04 02:05:47 UTC (rev 9678)
@@ -82,9 +82,100 @@
    done_testing;
 };
 
+subtest html => sub {
+   my $sqlat = SQL::Abstract::Tree->new({
+      profile => 'html',
+   });
+
+   {
+      my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'";
+      my $expected_sql =
+         qq{<span class="select">SELECT</span> a, b, c <br />\n} .
+         qq{&nbsp;&nbsp;<span class="from">FROM</span> foo <br />\n} .
+         qq{&nbsp;&nbsp;<span class="where">WHERE</span> foo.a = 1 AND foo.b LIKE 'station' };
+      is($sqlat->format($sql), $expected_sql,
+         'simple statement formatted correctly'
+      );
+   }
+
+   {
+      my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'";
+      my $expected_sql =
+         qq{<span class="select">SELECT</span> * <br />\n} .
+         qq{&nbsp;&nbsp;<span class="from">FROM</span> (<br />\n} .
+         qq{&nbsp;&nbsp;&nbsp;&nbsp;<span class="select">SELECT</span> * <br />\n} .
+         qq{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="from">FROM</span> foobar <br />\n} .
+         qq{&nbsp;&nbsp;) <br />\n} .
+         qq{&nbsp;&nbsp;<span class="where">WHERE</span> foo.a = 1 AND foo.b LIKE 'station' };
+
+      is($sqlat->format($sql), $expected_sql,
+         'subquery statement formatted correctly'
+      );
+   }
+
+   {
+      my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
+      my $expected_sql =
+         qq{<span class="select">SELECT</span> * <br />\n} .
+         qq{&nbsp;&nbsp;<span class="from">FROM</span> lolz <br />\n} .
+         qq{&nbsp;&nbsp;<span class="where">WHERE</span> (foo.a = 1) AND foo.b LIKE 'station' };
+
+      is($sqlat->format($sql), $expected_sql,
+         'simple statement with parens in where formatted correctly'
+      );
+   }
+   done_testing;
+};
+
+subtest configuration => sub {
+   my $sqlat = SQL::Abstract::Tree->new({
+      profile => 'console_monochrome',
+      indent_string => "\t",
+      indent_amount => 1,
+      newline => "\r\n",
+   });
+
+   {
+      my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'";
+      my $expected_sql =
+         qq{SELECT a, b, c \r\n} .
+         qq{\tFROM foo \r\n} .
+         qq{\tWHERE foo.a = 1 AND foo.b LIKE 'station' };
+      is($sqlat->format($sql), $expected_sql,
+         'simple statement formatted correctly'
+      );
+   }
+
+   {
+      my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'";
+      my $expected_sql =
+         qq{SELECT * \r\n} .
+         qq{\tFROM (\r\n} .
+         qq{\t\tSELECT * \r\n} .
+         qq{\t\t\tFROM foobar \r\n} .
+         qq{\t) \r\n} .
+         qq{\tWHERE foo.a = 1 AND foo.b LIKE 'station' };
+
+      is($sqlat->format($sql), $expected_sql,
+         'subquery statement formatted correctly'
+      );
+   }
+
+   {
+      my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
+      my $expected_sql =
+         qq{SELECT * \r\n} .
+         qq{\tFROM lolz \r\n} .
+         qq{\tWHERE (foo.a = 1) AND foo.b LIKE 'station' };
+
+      is($sqlat->format($sql), $expected_sql,
+         'simple statement with parens in where formatted correctly'
+      );
+   }
+   done_testing;
+};
+
 done_testing;
 # stuff we want:
-#    Nested indentation
 #    Max Width
-#    Color coding (console)
 #    Color coding (html)




More information about the Bast-commits mailing list