8000 Support multi-row inserts · theasp/postgres.async@c7774c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c7774c8

Browse files
committed
Support multi-row inserts
1 parent 939be62 commit c7774c8

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/postgres/async.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
[db sql-spec data f]
7272
(execute! db (flatten [(pg/create-update-sql sql-spec data)
7373
(rest (:where sql-spec))
74-
(for [e data] (second e))])
74+
(vals data)])
7575
f))
7676

7777
(defn begin!

src/postgres/async/impl.clj

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828

2929
(defn- list-columns [data]
3030
(if (map? data)
31-
(map name (keys data))
31+
(str " (" (string/join ", " (map name (keys data))) ") ")
3232
(recur (first data))))
3333

3434
(defn- list-params [start end]
35-
(for [i (range start end)] (str "$" i)))
35+
(str "(" (string/join ", " (for [i (range start end)]
36+
(str "$" i)))
37+
")"))
3638

3739
(defn- list-params-seq [datas]
3840
(if (map? datas)
@@ -41,23 +43,24 @@
4143
max (inc (* (count datas) size))]
4244
(string/join ", " (map
4345
#(str "(" (string/join ", " %) ")")
44-
(partition size (list-params 1 max)))))))
46+
(partition size (for [i (range 1 max)]
47+
(str "$" i))))))))
4548

46-
(defn create-insert-sql [{:keys [table returning]} datas]
47-
(str "INSERT INTO " table " ("
48-
(string/join ", " (list-columns datas))
49-
") VALUES "
50-
(list-params-seq datas)
49+
(defn create-insert-sql [{:keys [table returning]} data]
50+
(str "INSERT INTO " table
51+
(list-columns data)
52+
" VALUES "
53+
(list-params-seq data)
5154
(when returning
5255
(str " RETURNING " returning))))
5356

5457
(defn create-update-sql [{:keys [table returning where]} data]
5558
(str "UPDATE " table
56-
" SET ("
57-
(string/join "," (list-columns data))
58-
")=("
59-
(string/join "," (list-params (count where) (+ (count where) (count data))))
60-
") WHERE " (first where)
59+
" SET "
60+
(list-columns data)
61+
" = "
62+
(list-params (count where) (+ (count where) (count data)))
63+
" WHERE " (first where)
6164
(when returning
6265
(str " RETURNING " returning))))
6366

0 commit comments

Comments
 (0)
0