From 49538bfa10451a0302a4db2c917cd23f16af8757 Mon Sep 17 00:00:00 2001 From: Niels van Klaveren Date: Mon, 18 Aug 2014 17:08:20 +0200 Subject: [PATCH 1/4] Added separate :delete that can take a :from clause Supports MS SQL style join DELETE alias1 from table1 alias1 table2 alias2 --- src/honeysql/format.clj | 5 ++++- src/honeysql/helpers.clj | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..ab2bf89 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 + [:select :insert-into :update :delete :delete-from :columns :set :from :join :left-join :right-join :where :group-by :having :order-by :limit :offset :values :query-values]) @@ -386,3 +386,6 @@ (defmethod format-clause :delete-from [[_ table] _] (str "DELETE FROM " (to-sql table))) + +(defmethod format-clause :delete [[_ table] _] + (str "DELETE " (to-sql table))) \ No newline at end of file diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index 91c1ca2..6cf2ef5 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -209,3 +209,10 @@ (defn delete-from ([table] (delete-from nil table)) ([m table] (build-clause :delete-from m table))) + +(defmethod build-clause :delete [_ m table] + (assoc m :delete table)) + +(defn delete + ([table] (delete nil table)) + ([m table] (build-clause :delete m table))) \ No newline at end of file From bdeb95d95680fdf0916f5a8144967410b0cdbf8d Mon Sep 17 00:00:00 2001 From: Niels van Klaveren Date: Mon, 18 Aug 2014 17:11:53 +0200 Subject: [PATCH 2/4] Added support for :aggregate and :over clauses Supports COUNT(*) OVER (PARTITION BY a ORDER BY b) clauses --- src/honeysql/format.clj | 16 +++++++++++++--- src/honeysql/helpers.clj | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index ab2bf89..9d09f0d 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -157,8 +157,8 @@ (def clause-order "Determines the order that clauses will be placed within generated SQL" [:select :insert-into :update :delete :delete-from :columns :set :from :join - :left-join :right-join :where :group-by :having :order-by :limit :offset - :values :query-values]) + :left-join :right-join :where :aggregate :over :partition-by :group-by :having + :order-by :limit :offset :values :query-values]) (def known-clauses (set clause-order)) @@ -388,4 +388,14 @@ (str "DELETE FROM " (to-sql table))) (defmethod format-clause :delete [[_ table] _] - (str "DELETE " (to-sql table))) \ No newline at end of file + (str "DELETE " (to-sql table))) + +(defmethod format-clause :over [[_ part-clause] _] + (str "OVER " (to-sql part-clause))) + +(defmethod format-clause :aggregate [[_ [aggregate-fn _]] _] + (str (to-sql aggregate-fn))) + +(defmethod format-clause :partition-by [[_ fields] _] + (str "PARTITION BY " + (comma-join (map to-sql fields)))) \ No newline at end of file diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index 6cf2ef5..ba4fa9d 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -215,4 +215,23 @@ (defn delete ([table] (delete nil table)) - ([m table] (build-clause :delete m table))) \ No newline at end of file + ([m table] (build-clause :delete m table))) + +(defmethod build-clause :over [_ m table] + (assoc m :over table)) + +(defn over + ([part-clause] (over nil part-clause)) + ([m part-clause] (build-clause :over m part-clause))) + +(defhelper merge-over [m fields] + (update-in m [:over] (partial apply merge) (collify fields))) + +(defhelper aggregate [m aggregate-fn] + (assoc m :aggregate aggregate-fn)) + +(defhelper spartition-by [m fields] + (assoc m :partition-by (collify fields))) + +(defhelper merge-partition-by [m fields] + (update-in m [:partition-by] concat (collify fields))) \ No newline at end of file From 41095d18ce2e0a3750db41bffcbf7bed097be2a5 Mon Sep 17 00:00:00 2001 From: Menthalion Date: Mon, 18 Aug 2014 23:00:05 +0200 Subject: [PATCH 3/4] Seemingly more idiomatic use of aggregate clause by preventing aliasing in defhelper instead of by destructuring in formatter. --- src/honeysql/format.clj | 2 +- src/honeysql/helpers.clj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 9d09f0d..a523176 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -393,7 +393,7 @@ (defmethod format-clause :over [[_ part-clause] _] (str "OVER " (to-sql part-clause))) -(defmethod format-clause :aggregate [[_ [aggregate-fn _]] _] +(defmethod format-clause :aggregate [[_ aggregate-fn] _] (str (to-sql aggregate-fn))) (defmethod format-clause :partition-by [[_ fields] _] diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index ba4fa9d..e2fc39c 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -228,7 +228,7 @@ (update-in m [:over] (partial apply merge) (collify fields))) (defhelper aggregate [m aggregate-fn] - (assoc m :aggregate aggregate-fn)) + (assoc m :aggregate (first aggregate-fn))) (defhelper spartition-by [m fields] (assoc m :partition-by (collify fields))) From 0ce17ccfcf496075538c47356d80c36ecaa6a3ce Mon Sep 17 00:00:00 2001 From: Niels van Klaveren Date: Tue, 19 Aug 2014 10:29:28 +0200 Subject: [PATCH 4/4] Fixed aggregate without having to first arguments, which broke SELECT field order independence and aliasing --- src/honeysql/helpers.clj | 60 +++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index e2fc39c..66a87c0 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -20,19 +20,19 @@ (if (coll? x) x [x])) (defhelper select [m fields] - (assoc m :select (collify fields))) + (assoc m :select (collify fields))) (defhelper merge-select [m fields] - (update-in m [:select] concat (collify fields))) + (update-in m [:select] concat (collify fields))) (defhelper un-select [m fields] - (update-in m [:select] #(remove (set (collify fields)) %))) + (update-in m [:select] #(remove (set (collify fields)) %))) (defhelper from [m tables] - (assoc m :from (collify tables))) + (assoc m :from (collify tables))) (defhelper merge-from [m tables] - (update-in m [:from] concat (collify tables))) + (update-in m [:from] concat (collify tables))) (defmethod build-clause :where [_ m pred] (if (nil? pred) @@ -73,22 +73,22 @@ pred))))) (defhelper join [m clauses] - (assoc m :join clauses)) + (assoc m :join clauses)) (defhelper merge-join [m clauses] - (update-in m [:join] concat clauses)) + (update-in m [:join] concat clauses)) (defhelper left-join [m clauses] - (assoc m :left-join clauses)) + (assoc m :left-join clauses)) (defhelper merge-left-join [m clauses] - (update-in m [:left-join] concat clauses)) + (update-in m [:left-join] concat clauses)) (defhelper right-join [m clauses] - (assoc m :right-join clauses)) + (assoc m :right-join clauses)) (defhelper merge-right-join [m clauses] - (update-in m [:right-join] concat clauses)) + (update-in m [:right-join] concat clauses)) (defmethod build-clause :group-by [_ m fields] (assoc m :group-by (collify fields))) @@ -100,7 +100,7 @@ (build-clause :group-by m fields))) (defhelper merge-group-by [m fields] - (update-in m [:group-by] concat (collify fields))) + (update-in m [:group-by] concat (collify fields))) (defmethod build-clause :having [_ m pred] (if (nil? pred) @@ -129,30 +129,30 @@ pred))))) (defhelper order-by [m fields] - (assoc m :order-by (collify fields))) + (assoc m :order-by (collify fields))) (defhelper merge-order-by [m fields] - (update-in m [:order-by] concat (collify fields))) + (update-in m [:order-by] concat (collify fields))) (defhelper limit [m l] - (if (nil? l) - m - (assoc m :limit (if (coll? l) (first l) l)))) + (if (nil? l) + m + (assoc m :limit (if (coll? l) (first l) l)))) (defhelper offset [m o] - (if (nil? o) - m - (assoc m :offset (if (coll? o) (first o) o)))) + (if (nil? o) + m + (assoc m :offset (if (coll? o) (first o) o)))) (defhelper modifiers [m ms] - (if (nil? ms) - m - (assoc m :modifiers (collify ms)))) + (if (nil? ms) + m + (assoc m :modifiers (collify ms)))) (defhelper merge-modifiers [m ms] - (if (nil? ms) - m - (update-in m [:modifiers] concat (collify ms)))) + (if (nil? ms) + m + (update-in m [:modifiers] concat (collify ms)))) (defmethod build-clause :insert-into [_ m table] (assoc m :insert-into table)) @@ -162,10 +162,10 @@ ([m table] (build-clause :insert-into m table))) (defhelper columns [m fields] - (assoc m :columns (collify fields))) + (assoc m :columns (collify fields))) (defhelper merge-columns [m fields] - (update-in m [:columns] concat (collify fields))) + (update-in m [:columns] concat (collify fields))) (defmethod build-clause :values [_ m vs] (assoc m :values vs)) @@ -230,6 +230,10 @@ (defhelper aggregate [m aggregate-fn] (assoc m :aggregate (first aggregate-fn))) +(defn aggregate + ([table] (aggregate nil table)) + ([m table] (build-clause :aggregate m table))) + (defhelper spartition-by [m fields] (assoc m :partition-by (collify fields)))