Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…escape-name function to handle it
  • Loading branch information...
commit 39cf023bbe75164fc8b02113cdb987c32c2c5836 1 parent fa64448
Ruiyun Wen authored December 07, 2012

Showing 1 changed file with 19 additions and 13 deletions. Show diff stats Hide diff stats

  1. 32  src/clojureql/internal.clj
32  src/clojureql/internal.clj
@@ -47,20 +47,26 @@
47 47
 (defn qualified? [c]
48 48
   (.contains (nskeyword c) "."))
49 49
 
  50
+(defn escape-name
  51
+  [n]
  52
+  (if (.contains n "-")
  53
+    (str "\"" n "\"")
  54
+    n))
  55
+
50 56
 (defn add-tname
51 57
   [tname colname]
52 58
   (let [tname   (if (map? tname)
53 59
                   (-> tname vals first)
54 60
                   tname)
55 61
         colname (if (vector? colname)
56  
-                  (str (-> colname first nskeyword) " AS " (-> colname last nskeyword))
  62
+                  (str (escape-name (-> colname first nskeyword)) " AS " (escape-name (-> colname last nskeyword)))
57 63
                   colname)]
58 64
     (if (qualified? colname)
59 65
       (nskeyword colname)
60 66
       (-> (str (if (or (keyword? tname)
61 67
                        (not (empty? tname)))
62 68
                  (str (nskeyword tname) \.) "")
63  
-               (nskeyword colname))
  69
+               (escape-name (nskeyword colname)))
64 70
           (.replaceAll "\\.\\." "\\.")))))
65 71
 
66 72
 (defn aggregate? [c]
@@ -144,7 +150,7 @@
144 150
          (str "'" c "'")
145 151
          (if (aggregate? c)
146 152
            (let [[aggr col] (-> (nskeyword c) (.split "/"))]
147  
-             (str aggr "(" (split-fields p col) ")"))
  153
+             (str aggr "(" (split-fields p (escape-name col)) ")"))
148 154
            (add-tname p c))))))
149 155
 
150 156
 (defn to-tablealias
@@ -375,7 +381,7 @@
375 381
   "When supported by the JDBC driver, returns the generated keys of the latest executed statement"
376 382
   (when (supports-generated-keys? (.getConnection stmt))
377 383
     (let [ks (.getGeneratedKeys stmt)]
378  
-      {:last-index 
  384
+      {:last-index
379 385
        (and
380 386
         (.next ks)
381 387
         (.getObject ks 1))})))
@@ -490,14 +496,14 @@
490 496
       (or alias table-name))))
491 497
 
492 498
 (defn joins-by-table-alias
493  
-  "given a list of joins return a map of joins 
  499
+  "given a list of joins return a map of joins
494 500
    keyed by the join table-alias or,
495 501
    in the case of subselects, the subselect table alias"
496 502
   [joins]
497 503
   (reduce #(let [{[table-name-alias join-on] :data :as join} %2
498 504
                  k (join-table-alias table-name-alias)]
499  
-             (assoc %1 k join)) 
500  
-          {} 
  505
+             (assoc %1 k join))
  506
+          {}
501 507
           joins))
502 508
 
503 509
 (defn join-column-names
@@ -518,7 +524,7 @@
518 524
       (concat renamed-subselect-col-strs other-col-strs))
519 525
     cols))
520 526
 
521  
-(defn to-tbl-name 
  527
+(defn to-tbl-name
522 528
   "given a join definition, return a table alias
523 529
    that the join depends on"
524 530
   [{[table-name-alias join-on] :data :as join}]
@@ -528,25 +534,25 @@
528 534
        (filter #(not= % (join-table-alias table-name-alias)))
529 535
        first))
530 536
 
531  
-(defn to-graph-el 
  537
+(defn to-graph-el
532 538
   "given a join definition return an edge in the table
533 539
    alias dependency graph"
534 540
   [m {[table-name-alias join-on] :data :as join}]
535 541
   (let [required-table (to-tbl-name join)]
536 542
     (assoc m (join-table-alias table-name-alias) required-table)))
537 543
 
538  
-(defn add-deps 
  544
+(defn add-deps
539 545
   "recursively add dependencies of tbl into a list"
540 546
   [map-of-joins edges tbl]
541  
-  (into [(map-of-joins tbl)] (map #(add-deps map-of-joins edges %) 
542  
-                                  (filter #(= tbl (edges %)) 
  547
+  (into [(map-of-joins tbl)] (map #(add-deps map-of-joins edges %)
  548
+                                  (filter #(= tbl (edges %))
543 549
                                           (keys edges)))))
544 550
 
545 551
 (defn flatten-deps
546 552
   [map-of-joins edges set-of-root-nodes]
547 553
   (filter #(not (nil? %)) (flatten (map #(add-deps map-of-joins edges %) set-of-root-nodes))))
548 554
 
549  
-(defn sort-joins 
  555
+(defn sort-joins
550 556
   "sort a list of joins into dependency order"
551 557
   [joins]
552 558
   (let [map-of-joins (joins-by-table-alias joins)

0 notes on commit 39cf023

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