Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MSSQL square bracket quotes #78

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/macaw/collect.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@

;;; tables

(def ^:private quotes (map str [\` \"]))
(def ^:private quotes (map str "`\"["))

(def ^:private closing {"[" "]"})

(defn- quoted? [s]
(some (fn [q]
(and (str/starts-with? s q)
(str/ends-with? s q)))
(str/ends-with? s (closing q q))))
quotes))

(defn- strip-quotes [s]
Expand Down
22 changes: 20 additions & 2 deletions src/macaw/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[macaw.collect :as collect]
[macaw.rewrite :as rewrite])
(:import
(net.sf.jsqlparser.parser CCJSqlParserUtil)))
(java.util.function Consumer)
(net.sf.jsqlparser.parser CCJSqlParser CCJSqlParserUtil)
(net.sf.jsqlparser.parser.feature Feature)))

(set! *warn-on-reflection* true)

Expand All @@ -19,6 +21,22 @@
(defn- unescape-keywords [sql _keywords]
(str/replace sql "____escaped____" ""))

(def ^:private features
{:backslash-escape-char Feature/allowBackslashEscapeCharacter
:complex-parsing Feature/allowComplexParsing
:postgres-syntax Feature/allowPostgresSpecificSyntax
:square-bracket-quotes Feature/allowSquareBracketQuotation
:unsupported-statements Feature/allowUnsupportedStatements})

(defn- ->Feature ^Feature [k]
(get features k))

(defn- ->parser-fn ^Consumer [opts]
(reify Consumer
(accept [_this parser]
(doseq [[f ^boolean v] (:features opts)]
(.withFeature ^CCJSqlParser parser (->Feature f) v)))))

(defn parsed-query
"Main entry point: takes a string query and returns a `Statement` object that can be handled by the other functions."
[^String query & {:as opts}]
Expand All @@ -30,7 +48,7 @@
(-> query
(str/replace #"\n{2,}" "\n")
(escape-keywords (:non-reserved-words opts))
(CCJSqlParserUtil/parse)))
(CCJSqlParserUtil/parse (->parser-fn opts))))

(defn query->components
"Given a parsed query (i.e., a [subclass of] `Statement`) return a map with the elements found within it.
Expand Down
19 changes: 15 additions & 4 deletions test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
(every? true? xs)
false))

(def components (comp m/query->components m/parsed-query))
(defn components [sql & {:as opts}]
(m/query->components (m/parsed-query sql opts) opts))

(def raw-components #(let [xs (empty %)] (into xs (keep :component) %)))
(def columns (comp raw-components :columns components))
(def source-columns (comp :source-columns components))
Expand Down Expand Up @@ -88,7 +90,6 @@ from foo")
(is (= #{{:table "core_user"}}
(tables "SELECT * FROM (SELECT DISTINCT email FROM core_user) q;")))))


(deftest tables-with-complex-aliases-issue-14-test
(testing "With an alias that is also a table name"
(is (= #{{:table "user"}
Expand Down Expand Up @@ -314,8 +315,8 @@ from foo")

(deftest complicated-mutations-test
;; https://github.com/metabase/macaw/issues/18
#_ (is (= #{"delete" "insert"}
(mutations "WITH outdated_orders AS (
#_(is (= #{"delete" "insert"}
(mutations "WITH outdated_orders AS (
DELETE FROM orders
WHERE
date <= '2018-01-01'
Expand Down Expand Up @@ -603,6 +604,16 @@ from foo")
:columns {{:table "x" :column "final"} "y"}}
{:non-reserved-words [:final]})))))

(deftest square-bracket-test
(testing "We can opt into allowing square brackets to quote things"
(is (=? {:tables #{{:schema "s" :table "t"}}
:columns #{{:schema "s" :table "t" :column "f"}}}
(update-vals
(components "SELECT [f] FROM [s].[t]"
{:features {:square-bracket-quotes true}
:preserve-identifiers? false})
raw-components)))))

(comment
(require 'hashp.core)
(require 'virgil)
Expand Down
Loading