4 use SQL::Abstract::Test import => [ qw(is_same_sql_bind is_same_sql) ];
5 use SQL::Abstract::ExtraClauses;
7 my $sqlac = SQL::Abstract::ExtraClauses->new(unknown_unop_always_func => 1);
9 my ($sql, @bind) = $sqlac->select({
10 select => [ qw(artist.id artist.name), { -json_agg => 'cd' } ],
12 { artists => { -as => 'artist' } },
13 -join => [ cds => as => 'cd' => on => { 'cd.artist_id' => 'artist.id' } ],
15 where => { 'artist.genres', => { '@>', { -value => [ 'Rock' ] } } },
16 order_by => 'artist.name',
17 group_by => 'artist.id',
18 having => { '>' => [ { -count => 'cd.id' }, 3 ] }
24 SELECT artist.id, artist.name, JSON_AGG(cd)
25 FROM artists AS artist JOIN cds AS cd ON cd.artist_id = artist.id
26 WHERE artist.genres @> ?
28 HAVING COUNT(cd.id) > ?
34 ($sql) = $sqlac->select({
36 from => [ { -values => [ [ 1, 2 ], [ 3, 4 ] ] }, -as => [ qw(t a b) ] ],
39 is_same_sql($sql, q{SELECT a FROM (VALUES (1, 2), (3, 4)) AS t(a,b)});
41 ($sql) = $sqlac->update({
42 update => 'employees',
43 set => { sales_count => { sales_count => { '+', \1 } } },
46 'accounts.name' => { '=' => \"'Acme Corporation'" },
47 'employees.id' => { -ident => 'accounts.sales_person' },
53 q{UPDATE employees SET sales_count = sales_count + 1 FROM accounts
54 WHERE accounts.name = 'Acme Corporation'
55 AND employees.id = accounts.sales_person
59 ($sql) = $sqlac->update({
60 update => [ qw(tab1 tab2) ],
62 'tab1.column1' => { -ident => 'value1' },
63 'tab1.column2' => { -ident => 'value2' },
65 where => { 'tab1.id' => { -ident => 'tab2.id' } },
70 q{UPDATE tab1, tab2 SET tab1.column1 = value1, tab1.column2 = value2
71 WHERE tab1.id = tab2.id}
78 where => { 'x.id' => { -ident => 'y.x_id' } }
80 q{DELETE FROM x USING y WHERE x.id = y.x_id}
85 select => [ 'x.*', 'y.*' ],
86 from => [ 'x', -join => [ 'y', using => 'y_id' ] ],
88 q{SELECT x.*, y.* FROM x JOIN y USING (y_id)},
94 from => [ { -select => { select => '*', from => 'y' } }, -as => 'x' ],
96 q{SELECT x.* FROM (SELECT * FROM y) AS x},
102 select => { select => '*', from => 'bar' }
104 q{INSERT INTO foo SELECT * FROM bar}
107 ($sql, @bind) = $sqlac->insert({
109 rowvalues => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
114 q{INSERT INTO eh VALUES (?, ?), (?, ?), (?, ?)},
122 where => { -not_exists => {
126 where => { 'foo.id' => { -ident => 'bar.foo_id' } }
131 WHERE NOT EXISTS (SELECT 1 FROM bar WHERE foo.id = bar.foo_id)},
139 '=' => { -select => { select => { -max => 'id' }, from => 'foo' } }
142 q{SELECT * FROM foo WHERE id = (SELECT MAX(id) FROM foo)},
146 my $sqlac = $sqlac->clone
149 $sqlac->clauses_of('select'),
154 ($sql, @bind) = $sqlac->select({
163 q{SELECT * FROM foo LIMIT ? OFFSET ?}, [ 10, 20 ]
167 ($sql) = $sqlac->select({
168 select => { -as => [ 1, 'x' ] },
169 union => { -select => { select => { -as => [ 2, 'x' ] } } },
170 order_by => { -desc => 'x' },
175 q{(SELECT 1 AS x) UNION (SELECT 2 AS x) ORDER BY x DESC},