Skip to content

Commit

Permalink
Allow ident table name
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitem committed Oct 31, 2023
1 parent 3bb38e6 commit 1181f28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/dsql/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@
(defn safe-identifier? [s]
(some? (re-matches #"^[_a-zA-Z][_.a-zA-Z0-9]*$" s)))


(defn escape-ident [keywords node]
(let [norm-name (str/upper-case (name node))]
(if (or (not (safe-identifier? norm-name))
(contains? keywords (keyword norm-name)))
(str "\"" (name node) "\"")
(name node))))
(if (= \" (first (name node)))
(name node)
(if (or (not (safe-identifier? norm-name))
(contains? keywords (keyword norm-name)))
(str "\"" (name node) "\"")
(name node)))))

(defn parens [acc body-cb]
(-> (conj acc "(")
Expand Down
15 changes: 15 additions & 0 deletions test/dsql/pg_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,12 @@
["( case ( resource->>'date' ) when ( '123' ) then ( e.resource ->> 'processed' ) when ( '456' ) then ( e.resource ->> 'order_id' ) else ( d.resource #>> '{partOf,id}' ) end )"])

(testing "CREATE TABLE"
(format=
{:ql/type :pg/create-table
:table-name "\"MyTable\""
:columns {:a {:type "integer" }}}
["CREATE TABLE \"MyTable\" ( \"a\" integer )"])

(format=
{:ql/type :pg/create-table
:table-name "mytable"
Expand All @@ -625,6 +631,15 @@
["CREATE TABLE mytable ( \"id\" uuid not null , \"version\" uuid not null , \"cts\" timestamptz not null DEFAULT current_timestamp , \"ts\" timestamptz not null DEFAULT current_timestamp , \"status\" resource_status not null , \"partition\" int not null , \"resource\" jsonb not null )"])

(testing "default value"



(format=
{:ql/type :pg/create-table
:table-name :mytable
:columns {:a {:type "integer" :not-null true :default 8}}}
["CREATE TABLE mytable ( \"a\" integer NOT NULL DEFAULT ? )" 8])

(format=
{:ql/type :pg/create-table
:table-name "mytable"
Expand Down

0 comments on commit 1181f28

Please sign in to comment.