7 use SQL::Abstract::Test import => [qw(is_same_sql dumper)];
8 use SQL::Abstract::Tree;
10 my $sqlat = SQL::Abstract::Tree->new;
13 "INSERT INTO artist DEFAULT VALUES",
14 "INSERT INTO artist VALUES ()",
15 "SELECT a, b, c FROM foo WHERE foo.a = 1 and foo.b LIKE 'station'",
16 "SELECT COUNT( * ) FROM foo",
17 "SELECT COUNT( * ), SUM( blah ) FROM foo",
18 "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a = 1 and foo.b LIKE 'station'",
19 "SELECT * FROM lolz WHERE ( foo.a = 1 ) and foo.b LIKE 'station'",
20 "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]",
21 "SELECT * FROM foo WHERE NOT EXISTS (SELECT bar FROM baz)",
22 "SELECT * FROM (SELECT SUM (CASE WHEN me.artist = 'foo' THEN 1 ELSE 0 END AS artist_sum) FROM foobar) WHERE foo.a = 1 and foo.b LIKE 'station'",
23 "SELECT COUNT( * ) FROM foo me JOIN bar rel_bar ON rel_bar.id_bar = me.fk_bar WHERE NOT EXISTS (SELECT inner_baz.id_baz FROM baz inner_baz WHERE ( ( inner_baz.fk_a != ? AND ( fk_bar = me.fk_bar AND name = me.name ) ) ) )",
24 "SELECT foo AS bar FROM baz ORDER BY x + ? DESC, oomph, y - ? DESC, unf, baz.g / ? ASC, buzz * 0 DESC, foo DESC, ickk ASC",
25 "SELECT inner_forum_roles.forum_id FROM forum_roles AS inner_forum_roles LEFT JOIN user_roles AS inner_user_roles USING(user_role_type_id) WHERE inner_user_roles.user_id = users__row.user_id",
26 "SELECT * FROM foo WHERE foo.a @@ to_tsquery('word')",
27 "SELECT * FROM foo ORDER BY name + ?, [me].[id]",
31 # The formatter/unparser accumulated a ton of technical debt,
32 # and I don't have time to fix it all :( Some of the problems:
33 # - format() does an implicit parenthesis unroll for prettyness
34 # which makes it hard to do exact comparisons
35 # - there is no space preservation framework (also makes comparisons
37 # - there is no operator case preservation framework either
39 # So what we do instead is resort to some monkey patching and
40 # lowercasing and stuff to get something we can compare to the
42 # Ugly but somewhat effective
45 my $plain_formatted = $sqlat->format($orig);
46 is_same_sql( $plain_formatted, $orig, 'Formatted string is_same_sql()-matched' );
48 my $ast = $sqlat->parse($orig);
49 my $reassembled = do {
50 no warnings 'redefine';
51 local *SQL::Abstract::Tree::_parenthesis_unroll = sub {};
52 $sqlat->unparse($ast);
55 # deal with parenthesis readjustment
56 $_ =~ s/\s*([\(\)])\s*/$1 /g
57 for ($orig, $reassembled);
62 sprintf( 'roundtrip works (%s...)', substr $orig, 0, 20 )
64 my ($ast1, $ast2) = map { dumper( $sqlat->parse($_) ) } ( $orig, $reassembled );
71 lives_ok { $sqlat->unparse( $sqlat->parse( <<'EOS' ) ) } 'Able to parse/unparse grossly malformed sql';
74 SELECT *, * FROM EXISTS bar JOIN ON a = b
79 SELECT * FROM bar WHERE NOT NOT EXISTS (SELECT 1)
81 WHERE NOT NOT 1 AND OR foo IN (1,2,,,3,,,),