Skip to content

Commit

Permalink
allow table names to be specified for join conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 28, 2018
1 parent 2158464 commit 9a39471
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/SQL/Abstract/Pg.pm
Expand Up @@ -150,6 +150,7 @@ sub _table {
}

$table = $self->SUPER::_table(\@table);
my $sep = $self->{name_sep} // '';
for my $join (@join) {
puke 'join must be in the form [$table, $fk => $pk]' if @$join < 3;
my $type = @$join > 3 ? shift @$join : '';
Expand All @@ -158,8 +159,8 @@ sub _table {
.= $self->_sqlcase($type =~ /^-(.+)$/ ? " $1 join " : ' join ')
. $self->_quote($name)
. $self->_sqlcase(' on ') . '('
. $self->_quote("$name.$fk") . ' = '
. $self->_quote("$table[0].$pk") . ')';
. $self->_quote(index($fk, $sep) > 0 ? $fk : "$name.$fk") . ' = '
. $self->_quote(index($pk, $sep) > 0 ? $pk : "$table[0].$pk") . ')';
}

return $table;
Expand Down Expand Up @@ -241,6 +242,9 @@ for.
# "select * from foo join bar on (bar.foo_id = foo.id)"
$abstract->select(['foo', ['bar', foo_id => 'id']]);
# "select * from foo join bar on (foo.id = bar.foo_id)"
$abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id']]);
# "select * from a join b on (b.a_id = a.id) join c on (c.a_id = a.id)"
$abstract->select(['a', ['b', a_id => 'id'], ['c', a_id => 'id']]);
Expand Down
4 changes: 4 additions & 0 deletions t/sql.t
Expand Up @@ -138,6 +138,10 @@ like $@, qr/field alias must be in the form \[\$name => \$alias\]/,
is_deeply \@sql,
['SELECT * FROM "foo" JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'],
'right query';
@sql = $abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id']]);
is_deeply \@sql,
['SELECT * FROM "foo" JOIN "bar" ON ("foo"."id" = "bar"."foo_id")'],
'right query';
@sql = $abstract->select(
['foo', ['bar', foo_id => 'id'], ['baz', foo_id => 'id']]);
$result
Expand Down

0 comments on commit 9a39471

Please sign in to comment.