|
@@ -1,11 +1,11 @@
|
|
|
(ns frontend.db.query-custom
|
|
|
- "Custom queries."
|
|
|
+ "Handles executing custom queries a.k.a. advanced queries"
|
|
|
(:require [frontend.state :as state]
|
|
|
- [clojure.string :as string]
|
|
|
- [cljs.reader :as reader]
|
|
|
[frontend.db.query-react :as react]
|
|
|
[frontend.db.query-dsl :as query-dsl]
|
|
|
[frontend.db.model :as model]
|
|
|
+ [frontend.db.rules :as rules]
|
|
|
+ [frontend.util.datalog :as datalog-util]
|
|
|
[clojure.walk :as walk]))
|
|
|
|
|
|
;; FIXME: what if users want to query other attributes than block-attrs?
|
|
@@ -21,24 +21,30 @@
|
|
|
f))
|
|
|
l))
|
|
|
|
|
|
+(defn- add-rules-to-query
|
|
|
+ "Searches query's :where for rules and adds them to query if used"
|
|
|
+ [{:keys [query] :as query-m}]
|
|
|
+ (let [{:keys [where]} (datalog-util/query-vec->map query)
|
|
|
+ rules-found (datalog-util/find-rules-in-where where (-> rules/query-dsl-rules keys set))]
|
|
|
+ (if (seq rules-found)
|
|
|
+ (-> query-m
|
|
|
+ (update :query (fn [q]
|
|
|
+ (if (contains? (set q) :in)
|
|
|
+ (datalog-util/add-to-end-of-query-section q :in ['%])
|
|
|
+ (into q [:in '$ '%]))))
|
|
|
+ (assoc :rules (mapv rules/query-dsl-rules rules-found)))
|
|
|
+ query-m)))
|
|
|
+
|
|
|
(defn custom-query
|
|
|
+ "Executes a datalog query through query-react, given either a regular datalog
|
|
|
+ query or a simple query"
|
|
|
([query]
|
|
|
(custom-query query {}))
|
|
|
([query query-opts]
|
|
|
- #_:clj-kondo/ignore
|
|
|
- (when-let [query' (cond
|
|
|
- (and (string? query)
|
|
|
- (not (string/blank? query)))
|
|
|
- (reader/read-string query)
|
|
|
-
|
|
|
- (map? query)
|
|
|
- query
|
|
|
-
|
|
|
- :else
|
|
|
- nil)]
|
|
|
- (let [repo (state/get-current-repo)
|
|
|
- query' (replace-star-with-block-attrs! query)]
|
|
|
- (if (or (list? (:query query'))
|
|
|
- (not= :find (first (:query query')))) ; dsl query
|
|
|
- (query-dsl/custom-query repo query' query-opts)
|
|
|
- (react/react-query repo query' query-opts))))))
|
|
|
+ (custom-query (state/get-current-repo) query query-opts))
|
|
|
+ ([repo query query-opts]
|
|
|
+ (let [query' (replace-star-with-block-attrs! query)]
|
|
|
+ (if (or (list? (:query query'))
|
|
|
+ (not= :find (first (:query query')))) ; dsl query
|
|
|
+ (query-dsl/custom-query repo query' query-opts)
|
|
|
+ (react/react-query repo (add-rules-to-query query') query-opts)))))
|