Merge 36d13d32dc into d5a0784e16
This commit is contained in:
commit
3d43d6289c
2 changed files with 19 additions and 1 deletions
|
|
@ -17,6 +17,15 @@
|
|||
(defn paren-wrap [x]
|
||||
(str "(" x ")"))
|
||||
|
||||
;; Checks if the first elemnet of the list is a `DISTINCT ON` statement, if yes
|
||||
;; then return a string with an appended space at the end of the first and then
|
||||
;; comma joining the rest else simply return a comma joined string
|
||||
(defn construct-select [s]
|
||||
(let [[fst & rst] s]
|
||||
(if (re-find #"DISTINCT ON" fst)
|
||||
(str fst " " (comma-join rst))
|
||||
(comma-join s))))
|
||||
|
||||
(def ^:dynamic *clause*
|
||||
"During formatting, *clause* is bound to :select, :from, :where, etc."
|
||||
nil)
|
||||
|
|
@ -403,7 +412,7 @@
|
|||
(str (space-join (map (comp string/upper-case name)
|
||||
(:modifiers sql-map)))
|
||||
" "))
|
||||
(comma-join (map to-sql fields))))
|
||||
(construct-select (map to-sql fields))))
|
||||
|
||||
(defmethod format-clause :from [[_ tables] _]
|
||||
(str "FROM " (comma-join (map to-sql tables))))
|
||||
|
|
|
|||
|
|
@ -174,3 +174,12 @@
|
|||
(from :foo)
|
||||
(join :x [:= :foo.id :x.id] :y nil)
|
||||
sql/format)))))
|
||||
|
||||
(deftest distinct-on
|
||||
(testing "Distinct on query generation"
|
||||
(is (= ["SELECT DISTINCT ON (name) name, created_by"]
|
||||
(-> (select (sql/call :distinct-on :name) :name :created_by)
|
||||
sql/format)))
|
||||
(is (= ["SELECT DISTINCT ON (name, created_by) name, created_by"]
|
||||
(-> (select (sql/call :distinct-on :name :created_by) :name :created_by)
|
||||
sql/format)))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue