Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
12 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
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,65 @@ | ||
package SQL::Abstract::Pg; | ||
use Mojo::Base 'SQL::Abstract'; | ||
|
||
sub _order_by { | ||
my ($self, $arg) = @_; | ||
|
||
# Legacy | ||
return $self->SUPER::_order_by($arg) | ||
if ref $arg ne 'HASH' | ||
or grep {/^-(?:desc|asc)/i} keys %$arg; | ||
|
||
return $self->_parse($arg); | ||
} | ||
|
||
sub _parse { | ||
my ($self, $options) = @_; | ||
|
||
# ORDER BY | ||
my $sql = ''; | ||
my @bind; | ||
if (exists $options->{order_by}) { | ||
$sql .= $self->_order_by($options->{order_by}); | ||
} | ||
|
||
# LIMIT | ||
if (defined $options->{limit}) { | ||
$sql .= ' LIMIT ?'; | ||
push @bind, $options->{limit}; | ||
} | ||
|
||
# OFFSET | ||
if (defined $options->{offset}) { | ||
$sql .= ' OFFSET ?'; | ||
push @bind, $options->{offset}; | ||
} | ||
|
||
return $sql, @bind; | ||
} | ||
|
||
1; | ||
|
||
=encoding utf8 | ||
=head1 NAME | ||
SQL::Abstract::Pg - PostgreSQL Magic | ||
=head1 SYNOPSIS | ||
my $abstract = SQL::Abstract::Pg->new; | ||
=head1 DESCRIPTION | ||
L<SQL::Abstract::Pg> extends L<SQL::Abstract> with a few PostgreSQL features | ||
used by L<Mojo::Pg>. | ||
=head1 METHODS | ||
L<SQL::Abstract::Pg> inherits all methods from L<SQL::Abstract>. | ||
=head1 SEE ALSO | ||
L<Mojo::Pg>, L<Mojolicious::Guides>, L<http://mojolicious.org>. | ||
=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
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,27 @@ | ||
use Mojo::Base -strict; | ||
|
||
use Test::More; | ||
use Mojo::Pg; | ||
|
||
# Basics | ||
my $pg = Mojo::Pg->new; | ||
my $abstract = $pg->abstract; | ||
is_deeply [$abstract->select('foo', '*')], ['SELECT * FROM "foo"'], | ||
'right query'; | ||
|
||
# ORDER BY | ||
my @sql = $abstract->select('foo', '*', {bar => 'baz'}, {-desc => 'yada'}); | ||
is_deeply \@sql, | ||
['SELECT * FROM "foo" WHERE ( "bar" = ? ) ORDER BY "yada" DESC', 'baz'], | ||
'right query'; | ||
@sql = $abstract->select('foo', '*', {bar => 'baz'}, | ||
{order_by => {-desc => 'yada'}}); | ||
is_deeply \@sql, | ||
['SELECT * FROM "foo" WHERE ( "bar" = ? ) ORDER BY "yada" DESC', 'baz'], | ||
'right query'; | ||
|
||
# LIMIT/OFFSET | ||
@sql = $abstract->select('foo', '*', undef, {limit => 10, offset => 5}); | ||
is_deeply \@sql, ['SELECT * FROM "foo" LIMIT ? OFFSET ?', 10, 5], 'right query'; | ||
|
||
done_testing(); |