Skip to content

Commit

Permalink
use flags for the join type
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 28, 2018
1 parent 6d27bbb commit 37042c1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/Mojo/Pg/Database.pm
Expand Up @@ -503,10 +503,10 @@ L<SQL::Abstract>.
As well as some PostgreSQL specific extensions added by L<SQL::Abstract::Pg>.
# "select * from foo join bar on (bar.foo_id = foo.id)"
$db->select(['foo', ['bar', 'foo_id', 'id']]);
$db->select(['foo', ['bar', foo_id => 'id']]);
# "select * from foo left join bar on (bar.foo_id = foo.id)"
$db->select(['foo', ['bar', 'foo_id', 'id', 'left']]);
$db->select(['foo', [-left => 'bar', foo_id => 'id']]);
# "select * from some_table where foo = 'bar' order by id desc"
$db->select('some_table', '*', {foo => 'bar'}, {order_by => {-desc => 'id'}});
Expand Down
9 changes: 5 additions & 4 deletions lib/SQL/Abstract/Pg.pm
Expand Up @@ -121,9 +121,10 @@ sub _table {

$table = $self->SUPER::_table(\@table);
for my $join (@join) {
my ($name, $fk, $pk, $type) = @$join;
my $type = @$join > 3 ? shift @$join : '';
my ($name, $fk, $pk) = @$join;
$table
.= $self->_sqlcase($type ? " $type join " : ' join ')
.= $self->_sqlcase($type =~ /^-(.+)$/ ? " $1 join " : ' join ')
. $self->_quote($name)
. $self->_sqlcase(' on ') . '('
. $self->_quote("$name.$fk") . ' = '
Expand Down Expand Up @@ -238,10 +239,10 @@ table names, but also array references with tables to generate C<JOIN> clauses
for.
# "select * from foo join bar on (bar.foo_id = foo.id)"
$abstract->select(['foo', ['bar', 'foo_id', 'id']]);
$abstract->select(['foo', ['bar', foo_id => 'id']]);
# "select * from foo left join bar on (bar.foo_id = foo.id)"
$abstract->select(['foo', ['bar', 'foo_id', 'id', 'left']]);
$abstract->select(['foo', [-left => 'bar', foo_id => 'id']]);
=head1 METHODS
Expand Down
2 changes: 1 addition & 1 deletion t/crud.t
Expand Up @@ -157,7 +157,7 @@ $db->query(
);
$db->insert('crud_test4', {test1 => 'hello'});
$db->insert('crud_test5', {test2 => 'world'});
is_deeply $db->select(['crud_test4', ['crud_test5', 'id', 'id']])
is_deeply $db->select(['crud_test4', ['crud_test5', id => 'id']])
->hashes->to_array, [{id => 1, test1 => 'hello', test2 => 'world'}],
'right structure';

Expand Down
6 changes: 3 additions & 3 deletions t/sql.t
Expand Up @@ -102,19 +102,19 @@ eval { $abstract->select('foo', '*', undef, {for => []}) };
like $@, qr/ARRAYREF/, 'right error';

# JOIN
@sql = $abstract->select(['foo', ['bar', 'foo_id', 'id']]);
@sql = $abstract->select(['foo', ['bar', foo_id => 'id']]);
is_deeply \@sql,
['SELECT * FROM "foo" JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'],
'right query';
@sql = $abstract->select(
['foo', ['bar', 'foo_id', 'id'], ['baz', 'foo_id', 'id']]);
['foo', ['bar', foo_id => 'id'], ['baz', foo_id => 'id']]);
$result
= [ 'SELECT * FROM "foo"'
. ' JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'
. ' JOIN "baz" ON ("baz"."foo_id" = "foo"."id")'
];
is_deeply \@sql, $result, 'right query';
@sql = $abstract->select(['foo', ['bar', 'foo_id', 'id', 'left']]);
@sql = $abstract->select(['foo', [-left => 'bar', foo_id => 'id']]);
is_deeply \@sql,
['SELECT * FROM "foo" LEFT JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'],
'right query';
Expand Down

0 comments on commit 37042c1

Please sign in to comment.