Skip to content
Browse files

Add more test case. And fix bad compile in join statement.

  • Loading branch information...
1 parent e946339 commit f14b09ee12be7b716d527f4f225df34c5fd2c0b8 @Ruiyun committed
Showing with 31 additions and 15 deletions.
  1. +12 −12 src/clojureql/internal.clj
  2. +1 −1 src/clojureql/predicates.clj
  3. +18 −2 test/clojureql/test/core.clj
View
24 src/clojureql/internal.clj
@@ -34,12 +34,19 @@
s
(recur (.replace s " " " ")))))
+(defn double-quote-wrap
+ [n]
+ (if (.contains n "-")
+ (str "\"" n "\"")
+ n))
+
(defn nskeyword
"Converts a namespace qualified keyword to a string"
[k]
(if (string? k)
k
- (let [[kns nm] ((juxt namespace name) k)]
+ (let [[kns nm] ((juxt namespace name) k)
+ nm (clojure.string/join \. (map double-quote-wrap (clojure.string/split nm #"\.")))]
(if kns
(apply str (interpose "/" [kns nm]))
nm))))
@@ -47,28 +54,21 @@
(defn qualified? [c]
(.contains (nskeyword c) "."))
-(defn doube-quote-wrap
- [n]
- (if (.contains n "-")
- (str "\"" n "\"")
- n))
-
(defn add-tname
[tname colname]
(let [tname (if (map? tname)
(-> tname vals first)
tname)
colname (if (vector? colname)
- (str (doube-quote-wrap (-> colname first nskeyword)) " AS " (doube-quote-wrap (-> colname last nskeyword)))
+ (str (-> colname first nskeyword) " AS " (-> colname last nskeyword))
colname)]
(if (qualified? colname)
(nskeyword colname)
(-> (str (if (or (keyword? tname)
(not (empty? tname)))
(str (nskeyword tname) \.) "")
- (doube-quote-wrap (nskeyword colname)))
- (.replaceAll "\\.\\." "\\.")
- (.replaceAll "\"\"" "\"")))))
+ (nskeyword colname))
+ (.replaceAll "\\.\\." "\\.")))))
(defn aggregate? [c]
(let [c (if (vector? c) (first c) c)]
@@ -151,7 +151,7 @@
(str "'" c "'")
(if (aggregate? c)
(let [[aggr col] (-> (nskeyword c) (.split "/"))]
- (str aggr "(" (split-fields p (doube-quote-wrap col)) ")"))
+ (str aggr "(" (split-fields p col) ")"))
(add-tname p c))))))
(defn to-tablealias
View
2 src/clojureql/predicates.clj
@@ -116,7 +116,7 @@
{:keys [stmt env cols]} pred
aggregates (set (map nskeyword (find-aggregates this)))
colnames (set (remove #(.contains % ".")
- (map #(doube-quote-wrap (nskeyword %)) cols)))
+ (map nskeyword cols)))
qualify? (set/difference colnames aggregates)]
(predicate
(map #(if (qualify? %) (str (to-tablealias (:tname this))
View
20 test/clojureql/test/core.clj
@@ -94,12 +94,20 @@
(are [x y] (= (-> x (compile nil) interpolate-sql) y)
(table :users)
"SELECT users.* FROM users"
+ (table :vip-users)
+ "SELECT \"vip-users\".* FROM \"vip-users\""
(-> (table :users) (project [:id :name]))
"SELECT users.id,users.name FROM users"
+ (-> (table :users) (project [:second-id :last-name]))
+ "SELECT users.\"second-id\",users.\"last-name\" FROM users"
(-> (table :users) (aggregate [:avg/wage]))
"SELECT avg(users.wage) FROM users"
+ (-> (table :vip-users) (aggregate [:avg/vip-years]))
+ "SELECT avg(\"vip-users\".\"vip-years\") FROM \"vip-users\""
(-> (table :users) (aggregate [[:avg/wage :as :avg]]))
- "SELECT avg(users.wage) AS avg FROM users"))
+ "SELECT avg(users.wage) AS avg FROM users"
+ (-> (table :vip-users) (aggregate [[:avg/vip-years :as :v-years]]))
+ "SELECT avg(\"vip-users\".\"vip-years\") AS \"v-years\" FROM \"vip-users\""))
(testing "modifiers"
(are [x y] (= (-> x (compile nil) interpolate-sql) y)
@@ -180,10 +188,18 @@
(join (table :salary) :id)
(project [:users.id :salary.wage]))
"SELECT users.id,salary.wage FROM users JOIN salary USING(id)"
+ (-> (table :vip-users)
+ (join (table :year-end-bonus) :id)
+ (project [:vip-users.id :year-end-bonus.bonus-amount]))
+ "SELECT \"vip-users\".id,\"year-end-bonus\".\"bonus-amount\" FROM \"vip-users\" JOIN \"year-end-bonus\" USING(id)"
(-> (table :users)
(join (table :salary) (where (= :users.id :salary.id)))
(project [:users.id :salary.wage]))
- "SELECT users.id,salary.wage FROM users JOIN salary ON (users.id = salary.id)"))
+ "SELECT users.id,salary.wage FROM users JOIN salary ON (users.id = salary.id)"
+ (-> (table :vip-users)
+ (join (table :salary) (where (= :vip-users.second-id :salary.total-wage)))
+ (project [:vip-users.second-id :salary.wage]))
+ "SELECT \"vip-users\".\"second-id\",salary.wage FROM \"vip-users\" JOIN salary ON (\"vip-users\".\"second-id\" = salary.\"total-wage\")"))
(testing "ordering in joins"
(are [x y] (= (-> x (compile nil) interpolate-sql) y)

0 comments on commit f14b09e

Please sign in to comment.
Something went wrong with that request. Please try again.