Fix operators in value positions
This commit is contained in:
parent
5e7e224df4
commit
f8b913d667
1 changed files with 26 additions and 13 deletions
|
|
@ -37,6 +37,8 @@
|
||||||
|
|
||||||
(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)
|
||||||
|
|
@ -91,6 +93,10 @@
|
||||||
(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]
|
||||||
|
|
@ -121,40 +127,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 b) " IS NULL")
|
(nil? a) (str (to-sql-value b) " IS NULL")
|
||||||
(nil? b) (str (to-sql a) " IS NULL")
|
(nil? b) (str (to-sql-value a) " IS NULL")
|
||||||
:else (str (to-sql a) " = " (to-sql b)))))
|
:else (str (to-sql-value a) " = " (to-sql-value 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 b) " IS NOT NULL")
|
(nil? a) (str (to-sql-value b) " IS NOT NULL")
|
||||||
(nil? b) (str (to-sql a) " IS NOT NULL")
|
(nil? b) (str (to-sql-value a) " IS NOT NULL")
|
||||||
:else (str (to-sql a) " <> " (to-sql b)))))
|
:else (str (to-sql-value a) " <> " (to-sql-value 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 a) " < " (to-sql b))))
|
(str (to-sql-value a) " < " (to-sql-value 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 a) " <= " (to-sql b))))
|
(str (to-sql-value a) " <= " (to-sql-value 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 a) " > " (to-sql b))))
|
(str (to-sql-value a) " > " (to-sql-value 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 a) " >= " (to-sql b))))
|
(str (to-sql-value a) " >= " (to-sql-value b))))
|
||||||
|
|
||||||
(defmethod fn-handler "between" [_ field lower upper]
|
(defmethod fn-handler "between" [_ field lower upper]
|
||||||
(str (to-sql field) " BETWEEN " (to-sql lower) " AND " (to-sql upper)))
|
(str (to-sql-value field) " BETWEEN " (to-sql-value lower) " AND " (to-sql-value 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.
|
||||||
|
|
@ -163,7 +169,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 pattern)
|
(to-sql-value pattern)
|
||||||
(when (seq opts)
|
(when (seq opts)
|
||||||
(str " " (space-join (for [opt opts]
|
(str " " (space-join (for [opt opts]
|
||||||
(case opt
|
(case opt
|
||||||
|
|
@ -310,10 +316,17 @@
|
||||||
(paren-wrap sql-str)
|
(paren-wrap sql-str)
|
||||||
sql-str)))
|
sql-str)))
|
||||||
|
|
||||||
|
(declare format-predicate*)
|
||||||
|
|
||||||
(defn seq->sql [x]
|
(defn seq->sql [x]
|
||||||
(if *fn-context?*
|
(cond
|
||||||
|
*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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue