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

Do not wrap subqueries of combined queries, fixes #328. #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/korma/sql/engine.clj
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,19 @@
;; Combination Queries
;;*****************************************************

;; The usual map-val always wraps subqueries in parenthesis, but that is
;; not allowed for combined queries in some databases, mainly SQLite.
;; Therefore we use a lightweight version of map-val that does not do any
;; wrapping and only handles subqueries.
(defn- map-combination-val [v]
(if-let [sub (utils/sub-query? v)]
(do
(swap! *bound-params* utils/vconcat (:params sub))
(:sql-str sub))
(pred-map v)))

(defn- sql-combination-query [type query]
(let [sub-query-sqls (map map-val (:queries query))
(let [sub-query-sqls (map map-combination-val (:queries query))
neue-sql (string/join (str " " type " ") sub-query-sqls)]
(assoc query :sql-str neue-sql)))

Expand Down
12 changes: 6 additions & 6 deletions test/korma/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -802,31 +802,31 @@
;;*****************************************************

(deftest test-union
(is (= "dry run :: (SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?)) UNION (SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?)) :: [1 2 3]\n"
(is (= "dry run :: SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?) UNION SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?) :: [1 2 3]\n"
(with-out-str (dry-run (union (queries (subselect users
(where {:a 1}))
(subselect state
(where {:b 2
:c 3})))))))))

(deftest test-union-all
(is (= "dry run :: (SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?)) UNION ALL (SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?)) :: [1 2 3]\n"
(is (= "dry run :: SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?) UNION ALL SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?) :: [1 2 3]\n"
(with-out-str (dry-run (union-all (queries (subselect users
(where {:a 1}))
(subselect state
(where {:b 2
:c 3})))))))))

(deftest test-intersect
(is (= "dry run :: (SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?)) INTERSECT (SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?)) :: [1 2 3]\n"
(is (= "dry run :: SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?) INTERSECT SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?) :: [1 2 3]\n"
(with-out-str (dry-run (intersect (queries (subselect users
(where {:a 1}))
(subselect state
(where {:b 2
:c 3})))))))))

(deftest test-order-by-in-union
(is (= "dry run :: (SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?)) UNION (SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?)) ORDER BY \"a\" ASC :: [1 2 3]\n"
(is (= "dry run :: SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?) UNION SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?) ORDER BY \"a\" ASC :: [1 2 3]\n"
(with-out-str (dry-run (union (queries (subselect users
(where {:a 1}))
(subselect state
Expand All @@ -835,7 +835,7 @@
(order :a)))))))

(deftest test-order-by-in-union-all
(is (= "dry run :: (SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?)) UNION ALL (SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?)) ORDER BY \"a\" ASC :: [1 2 3]\n"
(is (= "dry run :: SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?) UNION ALL SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?) ORDER BY \"a\" ASC :: [1 2 3]\n"
(with-out-str (dry-run (union-all (queries (subselect users
(where {:a 1}))
(subselect state
Expand All @@ -844,7 +844,7 @@
(order :a)))))))

(deftest test-order-by-in-intersect
(is (= "dry run :: (SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?)) INTERSECT (SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?)) ORDER BY \"a\" ASC :: [1 2 3]\n"
(is (= "dry run :: SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"a\" = ?) INTERSECT SELECT \"state\".* FROM \"state\" WHERE (\"state\".\"b\" = ? AND \"state\".\"c\" = ?) ORDER BY \"a\" ASC :: [1 2 3]\n"
(with-out-str (dry-run (intersect (queries (subselect users
(where {:a 1}))
(subselect state
Expand Down