Implement array as SQL syntax instead of special HoneySQL syntax

This commit is contained in:
Sean Corfield 2020-09-23 22:25:13 -07:00
parent 4408a6e7d2
commit 97531fa4cf
2 changed files with 25 additions and 24 deletions

View file

@ -402,7 +402,11 @@
(->> (into #{} (map keyword))))) (->> (into #{} (map keyword)))))
(def ^:private special-syntax (def ^:private special-syntax
{:between {:array
(fn [[arr]]
(let [[sqls params] (format-expr-list arr)]
(into [(str "ARRAY[" (str/join ", " sqls) "]")] params)))
:between
(fn [[x a b]] (fn [[x a b]]
(let [[sql-x & params-x] (format-expr x {:nested? true}) (let [[sql-x & params-x] (format-expr x {:nested? true})
[sql-a & params-a] (format-expr a {:nested? true}) [sql-a & params-a] (format-expr a {:nested? true})

View file

@ -13,19 +13,19 @@
(deftest expr-tests (deftest expr-tests
(is (= ["id = ?" 1] (is (= ["id = ?" 1]
(#'sut/format-expr [:= :id 1]))) (sut/format-expr [:= :id 1])))
(is (= ["id + ?" 1] (is (= ["id + ?" 1]
(#'sut/format-expr [:+ :id 1]))) (sut/format-expr [:+ :id 1])))
(is (= ["? + (? + quux)" 1 1] (is (= ["? + (? + quux)" 1 1]
(#'sut/format-expr [:+ 1 [:+ 1 :quux]]))) (sut/format-expr [:+ 1 [:+ 1 :quux]])))
(is (= ["FOO(BAR(? + G(abc)), F(?, quux))" 2 1] (is (= ["FOO(BAR(? + G(abc)), F(?, quux))" 2 1]
(#'sut/format-expr [:foo [:bar [:+ 2 [:g :abc]]] [:f 1 :quux]]))) (sut/format-expr [:foo [:bar [:+ 2 [:g :abc]]] [:f 1 :quux]])))
(is (= ["id"] (is (= ["id"]
(#'sut/format-expr :id))) (sut/format-expr :id)))
(is (= ["?" 1] (is (= ["?" 1]
(#'sut/format-expr 1))) (sut/format-expr 1)))
(is (= ["INTERVAL ? DAYS" 30] (is (= ["INTERVAL ? DAYS" 30]
(#'sut/format-expr [:interval 30 :days])))) (sut/format-expr [:interval 30 :days]))))
(deftest where-test (deftest where-test
(is (= ["WHERE id = ?" 1] (is (= ["WHERE id = ?" 1]
@ -119,16 +119,14 @@
["SELECT id FROM foo WHERE EXISTS (SELECT ? FROM bar WHERE deleted)" 1]))) ["SELECT id FROM foo WHERE EXISTS (SELECT ? FROM bar WHERE deleted)" 1])))
(deftest array-test (deftest array-test
(is nil "sql-array unimplemented") (is (= (format {:insert-into :foo
#_(is (= (format {:insert-into :foo :columns [:baz]
:columns [:baz] :values [[[:array [1 2 3 4]]]]})
:values [[(sql/array [1 2 3 4])]]}) ["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?, ?])" 1 2 3 4]))
["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?, ?])" 1 2 3 4])) (is (= (format {:insert-into :foo
(is nil "sql-array unimplemented") :columns [:baz]
#_(is (= (format {:insert-into :foo :values [[[:array ["one" "two" "three"]]]]})
:columns [:baz] ["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?])" "one" "two" "three"])))
:values [[(sql/array ["one" "two" "three"])]]})
["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?])" "one" "two" "three"])))
(deftest union-test (deftest union-test
;; UNION and INTERSECT subexpressions should not be parenthesized. ;; UNION and INTERSECT subexpressions should not be parenthesized.
@ -216,12 +214,11 @@
(deftest parameterizer-none (deftest parameterizer-none
(testing "array parameter" (testing "array parameter"
(is nil "sql-array unimplemented") (is (= (format {:insert-into :foo
#_(is (= (format {:insert-into :foo :columns [:baz]
:columns [:baz] :values [[[:array [1 2 3 4]]]]}
:values [[(sql/array [1 2 3 4])]]} {:parameterizer :none})
{:parameterizer :none}) ["INSERT INTO foo (baz) VALUES (ARRAY[1, 2, 3, 4])"])))
["INSERT INTO foo (baz) VALUES (ARRAY[1, 2, 3, 4])"])))
(testing "union complex values" (testing "union complex values"
(is (= (format {:union [{:select [:foo] :from [:bar1]} (is (= (format {:union [{:select [:foo] :from [:bar1]}