File tree Expand file tree Collapse file tree 3 files changed +34
-14
lines changed Expand file tree Collapse file tree 3 files changed +34
-14
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ Asynchronous PostgreSQL Clojure library
36
36
(<!! (<update! db {:table " users" :where [" id=$1" 1001 }} {:price 6 }))
37
37
; [{:updated 1, :rows []} nil]
38
38
39
- (<!! (<execute! d [" select 1 as anything" ]))
39
+ (<!! (<execute! db [" select 1 as anything" ]))
40
40
; [{:updated 0, :rows [{:anything 1}]} nil]
41
41
42
42
; ; Asynchronous composition! dosql returns [nil exception] on first error
Original file line number Diff line number Diff line change 59
59
f))
60
60
61
61
(defn- create-update-sql [spec data]
62
- (let [where (:where spec)
62
+ (let [where (:where spec)
63
63
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)]
65
67
(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)))))
69
73
70
74
(defn update! [db spec data f]
71
75
(execute! db (flatten [(create-update-sql spec data)
Original file line number Diff line number Diff line change 17
17
:username (env " PG_USER" " postgres" )
18
18
:password (env " PG_PASSWORD" " postgres" )
19
19
: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*)))))
22
30
23
31
(use-fixtures :each db-fixture)
24
32
25
33
(deftest queries
26
34
(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 ])))))
29
45
30
46
(deftest sql-macro
31
47
(testing " dosql returns last form"
32
48
(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)))))))))
You can’t perform that action at this time.
0 commit comments