Skip to content

Commit 5d28b39

Browse files
committed
Use alternate column syntax in update
1 parent 551f0fd commit 5d28b39

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Asynchronous PostgreSQL Clojure library
3636
(<!! (<update! db {:table "users" :where ["id=$1" 1001}} {:price 6}))
3737
; [{:updated 1, :rows []} nil]
3838

39-
(<!! (<execute! d ["select 1 as anything"]))
39+
(<!! (<execute! db ["select 1 as anything"]))
4040
; [{:updated 0, :rows [{:anything 1}]} nil]
4141

4242
;; Asynchronous composition! dosql returns [nil exception] on first error

src/clj_postgres_async/core.clj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@
5959
f))
6060

6161
(defn- create-update-sql [spec data]
62-
(let [where (:where spec)
62+
(let [where (:where spec)
6363
cols (for [e data] (-> e (first) (name)))
64-
params (range (count where) (+ (count where) (count data)))]
64+
params (for [i (range (count where) (+ (count where) (count data)))]
65+
(str "$" i))
66+
ret (:returning spec)]
6567
(str "UPDATE " (:table spec)
66-
" SET " (string/join ", " (map #(str (first %1) " = $" (second %1))
67-
(partition 2 (interleave cols params))))
68-
" WHERE " (first where))))
68+
" SET (" (string/join "," cols)
69+
")=(" (string/join "," params) ")"
70+
" WHERE " (first where)
71+
(when ret
72+
(str " RETURNING " ret)))))
6973

7074
(defn update! [db spec data f]
7175
(execute! db (flatten [(create-update-sql spec data)

test/clj_postgres_async/core_test.clj

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,36 @@
1717
:username (env "PG_USER" "postgres")
1818
:password (env "PG_PASSWORD" "postgres")
1919
:pool-size 1})]
20-
(try (f)
21-
(finally (close-db! *db*)))))
20+
(try
21+
(let [[_ err] (<!! (go (dosql
22+
[_ (<execute! *db* ["drop table if exists clj_pg_test"])
23+
_ (<execute! *db* ["create table clj_pg_test (
24+
id serial, t varchar(10)
25+
)"])])))]
26+
(if err
27+
(throw err)
28+
(f)))
29+
(finally (close-db! *db*)))))
2230

2331
(use-fixtures :each db-fixture)
2432

2533
(deftest queries
2634
(testing "<query! returns rows as map"
27-
(is (= [[{:x 1}] nil]
28-
(<!! (<query! *db* ["select 1 as x"]))))))
35+
(let [[rs err] (<!! (<query! *db* ["select 1 as x"]))]
36+
(is (= 1 (get-in rs [0 :x]))))))
37+
38+
(deftest inserts
39+
(testing "insert return row count"
40+
(let [[rs err] (<!! (<insert! *db* {:table "clj_pg_test"} {:t "x"}))]
41+
(is (= 1 (:updated rs)))))
42+
(testing "insert with returning returns generated keys"
43+
(let [[rs err] (<!! (<insert! *db* {:table "clj_pg_test" :returning "id"} {:t "y"}))]
44+
(is (get-in rs [:rows 0 :id])))))
2945

3046
(deftest sql-macro
3147
(testing "dosql returns last form"
3248
(is (= ["123" nil]
33-
(<!! (go
34-
(dosql [rs (<query! *db* ["select 123 as x"])
35-
rs (<query! *db* ["select $1::text as t" (:x (first rs))])]
36-
(:t (first rs)))))))))
49+
(<!! (go (dosql
50+
[rs (<query! *db* ["select 123 as x"])
51+
rs (<query! *db* ["select $1::text as t" (:x (first rs))])]
52+
(:t (first rs)))))))))

0 commit comments

Comments
 (0)