-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into refactor/change-passthru
- Loading branch information
Showing
10 changed files
with
234 additions
and
29 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
{{$NEXT}} | ||
Initial release; ExtraClauses, BangOverrides and Upsert support! | ||
|
||
0.01_2 2023-05-27T20:34:54Z | ||
|
||
Update hashrefref support to hook _recurse_fields such that join pruning no longer | ||
explodes | ||
|
||
0.01 2023-05-21T15:01:10Z | ||
Initial release; ExtraClauses, BangOverrides, Upsert, WindowFunction and CaseExpr support! | ||
SUPER EXPERIMENTAL! |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,8 @@ | |
"runtime" : { | ||
"requires" : { | ||
"DBIx::Class" : "0.082843", | ||
"SQL::Abstract" : "2.000001" | ||
"SQL::Abstract" : "2.000001", | ||
"perl" : "5.22" | ||
} | ||
}, | ||
"test" : { | ||
|
@@ -66,7 +67,7 @@ | |
"web" : "https://github.com/rabbiveesh/dbic-sqla2" | ||
} | ||
}, | ||
"version" : "0.01", | ||
"version" : "0.01_2", | ||
"x_contributors" : [ | ||
"Roy Storey <[email protected]>", | ||
"Veesh Goldman <[email protected]>" | ||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package SQL::Abstract::Plugin::CaseExpr; | ||
use feature qw/signatures postderef/; | ||
|
||
our $VERSION = '0.01_2'; | ||
use Moo; | ||
with 'SQL::Abstract::Role::Plugin'; | ||
use List::Util qw/pairmap/; | ||
|
||
no warnings 'experimental::signatures'; | ||
|
||
sub register_extensions ($self, $sqla) { | ||
|
||
$sqla->expander( | ||
case => sub ($sqla, $name, $value) { | ||
# if the user passed in the double array-ref, then we assume it's already expanded | ||
return { -case => $value } if ref $value->[0] eq 'ARRAY'; | ||
my $else; | ||
my @conditions = $value->@*; | ||
$else = pop @conditions unless @conditions * %2; | ||
return { | ||
-case => [ | ||
[ map +($sqla->expand_expr($_->{if}, -ident), $sqla->expand_expr($_->{then}, -value)), @conditions ], | ||
$else ? $sqla->expand_expr($else, -value) : () | ||
] | ||
}; | ||
} | ||
); | ||
$sqla->renderer( | ||
case => sub ($sqla, $name, $value) { | ||
my $else = $value->[1]; | ||
$sqla->join_query_parts( | ||
' ', | ||
{ -keyword => 'CASE' }, | ||
(pairmap { ({ -keyword => 'WHEN' }, $a, { -keyword => 'THEN' }, $b) } $value->[0]->@*), | ||
$else ? ({ -keyword => 'ELSE' }, $else) : (), | ||
{ -keyword => 'END' } | ||
); | ||
} | ||
); | ||
|
||
} | ||
|
||
1; | ||
|
||
=encoding utf8 | ||
=head1 NAME | ||
SQL::Abstract::Plugin::CaseExpr - Case Expression support for SQLA2! | ||
=head1 SYNOPSIS | ||
# pass this to anything that SQLA will render | ||
# arrayref b/c order matters | ||
{ -case => [ | ||
# if/then is a bit more familiar than WHEN/THEN | ||
{ | ||
if => { sales => { '>' => 9000 } }, | ||
# scalars default to bind params | ||
then => 'Scouter Breaking' | ||
}, | ||
{ | ||
if => { sales => { '>' => 0 } }, | ||
then => 'At least something' | ||
}, | ||
# if the final node does not contain if, it's the ELSE clause | ||
'How did this happen?' | ||
]} | ||
# CASE WHEN sales > 9000 THEN ? WHEN sales > 0 THEN ? ELSE ? END | ||
# [ 'Scouter Breaking', 'At least something', 'How did this happen?' ] | ||
=head1 DESCRIPTION | ||
This is a work in progress to support CASE expressions in SQLA2 | ||
B<EXPERIMENTAL> | ||
=head2 Using with DBIx::Class | ||
In order to use this with DBIx::Class, you simply need to apply the DBIC-SQLA2 plugin, and | ||
then your SQLMaker will support this syntax! | ||
=head2 New Syntax | ||
=head3 -case node | ||
The entry point for the new handling is the -case node. This takes an arrayref of hashrefs which represent the branches of the conditional tree, and optionally a final entry as the default clause. | ||
The hashrefs must have the following two keys: | ||
=over 4 | ||
=item if | ||
The condition to be checked against. It is processed like a WHERE clause. | ||
=item then | ||
The value to be returned if this condition is true. Scalars here default to -value, which means they are taken as bind parameters | ||
=back | ||
=cut |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ sub register_extensions { | |
); | ||
} | ||
|
||
our $VERSION = '0.01'; | ||
our $VERSION = '0.01_2'; | ||
|
||
1; | ||
|
||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
name = 'DBIx-Class-SQLA2' | ||
|
||
[FileGatherer] | ||
exclude_match = [ '^ideas/*' ] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use File::Temp (); | ||
use lib 't/lib'; | ||
use Local::Schema; | ||
|
||
my $tmpdir = File::Temp->newdir; | ||
my $schema = Local::Schema->connect("dbi:SQLite:$tmpdir/on_conflict.sqlite"); | ||
ok $schema, 'created'; | ||
$schema->storage->ensure_connected; | ||
|
||
# deploy + populate | ||
$schema->deploy({ add_drop_table => 1 }); | ||
$schema->resultset('Artist')->populate([ | ||
{ | ||
artistid => 2, | ||
name => 'Portishead', | ||
albums => | ||
[ { title => 'Portishead', rank => 2 }, { title => 'Dummy', rank => 3 }, { title => 'Third', rank => 4 }, ] | ||
}, | ||
{ artistid => 1, name => 'Stone Roses', albums => [ { title => 'Second Coming', rank => 1 }, ] }, | ||
{ artistid => 3, name => 'LSG' } | ||
]); | ||
|
||
subtest 'using a CASE expression' => sub { | ||
my @oddity = $schema->resultset('Album')->search(undef, { | ||
'columns' => [ | ||
'rank', | ||
'title', | ||
{ oddity => \{ -case => [ | ||
{ if => { -mod => [ 'rank', 2 ] }, then => 'Quite Odd' }, | ||
'Even' | ||
] , | ||
-as => 'oddity'} | ||
}], | ||
order_by => { -asc => 'rank'} | ||
})->all; | ||
|
||
is_deeply \@oddity, [ | ||
{ title => 'Second Coming', rank => 1, oddity => 'Quite Odd'}, | ||
{ title => 'Portishead', rank => 2, oddity => 'Even'}, | ||
{ title => 'Dummy', rank => 3, oddity => 'Quite Odd'}, | ||
{ title => 'Third', rank => 4, oddity => 'Even'}, | ||
], 'got expected result'; | ||
}; | ||
|
||
subtest 'passes through the double arrayref syntax' => sub { | ||
my @oddity = $schema->resultset('Album')->search(undef, { | ||
'columns' => [ | ||
'rank', | ||
'title', | ||
{ oddity => \{ -case => [ | ||
[{ -func => [ 'mod', 'rank', 2 ] } => { -bind => [ undef, 'Quite Odd' ] }], | ||
{ -bind => [ undef, 'Even']} | ||
] , | ||
-as => 'oddity'} | ||
}], | ||
order_by => { -asc => 'rank'} | ||
})->all; | ||
|
||
is_deeply \@oddity, [ | ||
{ title => 'Second Coming', rank => 1, oddity => 'Quite Odd'}, | ||
{ title => 'Portishead', rank => 2, oddity => 'Even'}, | ||
{ title => 'Dummy', rank => 3, oddity => 'Quite Odd'}, | ||
{ title => 'Third', rank => 4, oddity => 'Even'}, | ||
], 'got expected result'; | ||
|
||
}; | ||
|
||
done_testing; |