diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..85315ce 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -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) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index 161b52f..7f87d5b 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -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)]}))))