From: Dagfinn Ilmari Mannsåker Date: Sat, 26 Jan 2013 18:32:09 +0000 (+0000) Subject: Add support for NULLS FIRST/LAST in ORDER BY X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Forder-by-nulls;p=dbsrgits%2FSQL-Abstract.git Add support for NULLS FIRST/LAST in ORDER BY --- diff --git a/Changes b/Changes index 0d8df7b..3d61c0a 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ Revision history for SQL::Abstract - Fix ->insert($table, \@values) with >26 values (RT#112684) - Teach ::Tree that ILIKE (PostgreSQL) and REGEXP (MySQL) are binary ops - Support for UPDATE ... RETURNING + - Add support for NULLS FIRST/LAST in ORDER BY revision 1.81 2014-10-25 ---------------------------- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 9741efb..7c6def5 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -1338,16 +1338,24 @@ sub _order_by_chunks { SCALARREF => sub {$$arg}, # literal SQL, no quoting HASHREF => sub { - # get first pair in hash - my ($key, $val, @rest) = %$arg; - - return () unless $key; - - if ( @rest or not $key =~ /^-(desc|asc)/i ) { - puke "hash passed to _order_by must have exactly one key (-desc or -asc)"; + return () unless %$arg; + + my ($direction, $nulls, $val); + foreach my $key (keys %$arg) { + if ( $key =~ /^-(desc|asc)/i ) { + puke "hash passed to _order_by must have exactly one of -desc or -asc" + if defined $direction; + $direction = $1; + $val = $arg->{$key}; + } elsif ($key =~ /^-nulls$/i) { + $nulls = $arg->{$key}; + puke "invalid value for -nulls" unless $nulls =~ /\A(?:first|last)\z/i; + } else { + puke "invalid key in hash passed to _order_by"; + } } - - my $direction = $1; + puke "hash passed to _order_by must have exactly one of -desc or -asc" + unless defined $direction; my @ret; for my $c ($self->_order_by_chunks ($val)) { @@ -1362,7 +1370,9 @@ sub _order_by_chunks { }, }); - $sql = $sql . ' ' . $self->_sqlcase($direction); + $sql .= ' ' . $self->_sqlcase($direction); + $sql .= ' ' . $self->_sqlcase("nulls $nulls") + if defined $nulls; push @ret, [ $sql, @bind]; } @@ -2905,6 +2915,7 @@ script. Some functions take an order by clause. This can either be a scalar (just a column name,) a hash of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } >>, +optionally with C<< -nulls => 'first' >> or C<< -nulls => 'last' >>, or an array of either of the two previous forms. Examples: Given | Will Generate @@ -2920,10 +2931,20 @@ or an array of either of the two previous forms. Examples: | {-desc => 'colB'} | ORDER BY colB DESC | + { | + -asc => 'colA', | ORDER BY colA ASC NULLS LAST + -nulls => 'last', | + } | + | ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC | { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC | + { | + -asc => [qw/colA colB/] | ORDER BY colA ASC NULLS FIRST, + -nulls => 'first' | colB ASC NULLS FIRST + } | + | [ | { -asc => 'colA' }, | ORDER BY colA ASC, colB DESC, { -desc => [qw/colB/], | colC ASC, colD ASC diff --git a/t/06order_by.t b/t/06order_by.t index 42abaa6..4fa2855 100644 --- a/t/06order_by.t +++ b/t/06order_by.t @@ -102,6 +102,21 @@ my @cases = expects_quoted => ' ORDER BY colA ASC, colB LIKE ? DESC, colC LIKE ? ASC', bind => [qw/test tost/], }, + { + given => [ { -asc => 'colA', -nulls => 'first' }, { -desc => 'colB', -nulls => 'last' } ], + expects => ' ORDER BY colA ASC NULLS FIRST, colB DESC NULLS LAST', + expects_quoted => ' ORDER BY `colA` ASC NULLS FIRST, `colB` DESC NULLS LAST', + }, + { + given => [ { -asc => 'colA', -nulls => 'first' }, { -desc => 'colB', -nulls => 'last' } ], + expects => ' ORDER BY colA ASC NULLS FIRST, colB DESC NULLS LAST', + expects_quoted => ' ORDER BY `colA` ASC NULLS FIRST, `colB` DESC NULLS LAST', + }, + { + given => { -asc => [qw/colA colB/], -nulls => 'first' } , + expects => ' ORDER BY colA ASC NULLS FIRST, colB ASC NULLS FIRST', + expects_quoted => ' ORDER BY `colA` ASC NULLS FIRST, `colB` ASC NULLS FIRST', + }, ); my $sql = SQL::Abstract->new; @@ -129,14 +144,26 @@ for my $case( @cases) { throws_ok ( sub { $sql->_order_by({-desc => 'colA', -asc => 'colB' }) }, - qr/hash passed .+ must have exactly one key/, + qr/hash passed .+ must have exactly one of/, 'Undeterministic order exception', ); throws_ok ( sub { $sql->_order_by({-desc => [ qw/colA colB/ ], -asc => [ qw/colC colD/ ] }) }, - qr/hash passed .+ must have exactly one key/, + qr/hash passed .+ must have exactly one of/, 'Undeterministic order exception', ); +throws_ok( + sub { $sql->_order_by({-wibble => "fleem" }) }, + qr/invalid key in hash/, + 'Invalid order exception', +); + +throws_ok( + sub { $sql->_order_by({-nulls => "fleem" }) }, + qr/invalid value for -nulls/, + 'Invalid nulls exception', +); + done_testing;