diff options
Diffstat (limited to 'src/clojure_sql/compiler.clj')
-rw-r--r-- | src/clojure_sql/compiler.clj | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/clojure_sql/compiler.clj b/src/clojure_sql/compiler.clj index 60a5530..9ba0c3a 100644 --- a/src/clojure_sql/compiler.clj +++ b/src/clojure_sql/compiler.clj @@ -2,7 +2,7 @@ (:refer-clojure :exclude [compile sequence]) (:require [clojure.string :as string] [clojure-sql.query :refer [query?]] - [clojure-sql.util :as u] + [clojure-sql.util :as u :refer [named?]] [clojure-sql.writer :as w :refer [return lift p-lift sequence do-m tell >>]])) (defn add-parentheses [s] @@ -105,9 +105,9 @@ (if (or (= table alias) (nil? alias)) (return (table-name db table)) ($str (condp #(%1 %2) table - query? ($add-parentheses (compile-query db table)) - named? (return (table-name db table)) - (compile-expression db table)) + query? ($add-parentheses (compile-query db table)) + named? (return (table-name db table)) + (compile-expression db table)) (return " AS ") (return (table-name db alias))))) @@ -191,17 +191,22 @@ (return nil))) (defmulti compile-query (fn [db _] db)) -(defmethod compile-query :default [db {:keys [tables fields joins where sort group having]}] - ($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))) +(defmethod compile-query :default [db {:keys [tables fields joins where sort group having union]}] + (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)))) @@ -241,7 +246,6 @@ (defn insert! [db query & records] {:pre [(empty? (:joins query))]} ;; some code here - #_($str (return "Carlo")) ) (defn update! [db query & partial-records] |