From: Arthur Axel "fREW" Schmidt Date: Sat, 4 Sep 2010 03:21:05 +0000 (+0000) Subject: add test for somewhat complex sql and add extra config for missing keywords X-Git-Tag: v1.70~91^2~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1bb3956e85e58f2cbbd2d54545b99dd071ef9441;p=dbsrgits%2FSQL-Abstract.git add test for somewhat complex sql and add extra config for missing keywords --- diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm index ce36f30..24dbf6a 100644 --- a/lib/SQL/Abstract/Tree.pm +++ b/lib/SQL/Abstract/Tree.pm @@ -78,9 +78,12 @@ my $tokenizer_re = qr/ \s* ( $tokenizer_re_str | \( | \) | \? ) \s* /xi; sub _binary_op_keywords { @binary_op_keywords } my %indents = ( - select => 0, - where => 1, - from => 1, + select => 0, + where => 1, + from => 1, + join => 1, + on => 2, + 'group by' => 1, ); my %profiles = ( @@ -103,9 +106,12 @@ my %profiles = ( indent_amount => 2, newline => "
\n", colormap => { - select => ['', ''], - where => ['', ''], - from => ['', ''], + select => ['' , ''], + where => ['' , ''], + from => ['' , ''], + join => ['' , ''], + on => ['' , ''], + 'group by' => ['', ''], }, indentmap => { %indents }, }, diff --git a/t/11unparse.t b/t/11unparse.t index a91b88d..2b92623 100644 --- a/t/11unparse.t +++ b/t/11unparse.t @@ -34,6 +34,16 @@ subtest no_formatting => sub { 'simple statement with parens in where formatted correctly' ); } + + { + my $sql = "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] FROM [users_roles] [me] JOIN [roles] [role] ON [role].[id] = [me].[role_id] JOIN [roles_permissions] [role_permissions] ON [role_permissions].[role_id] = [role].[id] JOIN [permissions] [permission] ON [permission].[id] = [role_permissions].[permission_id] JOIN [permissionscreens] [permission_screens] ON [permission_screens].[permission_id] = [permission].[id] JOIN [screens] [screen] ON [screen].[id] = [permission_screens].[screen_id] WHERE ( [me].[user_id] = ? ) GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype]"; + my $expected_sql = + "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] FROM [users_roles] [me] JOIN [roles] [role] ON [role].[id] = [me].[role_id] JOIN [roles_permissions] [role_permissions] ON [role_permissions].[role_id] = [role].[id] JOIN [permissions] [permission] ON [permission].[id] = [role_permissions].[permission_id] JOIN [permissionscreens] [permission_screens] ON [permission_screens].[permission_id] = [permission].[id] JOIN [screens] [screen] ON [screen].[id] = [permission_screens].[screen_id] WHERE ([me].[user_id] = ?) GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] "; + + is($sqlat->format($sql), $expected_sql, + 'real life statement 1 formatted correctly' + ); + } done_testing; }; @@ -79,6 +89,28 @@ subtest console_monochrome => sub { 'simple statement with parens in where formatted correctly' ); } + + { + my $sql = "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] FROM [users_roles] [me] JOIN [roles] [role] ON [role].[id] = [me].[role_id] JOIN [roles_permissions] [role_permissions] ON [role_permissions].[role_id] = [role].[id] JOIN [permissions] [permission] ON [permission].[id] = [role_permissions].[permission_id] JOIN [permissionscreens] [permission_screens] ON [permission_screens].[permission_id] = [permission].[id] JOIN [screens] [screen] ON [screen].[id] = [permission_screens].[screen_id] WHERE ( [me].[user_id] = ? ) GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype]"; + my $expected_sql = + "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] \n". + " FROM [users_roles] [me] \n" . + " JOIN [roles] [role] \n" . + " ON [role].[id] = [me].[role_id] \n" . + " JOIN [roles_permissions] [role_permissions] \n" . + " ON [role_permissions].[role_id] = [role].[id] \n" . + " JOIN [permissions] [permission] \n" . + " ON [permission].[id] = [role_permissions].[permission_id] \n" . + " JOIN [permissionscreens] [permission_screens] \n" . + " ON [permission_screens].[permission_id] = [permission].[id] \n" . + " JOIN [screens] [screen] \n" . + " ON [screen].[id] = [permission_screens].[screen_id] \n" . + " WHERE ([me].[user_id] = ?) \n" . + " GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] "; + + my $gotten = $sqlat->format($sql); + is($gotten, $expected_sql, 'real life statement 1 formatted correctly'); + } done_testing; }; @@ -124,6 +156,28 @@ subtest html => sub { 'simple statement with parens in where formatted correctly' ); } + + { + my $sql = "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] FROM [users_roles] [me] JOIN [roles] [role] ON [role].[id] = [me].[role_id] JOIN [roles_permissions] [role_permissions] ON [role_permissions].[role_id] = [role].[id] JOIN [permissions] [permission] ON [permission].[id] = [role_permissions].[permission_id] JOIN [permissionscreens] [permission_screens] ON [permission_screens].[permission_id] = [permission].[id] JOIN [screens] [screen] ON [screen].[id] = [permission_screens].[screen_id] WHERE ( [me].[user_id] = ? ) GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype]"; + my $expected_sql = + qq{SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype]
\n}. + qq{  FROM [users_roles] [me]
\n}. + qq{  JOIN [roles] [role]
\n}. + qq{    ON [role].[id] = [me].[role_id]
\n}. + qq{  JOIN [roles_permissions] [role_permissions]
\n}. + qq{    ON [role_permissions].[role_id] = [role].[id]
\n}. + qq{  JOIN [permissions] [permission]
\n}. + qq{    ON [permission].[id] = [role_permissions].[permission_id]
\n}. + qq{  JOIN [permissionscreens] [permission_screens]
\n}. + qq{    ON [permission_screens].[permission_id] = [permission].[id]
\n}. + qq{  JOIN [screens] [screen]
\n}. + qq{    ON [screen].[id] = [permission_screens].[screen_id]
\n}. + qq{  WHERE ([me].[user_id] = ?)
\n}. + qq{  GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] }; + + my $gotten = $sqlat->format($sql); + is($gotten, $expected_sql, 'real life statement 1 formatted correctly'); + } done_testing; }; @@ -172,10 +226,31 @@ subtest configuration => sub { 'simple statement with parens in where formatted correctly' ); } + + { + my $sql = "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] FROM [users_roles] [me] JOIN [roles] [role] ON [role].[id] = [me].[role_id] JOIN [roles_permissions] [role_permissions] ON [role_permissions].[role_id] = [role].[id] JOIN [permissions] [permission] ON [permission].[id] = [role_permissions].[permission_id] JOIN [permissionscreens] [permission_screens] ON [permission_screens].[permission_id] = [permission].[id] JOIN [screens] [screen] ON [screen].[id] = [permission_screens].[screen_id] WHERE ( [me].[user_id] = ? ) GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype]"; + my $expected_sql = + "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] \r\n". + "\tFROM [users_roles] [me] \r\n" . + "\tJOIN [roles] [role] \r\n" . + "\t\tON [role].[id] = [me].[role_id] \r\n" . + "\tJOIN [roles_permissions] [role_permissions] \r\n" . + "\t\tON [role_permissions].[role_id] = [role].[id] \r\n" . + "\tJOIN [permissions] [permission] \r\n" . + "\t\tON [permission].[id] = [role_permissions].[permission_id] \r\n" . + "\tJOIN [permissionscreens] [permission_screens] \r\n" . + "\t\tON [permission_screens].[permission_id] = [permission].[id] \r\n" . + "\tJOIN [screens] [screen] \r\n" . + "\t\tON [screen].[id] = [permission_screens].[screen_id] \r\n" . + "\tWHERE ([me].[user_id] = ?) \r\n" . + "\tGROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] "; + + my $gotten = $sqlat->format($sql); + is($gotten, $expected_sql, 'real life statement 1 formatted correctly'); + } done_testing; }; done_testing; # stuff we want: # Max Width -# Color coding (html)