Merge pull request #40 from senior/add-cast-support

Added support for casts of the form CAST(foo AS type)
This commit is contained in:
Michael Blume 2015-03-03 20:18:21 -08:00
commit 12f73bfa5a
2 changed files with 11 additions and 0 deletions

View file

@ -99,6 +99,11 @@
(defmethod fn-handler "distinct-on" [_ & args]
(str "DISTINCT ON (" (comma-join (map to-sql args)) ")"))
(defmethod fn-handler "cast" [_ field cast-to-type]
(str "CAST" (paren-wrap (str (to-sql field)
" AS "
(to-sql cast-to-type)))))
(defmethod fn-handler "=" [_ a b & more]
(if (seq more)
(apply expand-binary-ops "=" a b more)

View file

@ -55,3 +55,9 @@
"bort" "gabba" 2])))
(testing "SQL data prints and reads correctly"
(is (= m1 (read-string (pr-str m1)))))))
(deftest test-cast
(is (= ["SELECT foo, CAST(bar AS integer)"]
(sql/format {:select [:foo (sql/call :cast :bar :integer)]})))
(is (= ["SELECT foo, CAST(bar AS integer)"]
(sql/format {:select [:foo (sql/call :cast :bar 'integer)]}))))