From 3ce695f6b2daf943490b2b4a9dac7be01bdd6356 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 20 Jun 2013 13:58:31 +1000 Subject: A few small changes/fixes to join behaviour, and code cleanup Fix compilation of cross joins. Remove the sort clauses on queries when they become subqueries in joins because then the sorting means nothing (although when take/drop stuff is added the sort will be relevant, so we'll see about that). Favour the left side of a join, which is mostly only relevant for the outer join case (and technically it's not quite right for full-outer joins, maybe, but I'll get to that later). --- src/clojure_sql/compiler.clj | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/clojure_sql/compiler.clj') 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] -- cgit v1.2.3