Merge pull request #169 from MichaelBlume/fix-tuples
Revert #149 and restore tuple behavior
This commit is contained in:
commit
3eaf5edbc0
2 changed files with 21 additions and 28 deletions
|
|
@ -37,8 +37,6 @@
|
||||||
|
|
||||||
(def ^:dynamic *fn-context?* false)
|
(def ^:dynamic *fn-context?* false)
|
||||||
|
|
||||||
(def ^:dynamic *value-context?* false)
|
|
||||||
|
|
||||||
(def ^:dynamic *subquery?* false)
|
(def ^:dynamic *subquery?* false)
|
||||||
|
|
||||||
(def ^:dynamic *allow-dashed-names?* false)
|
(def ^:dynamic *allow-dashed-names?* false)
|
||||||
|
|
@ -96,10 +94,6 @@
|
||||||
(defprotocol ToSql
|
(defprotocol ToSql
|
||||||
(to-sql [x]))
|
(to-sql [x]))
|
||||||
|
|
||||||
(defn to-sql-value [x]
|
|
||||||
(binding [*value-context?* true]
|
|
||||||
(to-sql x)))
|
|
||||||
|
|
||||||
(defmulti fn-handler (fn [op & args] op))
|
(defmulti fn-handler (fn [op & args] op))
|
||||||
|
|
||||||
(defn expand-binary-ops [op & args]
|
(defn expand-binary-ops [op & args]
|
||||||
|
|
@ -130,40 +124,40 @@
|
||||||
(if (seq more)
|
(if (seq more)
|
||||||
(apply expand-binary-ops "=" a b more)
|
(apply expand-binary-ops "=" a b more)
|
||||||
(cond
|
(cond
|
||||||
(nil? a) (str (to-sql-value b) " IS NULL")
|
(nil? a) (str (to-sql b) " IS NULL")
|
||||||
(nil? b) (str (to-sql-value a) " IS NULL")
|
(nil? b) (str (to-sql a) " IS NULL")
|
||||||
:else (str (to-sql-value a) " = " (to-sql-value b)))))
|
:else (str (to-sql a) " = " (to-sql b)))))
|
||||||
|
|
||||||
(defmethod fn-handler "<>" [_ a b & more]
|
(defmethod fn-handler "<>" [_ a b & more]
|
||||||
(if (seq more)
|
(if (seq more)
|
||||||
(apply expand-binary-ops "<>" a b more)
|
(apply expand-binary-ops "<>" a b more)
|
||||||
(cond
|
(cond
|
||||||
(nil? a) (str (to-sql-value b) " IS NOT NULL")
|
(nil? a) (str (to-sql b) " IS NOT NULL")
|
||||||
(nil? b) (str (to-sql-value a) " IS NOT NULL")
|
(nil? b) (str (to-sql a) " IS NOT NULL")
|
||||||
:else (str (to-sql-value a) " <> " (to-sql-value b)))))
|
:else (str (to-sql a) " <> " (to-sql b)))))
|
||||||
|
|
||||||
(defmethod fn-handler "<" [_ a b & more]
|
(defmethod fn-handler "<" [_ a b & more]
|
||||||
(if (seq more)
|
(if (seq more)
|
||||||
(apply expand-binary-ops "<" a b more)
|
(apply expand-binary-ops "<" a b more)
|
||||||
(str (to-sql-value a) " < " (to-sql-value b))))
|
(str (to-sql a) " < " (to-sql b))))
|
||||||
|
|
||||||
(defmethod fn-handler "<=" [_ a b & more]
|
(defmethod fn-handler "<=" [_ a b & more]
|
||||||
(if (seq more)
|
(if (seq more)
|
||||||
(apply expand-binary-ops "<=" a b more)
|
(apply expand-binary-ops "<=" a b more)
|
||||||
(str (to-sql-value a) " <= " (to-sql-value b))))
|
(str (to-sql a) " <= " (to-sql b))))
|
||||||
|
|
||||||
(defmethod fn-handler ">" [_ a b & more]
|
(defmethod fn-handler ">" [_ a b & more]
|
||||||
(if (seq more)
|
(if (seq more)
|
||||||
(apply expand-binary-ops ">" a b more)
|
(apply expand-binary-ops ">" a b more)
|
||||||
(str (to-sql-value a) " > " (to-sql-value b))))
|
(str (to-sql a) " > " (to-sql b))))
|
||||||
|
|
||||||
(defmethod fn-handler ">=" [_ a b & more]
|
(defmethod fn-handler ">=" [_ a b & more]
|
||||||
(if (seq more)
|
(if (seq more)
|
||||||
(apply expand-binary-ops ">=" a b more)
|
(apply expand-binary-ops ">=" a b more)
|
||||||
(str (to-sql-value a) " >= " (to-sql-value b))))
|
(str (to-sql a) " >= " (to-sql b))))
|
||||||
|
|
||||||
(defmethod fn-handler "between" [_ field lower upper]
|
(defmethod fn-handler "between" [_ field lower upper]
|
||||||
(str (to-sql-value field) " BETWEEN " (to-sql-value lower) " AND " (to-sql-value upper)))
|
(str (to-sql field) " BETWEEN " (to-sql lower) " AND " (to-sql upper)))
|
||||||
|
|
||||||
;; Handles MySql's MATCH (field) AGAINST (pattern). The third argument
|
;; Handles MySql's MATCH (field) AGAINST (pattern). The third argument
|
||||||
;; can be a set containing one or more of :boolean, :natural, or :expand.
|
;; can be a set containing one or more of :boolean, :natural, or :expand.
|
||||||
|
|
@ -172,7 +166,7 @@
|
||||||
(comma-join
|
(comma-join
|
||||||
(map to-sql (if (coll? fields) fields [fields])))
|
(map to-sql (if (coll? fields) fields [fields])))
|
||||||
") AGAINST ("
|
") AGAINST ("
|
||||||
(to-sql-value pattern)
|
(to-sql pattern)
|
||||||
(when (seq opts)
|
(when (seq opts)
|
||||||
(str " " (space-join (for [opt opts]
|
(str " " (space-join (for [opt opts]
|
||||||
(case opt
|
(case opt
|
||||||
|
|
@ -319,17 +313,10 @@
|
||||||
(paren-wrap sql-str)
|
(paren-wrap sql-str)
|
||||||
sql-str)))
|
sql-str)))
|
||||||
|
|
||||||
(declare format-predicate*)
|
|
||||||
|
|
||||||
(defn seq->sql [x]
|
(defn seq->sql [x]
|
||||||
(cond
|
(if *fn-context?*
|
||||||
*value-context?*
|
|
||||||
;; sequences are operators/functions
|
|
||||||
(format-predicate* x)
|
|
||||||
*fn-context?*
|
|
||||||
;; list argument in fn call
|
;; list argument in fn call
|
||||||
(paren-wrap (comma-join (map to-sql x)))
|
(paren-wrap (comma-join (map to-sql x)))
|
||||||
:else
|
|
||||||
;; alias
|
;; alias
|
||||||
(str (to-sql (first x))
|
(str (to-sql (first x))
|
||||||
; Omit AS in FROM, JOIN, etc. - Oracle doesn't allow it
|
; Omit AS in FROM, JOIN, etc. - Oracle doesn't allow it
|
||||||
|
|
|
||||||
|
|
@ -116,11 +116,11 @@
|
||||||
["SELECT amount, id, created_on FROM transactions UNION SELECT amount, id, created_on FROM (SELECT amount, id, created_on FROM other_transactions ORDER BY amount DESC LIMIT ?) ORDER BY amount ASC" 5]))))
|
["SELECT amount, id, created_on FROM transactions UNION SELECT amount, id, created_on FROM (SELECT amount, id, created_on FROM other_transactions ORDER BY amount DESC LIMIT ?) ORDER BY amount ASC" 5]))))
|
||||||
|
|
||||||
(deftest compare-expressions-test
|
(deftest compare-expressions-test
|
||||||
(testing "Sequences should be fns when in value/comparison spots"
|
(testing "sql/call should produce appropriate function calls"
|
||||||
(is (= ["SELECT foo FROM bar WHERE (col1 mod ?) = (col2 + ?)" 4 4]
|
(is (= ["SELECT foo FROM bar WHERE (col1 mod ?) = (col2 + ?)" 4 4]
|
||||||
(format {:select [:foo]
|
(format {:select [:foo]
|
||||||
:from [:bar]
|
:from [:bar]
|
||||||
:where [:= [:mod :col1 4] [:+ :col2 4]]})))))
|
:where (sql/call := (sql/call :mod :col1 4) (sql/call :+ :col2 4))})))))
|
||||||
|
|
||||||
(deftest union-with-cte
|
(deftest union-with-cte
|
||||||
(is (= (format {:union [{:select [:foo] :from [:bar1]}
|
(is (= (format {:union [{:select [:foo] :from [:bar1]}
|
||||||
|
|
@ -136,3 +136,9 @@
|
||||||
:with [[[:bar {:columns [:spam :eggs]}]
|
:with [[[:bar {:columns [:spam :eggs]}]
|
||||||
{:values [[1 2] [3 4] [5 6]]}]]})
|
{:values [[1 2] [3 4] [5 6]]}]]})
|
||||||
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION ALL SELECT foo FROM bar2" 1 2 3 4 5 6])))
|
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION ALL SELECT foo FROM bar2" 1 2 3 4 5 6])))
|
||||||
|
|
||||||
|
(deftest format-tuples
|
||||||
|
(is (= ["SELECT id FROM table WHERE (a, b) = (?, ?)" 1 2]
|
||||||
|
(format {:select [:id]
|
||||||
|
:from [:table]
|
||||||
|
:where [:= [:a :b] [1 2]]}))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue