Skip to content

Commit

Permalink
fix subselect table prefix issue
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information
ibdknox committed Jan 13, 2012
1 parent 0af04b8 commit 319a35b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/korma/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
(throw (Exception. (str "Invalid entity provided for the query: " ent)))))

(defn empty-query [ent]
(let [[ent table alias db opts] (if (string? ent)
(let [ent (if (keyword? ent)
(name ent)
ent)
[ent table alias db opts] (if (string? ent)
[{:table ent} ent nil nil nil]
[ent (:table ent) (:alias ent)
(:db ent) (get-in ent [:db :options])])]
Expand Down Expand Up @@ -177,8 +180,8 @@
[query form]
`(let [q# ~query]
(where* q#
(eng/pred-map
(bind-query q#
(bind-query q#
(eng/pred-map
~(eng/parse-where `~form))))))

(defn order
Expand Down
10 changes: 10 additions & 0 deletions test/korma/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@
(exec)))
"SELECT MIN(\"the_table\".\"date_created\") \"start_date\", MAX(\"the_table\".\"date_created\") \"end_date\" FROM \"the_table\" WHERE (\"the_table\".\"id\" IN (?, ?, ?))")))

(deftest subselect-table-prefix
(defentity first_table)
(is (= (sql-only
(select first_table
(where {:first_table_column
(subselect :second_table
(fields :second_table_column)
(where {:second_table_column 1}))})))
"SELECT \"first_table\".* FROM \"first_table\" WHERE (\"first_table\".\"first_table_column\" = (SELECT \"second_table\".\"second_table_column\" FROM \"second_table\" WHERE (\"second_table\".\"second_table_column\" = ?)))")))

(deftest entity-as-subselect
(defentity subsel
(table (subselect "test") :test))
Expand Down

0 comments on commit 319a35b

Please sign in to comment.