diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 7ff2220..7e28425 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -161,7 +161,9 @@ (def default-clause-priorities "Determines the order that clauses will be placed within generated SQL" - {:select 50 + {:with 30 + :with-recursive 40 + :select 50 :insert-into 60 :update 70 :delete-from 80 @@ -416,3 +418,19 @@ (defmethod format-clause :delete-from [[_ table] _] (str "DELETE FROM " (to-sql table))) + +(defn cte->sql + [[cte-name query]] + (str (to-sql cte-name) " AS " (to-sql query))) + +(defmethod format-clause :with [[_ ctes] _] + (str "WITH " (comma-join (map cte->sql ctes)))) + +(defmethod format-clause :with-recursive [[_ ctes] _] + (str "WITH RECURSIVE " (comma-join (map cte->sql ctes)))) + +(defmethod format-clause :union [[_ maps] _] + (string/join " UNION " (map to-sql maps))) + +(defmethod format-clause :union-all [[_ maps] _] + (string/join " UNION ALL " (map to-sql maps))) diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index a0bf280..e94eae6 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -219,3 +219,15 @@ (defn delete-from ([table] (delete-from nil table)) ([m table] (build-clause :delete-from m table))) + +(defmethod build-clause :with [_ m ctes] + (assoc m :with ctes)) + +(defmethod build-clause :with-recursive [_ m ctes] + (assoc m :with-recursive ctes)) + +(defmethod build-clause :union [_ m maps] + (assoc m :union maps)) + +(defmethod build-clause :union-all [_ m maps] + (assoc m :union-all maps))