summaryrefslogtreecommitdiff
path: root/src/clojure_sql
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-05-14 15:39:54 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-05-14 15:39:54 +1000
commit08ad526da54417f0e8319f266635b8ed2afcba1d (patch)
treeb6c687476e9fd69bd772908f36a6839aa319da9c /src/clojure_sql
parenta9c32acfd2a160d82a1c0c0f8484ddd202126b95 (diff)
Throw a nicer exception on ambiguous field, also some cleanup
Diffstat (limited to 'src/clojure_sql')
-rw-r--r--src/clojure_sql/core.clj18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/clojure_sql/core.clj b/src/clojure_sql/core.clj
index 0032bb6..2ff64e0 100644
--- a/src/clojure_sql/core.clj
+++ b/src/clojure_sql/core.clj
@@ -184,19 +184,21 @@
{:table arg}
{:table {arg arg}})))
+(defn ambiguous-error [field]
+ (throw (ex-info (str "Ambiguous field " field)
+ {:field field})))
+
(defn project [query fields]
(let [table (if-not (:joins query)
(-> query :table first val))
- alias-lookup (->> query :fields
- (map (comp vec reverse))
- (into {}))
+ alias-lookup (u/flip-map (:fields query))
original-name (fn [field]
(if (vector? field)
field
(or (get alias-lookup field nil)
(if table
- [table field]
- (throw (RuntimeException. (str "Ambiguous field " field " cannot be resolved")))))))]
+ [table field]
+ (ambiguous-error field)))))]
(assoc query
:fields (->> (for [[key val] (if (map? fields)
fields
@@ -206,9 +208,7 @@
(defn rename [query field-renames]
{:pre [(map? field-renames)]}
- (let [alias-lookup (->> query :fields
- (map (comp vec reverse))
- (into {}))
+ (let [alias-lookup (u/flip-map (:fields query))
original-name (fn [field]
(cond (vector? field) field
(contains? alias-lookup field) (get alias-lookup field)
@@ -227,7 +227,7 @@
(or (field-alias-lookup field)
(if table
[table field]
- (throw (RuntimeException. (str "Ambiguous field " field " cannot be resolved")))))))
+ (ambiguous-error field)))))
(defn ^:private resolve-fields [table aliases expression]
(clojure.walk/postwalk (fn [expr]