Permalink
Browse files

When colname contains "-", the final SQL statment is illegal. Add an …

…escape-name function to handle it
  • Loading branch information...
1 parent fa64448 commit 39cf023bbe75164fc8b02113cdb987c32c2c5836 @Ruiyun committed Dec 6, 2012
Showing with 19 additions and 13 deletions.
  1. +19 −13 src/clojureql/internal.clj
View
@@ -47,20 +47,26 @@
(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 (-> colname first nskeyword) " AS " (-> colname last nskeyword))
+ (str (escape-name (-> colname first nskeyword)) " AS " (escape-name (-> colname last nskeyword)))
colname)]
(if (qualified? colname)
(nskeyword colname)
(-> (str (if (or (keyword? tname)
(not (empty? tname)))
(str (nskeyword tname) \.) "")
- (nskeyword colname))
+ (escape-name (nskeyword colname)))
(.replaceAll "\\.\\." "\\.")))))
(defn aggregate? [c]
@@ -144,7 +150,7 @@
(str "'" c "'")
(if (aggregate? c)
(let [[aggr col] (-> (nskeyword c) (.split "/"))]
- (str aggr "(" (split-fields p col) ")"))
+ (str aggr "(" (split-fields p (escape-name col)) ")"))
(add-tname p c))))))
(defn to-tablealias
@@ -375,7 +381,7 @@
"When supported by the JDBC driver, returns the generated keys of the latest executed statement"
(when (supports-generated-keys? (.getConnection stmt))
(let [ks (.getGeneratedKeys stmt)]
- {:last-index
+ {:last-index
(and
(.next ks)
(.getObject ks 1))})))
@@ -490,14 +496,14 @@
(or alias table-name))))
(defn joins-by-table-alias
- "given a list of joins return a map of joins
+ "given a list of joins return a map of joins
keyed by the join table-alias or,
in the case of subselects, the subselect table alias"
[joins]
(reduce #(let [{[table-name-alias join-on] :data :as join} %2
k (join-table-alias table-name-alias)]
- (assoc %1 k join))
- {}
+ (assoc %1 k join))
+ {}
joins))
(defn join-column-names
@@ -518,7 +524,7 @@
(concat renamed-subselect-col-strs other-col-strs))
cols))
-(defn to-tbl-name
+(defn to-tbl-name
"given a join definition, return a table alias
that the join depends on"
[{[table-name-alias join-on] :data :as join}]
@@ -528,25 +534,25 @@
(filter #(not= % (join-table-alias table-name-alias)))
first))
-(defn to-graph-el
+(defn to-graph-el
"given a join definition return an edge in the table
alias dependency graph"
[m {[table-name-alias join-on] :data :as join}]
(let [required-table (to-tbl-name join)]
(assoc m (join-table-alias table-name-alias) required-table)))
-(defn add-deps
+(defn add-deps
"recursively add dependencies of tbl into a list"
[map-of-joins edges tbl]
- (into [(map-of-joins tbl)] (map #(add-deps map-of-joins edges %)
- (filter #(= tbl (edges %))
+ (into [(map-of-joins tbl)] (map #(add-deps map-of-joins edges %)
+ (filter #(= tbl (edges %))
(keys edges)))))
(defn flatten-deps
[map-of-joins edges set-of-root-nodes]
(filter #(not (nil? %)) (flatten (map #(add-deps map-of-joins edges %) set-of-root-nodes))))
-(defn sort-joins
+(defn sort-joins
"sort a list of joins into dependency order"
[joins]
(let [map-of-joins (joins-by-table-alias joins)

0 comments on commit 39cf023

Please sign in to comment.