final query cleanup.
This commit is contained in:
@@ -12,13 +12,16 @@
|
|||||||
:else
|
:else
|
||||||
(keyword "transaction" sort-by)))
|
(keyword "transaction" sort-by)))
|
||||||
|
|
||||||
|
;; TODO - unneeeded with merge-query
|
||||||
(defn add-where [query & wheres]
|
(defn add-where [query & wheres]
|
||||||
(reduce #(update-in %1 [:query :where] conj %2) query wheres))
|
(reduce #(update-in %1 [:query :where] conj %2) query wheres))
|
||||||
|
|
||||||
|
;; TODO - unneeeded with merge-query
|
||||||
(defn add-wheres [query wheres]
|
(defn add-wheres [query wheres]
|
||||||
(apply add-where query wheres))
|
(apply add-where query wheres))
|
||||||
|
|
||||||
|
|
||||||
|
;; TODO - unneeeded with merge-query
|
||||||
(defn add-arg [query name value where & rest]
|
(defn add-arg [query name value where & rest]
|
||||||
(let [query (-> query
|
(let [query (-> query
|
||||||
(update :args conj value)
|
(update :args conj value)
|
||||||
@@ -26,12 +29,19 @@
|
|||||||
(update-in [:query :where] conj where))]
|
(update-in [:query :where] conj where))]
|
||||||
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
||||||
|
|
||||||
|
(defn merge-query [query-part-1 query-part-2]
|
||||||
|
(-> query-part-1
|
||||||
|
(update-in [:query :find] into (get-in query-part-2 [:query :find]))
|
||||||
|
(update-in [:query :in] into (get-in query-part-2 [:query :in]))
|
||||||
|
(update-in [:query :where] into (get-in query-part-2 [:query :where]))
|
||||||
|
(update-in [:args] into (get-in query-part-2 [:args]))))
|
||||||
|
|
||||||
(defn add-sorter-field [q sort-map args]
|
(defn add-sorter-field [q sort-map args]
|
||||||
(-> q
|
(merge-query q
|
||||||
(update-in [:query :find] conj '?sorter)
|
{:query {:find ['?sorter]
|
||||||
(add-wheres (sort-map
|
:where (sort-map
|
||||||
(:sort-by args)
|
(:sort-by args)
|
||||||
(println "Warning, trying to sort by unsupported field" (:sort-by args))))))
|
(println "Warning, trying to sort by unsupported field" (:sort-by args)))}}))
|
||||||
|
|
||||||
(defn apply-sort [args sort-fn results ]
|
(defn apply-sort [args sort-fn results ]
|
||||||
(cond->> results
|
(cond->> results
|
||||||
@@ -51,8 +61,7 @@
|
|||||||
:where ['[?e :transaction/id]]}
|
:where ['[?e :transaction/id]]}
|
||||||
:args [db]}
|
:args [db]}
|
||||||
|
|
||||||
(:sort-by args) (add-sorter-field {"client" ['[?e :transaction/id]
|
(:sort-by args) (add-sorter-field {"client" ['[?e :transaction/client ?c]
|
||||||
'[?e :transaction/client ?c]
|
|
||||||
'[?c :client/name ?sorter]]
|
'[?c :client/name ?sorter]]
|
||||||
"description-original" ['[?e :transaction/description-original ?sorter]]
|
"description-original" ['[?e :transaction/description-original ?sorter]]
|
||||||
"date" ['[?e :transaction/date ?sorter]]
|
"date" ['[?e :transaction/date ?sorter]]
|
||||||
@@ -60,13 +69,20 @@
|
|||||||
"status" ['[?e :transaction/status ?sorter]]}
|
"status" ['[?e :transaction/status ?sorter]]}
|
||||||
args)
|
args)
|
||||||
|
|
||||||
(limited-clients (:id args)) (add-arg '[?xx ...] (set (map :db/id (limited-clients (:id args))))
|
(limited-clients (:id args))
|
||||||
'[?e :transaction/client ?xx])
|
(merge-query {:query {:in ['[?xx ...]]
|
||||||
(:client-id args) (add-arg '?client-id (:client-id args)
|
:where ['[?e :transaction/client ?xx]]}
|
||||||
'[?e :transaction/client ?client-id])
|
:args [(set (map :db/id (limited-clients (:id args))))]})
|
||||||
(:original-id args) (add-arg '?original-id (cond-> (:original-id args) (string? (:original-id args)) Long/parseLong )
|
|
||||||
'[?e :transaction/client ?c]
|
(:client-id args)
|
||||||
'[?c :client/original-id ?original-id]))]
|
(merge-query {:query {:in ['?client-id]
|
||||||
|
:where ['[?e :transaction/client ?client-id]]}
|
||||||
|
:args [(:client-id args)]})
|
||||||
|
(:original-id args)
|
||||||
|
(merge-query {:query {:in ['?original-id]
|
||||||
|
:where ['[?e :transaction/client ?c]
|
||||||
|
'[?c :client/original-id ?original-id]]}
|
||||||
|
:args [(:original-id args)]}))]
|
||||||
(cond->> query
|
(cond->> query
|
||||||
true (d/query)
|
true (d/query)
|
||||||
(:sort-by args) (apply-sort args second)
|
(:sort-by args) (apply-sort args second)
|
||||||
|
|||||||
Reference in New Issue
Block a user