Anonymous committed 312bf36

add SELECT, FROM, WHERE and GROUP-BY short hand functions to clause.clj
re-factor merge-key-clause function to cater for multiple clause args
rename cscols function to csnames

  • Participants
  • Parent commits ba17af5

Comments (0)

Files changed (3)

File src/main/clj/org/bituf/sqlrat/clause.clj

     (as-clause (into [qexpr] qparm))))
-(defn merge-key-clause
+(defn merge-topkey-clauses
   "Merge a clause-key with clause.
    Example: (merge-key-clause :where (as-clause \"\"))
             >> [\"\"]
    Example: (merge-key-clause :where (as-clause [\"id = ?\" 45]))
             >> [\"WHERE id=?\" 45]
-   Example: (merge-key-clause :group-by (cscols [:qty :cost]))
+   Example: (merge-key-clause :group-by (csnames [:qty :cost]))
             >> [\"GROUP BY qty, cost\"]"
-  ([^Keyword k ^Vector clause]
-    (merge-key-clause k false clause))
-  ([^Keyword k ^Boolean toplevel-key ^Vector clause]
-    (if (empty-clause? clause) (empty-clause)
+  [^Keyword k ^Boolean toplevel-key ^Vector clause & more]
+  (let [clauses (map as-clause (into [clause] more))
+        sclause (apply merge-clauses clauses)] ;; sclause = super-clause
+    (if (empty-clause? sclause) (empty-clause)
       (merge-clauses (str-clause-key k (if toplevel-key nil \ ))
-        clause))))
+        sclause))))
+(defn merge-key-clauses
+  [^Keyword k ^Vector clause & more]
+  (apply merge-topkey-clauses k false (into [clause] more)))
 ;; short hand functions for merging clauses
   ([clause prefix suffix] (merge-clauses prefix clause suffix)))
 (def E enclose) ;; short hand for enclose
+;; short hand
+(defn SELECT  [^Vector clause & more] (apply merge-topkey-clauses :select true
+                                        (into [clause] more)))
+(defn FROM    [^Vector clause & more] (apply merge-key-clauses :from
+                                        (into [clause] more)))
+(defn WHERE   [^Vector clause & more] (apply merge-key-clauses :where
+                                        (into [clause] more)))
+(defn GROUPBY [^Vector clause & more] (apply merge-key-clauses :group-by
+                                        (into [clause] more)))
 ;; ========== WHERE clause functions ===========
    Example: (is-null :location) >> [\"location IS NULL\"]"
   (assert k)
-  (as-clause [(str (str-col k) " IS NULL")]))
+  (as-clause [(str (str-name k) " IS NULL")]))
 (defn not-null
    Example: (not-null :location) >> [\"location NOT NULL\"]"
   (assert k)
-  (as-clause [(str (str-col k) " NOT NULL")]))
+  (as-clause [(str (str-name k) " NOT NULL")]))
   ([op k v]
     (assert op)
     (assert k)
-    (if (clause? v) (merge-clauses (as-clause (str (str-col k) op))
+    (if (clause? v) (merge-clauses (as-clause (str (str-name k) op))
                       (E v)) ;; sub-query
-      (as-clause [(str (str-col k) op \?) v])))
+      (as-clause [(str (str-name k) op \?) v])))
   ([op k v ^IFn if-nil-fn1]
     (if (nil? v) (if-nil-fn1 k)
       (op2 op k v))))
   (assert k)
   (assert v1)
   (assert v2)
-  (as-clause [(str (str-col k) " BETWEEN ? AND ?") v1 v2]))
+  (as-clause [(str (str-name k) " BETWEEN ? AND ?") v1 v2]))
         _     (if (empty? v-vec)
                 (throw (IllegalArgumentException. "Value collection is empty")))
         v-cnt (count v-vec)
-        qexpr (apply str (str-col k) kw "("
+        qexpr (apply str (str-name k) kw "("
                 (apply str
                   (interpose ", " (take v-cnt (repeat \?)))) ")")]
     (as-clause (into [qexpr] v-vec))))
                            (interpose (as-clause " OR ")  clauses))))
-;; === Comma separated columns (for SELECT columns, GROUP BY, ORDER BY etc) ===
+;; === Comma separated names (for SELECT columns, GROUP BY, ORDER BY etc) ===
-(defn cscols
+(defn csnames
   "Return columns in comma separated string form.
    Example: [:qty :price \"order_date\"] >> [\"qty, price, order_date\"] "
   (if (empty? tokens) (empty-clause)
     (let [tokens-vec (as-vector tokens)]
       (as-clause (apply str
-                   (interpose ", " (map #(str-col %) tokens-vec)))))))
+                   (interpose ", " (map #(str-name %) tokens-vec)))))))
 ;; === The LIMIT clause ===

File src/main/clj/org/bituf/sqlrat/clause/internal.clj

 ;; ======== Keyword to String conversion ========
-(defn str-col
+(defn str-name
   "Convert keyword/anything to a string (colname)."
   (if (keyword? k) (name k)

File src/test/clj/org/bituf/sqlrat/test/clause.clj

       (is (= cc (merge-clauses c1 c2))))))
-(deftest test-merge-key-clause
+(deftest test-merge-key-clauses
   (testing "Merge WHERE clause"
     (let [c (=? :id 45)]
       (is (= [" WHERE id=?" 45]
-            (merge-key-clause :where c)))))
+            (merge-key-clauses :where c)))))
   (testing "Merge WHERE with empty clause"
     (let [c (as-clause "")]
       (is (= [""]
-            (merge-key-clause :where c)))))
+            (merge-key-clauses :where c)))))
   (testing "Merge GROUP BY clause"
-    (let [c (cscols [:qty :cost])]
+    (let [c (csnames [:qty :cost])]
       (is (= [" GROUP BY qty, cost"]
-            (merge-key-clause :group-by c))))))
+            (merge-key-clauses :group-by c))))))
 (deftest test-op2-with-value
-  (test-merge-key-clause)
+  (test-merge-key-clauses)