add select-top clause

This commit is contained in:
Frank Henard 2014-01-28 14:07:45 -06:00
parent 88bc556578
commit d32d16e8e2
2 changed files with 22 additions and 7 deletions

View file

@ -156,7 +156,7 @@
(def clause-order (def clause-order
"Determines the order that clauses will be placed within generated SQL" "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 :left-join :right-join :where :group-by :having :order-by :limit :offset
:values :query-values]) :values :query-values])
@ -306,14 +306,21 @@
(defmethod format-clause :default [& _] (defmethod format-clause :default [& _]
"") "")
(defmethod format-clause :select [[_ fields] sql-map] (defn- -handle-select [fields sql-map]
(str "SELECT " (str
(when (:modifiers sql-map) (when (:modifiers sql-map)
(str (space-join (map (comp string/upper-case name) (str (space-join (map (comp string/upper-case name)
(:modifiers sql-map))) (:modifiers sql-map)))
" ")) " "))
(comma-join (map to-sql fields)))) (comma-join (map to-sql fields))))
(defmethod format-clause :select [[_ fields] sql-map]
(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] _] (defmethod format-clause :from [[_ tables] _]
(str "FROM " (comma-join (map to-sql tables)))) (str "FROM " (comma-join (map to-sql tables))))

View file

@ -55,3 +55,11 @@
"bort" "gabba" 2]))) "bort" "gabba" 2])))
(testing "SQL data prints and reads correctly" (testing "SQL data prints and reads correctly"
(is (= m1 (read-string (pr-str m1))))))) (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 "])))))