Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for delimiting table-names (and supporting queries covering multi-db or multi-schema) #105

Merged
merged 4 commits into from
Jan 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/korma/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[korma.sql.fns :as sfns]
[korma.sql.utils :as utils]
[clojure.set :as set]
[clojure.string :as string]
[korma.db :as db])
(:use [korma.sql.engine :only [bind-query]]))

Expand Down Expand Up @@ -428,18 +429,22 @@
:fields []
:rel {}})

(defn- simple-table-name
[ent]
(last (string/split (:table ent) #"\.")))

(defn create-relation
"Create a relation map describing how two entities are related."
[ent sub-ent type opts]
(let [[pk fk foreign-ent] (case type
:has-one [(raw (eng/prefix ent (:pk ent)))
(raw (eng/prefix sub-ent (keyword (str (:table ent) "_id"))))
(raw (eng/prefix sub-ent (keyword (str (simple-table-name ent) "_id"))))
sub-ent]
:belongs-to [(raw (eng/prefix sub-ent (:pk sub-ent)))
(raw (eng/prefix ent (keyword (str (:table sub-ent) "_id"))))
(raw (eng/prefix ent (keyword (str (simple-table-name sub-ent) "_id"))))
ent]
:has-many [(raw (eng/prefix ent (:pk ent)))
(raw (eng/prefix sub-ent (keyword (str (:table ent) "_id"))))
(raw (eng/prefix sub-ent (keyword (str (simple-table-name ent) "_id"))))
sub-ent])
opts (when (:fk opts)
{:fk (raw (eng/prefix foreign-ent (:fk opts)))})]
Expand Down
10 changes: 8 additions & 2 deletions src/korma/sql/engine.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
(defn table-alias [{:keys [type table alias]}]
(or alias table))

(defn table-identifier [table-name]
(let [parts (string/split table-name #"\.")]
(if-not (next parts)
(delimit-str table-name)
(string/join "." (map delimit-str parts)))))

(defn field-identifier [field]
(cond
(map? field) (map-val field)
Expand All @@ -80,7 +86,7 @@
(let [table (if (string? ent)
ent
(table-alias ent))]
(str (delimit-str table) "." field-name))
(str (table-identifier table) "." field-name))
field-name)))

(defn try-prefix [v]
Expand Down Expand Up @@ -114,7 +120,7 @@
(string? v) v
(map? v) (:table v)
:else (name v))]
(delimit-str tstr))))
(table-identifier tstr))))

(defn parameterize [v]
(when *bound-params*
Expand Down
19 changes: 19 additions & 0 deletions test/korma/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

(defentity blah (pk :cool) (has-many users {:fk :cool_id}))

(defentity book-with-db (table :korma.book))
(defentity author-with-db (table :other.author) (belongs-to book-with-db))

(defentity book-with-schema (table :korma.myschema.book))
(defentity author-with-schema (table :korma.otherschema.author) (belongs-to book-with-schema))

(deftest select-function
(is (= "SELECT \"users\".\"id\", \"users\".\"username\" FROM \"users\" WHERE (\"users\".\"username\" = ?) ORDER BY \"users\".\"created\" ASC LIMIT 5 OFFSET 3"
(-> (select* "users")
Expand Down Expand Up @@ -433,3 +439,16 @@
(select :test (where {:id [not-in [1 2 3]]}))
"SELECT \"test\".* FROM \"test\" WHERE (\"test\".\"id\" != ?)"
(select :test (where {:id [not= 1]})))))

(deftest dbname-on-tablename
(are [query result] (= query result)
(sql-only
(select author-with-db (with book-with-db)))
"SELECT \"other\".\"author\".*, \"korma\".\"book\".* FROM \"other\".\"author\" LEFT JOIN \"korma\".\"book\" ON \"korma\".\"book\".\"id\" = \"other\".\"author\".\"book_id\""))

(deftest schemaname-on-tablename
(are [query result] (= query result)
(sql-only
(select author-with-schema (with book-with-schema)))
"SELECT \"korma\".\"otherschema\".\"author\".*, \"korma\".\"myschema\".\"book\".* FROM \"korma\".\"otherschema\".\"author\" LEFT JOIN \"korma\".\"myschema\".\"book\" ON \"korma\".\"myschema\".\"book\".\"id\" = \"korma\".\"otherschema\".\"author\".\"book_id\""))