Skip to content

Commit

Permalink
ORDER BY support is now complete. It knows when to qualify fields and…
Browse files Browse the repository at this point in the history
… when not to
  • Loading branch information
Lau B. Jensen committed Jan 12, 2011
1 parent a9acd38 commit c979e25
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/clojureql/internal.clj
Expand Up @@ -2,7 +2,7 @@
(:require
[clojure.contrib.sql.internal :as sqlint]
[clojure.contrib.sql :as csql])
(:use [clojure.string :only [join] :rename {join join-str}]
(:use [clojure.string :only [join upper-case] :rename {join join-str}]
[clojure.contrib.core :only [-?> -?>>]]))

(defn upper-name [kw]
Expand Down Expand Up @@ -84,24 +84,29 @@
(str tname "_subselect." col)))
(remove aggregate? tcols))))

;TODO: This function is too complex. First step to simplyfing is getting rid
; of the support for a non-collection argument.
(defn to-orderlist
"Converts a list like [:id#asc :name#desc] to \"id asc, name desc\"
Also takes a single keyword argument. Does not qualify fields found
in aggregates"
in aggregates. aggregates can also be a keyword in which case all
fields are left unqualified."
[tname aggregates fields]
(->> (if (coll? fields) fields [fields])
(map #(if (.contains (name %) "#")
(->> (.split (name %) "#")
((juxt first last))
(map (fn [f] (if (or (= f "asc") (= f "desc"))
f
(if (some (fn [i] (= (nskeyword i) (nskeyword f))) aggregates)
(upper-case f)
(if (or (keyword? aggregates)
(some (fn [i] (= (nskeyword i) (nskeyword f))) aggregates))
f
(add-tname tname f)))))
(interpose " ")
(apply str))
(if (some (fn [i] (= (nskeyword i) (nskeyword %))) aggregates)
(if (or (keyword? aggregates)
(some (fn [i] (= (nskeyword i) (nskeyword %))) aggregates))
(str (nskeyword %) " asc")
(str (add-tname tname %) " asc"))))
(interpose ",")
Expand Down
2 changes: 1 addition & 1 deletion src/clojureql/sql92compiler.clj
Expand Up @@ -96,7 +96,7 @@
(str "GROUP BY " (to-fieldlist tname (first grouped-by))))
(when (and (seq having) (seq combs)) (str "HAVING " having))
(when (seq post-order)
(str "ORDER BY " (to-orderlist tname aggregates (first post-order))))
(str "ORDER BY " (to-orderlist tname :all (first post-order))))
(when-let [limit (-> scope :limit)]
(str "LIMIT " limit))
(when-let [offset (-> scope :offset)]
Expand Down
4 changes: 2 additions & 2 deletions test/clojureql/test/core.clj
Expand Up @@ -281,13 +281,13 @@
(sort [:id])
(take 5)
(sort [:wage]))
"SELECT * FROM (SELECT t1.* FROM t1 ORDER BY t1.id asc LIMIT 5) ORDER BY t1.wage asc"
"SELECT * FROM (SELECT t1.* FROM t1 ORDER BY t1.id asc LIMIT 5) ORDER BY wage asc"
(-> (table :t1)
(sort [:id])
(drop 10)
(take 5)
(sort [:wage]))
"SELECT * FROM (SELECT t1.* FROM t1 ORDER BY t1.id asc LIMIT 5 OFFSET 10) ORDER BY t1.wage asc"))
"SELECT * FROM (SELECT t1.* FROM t1 ORDER BY t1.id asc LIMIT 5 OFFSET 10) ORDER BY wage asc"))
;TODO: Last two examples should not qualify wage?

(testing "combinations with sort/limit"
Expand Down

0 comments on commit c979e25

Please sign in to comment.