summaryrefslogtreecommitdiff
path: root/src/clojure_sql/compiler.clj
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-16 00:36:36 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-16 00:36:36 +1000
commit82aa383a9c0f9d9d921135a10e919125d9115924 (patch)
treedf23a2361b48688147b0dc075d464dcff9972f50 /src/clojure_sql/compiler.clj
parent148f752b5f48707dc3d7fe448d1faf33d5cd0228 (diff)
Bring the DSL back up to previous features, fix the compiler
It should generally be usable at this point. It should generate the queries correctly (including join order and stuff) and approximately properly do things. The type of join stuff still hasn't been finished, but if the code were left as-is it would still be possible to get whatever you wanted out of it, I think. Basically: lots of work has been done and we're approaching something more usable.
Diffstat (limited to 'src/clojure_sql/compiler.clj')
-rw-r--r--src/clojure_sql/compiler.clj63
1 files changed, 26 insertions, 37 deletions
diff --git a/src/clojure_sql/compiler.clj b/src/clojure_sql/compiler.clj
index ecd7bd6..0022256 100644
--- a/src/clojure_sql/compiler.clj
+++ b/src/clojure_sql/compiler.clj
@@ -39,9 +39,6 @@
;; Utility functions for the compile-* functions
;; ==============================================================
-
-(def ^:private named? (some-fn string? symbol? keyword?))
-
(def quote? (comp boolean '#{"quote"} name))
(def unary? (comp boolean '#{"not" "exists"} name))
(def binary? (comp boolean '#{"=" "<" ">" "<=" ">=" "is" "in"} name))
@@ -115,36 +112,27 @@
(defmulti compile-fields (fn [db _] db))
(defmethod compile-fields :default [db fields-map]
(if (seq fields-map)
- (->> (for [[field alias] fields-map]
+ (->> (for [[alias field] fields-map]
(make-field-name db field alias))
(apply sequence)
((p-lift string/join ", ")))
(return "*")))
-(defmulti compile-tables (fn [db _] db))
-(defmethod compile-tables :default [db tables-map]
- (->> (for [[table alias] tables-map]
- (make-table-name db table alias))
- (apply sequence)
- ((p-lift string/join ", "))))
-
-(defmulti compile-join (fn [db _] db))
-(defmethod compile-join :default [db [type table-map on]]
- ($str (return (case type
- :left " LEFT OUTER"
- :right " RIGHT OUTER"
- " INNER"))
- (return " JOIN ")
- (compile-tables db table-map)
- (return " ON ")
- (compile-expression db on)))
-
-(defmulti compile-joins (fn [db _] db))
-(defmethod compile-joins :default [db joins]
- (->> joins
- (map (partial compile-join db))
- (apply sequence)
- ((p-lift string/join ""))))
+(defmulti compile-tables (fn [db _ _] db))
+(defmethod compile-tables :default [db join tables-map]
+ (if (vector? join)
+ (->> (for [table-alias join]
+ (make-table-name db (get tables-map table-alias) table-alias))
+ (apply sequence)
+ ((p-lift string/join ", ")))
+ (let [{:keys [left right type on]} join]
+ ($str (return "(")
+ (compile-tables db left tables-map)
+ (return (str " " (name type) " JOIN "))
+ (compile-tables db right tables-map)
+ (return " ON ")
+ (compile-expression db on)
+ (return ")")))))
(defmulti compile-where (fn [db _] db))
(defmethod compile-where :default [db expr]
@@ -156,20 +144,21 @@
(defmethod compile-sort-by :default [db fields]
(if fields
(->> (for [[[table field] dir] fields]
- (str (make-field-name db table field) \space (string/upper-case (name dir))))
- (string/join ",")
- (apply $str (return " ORDER BY "))
- return)
+ ($str (make-field-name db [table field])
+ (return (str \space (string/upper-case (name dir))))))
+ (apply sequence)
+ ((p-lift string/join ","))
+ ($str (return " ORDER BY ")))
(return nil)))
(defmulti compile-query (fn [db _] db))
-(defmethod compile-query :default [db {:keys [table fields joins where sort-by]}]
+(defmethod compile-query :default [db {:keys [tables fields joins where sort-by]}]
($str (return "SELECT ")
(compile-fields db fields)
- (if table
- (return " FROM "))
- (compile-tables db table)
- (compile-joins db joins)
+ (if tables
+ ($str (return " FROM ")
+ (compile-tables db joins tables))
+ ($str ""))
(compile-where db where)
(compile-sort-by db sort-by)))