4 use SQL::Abstract::Test import => [ qw(is_same_sql_bind is_same_sql) ];
6 use SQL::Abstract::ExtraClauses;
8 my $sqlac = SQL::Abstract->new(
9 unknown_unop_always_func => 1,
10 lazy_join_sql_parts => 1,
13 SQL::Abstract::ExtraClauses->apply_to($sqlac);
16 [ $sqlac->statement_list ],
17 [ sort qw(select update insert delete) ],
20 my ($sql, @bind) = $sqlac->select({
21 select => [ qw(artist.id artist.name), { -json_agg => 'cd' } ],
23 { artists => { -as => 'artist' } },
24 -join => [ cds => as => 'cd' => on => { 'cd.artist_id' => 'artist.id' } ],
26 where => { 'artist.genres', => { '@>', { -value => [ 'Rock' ] } } },
27 order_by => 'artist.name',
28 group_by => 'artist.id',
29 having => { '>' => [ { -count => 'cd.id' }, 3 ] }
35 SELECT artist.id, artist.name, JSON_AGG(cd)
36 FROM artists AS artist JOIN cds AS cd ON cd.artist_id = artist.id
37 WHERE artist.genres @> ?
39 HAVING COUNT(cd.id) > ?
45 ($sql) = $sqlac->select({
47 from => [ { -values => [ [ 1, 2 ], [ 3, 4 ] ] }, -as => [ qw(t a b) ] ],
50 is_same_sql($sql, q{SELECT a FROM (VALUES (1, 2), (3, 4)) AS t(a,b)});
52 ($sql) = $sqlac->update({
53 update => 'employees',
54 set => { sales_count => { sales_count => { '+', \1 } } },
57 'accounts.name' => { '=' => \"'Acme Corporation'" },
58 'employees.id' => { -ident => 'accounts.sales_person' },
64 q{UPDATE employees SET sales_count = sales_count + 1 FROM accounts
65 WHERE accounts.name = 'Acme Corporation'
66 AND employees.id = accounts.sales_person
70 ($sql) = $sqlac->update({
71 update => [ qw(tab1 tab2) ],
73 'tab1.column1' => { -ident => 'value1' },
74 'tab1.column2' => { -ident => 'value2' },
76 where => { 'tab1.id' => { -ident => 'tab2.id' } },
81 q{UPDATE tab1, tab2 SET tab1.column1 = value1, tab1.column2 = value2
82 WHERE tab1.id = tab2.id}
89 where => { 'x.id' => { -ident => 'y.x_id' } }
91 q{DELETE FROM x USING y WHERE x.id = y.x_id}
96 select => [ 'x.*', 'y.*' ],
97 from => [ 'x', -join => [ 'y', using => 'y_id' ] ],
99 q{SELECT x.*, y.* FROM x JOIN y USING (y_id)},
105 from => [ { -select => { select => '*', from => 'y' } }, -as => 'x' ],
107 q{SELECT x.* FROM (SELECT * FROM y) AS x},
113 select => { select => '*', from => 'bar' }
115 q{INSERT INTO foo SELECT * FROM bar}
118 ($sql, @bind) = $sqlac->insert({
120 rowvalues => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
125 q{INSERT INTO eh VALUES (?, ?), (?, ?), (?, ?)},
133 where => { -not_exists => {
137 where => { 'foo.id' => { -ident => 'bar.foo_id' } }
142 WHERE NOT EXISTS (SELECT 1 FROM bar WHERE foo.id = bar.foo_id)},
150 '=' => { -select => { select => { -max => 'id' }, from => 'foo' } }
153 q{SELECT * FROM foo WHERE id = (SELECT MAX(id) FROM foo)},
157 my $sqlac = $sqlac->clone
160 $sqlac->clauses_of('select'),
165 ($sql, @bind) = $sqlac->select({
174 q{SELECT * FROM foo LIMIT ? OFFSET ?}, [ 10, 20 ]
178 $sql = $sqlac->select({
179 select => { -as => [ \1, 'x' ] },
180 union => { -select => { select => { -as => [ \2, 'x' ] } } },
181 order_by => { -desc => 'x' },
186 q{(SELECT 1 AS x) UNION (SELECT 2 AS x) ORDER BY x DESC},
189 $sql = $sqlac->select({
192 except => { -select => { select => '*', from => 'foo_exclusions' } }
197 q{(SELECT * FROM foo) EXCEPT (SELECT * FROM foo_exclusions)},
200 $sql = $sqlac->select({
201 with => [ foo => { -select => { select => \1 } } ],
208 q{WITH foo AS (SELECT 1) SELECT * FROM foo},
211 $sql = $sqlac->update({
212 _ => [ 'tree_table', -join => {
215 [ tree_with_path => qw(id parent_id path) ],
220 { -cast => { -as => [ id => char => 255 ] } },
224 from => 'tree_table',
225 where => { parent_id => undef },
228 _ => [ qw(t.id t.parent_id),
230 { -concat => [ 'r.path', \q{'/'}, 't.id' ] },
235 tree_table => -as => t =>
237 to => 'tree_with_path',
239 on => { 't.parent_id' => 'r.id' },
246 from => 'tree_with_path'
249 on => { 'tree.id' => 'tree_with_path.id' },
251 set => { path => { -ident => [ qw(tree path) ] } },
257 UPDATE tree_table JOIN (
258 WITH RECURSIVE tree_with_path(id, parent_id, path) AS (
260 SELECT id, parent_id, CAST(id AS char(255)) AS path
262 WHERE parent_id IS NULL
266 SELECT t.id, t.parent_id, CONCAT(r.path, '/', t.id) AS path
268 JOIN tree_with_path AS r ON t.parent_id = r.id
271 SELECT * FROM tree_with_path
273 ON tree.id = tree_with_path.id
279 ($sql, @bind) = $sqlac->insert({
283 _ => [qw /p.person p.email/],
284 from => [ person => -as => 'p' ],
286 'p.person_type' => 'faculty',
287 'p.person_status' => { '!=' => 'pending' },
288 'p.default_license_id' => undef,
295 fields => [ qw(kind expires_on valid_from) ],
297 select => [\(qw('grandfather' '2017-06-30' '2016-07-01'))],
300 returning => 'license_id',
304 into => 'license_person',
305 fields => [ qw(person_id license_id) ],
307 _ => ['person_id', 'license_id'],
308 from => ['grandfather'],
310 'a.index' => { -ident => 'b.index' },
319 SELECT p.person, p.email FROM person AS p
321 p.default_license_id IS NULL
322 AND p.person_status != ?
323 AND p.person_type = ?
326 INSERT INTO license (kind, expires_on, valid_from)
327 SELECT 'grandfather', '2017-06-30', '2016-07-01'
328 FROM faculty RETURNING license_id
329 ) INSERT INTO license_person (person_id, license_id)
330 SELECT person_id, license_id FROM grandfather WHERE a.index = b.index
332 [ qw(pending faculty) ],
336 ($sql, @bind) = $sqlac->delete({
340 _ => [qw/p.person_id email default_license_id/],
342 person => -as => 'p',
344 to => 'license_person',
346 on => { 'lp.person_id' => 'p.person_id' },
351 on => { 'l.license_id' => 'lp.license_id' },
355 'p.person_type' => 'faculty',
356 'p.person_status' => { '!=' => 'pending' },
357 'l.kind' => 'pending',
359 group_by => [qw/ p.person_id /],
360 having => { '>' => [ { -count => 'l.license_id' }, 1 ] }
363 deletable_licenses => {
365 _ => [qw/lp.ctid lp.person_id lp.license_id/],
367 instructors => -as => 'i',
369 to => 'license_person',
371 on => { 'lp.person_id' => 'i.person_id' },
376 on => { 'l.license_id' => 'lp.license_id' },
381 '<>' => {-ident => 'i.default_license_id'}
383 'l.kind' => 'pending',
388 from => 'license_person',
394 from => 'deletable_licenses',
404 with instructors as (
405 select p.person_id, email, default_license_id
407 join license_person as lp on lp.person_id = p.person_id
408 join license as l on l.license_id = lp.license_id
410 AND p.person_status != ?
411 AND p.person_type = ?
413 having COUNT(l.license_id) > ?),
414 deletable_licenses as (
415 select lp.ctid, lp.person_id, lp.license_id
416 from instructors as i
417 join license_person as lp on lp.person_id = i.person_id
418 join license as l on l.license_id = lp.license_id
420 and lp.license_id <> i.default_license_id
422 delete from license_person
424 (select ctid from deletable_licenses)
428 pending pending faculty 1 pending
432 ($sql, @bind) = $sqlac->update({
435 license_id => { -ident => 'info.default_license_id' },
439 select => [qw( s.survey_id p.default_license_id p.person_id)],
441 person => -as => 'p',
445 on => { 'c.faculty_id' => 'p.person_id' },
450 on => { 's.class_id' => 'c.class_id' },
453 where => { 'p.institution_id' => { -value => 15031 } },
458 'info.survey_id' => { -ident => 'survey.survey_id' },
466 set license_id=info.default_license_id
468 select s.survey_id, p.default_license_id, p.person_id
470 join class AS c on c.faculty_id = p.person_id
471 join survey AS s on s.class_id = c.class_id
472 where p.institution_id = ?
474 where info.survey_id=survey.survey_id