From a996b93dd3ae0a92194819fe5009ae0bf60fb8f6 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 26 Mar 2015 22:17:29 -0700 Subject: [PATCH 1/4] pull out a couple helpers for param logic --- src/honeysql/format.clj | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index cace514..1f3e6e7 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -247,6 +247,17 @@ (into [sql-str] @*params*) [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 (-to-sql [x])) @@ -305,18 +316,14 @@ (-to-sql [x] "NULL") Object (-to-sql [x] - (let [[x pname] (if (instance? SqlParam 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*)))) + (if (instance? SqlParam 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)))) + (add-anon-param x)))) (defn sqlable? [x] (satisfies? ToSql x)) From 48c501d2c4eddd27a17656e69e5600b5d2193be6 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 26 Mar 2015 22:20:22 -0700 Subject: [PATCH 2/4] break out SqlParam as its own ToSql instance --- src/honeysql/format.clj | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 1f3e6e7..8fdd27a 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -314,16 +314,17 @@ sql-str))) nil (-to-sql [x] "NULL") + SqlParam + (-to-seql [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 (-to-sql [x] - (if (instance? SqlParam 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)))) - (add-anon-param x)))) + (add-anon-param x))) (defn sqlable? [x] (satisfies? ToSql x)) From d74f2d2437d48615b65f906a30119d06f63db4eb Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 26 Mar 2015 17:03:38 -0700 Subject: [PATCH 3/4] Allow maps/vectors not to be read as subqueries --- src/honeysql/format.clj | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 8fdd27a..b42b020 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -261,6 +261,13 @@ (defprotocol ToSql (-to-sql [x])) +(defrecord Value [v] + ToSql + (-to-sql [_] + (add-anon-param v))) + +(defn value [x] (Value. x)) + (declare -format-clause) (extend-protocol ToSql @@ -315,7 +322,7 @@ nil (-to-sql [x] "NULL") SqlParam - (-to-seql [x] + (-to-sql [x] (let [pname (param-name x)] (if (map? @*input-params*) (add-param pname (get @*input-params* pname)) From 0c89c0362e7d5ad6fe9471356560e724165d07cb Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Tue, 31 Mar 2015 10:32:48 -0700 Subject: [PATCH 4/4] add value test --- test/honeysql/core_test.clj | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index e58c49c..d7c5543 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -67,3 +67,11 @@ (sql/format {:select [:foo (sql/call :cast :bar :integer)]}))) (is (= ["SELECT foo, CAST(bar AS 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))))