diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clojure_sql/core.clj | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/src/clojure_sql/core.clj b/src/clojure_sql/core.clj index 2ff64e0..6cdb115 100644 --- a/src/clojure_sql/core.clj +++ b/src/clojure_sql/core.clj @@ -207,8 +207,11 @@ (into {}))))) (defn rename [query field-renames] - {:pre [(map? field-renames)]} - (let [alias-lookup (u/flip-map (:fields query)) + {:pre [(map? field-renames) + (empty? (set/intersection (set (vals field-renames)) + (set (vals (:fields query)))))]} + (let [fields (:fields query) + alias-lookup (u/flip-map (:fields query)) original-name (fn [field] (cond (vector? field) field (contains? alias-lookup field) (get alias-lookup field) @@ -219,6 +222,7 @@ (get alias-lookup field))] (update-in query [:fields] #(->> (for [[key val] field-renames] + ;(if (contains? val (:fields query))) [(original-name key) val]) (into %))))) @@ -249,9 +253,12 @@ `(and ~acc ~where)))) nil wheres)) -(defn join [left right on & [type]] +(defn join [left right & [on type]] + {:pre [(empty? (set/intersection (set (vals (:fields left))) + (set (vals (:fields right)))))]} (let [joins-vector (or (:join left) []) - joined-fields (merge (:fields left) (:fields right))] + joined-fields (merge (:fields left) (:fields right)) + on (or on true)] (-> left (assoc :fields joined-fields) (assoc :joins (into (conj joins-vector @@ -268,10 +275,6 @@ (assoc query :where new-where))) (defn sort-by [query fields] - #_{:pre [(let [flipped-query-fields (u/flip-map (:fields query)) - field-names-seq (map (fn [x] (if (vector? x) (first x) x)) - (if (sequential? fields) fields [fields]))] - (every? flipped-query-fields field-names-seq))]} (let [table-name (if-not (:joins query) (-> query :table first val)) fields-seq (if (sequential? fields) @@ -324,10 +327,21 @@ (rename {:name :parent.name})) '(= :parent-id :id)) (project [:child.name :parent.name]) - deref #_println) + deref) + + (-> (table :users) + (project [:id]) + (join (-> (table :people) + (project [:id])) + true) + deref) + (-> (table :users) + (project [:id :name]) + (rename {:id :name}) + deref) - (deref (-> (table :anotherStack) - (project [:anotherNumber]) - (join (-> (table :collection) - (project [:number])) - true)))) + (-> (table :anotherStack) + (project [:anotherNumber]) + (join (-> (table :collection) + (project [:number]))) + deref)) |