X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=xt%2Fclauses.t;h=ce75f173886328af4a5368a807405533aec730fd;hb=2b0b3d43c5d042c60b97b0d84bfc85cfec9d82ac;hp=1c88964437b48b18d274d7b0531c45f51ba661df;hpb=c42752fcff779da7a99e168ad5b06bbbf0479cc2;p=scpubgit%2FQ-Branch.git diff --git a/xt/clauses.t b/xt/clauses.t index 1c88964..ce75f17 100644 --- a/xt/clauses.t +++ b/xt/clauses.t @@ -1,33 +1,178 @@ use strict; use warnings; use Test::More; -use SQL::Abstract::Test import => [ qw(is_same_sql_bind) ]; +use SQL::Abstract::Test import => [ qw(is_same_sql_bind is_same_sql) ]; use SQL::Abstract::ExtraClauses; -my $sqlac = SQL::Abstract::ExtraClauses->new; +my $sqlac = SQL::Abstract::ExtraClauses->new(unknown_unop_always_func => 1); my ($sql, @bind) = $sqlac->select({ - select => [ qw(artist.id artist.name), { -func => [ json_agg => 'cd' ] } ], + select => [ qw(artist.id artist.name), { -json_agg => 'cd' } ], from => [ - artist => -join => [ cd => on => { 'cd.artist_id' => 'artist.id' } ], + { artists => { -as => 'artist' } }, + -join => [ cds => as => 'cd' => on => { 'cd.artist_id' => 'artist.id' } ], ], where => { 'artist.genres', => { '@>', { -value => [ 'Rock' ] } } }, order_by => 'artist.name', group_by => 'artist.id', - having => { '>' => [ { -func => [ count => 'cd.id' ] }, 3 ] } + having => { '>' => [ { -count => 'cd.id' }, 3 ] } }); is_same_sql_bind( $sql, \@bind, q{ SELECT artist.id, artist.name, JSON_AGG(cd) - FROM artist JOIN cd ON cd.artist_id = artist.id + FROM artists AS artist JOIN cds AS cd ON cd.artist_id = artist.id WHERE artist.genres @> ? - ORDER BY artist.name GROUP BY artist.id HAVING COUNT(cd.id) > ? + ORDER BY artist.name }, [ [ 'Rock' ], 3 ] ); +($sql) = $sqlac->select({ + select => [ 'a' ], + from => [ { -values => [ [ 1, 2 ], [ 3, 4 ] ] }, -as => [ qw(t a b) ] ], +}); + +is_same_sql($sql, q{SELECT a FROM (VALUES (1, 2), (3, 4)) AS t(a,b)}); + +($sql) = $sqlac->update({ + update => 'employees', + set => { sales_count => { sales_count => { '+', \1 } } }, + from => 'accounts', + where => { + 'accounts.name' => { '=' => \"'Acme Corporation'" }, + 'employees.id' => { -ident => 'accounts.sales_person' }, + } +}); + +is_same_sql( + $sql, + q{UPDATE employees SET sales_count = sales_count + 1 FROM accounts + WHERE accounts.name = 'Acme Corporation' + AND employees.id = accounts.sales_person + } +); + +($sql) = $sqlac->update({ + update => [ qw(tab1 tab2) ], + set => { + 'tab1.column1' => { -ident => 'value1' }, + 'tab1.column2' => { -ident => 'value2' }, + }, + where => { 'tab1.id' => { -ident => 'tab2.id' } }, +}); + +is_same_sql( + $sql, + q{UPDATE tab1, tab2 SET tab1.column1 = value1, tab1.column2 = value2 + WHERE tab1.id = tab2.id} +); + +is_same_sql( + $sqlac->delete({ + from => 'x', + using => 'y', + where => { 'x.id' => { -ident => 'y.x_id' } } + }), + q{DELETE FROM x USING y WHERE x.id = y.x_id} +); + +is_same_sql( + $sqlac->select({ + select => [ 'x.*', 'y.*' ], + from => [ 'x', -join => [ 'y', using => 'y_id' ] ], + }), + q{SELECT x.*, y.* FROM x JOIN y USING (y_id)}, +); + +is_same_sql( + $sqlac->select({ + select => 'x.*', + from => [ { -select => { select => '*', from => 'y' } }, -as => 'x' ], + }), + q{SELECT x.* FROM (SELECT * FROM y) AS x}, +); + +is_same_sql( + $sqlac->insert({ + into => 'foo', + select => { select => '*', from => 'bar' } + }), + q{INSERT INTO foo SELECT * FROM bar} +); + +($sql, @bind) = $sqlac->insert({ + into => 'eh', + rowvalues => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +}); + +is_same_sql_bind( + $sql, \@bind, + q{INSERT INTO eh VALUES (?, ?), (?, ?), (?, ?)}, + [ 1..6 ], +); + +is_same_sql( + $sqlac->select({ + select => '*', + from => 'foo', + where => { -not_exists => { + -select => { + select => \1, + from => 'bar', + where => { 'foo.id' => { -ident => 'bar.foo_id' } } + }, + } }, + }), + q{SELECT * FROM foo + WHERE NOT EXISTS (SELECT 1 FROM bar WHERE foo.id = bar.foo_id)}, +); + +is_same_sql( + $sqlac->select({ + select => '*', + from => 'foo', + where => { id => { + '=' => { -select => { select => { -max => 'id' }, from => 'foo' } } + } }, + }), + q{SELECT * FROM foo WHERE id = (SELECT MAX(id) FROM foo)}, +); + +{ + my $sqlac = $sqlac->clone + ->clauses_of( + select => ( + $sqlac->clauses_of('select'), + qw(limit offset), + ) + ); + + ($sql, @bind) = $sqlac->select({ + select => '*', + from => 'foo', + limit => 10, + offset => 20, + }); + + is_same_sql_bind( + $sql, \@bind, + q{SELECT * FROM foo LIMIT ? OFFSET ?}, [ 10, 20 ] + ); +} + +($sql) = $sqlac->select({ + select => { -as => [ 1, 'x' ] }, + union => { -select => { select => { -as => [ 2, 'x' ] } } }, + order_by => { -desc => 'x' }, +}); + +is_same_sql( + $sql, + q{(SELECT 1 AS x) UNION (SELECT 2 AS x) ORDER BY x DESC}, +); + done_testing;