diff --git a/src/korma/sql/engine.clj b/src/korma/sql/engine.clj index 86c2eb9..ec1c2c9 100644 --- a/src/korma/sql/engine.clj +++ b/src/korma/sql/engine.clj @@ -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))) diff --git a/test/korma/test/core.clj b/test/korma/test/core.clj index 0ddc440..2c3a161 100644 --- a/test/korma/test/core.clj +++ b/test/korma/test/core.clj @@ -802,7 +802,7 @@ ;;***************************************************** (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 @@ -810,7 +810,7 @@ :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 @@ -818,7 +818,7 @@ :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 @@ -826,7 +826,7 @@ :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 @@ -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 @@ -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