diff --git a/project.clj b/project.clj index bdc49d3..729872f 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ :url "https://github.com/krisajenkins/yesql"} :profiles {:dev {:dependencies [[expectations "2.1.3" :exclusions [org.clojure/clojure]] [org.apache.derby/derby "10.12.1.1"]] - :plugins [[lein-autoexpect "1.4.0"] + :plugins [[lein-autoexpect "1.4.0" :exclusions [org.clojure/tools.namespace]] [lein-expectations "0.0.8" :exclusions [org.clojure/clojure]]]} :1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]} :1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]} diff --git a/src/yesql/generate.clj b/src/yesql/generate.clj index 80db969..bfdf2b5 100644 --- a/src/yesql/generate.clj +++ b/src/yesql/generate.clj @@ -36,7 +36,7 @@ (defn rewrite-query-for-jdbc [tokens initial-args] (let [{:keys [expected-keys expected-positional-count]} (analyse-statement-tokens tokens) - actual-keys (set (keys (dissoc initial-args :?))) + actual-keys (set (keys (dissoc (if (or (vector? initial-args) (list? initial-args)) (apply merge initial-args) initial-args) :?))) actual-positional-count (count (:? initial-args)) missing-keys (set/difference expected-keys actual-keys)] (if-not (empty? missing-keys) @@ -50,23 +50,33 @@ "Expected %d positional parameters. Got %d." "Supply positional parameters as {:? [...]}"]) expected-positional-count actual-positional-count)))) - (let [[final-query final-parameters consumed-args] - (reduce (fn [[query parameters args] token] - (cond - (string? token) [(str query token) - parameters - args] - (symbol? token) (let [[arg new-args] (if (= '? token) - [(first (:? args)) (update-in args [:?] rest)] - [(get args (keyword token)) args])] - [(str query (args-to-placeholders arg)) - (vec (if (in-list-parameter? arg) - (concat parameters arg) - (conj parameters arg))) - new-args]))) - ["" [] initial-args] - tokens)] - (concat [final-query] final-parameters)))) + (if (or (vector? initial-args) (list? initial-args)) + (let [[final-query final-parameters consumed-args] + (reduce (fn [[query parameters args] token] + (cond + (string? token) [(str query token) + parameters + args] + (symbol? token) [(str query (args-to-placeholders "")) + (conj parameters (keyword token)) + args])) ["" [] initial-args] tokens)] (concat [final-query] (mapv (apply juxt final-parameters) initial-args))) + (let [[final-query final-parameters consumed-args] + (reduce (fn [[query parameters args] token] + (cond + (string? token) [(str query token) + parameters + args] + (symbol? token) (let [[arg new-args] (if (= '? token) + [(first (:? args)) (update-in args [:?] rest)] + [(get args (keyword token)) args])] + [(str query (args-to-placeholders arg)) + (vec (if (in-list-parameter? arg) + (concat parameters arg) + (conj parameters arg))) + new-args]))) + ["" [] initial-args] + tokens)] + (concat [final-query] final-parameters))))) ;; Maintainer's note: clojure.java.jdbc.execute! returns a list of ;; rowcounts, because it takes a list of parameter groups. In our @@ -78,7 +88,9 @@ (defn insert-handler [db statement-and-params call-options] - (jdbc/db-do-prepared-return-keys db statement-and-params)) + (if (vector? (second statement-and-params)) + (apply jdbc/db-do-prepared db statement-and-params) + (jdbc/db-do-prepared-return-keys db statement-and-params))) (defn query-handler [db sql-and-params