Skip to content

Commit

Permalink
Add quotes for insert columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitem committed Oct 12, 2023
1 parent 6e19acb commit 1d733a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
34 changes: 20 additions & 14 deletions src/dsql/pg.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@
(identifier opts tbl)
(cond-> (map? proj)
(-> (conj "(")
(conj (->> (keys proj) (sort) (mapv name) (str/join ", ")))
(conj (->> (keys proj) (sort) (mapv #(str "\"" (name %) "\"")) (str/join ", ")))
(conj ")")))
(ql/to-sql opts (assoc sel :ql/type :pg/sub-select))
(cond->
Expand Down Expand Up @@ -1357,21 +1357,27 @@
(conj "<>")
(ql/to-sql opts b)))


(defn concat-columns [cols]
(->> cols (mapv #(str "\"" (name %) "\"")) (str/join ", ")))


(defmethod ql/to-sql
:pg/insert-many
[acc opts {tbl :into vls :values ret :returning on-conflict :on-conflict}]
(-> acc
(conj "INSERT INTO")
(conj (name tbl))
(conj "(")
(conj (->> (:keys vls) (mapv name) (str/join ", ")))
(conj ")")
(ql/to-sql opts (with-meta vls {:ql/type :pg/values}))
(cond->
on-conflict (-> (conj "ON CONFLICT")
(ql/to-sql opts (assoc on-conflict :ql/type :pg/conflict-update)))
ret (-> (conj "RETURNING")
(ql/to-sql opts ret)))))
(let [cols (->> vls :keys)]
(-> acc
(conj "INSERT INTO")
(conj (name tbl))
(conj "(")
(conj (concat-columns cols ))
(conj ")")
(ql/to-sql opts (with-meta vls {:ql/type :pg/values}))
(cond->
on-conflict (-> (conj "ON CONFLICT")
(ql/to-sql opts (assoc on-conflict :ql/type :pg/conflict-update)))
ret (-> (conj "RETURNING")
(ql/to-sql opts ret))))))

(defmethod ql/to-sql
:pg/insert
Expand All @@ -1381,7 +1387,7 @@
(conj "INSERT INTO")
(conj (name tbl))
(conj "(")
(conj (->> cols (mapv name) (str/join ", ")))
(conj (concat-columns cols ))
(conj ")")
(conj "VALUES")
(conj "(")
Expand Down
18 changes: 9 additions & 9 deletions test/dsql/pg_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@
{:ql/type :pg/insert-select
:into :mytable
:select {:select {:z :z :a "a" :b :b} :from :t}}
["INSERT INTO mytable ( a, b, z ) ( SELECT 'a' as a , b as b , z as z FROM t )"])
["INSERT INTO mytable ( \"a\", \"b\", \"z\" ) ( SELECT 'a' as a , b as b , z as z FROM t )"])

(format=
{:ql/type :pg/insert-select
:into "mytable"
:select {:select {:z :z :a "a" :b :b} :from :t}}
["INSERT INTO mytable ( a, b, z ) ( SELECT 'a' as a , b as b , z as z FROM t )"])
["INSERT INTO mytable ( \"a\", \"b\", \"z\" ) ( SELECT 'a' as a , b as b , z as z FROM t )"])

(format=
{:ql/type :pg/insert-select
Expand All @@ -636,21 +636,21 @@
:do {:set {:a :excluded.a}
:where [:= 1 2]}}}

["INSERT INTO mytable ( a, b, z ) ( SELECT 'a' as a , b as b , z as z FROM t ) ON CONFLICT ( id ) DO UPDATE SET a = excluded.a WHERE 1 = 2"])
["INSERT INTO mytable ( \"a\", \"b\", \"z\" ) ( SELECT 'a' as a , b as b , z as z FROM t ) ON CONFLICT ( id ) DO UPDATE SET a = excluded.a WHERE 1 = 2"])

(format=
{:ql/type :pg/insert-select
:into :mytable
:select {:select {:z :z :a "a" :b :b} :from :t}
:on-conflict {:on [:id] :do :nothing}}
["INSERT INTO mytable ( a, b, z ) ( SELECT 'a' as a , b as b , z as z FROM t ) ON CONFLICT ( id ) DO NOTHING"])
["INSERT INTO mytable ( \"a\", \"b\", \"z\" ) ( SELECT 'a' as a , b as b , z as z FROM t ) ON CONFLICT ( id ) DO NOTHING"])

(format=
{:ql/type :pg/insert-select
:into :mytable
:select {:select {:z :z :a "a" :b :b} :from :t}
:returning :*}
["INSERT INTO mytable ( a, b, z ) ( SELECT 'a' as a , b as b , z as z FROM t ) RETURNING *"])
["INSERT INTO mytable ( \"a\", \"b\", \"z\" ) ( SELECT 'a' as a , b as b , z as z FROM t ) RETURNING *"])

(format=
{:ql/type :pg/cte
Expand Down Expand Up @@ -739,9 +739,9 @@

(format= {:ql/type :pg/insert
:into :healthplan
:value {:a 1 :b :x :c "str"}
:value {:a 1 :b :x :c "str" :birthDate "1991-11-08"}
:returning :*}
["INSERT INTO healthplan ( a, b, c ) VALUES ( 1 , x , 'str' ) RETURNING *"])
["INSERT INTO healthplan ( \"a\", \"b\", \"birthDate\", \"c\" ) VALUES ( 1 , x , '1991-11-08' , 'str' ) RETURNING *"])


(format= {:ql/type :pg/insert
Expand All @@ -751,7 +751,7 @@
:do {:set {:a :excluded.a}
:where [:= 1 2]}}
:returning :*}
["INSERT INTO healthplan ( a, b, c ) VALUES ( 1 , x , 'str' ) ON CONFLICT ( id ) DO UPDATE SET a = excluded.a WHERE 1 = 2 RETURNING *"])
["INSERT INTO healthplan ( \"a\", \"b\", \"c\" ) VALUES ( 1 , x , 'str' ) ON CONFLICT ( id ) DO UPDATE SET a = excluded.a WHERE 1 = 2 RETURNING *"])

(format= [:resource|| ^:pg/obj{:a 1 :b 2}]
["resource || jsonb_build_object( 'a' , 1 , 'b' , 2 )"])
Expand Down Expand Up @@ -999,7 +999,7 @@
:values [{:id 1 :resource "1" :status "ready"}
{:id 2 :status "failure"}]}
:returning :*}
["INSERT INTO conceptmaprule ( id, txid, resource, status ) VALUES ( 1 , NULL , '1' , 'ready' ) , ( 2 , NULL , NULL , 'failure' ) RETURNING *"])
["INSERT INTO conceptmaprule ( \"id\", \"txid\", \"resource\", \"status\" ) VALUES ( 1 , NULL , '1' , 'ready' ) , ( 2 , NULL , NULL , 'failure' ) RETURNING *"])

(format=
{:ql/type :pg/select
Expand Down

0 comments on commit 1d733a6

Please sign in to comment.