Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 4 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
1  .gitignore
@@ -5,3 +5,4 @@ pom.xml
lib
classes
build
+target/
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 escape-name
- [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 (escape-name (-> colname first nskeyword)) " AS " (escape-name (-> 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) \.) "")
- (escape-name (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 (escape-name 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 #(escape-name (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)

No commit comments for this range

Something went wrong with that request. Please try again.