Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
First pass at adding batch inserts
  • Loading branch information
ninanator committed Mar 6, 2016
commit 0248f2da58e11ffb0bee2d76c4b248d9b3e37253
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject yesql "0.5.2"
(defproject yesql "0.6.0"
:description "A Clojure library for using SQL"
:url "https://github.com/krisajenkins/yesql"
:license {:name "Eclipse Public License"
Expand Down
23 changes: 15 additions & 8 deletions src/yesql/generate.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns yesql.generate
(:require [clojure.java.jdbc :as jdbc]
[clojure.set :as set]
[clojure.string :refer [join lower-case]]
[clojure.string :refer [join lower-case split]]
[yesql.util :refer [create-root-var]]
[yesql.types :refer [map->Query]]
[yesql.statement-parser :refer [tokenize]])
Expand Down Expand Up @@ -72,21 +72,26 @@
;; case, we only ever use one group, so we'll unpack the
;; single-element list with `first`.
(defn execute-handler
[db sql-and-params call-options]
(first (jdbc/execute! db sql-and-params)))
[db sql params call-options]
(first (jdbc/execute! db (rewrite-query-for-jdbc sql params))))

(defn insert-handler
[db [statement & params] call-options]
(jdbc/db-do-prepared-return-keys db statement params))
[db sql params call-options]
(let [table-string-position 0
table-position 2
table-keyword (keyword ((split (sql table-string-position) #" ") table-position))]
(if (vector? params)
(apply jdbc/insert! db table-keyword params)
(jdbc/insert! db table-keyword params))))

(defn query-handler
[db sql-and-params
[db sql params
{:keys [row-fn result-set-fn identifiers]
:or {identifiers lower-case
row-fn identity
result-set-fn doall}
:as call-options}]
(jdbc/query db sql-and-params
(jdbc/query db (rewrite-query-for-jdbc sql params)
:identifiers identifiers
:row-fn row-fn
:result-set-fn result-set-fn))
Expand Down Expand Up @@ -118,7 +123,9 @@
"Check the docs, and supply {:connection ...} as an option to the function call, or globally to the defquery declaration."])
name))
(jdbc-fn connection
(rewrite-query-for-jdbc tokens args)
tokens
args
;(rewrite-query-for-jdbc tokens args)
call-options)))
[display-args generated-function] (let [named-args (if-let [as-vec (seq (mapv (comp symbol clojure.core/name)
required-args))]
Expand Down
33 changes: 15 additions & 18 deletions test/yesql/acceptance_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
(expect (create-person-table!))

;; Insert -> Select.
(expect {:1 1M} (insert-person<! {:name "Alice"
:age 20}))
(expect {:1 2M} (insert-person<! {:name "Bob"
:age 25}))
(expect {:1 3M} (insert-person<! {:name "Charlie"
:age 35}))
(expect (list {:1 1M}) (insert-person<! {:name "Alice" :age 20}))
(expect (list {:1 2M}) (insert-person<! {:name "Bob" :age 25}))
(expect (list {:1 3M}) (insert-person<! {:name "Charlie" :age 35}))
(expect (list {:1 4M}) (insert-person<! [{:name "Pepper" :age 50} {:name "Tony" :age 55}]))

(expect 3 (count (find-older-than {:age 10})))
(expect 1 (count (find-older-than {:age 30})))
(expect 0 (count (find-older-than {:age 50})))
(expect 5 (count (find-older-than {:age 10})))
(expect 3 (count (find-older-than {:age 30})))
(expect 1 (count (find-older-than {:age 50})))

;;; Select with IN.
(expect 2 (count (find-by-age {:age [20 35]})))
Expand All @@ -46,20 +44,20 @@
(expect 0 (update-age! {:age 38
:name "David"}))

(expect 3 (count (find-older-than {:age 10})))
(expect 2 (count (find-older-than {:age 30})))
(expect 0 (count (find-older-than {:age 50})))
(expect 5 (count (find-older-than {:age 10})))
(expect 4 (count (find-older-than {:age 30})))
(expect 1 (count (find-older-than {:age 50})))

;; Delete -> Select.
(expect 1 (delete-person! {:name "Alice"}))

(expect 2 (count (find-older-than {:age 10})))
(expect 1 (count (find-older-than {:age 30})))
(expect 0 (count (find-older-than {:age 50})))
(expect 4 (count (find-older-than {:age 10})))
(expect 3 (count (find-older-than {:age 30})))
(expect 1 (count (find-older-than {:age 50})))

;; Failing transaction: Insert with abort.
;; Insert two rows in a transaction. The second throws a deliberate error, meaning no new rows created.
(expect 2 (count (find-older-than {:age 10})))
(expect 4 (count (find-older-than {:age 10})))

(expect SQLException
(jdbc/with-db-transaction [connection derby-db]
Expand All @@ -70,8 +68,7 @@
:age 25}
{:connection connection} )))

(expect 2
(count (find-older-than {:age 10})))
(expect 4 (count (find-older-than {:age 10})))

;;; Type error.
(expect SQLDataException
Expand Down