From 08ad526da54417f0e8319f266635b8ed2afcba1d Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 14 May 2013 15:39:54 +1000 Subject: Throw a nicer exception on ambiguous field, also some cleanup --- src/clojure_sql/core.clj | 18 +++++++++--------- 1 file 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] -- cgit v1.2.3