From d32d16e8e292330de0d54a071edb042e9b1b6fc1 Mon Sep 17 00:00:00 2001 From: Frank Henard Date: Tue, 28 Jan 2014 14:07:45 -0600 Subject: [PATCH 1/2] add select-top clause --- src/honeysql/format.clj | 21 ++++++++++++++------- test/honeysql/core_test.clj | 8 ++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..7ff284d 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 :select-top :insert-into :update :delete-from :columns :set :from :join :left-join :right-join :where :group-by :having :order-by :limit :offset :values :query-values]) @@ -306,13 +306,20 @@ (defmethod format-clause :default [& _] "") +(defn- -handle-select [fields sql-map] + (str + (when (:modifiers sql-map) + (str (space-join (map (comp string/upper-case name) + (:modifiers sql-map))) + " ")) + (comma-join (map to-sql fields)))) + (defmethod format-clause :select [[_ fields] sql-map] - (str "SELECT " - (when (:modifiers sql-map) - (str (space-join (map (comp string/upper-case name) - (:modifiers sql-map))) - " ")) - (comma-join (map to-sql fields)))) + (str "SELECT " (-handle-select fields sql-map))) + +(defmethod format-clause :select-top [[_ [limit-count fields]] sql-map] + (println "limit-count = " limit-count "; fields = " fields "; sql-map = " sql-map) + (str "SELECT TOP " limit-count " " (-handle-select fields sql-map))) (defmethod format-clause :from [[_ tables] _] (str "FROM " (comma-join (map to-sql tables)))) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index 161b52f..723b769 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -55,3 +55,11 @@ "bort" "gabba" 2]))) (testing "SQL data prints and reads correctly" (is (= m1 (read-string (pr-str m1))))))) + +(deftest test-select-top + (let [m1 {:select-top [10 [:fieldx :fieldy]] + :modifiers [:distinct] + :from [:foo]}] + (testing "select-top formats correctly" + (is (= (sql/format m1) + ["SELECT TOP 10 DISTINCT fieldx, fieldy FROM foo "]))))) From ff760e12de890e0cd05b8c8a14aebd2f86454cea Mon Sep 17 00:00:00 2001 From: Frank Henard Date: Tue, 28 Jan 2014 14:25:43 -0600 Subject: [PATCH 2/2] remove debugging --- src/honeysql/format.clj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 7ff284d..7c22de6 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -318,7 +318,6 @@ (str "SELECT " (-handle-select fields sql-map))) (defmethod format-clause :select-top [[_ [limit-count fields]] sql-map] - (println "limit-count = " limit-count "; fields = " fields "; sql-map = " sql-map) (str "SELECT TOP " limit-count " " (-handle-select fields sql-map))) (defmethod format-clause :from [[_ tables] _]