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;
10 SQL::Abstract::ExtraClauses->apply_to($sqlac);
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 ] }
32 SELECT artist.id, artist.name, JSON_AGG(cd)
33 FROM artists AS artist JOIN cds AS cd ON cd.artist_id = artist.id
34 WHERE artist.genres @> ?
36 HAVING COUNT(cd.id) > ?
42 ($sql) = $sqlac->select({
44 from => [ { -values => [ [ 1, 2 ], [ 3, 4 ] ] }, -as => [ qw(t a b) ] ],
47 is_same_sql($sql, q{SELECT a FROM (VALUES (1, 2), (3, 4)) AS t(a,b)});
49 ($sql) = $sqlac->update({
50 update => 'employees',
51 set => { sales_count => { sales_count => { '+', \1 } } },
54 'accounts.name' => { '=' => \"'Acme Corporation'" },
55 'employees.id' => { -ident => 'accounts.sales_person' },
61 q{UPDATE employees SET sales_count = sales_count + 1 FROM accounts
62 WHERE accounts.name = 'Acme Corporation'
63 AND employees.id = accounts.sales_person
67 ($sql) = $sqlac->update({
68 update => [ qw(tab1 tab2) ],
70 'tab1.column1' => { -ident => 'value1' },
71 'tab1.column2' => { -ident => 'value2' },
73 where => { 'tab1.id' => { -ident => 'tab2.id' } },
78 q{UPDATE tab1, tab2 SET tab1.column1 = value1, tab1.column2 = value2
79 WHERE tab1.id = tab2.id}
86 where => { 'x.id' => { -ident => 'y.x_id' } }
88 q{DELETE FROM x USING y WHERE x.id = y.x_id}
93 select => [ 'x.*', 'y.*' ],
94 from => [ 'x', -join => [ 'y', using => 'y_id' ] ],
96 q{SELECT x.*, y.* FROM x JOIN y USING (y_id)},
102 from => [ { -select => { select => '*', from => 'y' } }, -as => 'x' ],
104 q{SELECT x.* FROM (SELECT * FROM y) AS x},
110 select => { select => '*', from => 'bar' }
112 q{INSERT INTO foo SELECT * FROM bar}
115 ($sql, @bind) = $sqlac->insert({
117 rowvalues => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
122 q{INSERT INTO eh VALUES (?, ?), (?, ?), (?, ?)},
130 where => { -not_exists => {
134 where => { 'foo.id' => { -ident => 'bar.foo_id' } }
139 WHERE NOT EXISTS (SELECT 1 FROM bar WHERE foo.id = bar.foo_id)},
147 '=' => { -select => { select => { -max => 'id' }, from => 'foo' } }
150 q{SELECT * FROM foo WHERE id = (SELECT MAX(id) FROM foo)},
154 my $sqlac = $sqlac->clone
157 $sqlac->clauses_of('select'),
162 ($sql, @bind) = $sqlac->select({
171 q{SELECT * FROM foo LIMIT ? OFFSET ?}, [ 10, 20 ]
175 $sql = $sqlac->select({
176 select => { -as => [ \1, 'x' ] },
177 union => { -select => { select => { -as => [ \2, 'x' ] } } },
178 order_by => { -desc => 'x' },
183 q{(SELECT 1 AS x) UNION (SELECT 2 AS x) ORDER BY x DESC},
186 $sql = $sqlac->select({
189 except => { -select => { select => '*', from => 'foo_exclusions' } }
194 q{(SELECT * FROM foo) EXCEPT (SELECT * FROM foo_exclusions)},
197 $sql = $sqlac->select({
198 with => [ foo => { -select => { select => \1 } } ],
205 q{WITH foo AS (SELECT 1) SELECT * FROM foo},
208 $sql = $sqlac->update({
209 _ => [ 'tree_table', -join => {
212 [ tree_with_path => qw(id parent_id path) ],
217 { -cast => { -as => [ id => char => 255 ] } },
221 from => 'tree_table',
222 where => { parent_id => undef },
225 _ => [ qw(t.id t.parent_id),
227 { -concat => [ 'r.path', \q{'/'}, 't.id' ] },
232 tree_table => -as => t =>
234 to => 'tree_with_path',
236 on => { 't.parent_id' => 'r.id' },
243 from => 'tree_with_path'
246 on => { 'tree.id' => 'tree_with_path.id' },
248 set => { path => { -ident => [ qw(tree path) ] } },
254 UPDATE tree_table JOIN (
255 WITH RECURSIVE tree_with_path(id, parent_id, path) AS (
257 SELECT id, parent_id, CAST(id AS char(255)) AS path
259 WHERE parent_id IS NULL
263 SELECT t.id, t.parent_id, CONCAT(r.path, '/', t.id) AS path
265 JOIN tree_with_path AS r ON t.parent_id = r.id
268 SELECT * FROM tree_with_path
270 ON tree.id = tree_with_path.id
276 ($sql, @bind) = $sqlac->insert({
280 _ => [qw /p.person p.email/],
281 from => [ person => -as => 'p' ],
283 'p.person_type' => 'faculty',
284 'p.person_status' => { '!=' => 'pending' },
285 'p.default_license_id' => undef,
292 fields => [ qw(kind expires_on valid_from) ],
294 select => [\(qw('grandfather' '2017-06-30' '2016-07-01'))],
297 returning => 'license_id',
301 into => 'license_person',
302 fields => [ qw(person_id license_id) ],
304 _ => ['person_id', 'license_id'],
305 from => ['grandfather'],
307 'a.index' => { -ident => 'b.index' },
316 SELECT p.person, p.email FROM person AS p
318 p.default_license_id IS NULL
319 AND p.person_status != ?
320 AND p.person_type = ?
323 INSERT INTO license (kind, expires_on, valid_from)
324 SELECT 'grandfather', '2017-06-30', '2016-07-01'
325 FROM faculty RETURNING license_id
326 ) INSERT INTO license_person (person_id, license_id)
327 SELECT person_id, license_id FROM grandfather WHERE a.index = b.index
329 [ qw(pending faculty) ],
333 ($sql, @bind) = $sqlac->delete({
337 _ => [qw/p.person_id email default_license_id/],
339 person => -as => 'p',
341 to => 'license_person',
343 on => { 'lp.person_id' => 'p.person_id' },
348 on => { 'l.license_id' => 'lp.license_id' },
352 'p.person_type' => 'faculty',
353 'p.person_status' => { '!=' => 'pending' },
354 'l.kind' => 'pending',
356 group_by => [qw/ p.person_id /],
357 having => { '>' => [ { -count => 'l.license_id' }, 1 ] }
360 deletable_licenses => {
362 _ => [qw/lp.ctid lp.person_id lp.license_id/],
364 instructors => -as => 'i',
366 to => 'license_person',
368 on => { 'lp.person_id' => 'i.person_id' },
373 on => { 'l.license_id' => 'lp.license_id' },
378 '<>' => {-ident => 'i.default_license_id'}
380 'l.kind' => 'pending',
385 from => 'license_person',
391 from => 'deletable_licenses',
401 with instructors as (
402 select p.person_id, email, default_license_id
404 join license_person as lp on lp.person_id = p.person_id
405 join license as l on l.license_id = lp.license_id
407 AND p.person_status != ?
408 AND p.person_type = ?
410 having COUNT(l.license_id) > ?),
411 deletable_licenses as (
412 select lp.ctid, lp.person_id, lp.license_id
413 from instructors as i
414 join license_person as lp on lp.person_id = i.person_id
415 join license as l on l.license_id = lp.license_id
417 and lp.license_id <> i.default_license_id
419 delete from license_person
421 (select ctid from deletable_licenses)
425 pending pending faculty 1 pending
429 ($sql, @bind) = $sqlac->update({
432 license_id => { -ident => 'info.default_license_id' },
436 select => [qw( s.survey_id p.default_license_id p.person_id)],
438 person => -as => 'p',
442 on => { 'c.faculty_id' => 'p.person_id' },
447 on => { 's.class_id' => 'c.class_id' },
450 where => { 'p.institution_id' => { -value => 15031 } },
455 'info.survey_id' => { -ident => 'survey.survey_id' },
463 set license_id=info.default_license_id
465 select s.survey_id, p.default_license_id, p.person_id
467 join class AS c on c.faculty_id = p.person_id
468 join survey AS s on s.class_id = c.class_id
469 where p.institution_id = ?
471 where info.survey_id=survey.survey_id