Forked from jjn1056/gist:5b905c6ee1c0722906a1a63debb44396
Last active
May 2, 2019 19:28
-
-
Save shadowcat-mst/c8c64a77c6c503fa4290c2eab5f0d821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Test::More; | |
use Test::Warn; | |
use Test::Exception; | |
use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where dumper) ]; | |
use SQL::Abstract; | |
use SQL::Abstract::ExtraClauses; | |
ok my $sqlac = SQL::Abstract::ExtraClauses->new(unknown_unop_always_func => 1); | |
{ | |
=over | |
with faculty as ( | |
select p.person_id, p.email | |
from person p | |
where person_type='faculty' | |
and person_status != 'pending' | |
and default_license_id IS NULL | |
), | |
grandfather_license as ( | |
insert into license(kind, expires_on, valid_from) | |
select 'grandfather', '2017-06-30', '2016-07-01' from faculty | |
returning license_id ) | |
insert into license_person( | |
person_id, | |
license_id) | |
select person_id, license_id from | |
( select license_id, row_number() over(order by license_id) as index from grandfather_license) as a, | |
(select person_id, row_number() over(order by person_id) as index from faculty) as b | |
where a.index = b.index; | |
=cut | |
my $sql = $sqlac->insert({ | |
with => [ | |
faculty => { | |
-select => { | |
_ => [qw /p.person p.email/], | |
from => [ person => -as => 'p' ], | |
where => { | |
'p.person_type' => 'faculty', | |
'p.person_status' => { '!=' => 'pending' }, | |
'p.default_license_id' => undef, | |
}, | |
}, | |
}, | |
grandfather => { | |
-insert => { | |
into => 'license', | |
fields => [ qw(kind expires_on valid_from) ], | |
select => { | |
select => [\(qw('grandfather' '2017-06-30' '2016-07-01'))], | |
from => 'faculty', | |
}, | |
returning => 'license_id', | |
} | |
}, | |
], | |
into => 'license_person', | |
fields => [ qw(person_id, license_id) ], | |
select => { | |
_ => ['person_id', 'license_id'], | |
from => ['grandfather'], | |
where => { | |
'a.index' => { -ident => 'b.index' }, | |
}, | |
}, | |
}); | |
warn $sql; | |
} | |
done_testing; | |
__END__ | |
$sql is | |
INSERT INTO license_person(person_id, license_id) SELECT person_id, license_id FROM grandfather WHERE a.index = b.index |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment