make clause order extensible
(by using atom registry)
This commit is contained in:
parent
17145ea549
commit
05e74d9f30
1 changed files with 29 additions and 8 deletions
|
|
@ -159,13 +159,36 @@
|
||||||
:expand "WITH QUERY EXPANSION")))))
|
:expand "WITH QUERY EXPANSION")))))
|
||||||
")"))
|
")"))
|
||||||
|
|
||||||
(def clause-order
|
(def default-clause-priorities
|
||||||
"Determines the order that clauses will be placed within generated SQL"
|
"Determines the order that clauses will be placed within generated SQL"
|
||||||
[:select :insert-into :update :delete-from :columns :set :from :join
|
{:select 50
|
||||||
:left-join :right-join :full-join :where :group-by :having :order-by :limit :offset
|
:insert-into 60
|
||||||
:values :query-values])
|
:update 70
|
||||||
|
:delete-from 80
|
||||||
|
:columns 90
|
||||||
|
:set 100
|
||||||
|
:from 110
|
||||||
|
:join 120
|
||||||
|
:left-join 130
|
||||||
|
:right-join 140
|
||||||
|
:full-join 150
|
||||||
|
:where 160
|
||||||
|
:group-by 170
|
||||||
|
:having 180
|
||||||
|
:order-by 190
|
||||||
|
:limit 200
|
||||||
|
:offset 210
|
||||||
|
:values 220
|
||||||
|
:query-values 230})
|
||||||
|
|
||||||
(def known-clauses (set clause-order))
|
(def clause-store (atom default-clause-priorities))
|
||||||
|
|
||||||
|
(defn register-clause! [clause-key priority]
|
||||||
|
(swap! clause-store assoc clause-key priority))
|
||||||
|
|
||||||
|
(defn sort-clauses [clauses]
|
||||||
|
(let [m @clause-store]
|
||||||
|
(sort-by #(m % Long/MAX_VALUE) clauses)))
|
||||||
|
|
||||||
(defn format
|
(defn format
|
||||||
"Takes a SQL map and optional input parameters and returns a vector
|
"Takes a SQL map and optional input parameters and returns a vector
|
||||||
|
|
@ -251,9 +274,7 @@
|
||||||
SqlRaw
|
SqlRaw
|
||||||
(-to-sql [x] (.s x))
|
(-to-sql [x] (.s x))
|
||||||
clojure.lang.IPersistentMap
|
clojure.lang.IPersistentMap
|
||||||
(-to-sql [x] (let [clause-ops (concat
|
(-to-sql [x] (let [clause-ops (sort-clauses (keys x))
|
||||||
(filter #(contains? x %) clause-order)
|
|
||||||
(remove known-clauses (keys x)))
|
|
||||||
sql-str (binding [*subquery?* true
|
sql-str (binding [*subquery?* true
|
||||||
*fn-context?* false]
|
*fn-context?* false]
|
||||||
(space-join
|
(space-join
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue