Merge pull request #65 from MichaelBlume/value
Provide a wrapper to prevent maps/vectors from being read as subqueries
This commit is contained in:
commit
a7f331fd4a
2 changed files with 35 additions and 12 deletions
|
|
@ -247,9 +247,27 @@
|
||||||
(into [sql-str] @*params*)
|
(into [sql-str] @*params*)
|
||||||
[sql-str]))))
|
[sql-str]))))
|
||||||
|
|
||||||
|
(defn add-param [pname pval]
|
||||||
|
(swap! *param-names* conj pname)
|
||||||
|
(swap! *params* conj pval)
|
||||||
|
(*parameterizer*))
|
||||||
|
|
||||||
|
;; Anonymous param name -- :_1, :_2, etc.
|
||||||
|
(defn add-anon-param [pval]
|
||||||
|
(add-param
|
||||||
|
(keyword (str "_" (swap! *param-counter* inc)))
|
||||||
|
pval))
|
||||||
|
|
||||||
(defprotocol ToSql
|
(defprotocol ToSql
|
||||||
(-to-sql [x]))
|
(-to-sql [x]))
|
||||||
|
|
||||||
|
(defrecord Value [v]
|
||||||
|
ToSql
|
||||||
|
(-to-sql [_]
|
||||||
|
(add-anon-param v)))
|
||||||
|
|
||||||
|
(defn value [x] (Value. x))
|
||||||
|
|
||||||
(declare -format-clause)
|
(declare -format-clause)
|
||||||
|
|
||||||
(extend-protocol ToSql
|
(extend-protocol ToSql
|
||||||
|
|
@ -303,20 +321,17 @@
|
||||||
sql-str)))
|
sql-str)))
|
||||||
nil
|
nil
|
||||||
(-to-sql [x] "NULL")
|
(-to-sql [x] "NULL")
|
||||||
|
SqlParam
|
||||||
|
(-to-sql [x]
|
||||||
|
(let [pname (param-name x)]
|
||||||
|
(if (map? @*input-params*)
|
||||||
|
(add-param pname (get @*input-params* pname))
|
||||||
|
(let [x (first @*input-params*)]
|
||||||
|
(swap! *input-params* rest)
|
||||||
|
(add-param pname x)))))
|
||||||
Object
|
Object
|
||||||
(-to-sql [x]
|
(-to-sql [x]
|
||||||
(let [[x pname] (if (instance? SqlParam x)
|
(add-anon-param x)))
|
||||||
(let [pname (param-name x)]
|
|
||||||
(if (map? @*input-params*)
|
|
||||||
[(get @*input-params* pname) pname]
|
|
||||||
(let [x (first @*input-params*)]
|
|
||||||
(swap! *input-params* rest)
|
|
||||||
[x pname])))
|
|
||||||
;; Anonymous param name -- :_1, :_2, etc.
|
|
||||||
[x (keyword (str "_" (swap! *param-counter* inc)))])]
|
|
||||||
(swap! *param-names* conj pname)
|
|
||||||
(swap! *params* conj x)
|
|
||||||
(*parameterizer*))))
|
|
||||||
|
|
||||||
(defn sqlable? [x]
|
(defn sqlable? [x]
|
||||||
(satisfies? ToSql x))
|
(satisfies? ToSql x))
|
||||||
|
|
|
||||||
|
|
@ -67,3 +67,11 @@
|
||||||
(sql/format {:select [:foo (sql/call :cast :bar :integer)]})))
|
(sql/format {:select [:foo (sql/call :cast :bar :integer)]})))
|
||||||
(is (= ["SELECT foo, CAST(bar AS integer)"]
|
(is (= ["SELECT foo, CAST(bar AS integer)"]
|
||||||
(sql/format {:select [:foo (sql/call :cast :bar 'integer)]}))))
|
(sql/format {:select [:foo (sql/call :cast :bar 'integer)]}))))
|
||||||
|
|
||||||
|
(deftest test-value
|
||||||
|
(is (= ["INSERT INTO foo (bar) VALUES (?)" {:baz "my-val"}]
|
||||||
|
(->
|
||||||
|
(insert-into :foo)
|
||||||
|
(columns :bar)
|
||||||
|
(values [[(honeysql.format/value {:baz "my-val"})]])
|
||||||
|
sql/format))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue