mojolicious / mojo-pg

Mojolicious :heart: PostgreSQL
https://metacpan.org/release/Mojo-Pg
Artistic License 2.0
101 stars 46 forks source link

add support for multi-column joins to SQL::Abstract::Pg #54

Closed rsindlin closed 5 years ago

rsindlin commented 5 years ago

I have a need to do SQL joins on tables using composite keys, but the current SQL::Abstract::Pg module only supports joining on a single key. This adds support for multiple keys, with the following syntax.

$pg->abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id', 'foo.id2' => 'bar.foo_id2']]);

to yield

SELECT * FROM "foo" JOIN "bar" ON ("foo"."id" = "bar"."foo_id" AND "foo"."id2" = "bar"."foo_id2")

It's fairly straightforward, but I have one concern worth considering:

The argument to the select (the $source argument) is a reference to an array. In SQL::Abstract, the general convention is "things in arrays are OR'ed, and things in hashes are AND'ed". This PR assumes AND'ing the keys used in the join. It suits my needs, but is this a serious infraction of the SQL::Abstract convention, or does it not need to apply because this is specific to the join condition?