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(
8 unknown_unop_always_func => 1,
9 lazy_join_sql_parts => 1,
13 [ $sqlac->statement_list ],
14 [ sort qw(select update insert delete) ],
17 my ($sql, @bind) = $sqlac->select({
18 select => [ qw(artist.id artist.name), { -json_agg => 'cd' } ],
20 { artists => { -as => 'artist' } },
21 -join => [ cds => as => 'cd' => on => { 'cd.artist_id' => 'artist.id' } ],
23 where => { 'artist.genres', => { '@>', { -value => [ 'Rock' ] } } },
24 order_by => 'artist.name',
25 group_by => 'artist.id',
26 having => { '>' => [ { -count => 'cd.id' }, 3 ] }
29 ::Dwarn([ $sql->formatter->_simplify($sql->to_array), $sql->stringify ]);
34 SELECT artist.id, artist.name, JSON_AGG(cd)
35 FROM artists AS artist JOIN cds AS cd ON cd.artist_id = artist.id
36 WHERE artist.genres @> ?
38 HAVING COUNT(cd.id) > ?
44 ($sql) = $sqlac->select({
46 from => [ { -values => [ [ 1, 2 ], [ 3, 4 ] ] }, -as => [ qw(t a b) ] ],
49 is_same_sql($sql, q{SELECT a FROM (VALUES (1, 2), (3, 4)) AS t(a,b)});
51 ($sql) = $sqlac->update({
52 update => 'employees',
53 set => { sales_count => { sales_count => { '+', \1 } } },
56 'accounts.name' => { '=' => \"'Acme Corporation'" },
57 'employees.id' => { -ident => 'accounts.sales_person' },
63 q{UPDATE employees SET sales_count = sales_count + 1 FROM accounts
64 WHERE accounts.name = 'Acme Corporation'
65 AND employees.id = accounts.sales_person
69 ($sql) = $sqlac->update({
70 update => [ qw(tab1 tab2) ],
72 'tab1.column1' => { -ident => 'value1' },
73 'tab1.column2' => { -ident => 'value2' },
75 where => { 'tab1.id' => { -ident => 'tab2.id' } },
80 q{UPDATE tab1, tab2 SET tab1.column1 = value1, tab1.column2 = value2
81 WHERE tab1.id = tab2.id}
88 where => { 'x.id' => { -ident => 'y.x_id' } }
90 q{DELETE FROM x USING y WHERE x.id = y.x_id}
95 select => [ 'x.*', 'y.*' ],
96 from => [ 'x', -join => [ 'y', using => 'y_id' ] ],
98 q{SELECT x.*, y.* FROM x JOIN y USING (y_id)},
104 from => [ { -select => { select => '*', from => 'y' } }, -as => 'x' ],
106 q{SELECT x.* FROM (SELECT * FROM y) AS x},
112 select => { select => '*', from => 'bar' }
114 q{INSERT INTO foo SELECT * FROM bar}
117 ($sql, @bind) = $sqlac->insert({
119 rowvalues => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
124 q{INSERT INTO eh VALUES (?, ?), (?, ?), (?, ?)},
132 where => { -not_exists => {
136 where => { 'foo.id' => { -ident => 'bar.foo_id' } }
141 WHERE NOT EXISTS (SELECT 1 FROM bar WHERE foo.id = bar.foo_id)},
149 '=' => { -select => { select => { -max => 'id' }, from => 'foo' } }
152 q{SELECT * FROM foo WHERE id = (SELECT MAX(id) FROM foo)},
156 my $sqlac = $sqlac->clone
159 $sqlac->clauses_of('select'),
164 ($sql, @bind) = $sqlac->select({
173 q{SELECT * FROM foo LIMIT ? OFFSET ?}, [ 10, 20 ]
177 $sql = $sqlac->select({
178 select => { -as => [ \1, 'x' ] },
179 union => { -select => { select => { -as => [ \2, 'x' ] } } },
180 order_by => { -desc => 'x' },
185 q{(SELECT 1 AS x) UNION (SELECT 2 AS x) ORDER BY x DESC},
188 $sql = $sqlac->select({
191 except => { -select => { select => '*', from => 'foo_exclusions' } }
196 q{(SELECT * FROM foo) EXCEPT (SELECT * FROM foo_exclusions)},
199 $sql = $sqlac->select({
200 with => [ foo => { -select => { select => \1 } } ],
207 q{WITH foo AS (SELECT 1) SELECT * FROM foo},
210 $sql = $sqlac->update({
211 _ => [ 'tree_table', -join => {
214 [ tree_with_path => qw(id parent_id path) ],
219 { -cast => { -as => [ id => char => 255 ] } },
223 from => 'tree_table',
224 where => { parent_id => undef },
227 _ => [ qw(t.id t.parent_id),
229 { -concat => [ 'r.path', \q{'/'}, 't.id' ] },
234 tree_table => -as => t =>
236 to => 'tree_with_path',
238 on => { 't.parent_id' => 'r.id' },
245 from => 'tree_with_path'
248 on => { 'tree.id' => 'tree_with_path.id' },
250 set => { path => { -ident => [ qw(tree path) ] } },
256 UPDATE tree_table JOIN (
257 WITH RECURSIVE tree_with_path(id, parent_id, path) AS (
259 SELECT id, parent_id, CAST(id AS char(255)) AS path
261 WHERE parent_id IS NULL
265 SELECT t.id, t.parent_id, CONCAT(r.path, '/', t.id) AS path
267 JOIN tree_with_path AS r ON t.parent_id = r.id
270 SELECT * FROM tree_with_path
272 ON tree.id = tree_with_path.id
278 ($sql, @bind) = $sqlac->insert({
282 _ => [qw /p.person p.email/],
283 from => [ person => -as => 'p' ],
285 'p.person_type' => 'faculty',
286 'p.person_status' => { '!=' => 'pending' },
287 'p.default_license_id' => undef,
294 fields => [ qw(kind expires_on valid_from) ],
296 select => [\(qw('grandfather' '2017-06-30' '2016-07-01'))],
299 returning => 'license_id',
303 into => 'license_person',
304 fields => [ qw(person_id license_id) ],
306 _ => ['person_id', 'license_id'],
307 from => ['grandfather'],
309 'a.index' => { -ident => 'b.index' },
318 SELECT p.person, p.email FROM person AS p
320 p.default_license_id IS NULL
321 AND p.person_status != ?
322 AND p.person_type = ?
325 INSERT INTO license (kind, expires_on, valid_from)
326 SELECT 'grandfather', '2017-06-30', '2016-07-01'
327 FROM faculty RETURNING license_id
328 ) INSERT INTO license_person (person_id, license_id)
329 SELECT person_id, license_id FROM grandfather WHERE a.index = b.index
331 [ qw(pending faculty) ],
335 ($sql, @bind) = $sqlac->delete({
339 _ => [qw/p.person_id email default_license_id/],
341 person => -as => 'p',
343 to => 'license_person',
345 on => { 'lp.person_id' => 'p.person_id' },
350 on => { 'l.license_id' => 'lp.license_id' },
354 'p.person_type' => 'faculty',
355 'p.person_status' => { '!=' => 'pending' },
356 'l.kind' => 'pending',
358 group_by => [qw/ p.person_id /],
359 having => { '>' => [ { -count => 'l.license_id' }, 1 ] }
362 deletable_licenses => {
364 _ => [qw/lp.ctid lp.person_id lp.license_id/],
366 instructors => -as => 'i',
368 to => 'license_person',
370 on => { 'lp.person_id' => 'i.person_id' },
375 on => { 'l.license_id' => 'lp.license_id' },
380 '<>' => {-ident => 'i.default_license_id'}
382 'l.kind' => 'pending',
387 from => 'license_person',
393 from => 'deletable_licenses',
403 with instructors as (
404 select p.person_id, email, default_license_id
406 join license_person as lp on lp.person_id = p.person_id
407 join license as l on l.license_id = lp.license_id
409 AND p.person_status != ?
410 AND p.person_type = ?
412 having COUNT(l.license_id) > ?),
413 deletable_licenses as (
414 select lp.ctid, lp.person_id, lp.license_id
415 from instructors as i
416 join license_person as lp on lp.person_id = i.person_id
417 join license as l on l.license_id = lp.license_id
419 and lp.license_id <> i.default_license_id
421 delete from license_person
423 (select ctid from deletable_licenses)
427 pending pending faculty 1 pending
431 ($sql, @bind) = $sqlac->update({
434 license_id => { -ident => 'info.default_license_id' },
438 select => [qw( s.survey_id p.default_license_id p.person_id)],
440 person => -as => 'p',
444 on => { 'c.faculty_id' => 'p.person_id' },
449 on => { 's.class_id' => 'c.class_id' },
452 where => { 'p.institution_id' => { -value => 15031 } },
457 'info.survey_id' => { -ident => 'survey.survey_id' },
465 set license_id=info.default_license_id
467 select s.survey_id, p.default_license_id, p.person_id
469 join class AS c on c.faculty_id = p.person_id
470 join survey AS s on s.class_id = c.class_id
471 where p.institution_id = ?
473 where info.survey_id=survey.survey_id