Basic SQL tests and bug fix!
Multi-row insert did not apply `:column-fn` correctly.
This commit is contained in:
parent
67a2d3918c
commit
d59b1ec2fb
2 changed files with 63 additions and 37 deletions
|
|
@ -124,10 +124,11 @@
|
|||
Applies any :table-fn / :column-fn supplied in the options."
|
||||
[table cols rows opts]
|
||||
(assert (apply = (count cols) (map count rows)))
|
||||
(let [entity-fn (:table-fn opts identity)
|
||||
params (str/join ", " (map (comp entity-fn name) cols))
|
||||
(let [table-fn (:table-fn opts identity)
|
||||
column-fn (:column-fn opts identity)
|
||||
params (str/join ", " (map (comp column-fn name) cols))
|
||||
places (as-? (first rows) opts)]
|
||||
(into [(str "INSERT INTO " (entity-fn (name table))
|
||||
(into [(str "INSERT INTO " (table-fn (name table))
|
||||
" (" params ")"
|
||||
" VALUES "
|
||||
(str/join ", " (repeat (count rows) (str "(" places ")"))))]
|
||||
|
|
@ -219,36 +220,3 @@
|
|||
(delete! connectable table where-params {}))
|
||||
([connectable table where-params opts]
|
||||
(execute-one! connectable (for-delete table where-params opts) opts)))
|
||||
|
||||
(comment
|
||||
(require '[next.jdbc.quoted :refer [mysql sql-server]])
|
||||
(by-keys {:a nil :b 42 :c "s"} :where {})
|
||||
;=> ["WHERE a IS NULL AND b = ? AND c = ?" 42 "s"]
|
||||
(as-keys {:a nil :b 42 :c "s"} {})
|
||||
;=> a, b, c
|
||||
(as-? {:a nil :b 42 :c "s"} {})
|
||||
;=> ?, ?, ?
|
||||
(for-query :user {:id 9} {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["SELECT * FROM [user] WHERE `id` = ?" 9]
|
||||
(for-query :user {:id nil} {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["SELECT * FROM [user] WHERE `id` IS NULL"]
|
||||
(for-query :user ["id = ? and opt is null" 9] {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["SELECT * FROM [user] WHERE id = ? and opt is null" 9]
|
||||
(for-delete :user {:opt nil :id 9} {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["DELETE FROM [user] WHERE `opt` IS NULL AND `id` = ?" 9]
|
||||
(for-delete :user ["id = ? and opt is null" 9] {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["DELETE FROM [user] WHERE id = ? and opt is null" 9]
|
||||
(for-update :user {:status 42} {} {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["UPDATE [user] SET `status` = ? WHERE " 42]
|
||||
(for-update :user {:status 42} {:id 9} {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["UPDATE [user] SET `status` = ? WHERE `id` = ?" 42 9]
|
||||
(for-update :user {:status 42, :opt nil} ["id = ?" 9] {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["UPDATE [user] SET `status` = ?, `opt` = ? WHERE id = ?" 42 nil 9]
|
||||
(for-insert :user {:id 9 :status 42 :opt nil} {:table-fn sql-server :column-fn mysql})
|
||||
;=> ["INSERT INTO [user] (`id`, `status`, `opt`) VALUES (?, ?, ?)" 9 42 nil]
|
||||
(for-insert-multi :user [:id :status]
|
||||
[[42 "hello"]
|
||||
[35 "world"]
|
||||
[64 "dollars"]]
|
||||
{:table-fn sql-server :column-fn mysql}))
|
||||
;=> ["INSERT INTO [user] (`id`, `status`) VALUES (?, ?), (?, ?), (?, ?)" 42 "hello" 35 "world" 64 "dollars"])
|
||||
|
|
|
|||
|
|
@ -2,4 +2,62 @@
|
|||
|
||||
(ns next.jdbc.sql-test
|
||||
(:require [clojure.test :refer [deftest is testing]]
|
||||
[next.jdbc.sql :refer :all]))
|
||||
[next.jdbc.quoted :refer [mysql sql-server]]
|
||||
[next.jdbc.sql :as sql]))
|
||||
|
||||
(deftest test-by-keys
|
||||
(testing ":where clause"
|
||||
(is (= (#'sql/by-keys {:a nil :b 42 :c "s"} :where {})
|
||||
["WHERE a IS NULL AND b = ? AND c = ?" 42 "s"])))
|
||||
(testing ":set clause"
|
||||
(is (= (#'sql/by-keys {:a nil :b 42 :c "s"} :set {})
|
||||
["SET a = ?, b = ?, c = ?" nil 42 "s"]))))
|
||||
|
||||
(deftest test-as-keys
|
||||
(is (= (#'sql/as-keys {:a nil :b 42 :c "s"} {})
|
||||
"a, b, c")))
|
||||
|
||||
(deftest test-as-?
|
||||
(is (= (#'sql/as-? {:a nil :b 42 :c "s"} {})
|
||||
"?, ?, ?")))
|
||||
|
||||
(deftest test-for-query
|
||||
(testing "by example"
|
||||
(is (= (#'sql/for-query :user {:id 9} {:table-fn sql-server :column-fn mysql})
|
||||
["SELECT * FROM [user] WHERE `id` = ?" 9]))
|
||||
(is (= (#'sql/for-query :user {:id nil} {:table-fn sql-server :column-fn mysql})
|
||||
["SELECT * FROM [user] WHERE `id` IS NULL"])))
|
||||
(testing "by where clause"
|
||||
(is (= (#'sql/for-query :user ["id = ? and opt is null" 9] {:table-fn sql-server :column-fn mysql})
|
||||
["SELECT * FROM [user] WHERE id = ? and opt is null" 9]))))
|
||||
|
||||
(deftest test-for-delete
|
||||
(testing "by example"
|
||||
(is (= (#'sql/for-delete :user {:opt nil :id 9} {:table-fn sql-server :column-fn mysql})
|
||||
["DELETE FROM [user] WHERE `opt` IS NULL AND `id` = ?" 9])))
|
||||
(testing "by where clause"
|
||||
(is (= (#'sql/for-delete :user ["id = ? and opt is null" 9] {:table-fn sql-server :column-fn mysql})
|
||||
["DELETE FROM [user] WHERE id = ? and opt is null" 9]))))
|
||||
|
||||
(deftest test-for-update
|
||||
(testing "empty example (SQL error)"
|
||||
(is (= (#'sql/for-update :user {:status 42} {} {:table-fn sql-server :column-fn mysql})
|
||||
["UPDATE [user] SET `status` = ? WHERE " 42])))
|
||||
(testing "by example"
|
||||
(is (= (#'sql/for-update :user {:status 42} {:id 9} {:table-fn sql-server :column-fn mysql})
|
||||
["UPDATE [user] SET `status` = ? WHERE `id` = ?" 42 9])))
|
||||
(testing "by where clause, with nil set value"
|
||||
(is (= (#'sql/for-update :user {:status 42, :opt nil} ["id = ?" 9] {:table-fn sql-server :column-fn mysql})
|
||||
["UPDATE [user] SET `status` = ?, `opt` = ? WHERE id = ?" 42 nil 9]))))
|
||||
|
||||
(deftest test-for-inserts
|
||||
(testing "single insert"
|
||||
(is (= (#'sql/for-insert :user {:id 9 :status 42 :opt nil} {:table-fn sql-server :column-fn mysql})
|
||||
["INSERT INTO [user] (`id`, `status`, `opt`) VALUES (?, ?, ?)" 9 42 nil])))
|
||||
(testing "multi-row insert"
|
||||
(is (= (#'sql/for-insert-multi :user [:id :status]
|
||||
[[42 "hello"]
|
||||
[35 "world"]
|
||||
[64 "dollars"]]
|
||||
{:table-fn sql-server :column-fn mysql})
|
||||
["INSERT INTO [user] (`id`, `status`) VALUES (?, ?), (?, ?), (?, ?)" 42 "hello" 35 "world" 64 "dollars"]))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue