Skip to content

Commit

Permalink
allow for the join type to be specified as well
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 27, 2018
1 parent 71bf0a2 commit 64c97a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/Mojo/Pg/Database.pm
Expand Up @@ -505,6 +505,9 @@ 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']], '*');
# "select * from foo left join bar on (bar.foo_id = foo.id)"
$db->select(['foo', ['bar', 'foo_id', 'id', 'left']]);
# "select * from some_table where foo = 'bar' order by id desc"
$db->select('some_table', '*', {foo => 'bar'}, {order_by => {-desc => 'id'}});
Expand Down
11 changes: 7 additions & 4 deletions lib/SQL/Abstract/Pg.pm
Expand Up @@ -121,13 +121,13 @@ sub _table {

$table = $self->SUPER::_table(\@table);
for my $join (@join) {
my ($name, $pk, $fk) = @$join;
my ($name, $fk, $pk, $type) = @$join;
$table
.= $self->_sqlcase(' join ')
.= $self->_sqlcase($type ? " $type join " : ' join ')
. $self->_quote($name)
. $self->_sqlcase(' on ') . '('
. $self->_quote("$name.$pk") . ' = '
. $self->_quote("$table[0].$fk") . ')';
. $self->_quote("$name.$fk") . ' = '
. $self->_quote("$table[0].$pk") . ')';
}

return $table;
Expand Down Expand Up @@ -240,6 +240,9 @@ for.
# "select * from foo join bar on (bar.foo_id = foo.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']]);
=head1 METHODS
L<SQL::Abstract::Pg> inherits all methods from L<SQL::Abstract>.
Expand Down
4 changes: 4 additions & 0 deletions t/sql.t
Expand Up @@ -114,5 +114,9 @@ $result
. ' JOIN "baz" ON ("baz"."foo_id" = "foo"."id")'
];
is_deeply \@sql, $result, 'right query';
@sql = $abstract->select(['foo', ['bar', 'foo_id', 'id', 'left']]);
is_deeply \@sql,
['SELECT * FROM "foo" LEFT JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'],
'right query';

done_testing();

0 comments on commit 64c97a9

Please sign in to comment.