From fb946311da111e2a422a938b0b8720c3ecf3341c Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 21 Jun 2013 10:42:56 +1000 Subject: Add take/drop, move jdbc stuff, document default of postgres Add take and drop functionality to the queries, so now you can use the take and drop functions in a similar way to how they work on seqs in clojure. Move jdbc interface stuff into clojure-sql.jdbc, so if you're using jdbc you can include it yourself. (If you're not using jdbc then it shouldn't bother you). Given the default compilation target is actually postgres, document that. --- src/clojure_sql/dsl.clj | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'src/clojure_sql/dsl.clj') diff --git a/src/clojure_sql/dsl.clj b/src/clojure_sql/dsl.clj index c091d0e..10b1e8f 100644 --- a/src/clojure_sql/dsl.clj +++ b/src/clojure_sql/dsl.clj @@ -1,5 +1,5 @@ (ns clojure-sql.dsl - (:refer-clojure :exclude [sort group]) + (:refer-clojure :exclude [sort group take drop]) (:require [clojure.set :as set] [clojure.walk :as walk] [clojure-sql.query :as q] @@ -159,7 +159,9 @@ (defn ^:private joinable? [query] (and (nil? (:group query)) - (nil? (:having query)))) + (nil? (:having query)) + (nil? (:take query)) + (nil? (:drop query)))) (defn ^:private convert-to-subquery [query] (-> (table query) @@ -168,6 +170,13 @@ (defn ^:private remove-sort [query] (dissoc query :sort)) +(defn ^:private make-join-subquery [query] + (if (joinable? query) + query + (if (or (:take query) (:drop query)) + (convert-to-subquery query) + (convert-to-subquery (remove-sort query))))) + (def ^:private valid-join-type? (comp boolean #{:cross :inner :outer :full-outer})) (defn join [left right & {:keys [on type]}] (if (= type :right) @@ -175,18 +184,14 @@ (let [type (if (= type :left) :outer type) - left (if (joinable? left) - left - (convert-to-subquery (remove-sort left))) - right (if (joinable? right) - right - (convert-to-subquery (remove-sort right))) + left (make-join-subquery left) + right (make-join-subquery right) common-tables (set/intersection (set (keys (:tables left))) (set (keys (:tables right)))) right (reduce (fn [query [alias table]] (rename-table query alias (keyword (gensym (name table))))) right (:tables right)) - ;_ (assert (empty? common-tables) "Cannot join two tables with the same name") + ;;_ (assert (empty? common-tables) "Cannot join two tables with the same name") merged-tables (merge (:tables left) (:tables right)) common-fields (set/intersection (set (keys (:fields left))) (set (keys (:fields right)))) @@ -263,3 +268,16 @@ resolved-expression (process-expression table-name (:fields query) expression) new-having (combine-conjunctions old-having resolved-expression)] (assoc query :having new-having))) + +(defn take [query n] + (if-let [old-take (:take query)] + (assoc query :take (min old-take n)) + (assoc query :take n))) + +(defn drop [query n] + (let [query (if-let [old-take (:take query)] + (assoc query :take (- old-take n)) + query)] + (if-let [old-drop (:drop query)] + (assoc query :drop (+ old-drop n)) + (assoc query :drop n)))) -- cgit v1.2.3