Skip to content

Commit

Permalink
added batch insert support
Browse files Browse the repository at this point in the history
  • Loading branch information
ibarrick committed Jun 20, 2016
1 parent b6de247 commit d634f7d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]}
Expand Down
50 changes: 31 additions & 19 deletions src/yesql/generate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d634f7d

Please sign in to comment.