summaryrefslogtreecommitdiff
path: root/src/clojure_sql
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-05-14 18:20:39 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-05-14 18:20:39 +1000
commit1d7cc24207467f09940525e7e6716546504909d9 (patch)
treea99638fff61a75351dd17e6dc3b794a0779ec296 /src/clojure_sql
parent08ad526da54417f0e8319f266635b8ed2afcba1d (diff)
join and rename preconditions removing ambiguity, `on` now optional
`join` and `rename` both had the ability to introduce some ambiguity, whether by renaming two fields to the same alias (which one should use that alias?) or by joining two fields with the same alias (which one should use it?). Some preconditions have been added to warn against some ambiguous use cases (although I have just now realised the rename precondition is insufficient, so a new commit will be coming soon).
Diffstat (limited to 'src/clojure_sql')
-rw-r--r--src/clojure_sql/core.clj42
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))