diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 5fa997e..c213418 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -227,19 +227,15 @@ :else ["?" x])) -(defn- sql-format - "Format the data into SQL + params according to the options." - [data opts] - (let [dialect (get dialects (get opts :dialect :ansi))] - (binding [*dialect* dialect - *quoted* (if (contains? opts :quoted) (:quoted opts) true)] - (format-dsl data)))) - (defn format "Turn the data DSL into a vector containing a SQL string followed by any parameter values that were encountered in the DSL structure." - ([data] (sql-format data {})) - ([data opts] (sql-format data opts))) + ([data] (format data {})) + ([data opts] + (let [dialect (get dialects (get opts :dialect :ansi))] + (binding [*dialect* dialect + *quoted* (if (contains? opts :quoted) (:quoted opts) true)] + (format-dsl data))))) (defn set-dialect! "Set the default dialect for formatting. @@ -257,13 +253,13 @@ (format-expr 1) (format-where :where [:= :id 1]) (format-dsl {:select [:*] :from [:table] :where [:= :id 1]}) - (sql-format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} {}) - (sql-format {:select [:*] :from [:table] :group-by [:foo :bar]} {}) - (sql-format {:select [:*] :from [:table] :group-by [[:date :bar]]} {}) - (sql-format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} {}) - (sql-format {:select [:*] :from [:table] :order-by [[[:date :expiry] :desc] :bar]} {}) - (sql-format {:select [:*] :from [:table] :where [:< [:date_add :expiry [:interval 30 :days]] [:now]]} {}) + (format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} {}) + (format {:select [:*] :from [:table] :group-by [:foo :bar]} {}) + (format {:select [:*] :from [:table] :group-by [[:date :bar]]} {}) + (format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} {}) + (format {:select [:*] :from [:table] :order-by [[[:date :expiry] :desc] :bar]} {}) + (format {:select [:*] :from [:table] :where [:< [:date_add :expiry [:interval 30 :days]] [:now]]} {}) (format-expr [:interval 30 :days]) - (sql-format {:select [:*] :from [:table] :where [:= :id 1]} {:dialect :mysql}) - (sql-format {:select [:*] :from [:table] :where [:in :id [1 2 3 4]]} {}) + (format {:select [:*] :from [:table] :where [:= :id 1]} {:dialect :mysql}) + (format {:select [:*] :from [:table] :where [:in :id [1 2 3 4]]} {}) ,) diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 8c0f0f8..3b16a68 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -7,7 +7,7 @@ (deftest mysql-tests (is (= ["SELECT * FROM `table` WHERE `id` = ?" 1] - (#'sut/sql-format {:select [:*] :from [:table] :where [:= :id 1]} + (#'sut/format {:select [:*] :from [:table] :where [:= :id 1]} {:dialect :mysql})))) (deftest expr-tests @@ -31,20 +31,20 @@ (deftest general-tests (is (= ["SELECT * FROM \"table\" WHERE \"id\" = ?" 1] - (#'sut/sql-format {:select [:*] :from [:table] :where [:= :id 1]} {}))) + (#'sut/format {:select [:*] :from [:table] :where [:= :id 1]} {}))) (is (= ["SELECT \"t\".* FROM \"table\" AS \"t\" WHERE \"id\" = ?" 1] - (#'sut/sql-format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} {}))) + (#'sut/format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} {}))) (is (= ["SELECT * FROM \"table\" GROUP BY \"foo\", \"bar\""] - (#'sut/sql-format {:select [:*] :from [:table] :group-by [:foo :bar]} {}))) + (#'sut/format {:select [:*] :from [:table] :group-by [:foo :bar]} {}))) (is (= ["SELECT * FROM \"table\" GROUP BY DATE(\"bar\")"] - (#'sut/sql-format {:select [:*] :from [:table] :group-by [[:date :bar]]} {}))) + (#'sut/format {:select [:*] :from [:table] :group-by [[:date :bar]]} {}))) (is (= ["SELECT * FROM \"table\" ORDER BY \"foo\" DESC, \"bar\" ASC"] - (#'sut/sql-format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} {}))) + (#'sut/format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} {}))) (is (= ["SELECT * FROM \"table\" ORDER BY DATE(\"expiry\") DESC, \"bar\" ASC"] - (#'sut/sql-format {:select [:*] :from [:table] :order-by [[[:date :expiry] :desc] :bar]} {}))) + (#'sut/format {:select [:*] :from [:table] :order-by [[[:date :expiry] :desc] :bar]} {}))) (is (= ["SELECT * FROM \"table\" WHERE DATE_ADD(\"expiry\", INTERVAL ? DAYS) < NOW()" 30] - (#'sut/sql-format {:select [:*] :from [:table] :where [:< [:date_add :expiry [:interval 30 :days]] [:now]]} {}))) + (#'sut/format {:select [:*] :from [:table] :where [:< [:date_add :expiry [:interval 30 :days]] [:now]]} {}))) (is (= ["SELECT * FROM `table` WHERE `id` = ?" 1] - (#'sut/sql-format {:select [:*] :from [:table] :where [:= :id 1]} {:dialect :mysql}))) + (#'sut/format {:select [:*] :from [:table] :where [:= :id 1]} {:dialect :mysql}))) (is (= ["SELECT * FROM \"table\" WHERE \"id\" IN (?,?,?,?)" 1 2 3 4] - (#'sut/sql-format {:select [:*] :from [:table] :where [:in :id [1 2 3 4]]} {})))) + (#'sut/format {:select [:*] :from [:table] :where [:in :id [1 2 3 4]]} {}))))