korma/Korma — pull request #105 by tyano (2013-01-06 10:37:48+00:00)
Title: Fix for delimiting table-names (and supporting queries covering multi-db or multi-schema)

This patch is for fixing the way of delimiting table-names. At same time, we can support Postgres' schema and queries covering multiple databases with this patch.

This patch will fix the issue #98 too.

In MySQL, it is very usual that an query use multiple databases like bellow:

SELECT * FROM db1.table1 LEFT JOIN other_db.table2 ON db1.table1.key = other_db.table2.key;

Korma currently don't support this kind of query well, because Korma don't delimit table-names correctly.

Think about a situation where we use multiple database. We probably define entites with db-name like bellow:

(defentity book (table :mydb.book))
(defentity author (table :mydb.book) (belongs-to book))

and query:

(select author (with book))

Korma will generate an query like:

SELECT "mydb.author".*, "mydb.book".* FROM "mydb.role" LEFT JOIN "mydb.book" ON "mydb.book"."id" = "mydb.role"."book_id"

This query doesn't work. We should delimit table-names like bellow:

SELECT "mydb"."author".*, "mydb"."book".* FROM "mydb"."role" LEFT JOIN "mydb"."book" ON "mydb"."book"."id" = "mydb"."role"."book_id"

If korma delimit table-names as above, users of mysql can use multi-db well, and at same time, users of postgres can use schema-name as well.

With my patch, Korma will delimit table-names like the last example.