From 4ea630ed9078a32d73cd58204669778a0ffe10f3 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 16 Dec 2022 23:20:17 -0800 Subject: [PATCH] basic testing for numbered params --- src/honey/sql.cljc | 4 ++-- test/honey/sql_test.cljc | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index f051034..34b52ab 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -326,13 +326,13 @@ (let [n (count (swap! *numbered* conj v))] [(str "$" n) (with-meta (constantly (dec n)) {::wrapper - (fn [fk _] (get *numbered* (fk)))})])) + (fn [fk _] (get @*numbered* (fk)))})])) (defn ->numbered-param [k] (let [n (count (swap! *numbered* conj k))] [(str "$" n) (with-meta (constantly k) {::wrapper - (fn [fk _] (param-value (get *numbered* (fk))))})])) + (fn [fk _] (param-value (get @*numbered* (fk))))})])) (def ^:private ^:dynamic *formatted-column* (atom false)) diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 1b54476..8fe0621 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -94,6 +94,38 @@ (is (= ["SELECT * FROM \"table\" WHERE \"id\" IN (?, ?, ?, ?)" 1 2 3 4] (sut/format {:select [:*] :from [:table] :where [:in :id [1 2 3 4]]} {:quoted true})))) +(deftest general-numbered-tests + (is (= ["SELECT * FROM \"table\" WHERE \"id\" = $1" 1] + (sut/format {:select [:*] :from [:table] :where [:= :id 1]} + {:quoted true :numbered true}))) + (is (= ["SELECT * FROM \"table\" WHERE \"id\" = $1" 1] + (sut/format {:select [:*] :from [:table] :where (sut/map= {:id 1})} + {:quoted true :numbered true}))) + (is (= ["SELECT \"t\".* FROM \"table\" AS \"t\" WHERE \"id\" = $1" 1] + (sut/format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} + {:quoted true :numbered true}))) + (is (= ["SELECT * FROM \"table\" GROUP BY \"foo\", \"bar\""] + (sut/format {:select [:*] :from [:table] :group-by [:foo :bar]} + {:quoted true :numbered true}))) + (is (= ["SELECT * FROM \"table\" GROUP BY DATE(\"bar\")"] + (sut/format {:select [:*] :from [:table] :group-by [[:date :bar]]} + {:quoted true :numbered true}))) + (is (= ["SELECT * FROM \"table\" ORDER BY \"foo\" DESC, \"bar\" ASC"] + (sut/format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} + {:quoted true :numbered true}))) + (is (= ["SELECT * FROM \"table\" ORDER BY DATE(\"expiry\") DESC, \"bar\" ASC"] + (sut/format {:select [:*] :from [:table] :order-by [[[:date :expiry] :desc] :bar]} + {:quoted true :numbered true}))) + (is (= ["SELECT * FROM \"table\" WHERE DATE_ADD(\"expiry\", INTERVAL $1 DAYS) < NOW()" 30] + (sut/format {:select [:*] :from [:table] :where [:< [:date_add :expiry [:interval 30 :days]] [:now]]} + {:quoted true :numbered true}))) + (is (= ["SELECT * FROM `table` WHERE `id` = $1" 1] + (sut/format {:select [:*] :from [:table] :where [:= :id 1]} + {:dialect :mysql :numbered true}))) + (is (= ["SELECT * FROM \"table\" WHERE \"id\" IN ($1, $2, $3, $4)" 1 2 3 4] + (sut/format {:select [:*] :from [:table] :where [:in :id [1 2 3 4]]} + {:quoted true :numbered true})))) + ;; issue-based tests (deftest subquery-alias-263