From be3302010635abd27adb64f58964baa7d205428e Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 1 Jul 2013 08:15:44 +1000 Subject: Add union/intersection to the dsl --- src/clojure_sql/compiler.clj | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'src/clojure_sql/compiler.clj') diff --git a/src/clojure_sql/compiler.clj b/src/clojure_sql/compiler.clj index 6b3ec4a..d0b1206 100644 --- a/src/clojure_sql/compiler.clj +++ b/src/clojure_sql/compiler.clj @@ -223,24 +223,28 @@ (if drop (str " OFFSET " drop))))) +(def ^:private set-operations {:union "UNION", :intersect "INTERSECT"}) + (defmulti compile-query (fn [db _] db) :default :postgres) -(defmethod compile-query :postgres [db {:keys [tables fields joins where sort group having union take drop]}] - (if union - (->> union - (map (partial compile-query db)) - (apply sequence) - ((p-lift string/join " UNION "))) - ($str (return "SELECT ") - (compile-fields db fields) - (if tables - ($str (return " FROM ") - (compile-tables db joins tables)) - ($str "")) - (compile-where db where) - (compile-group db group) - (compile-having db having) - (compile-sort db sort) - (compile-limit db take drop)))) +(defmethod compile-query :postgres [db {:keys [tables fields joins where sort group having take drop set-operation queries]}] + (or (if set-operation + (let [op-str (str ") " (get set-operations set-operation) " (")] + (->> queries + (map (partial compile-query db)) + (apply sequence) + ((p-lift string/join op-str)) + $add-parentheses))) + ($str (return "SELECT ") + (compile-fields db fields) + (if tables + ($str (return " FROM ") + (compile-tables db joins tables)) + ($str "")) + (compile-where db where) + (compile-group db group) + (compile-having db having) + (compile-sort db sort) + (compile-limit db take drop)))) -- cgit v1.2.3