Browse Source

Fix bug where advanced query failed with rules arg and a found rule

Gabriel Horner 3 years ago
parent
commit
862350ee0e
2 changed files with 34 additions and 7 deletions
  1. 22 7
      src/main/frontend/db/query_custom.cljs
  2. 12 0
      src/test/frontend/db/query_custom_test.cljs

+ 22 - 7
src/main/frontend/db/query_custom.cljs

@@ -24,15 +24,30 @@
 (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)
+  (let [{:keys [where in]} (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)))
+      (if (= '% (last in))
+        ;; Add to existing :inputs rules
+        (update query-m
+                :inputs
+                (fn [inputs]
+                  (assoc (vec inputs)
+                         ;; last position is rules
+                         (dec (count inputs))
+                         (->> (mapv rules/query-dsl-rules rules-found)
+                              (into (last inputs))
+                              ;; user could give rules that we already have
+                              distinct
+                              vec))))
+        ;; Add new rules
+        (-> 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

+ 12 - 0
src/test/frontend/db/query_custom_test.cljs

@@ -39,6 +39,18 @@
                                         (task ?b #{"LATER"})]})))
         "advanced query with an :in works")
 
+    (is (= ["foo:: bar\n" "b3"]
+           (map :block/content
+                (custom-query {:query '[:find (pull ?b [*])
+                                        :in $ ?query %
+                                        :where
+                                        (block-content ?b ?query)
+                                        (not-task ?b)]
+                               :inputs ["b"
+                                        '[[(not-task ?b)
+                                           (not [?b :block/marker _])]]]})))
+        "advanced query that uses rule from logseq and rule from :inputs")
+
     (is (= #{"page1"}
            (set
             (map #(get-in % [:block/page :block/name])