allow inlining values

Most of the time we want to parameterize numbers, but sometimes we don't

Fixes #171
This commit is contained in:
Michael Blume 2017-07-18 11:27:39 -07:00
parent 942756d2b3
commit d99efb8606
4 changed files with 31 additions and 2 deletions

View file

@ -9,6 +9,7 @@
(#?(:clj defalias :cljs def) call types/call) (#?(:clj defalias :cljs def) call types/call)
(#?(:clj defalias :cljs def) raw types/raw) (#?(:clj defalias :cljs def) raw types/raw)
(#?(:clj defalias :cljs def) param types/param) (#?(:clj defalias :cljs def) param types/param)
(#?(:clj defalias :cljs def) inline types/inline)
(#?(:clj defalias :cljs def) format format/format) (#?(:clj defalias :cljs def) format format/format)
(#?(:clj defalias :cljs def) format-predicate format/format-predicate) (#?(:clj defalias :cljs def) format-predicate format/format-predicate)
(#?(:clj defalias :cljs def) quote-identifier format/quote-identifier) (#?(:clj defalias :cljs def) quote-identifier format/quote-identifier)

View file

@ -1,9 +1,9 @@
(ns honeysql.format (ns honeysql.format
(:refer-clojure :exclude [format]) (:refer-clojure :exclude [format])
(:require [honeysql.types :refer [call raw param param-name (:require [honeysql.types :refer [call raw param param-name
#?@(:cljs [SqlCall SqlRaw SqlParam SqlArray])]] #?@(:cljs [SqlCall SqlRaw SqlParam SqlArray SqlInline])]]
[clojure.string :as string]) [clojure.string :as string])
#?(:clj (:import [honeysql.types SqlCall SqlRaw SqlParam SqlArray]))) #?(:clj (:import [honeysql.types SqlCall SqlRaw SqlParam SqlArray SqlInline])))
;;(set! *warn-on-reflection* true) ;;(set! *warn-on-reflection* true)
@ -388,6 +388,9 @@
SqlArray SqlArray
(to-sql [x] (to-sql [x]
(str "ARRAY[" (comma-join (map to-sql (.-values x))) "]")) (str "ARRAY[" (comma-join (map to-sql (.-values x))) "]"))
SqlInline
(to-sql [x]
(str (.-value x)))
#?(:clj Object :cljs default) #?(:clj Object :cljs default)
(to-sql [x] (to-sql [x]
#?(:clj (add-anon-param x) #?(:clj (add-anon-param x)

View file

@ -57,6 +57,18 @@
;; late bind, as above ;; late bind, as above
(#?(:clj (resolve `array) :cljs array) form)) (#?(:clj (resolve `array) :cljs array) form))
;;;;
(defrecord SqlInline [value])
(defn inline
"Prevents parameterization"
[value]
(SqlInline. value))
(defn read-sql-inline [form]
(#?(:clj (resolve `inline) :cljs inline) form))
#?(:clj #?(:clj
(do (do
(defmethod print-method SqlCall [^SqlCall o ^java.io.Writer w] (defmethod print-method SqlCall [^SqlCall o ^java.io.Writer w]
@ -81,4 +93,10 @@
(.write w (str "#sql/array " (pr-str (.values a))))) (.write w (str "#sql/array " (pr-str (.values a)))))
(defmethod print-dup SqlArray [a w] (defmethod print-dup SqlArray [a w]
(print-method a w))
(defmethod print-method SqlInline [^SqlInline a ^java.io.Writer w]
(.write w (str "#sql/inline " (pr-str (.value a)))))
(defmethod print-dup SqlInline [a w]
(print-method a w)))) (print-method a w))))

View file

@ -192,4 +192,11 @@
(join :x [:= :foo.id :x.id] :y nil) (join :x [:= :foo.id :x.id] :y nil)
sql/format))))) sql/format)))))
(deftest inline-test
(is (= ["SELECT * FROM foo WHERE id = 5"]
(-> (select :*)
(from :foo)
(where [:= :id (sql/inline 5)])
sql/format))))
#?(:cljs (cljs.test/run-all-tests)) #?(:cljs (cljs.test/run-all-tests))