From 7fc3b371b0ebe513c8a6d222e817f69f3b53ae58 Mon Sep 17 00:00:00 2001 From: "Alexander K. Hudek" Date: Tue, 30 Dec 2014 00:14:37 -0500 Subject: [PATCH 1/3] 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)) From cbb7929c3660cc49a3829555827ec0a9af60413d Mon Sep 17 00:00:00 2001 From: "Alexander K. Hudek" Date: Tue, 20 Jan 2015 17:23:24 -0500 Subject: [PATCH 2/3] Added recursive queries and unions. --- src/honeysql/format.clj | 11 ++++++++++- src/honeysql/helpers.clj | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index f42bb32..627753a 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" - [:with :select :insert-into :update :delete-from :columns :set :from :join + [:with :with-recursive :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]) @@ -393,3 +393,12 @@ (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 fmt/format-clause :union [[_ maps] _] + (string/join " UNION " (map fmt/to-sql maps))) + +(defmethod fmt/format-clause :union-all [[_ maps] _] + (string/join " UNION ALL " (map fmt/to-sql maps))) \ No newline at end of file diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index 41d4b8b..2aa9c63 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -215,3 +215,12 @@ (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)) From 404c0b7272ff7c80c85e9d67ba73597ca9fc4406 Mon Sep 17 00:00:00 2001 From: "Alexander K. Hudek" Date: Tue, 20 Jan 2015 17:50:42 -0500 Subject: [PATCH 3/3] Clean up namespace from porting. --- src/honeysql/format.clj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 627753a..1720986 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -397,8 +397,8 @@ (defmethod format-clause :with-recursive [[_ ctes] _] (str "WITH RECURSIVE " (comma-join (map cte->sql ctes)))) -(defmethod fmt/format-clause :union [[_ maps] _] - (string/join " UNION " (map fmt/to-sql maps))) +(defmethod format-clause :union [[_ maps] _] + (string/join " UNION " (map to-sql maps))) -(defmethod fmt/format-clause :union-all [[_ maps] _] - (string/join " UNION ALL " (map fmt/to-sql maps))) \ No newline at end of file +(defmethod format-clause :union-all [[_ maps] _] + (string/join " UNION ALL " (map to-sql maps))) \ No newline at end of file