Rename sql-format back to format now Chlorine is updated!
This commit is contained in:
parent
1dc0447244
commit
834ac3a096
2 changed files with 24 additions and 28 deletions
|
|
@ -227,19 +227,15 @@
|
||||||
:else
|
:else
|
||||||
["?" x]))
|
["?" 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
|
(defn format
|
||||||
"Turn the data DSL into a vector containing a SQL string followed by
|
"Turn the data DSL into a vector containing a SQL string followed by
|
||||||
any parameter values that were encountered in the DSL structure."
|
any parameter values that were encountered in the DSL structure."
|
||||||
([data] (sql-format data {}))
|
([data] (format data {}))
|
||||||
([data opts] (sql-format data opts)))
|
([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!
|
(defn set-dialect!
|
||||||
"Set the default dialect for formatting.
|
"Set the default dialect for formatting.
|
||||||
|
|
@ -257,13 +253,13 @@
|
||||||
(format-expr 1)
|
(format-expr 1)
|
||||||
(format-where :where [:= :id 1])
|
(format-where :where [:= :id 1])
|
||||||
(format-dsl {:select [:*] :from [:table] :where [:= :id 1]})
|
(format-dsl {:select [:*] :from [:table] :where [:= :id 1]})
|
||||||
(sql-format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} {})
|
(format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} {})
|
||||||
(sql-format {:select [:*] :from [:table] :group-by [:foo :bar]} {})
|
(format {:select [:*] :from [:table] :group-by [:foo :bar]} {})
|
||||||
(sql-format {:select [:*] :from [:table] :group-by [[:date :bar]]} {})
|
(format {:select [:*] :from [:table] :group-by [[:date :bar]]} {})
|
||||||
(sql-format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} {})
|
(format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} {})
|
||||||
(sql-format {:select [:*] :from [:table] :order-by [[[:date :expiry] :desc] :bar]} {})
|
(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 [:*] :from [:table] :where [:< [:date_add :expiry [:interval 30 :days]] [:now]]} {})
|
||||||
(format-expr [:interval 30 :days])
|
(format-expr [:interval 30 :days])
|
||||||
(sql-format {:select [:*] :from [:table] :where [:= :id 1]} {:dialect :mysql})
|
(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 [:in :id [1 2 3 4]]} {})
|
||||||
,)
|
,)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
(deftest mysql-tests
|
(deftest mysql-tests
|
||||||
(is (= ["SELECT * FROM `table` WHERE `id` = ?" 1]
|
(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}))))
|
{:dialect :mysql}))))
|
||||||
|
|
||||||
(deftest expr-tests
|
(deftest expr-tests
|
||||||
|
|
@ -31,20 +31,20 @@
|
||||||
|
|
||||||
(deftest general-tests
|
(deftest general-tests
|
||||||
(is (= ["SELECT * FROM \"table\" WHERE \"id\" = ?" 1]
|
(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]
|
(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\""]
|
(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\")"]
|
(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"]
|
(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"]
|
(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]
|
(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]
|
(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]
|
(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]]} {}))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue