This commit is contained in:
DTB01 2015-07-22 15:14:29 +00:00
commit fd4883ebaa
2 changed files with 20 additions and 6 deletions

View file

@ -85,7 +85,9 @@
"regex" "regexp"}) "regex" "regexp"})
(defprotocol ToSql (defprotocol ToSql
(to-sql [x])) (to-sql
[x]
[x & args]))
(defmulti fn-handler (fn [op & args] op)) (defmulti fn-handler (fn [op & args] op))
@ -277,13 +279,14 @@
(extend-protocol ToSql (extend-protocol ToSql
clojure.lang.Keyword clojure.lang.Keyword
(to-sql [x] (to-sql [x & {:as args}]
(let [s (name x)] (let [s (name x)]
(case (.charAt s 0) (case (.charAt s 0)
\% (let [call-args (string/split (subs s 1) #"\." 2)] \% (let [call-args (string/split (subs s 1) #"\." 2)]
(to-sql (apply call (map keyword call-args)))) (to-sql (apply call (map keyword call-args))))
\? (to-sql (param (keyword (subs s 1)))) \? (to-sql (param (keyword (subs s 1))))
(quote-identifier x)))) (->> (apply concat (select-keys args [:split]))
(apply quote-identifier x)))))
clojure.lang.Symbol clojure.lang.Symbol
(to-sql [x] (quote-identifier x)) (to-sql [x] (quote-identifier x))
java.lang.Number java.lang.Number
@ -302,9 +305,13 @@
(if (= :select *clause*) (if (= :select *clause*)
" AS " " AS "
" ") " ")
(if (string? (second x)) (cond (string? (second x))
(quote-identifier (second x)) (quote-identifier (second x) :split false)
(to-sql (second x)))))) (keyword? (second x))
(to-sql (second x) :split false)
:else
(to-sql (second x))
))))
SqlCall SqlCall
(to-sql [x] (to-sql [x]
(binding [*fn-context?* true] (binding [*fn-context?* true]

View file

@ -127,3 +127,10 @@
:from [:customers] :from [:customers]
:where [:in :id :?ids]} :where [:in :id :?ids]}
{:ids values}))))))) {:ids values})))))))
(deftest test-quoting
(testing ":quoting :ansi"
(is (= ["SELECT \"b\".\"bla\" AS \"bla.bla\", \"b\".\"bla\" AS \"blu.blu\" FROM \"baz\" \"b\"" ]
(-> (select [:b.bla :bla.bla] [:b.bla "blu.blu"])
(from [:baz :b])
(sql/format :quoting :ansi))))))