From 7fc3b371b0ebe513c8a6d222e817f69f3b53ae58 Mon Sep 17 00:00:00 2001 From: "Alexander K. Hudek" Date: Tue, 30 Dec 2014 00:14:37 -0500 Subject: [PATCH] Support basic common table expressions. --- src/honeysql/format.clj | 9 ++++++++- src/honeysql/helpers.clj | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..f42bb32 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -156,7 +156,7 @@ (def clause-order "Determines the order that clauses will be placed within generated SQL" - [:select :insert-into :update :delete-from :columns :set :from :join + [:with :select :insert-into :update :delete-from :columns :set :from :join :left-join :right-join :where :group-by :having :order-by :limit :offset :values :query-values]) @@ -386,3 +386,10 @@ (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)))) diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index 0646928..41d4b8b 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -212,3 +212,6 @@ (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))