Browse Source

fix: or nested queries

related to #2218
Tienson Qin 4 years ago
parent
commit
903a757ce2

+ 29 - 4
src/main/frontend/db/query_dsl.cljs

@@ -131,10 +131,21 @@
     (swap! counter inc)
     result))
 
+(defn- collect-vars
+  [l]
+  (let [vars (atom #{})]
+    (walk/postwalk
+     (fn [f]
+       (when (and (symbol? f) (= \? (first (name f))))
+         (swap! vars conj f))
+       f)
+     l)
+    @vars))
+
 (defn build-query
   ([repo e env]
-   (build-query repo e env 0))
-  ([repo e {:keys [sort-by blocks? counter current-filter] :as env} level]
+   (build-query repo e (assoc env :vars (atom {})) 0))
+  ([repo e {:keys [sort-by blocks? counter current-filter vars] :as env} level]
    ;; TODO: replace with multi-methods for extensibility.
    (let [fe (first e)
          page-ref? (text/page-ref? e)]
@@ -171,15 +182,29 @@
                             (cons fe (seq clauses)))
 
                           (coll? (first clauses))
-                          (if (= current-filter 'not)
+                          (cond
+                            (= current-filter 'not)
                             (->> (apply concat clauses)
                                  (apply list fe))
+
+                            (= current-filter 'or)
+                            (apply concat clauses)
+
+                            :else
                             (->> (map #(cons 'and (seq %)) clauses)
                                  (apply list fe)))
 
                           :else
-                          (apply list fe clauses))]
+                          (apply list fe clauses))
+                 vars' (set/union (set @vars) (collect-vars result))]
+             (reset! vars vars')
              (cond
+               ;; TODO: more thoughts
+               (and (= current-filter 'and)
+                    (= 'or fe)
+                    (= #{'?b} vars'))
+               [(concat result [['?b]])]
+
                (and (zero? level) (= 'and fe))
                (distinct (apply concat clauses))
 

+ 4 - 1
src/main/frontend/modules/file/core.cljs

@@ -35,7 +35,10 @@
                     (str content "\n"))
 
                   :else
-                  (let [markdown-top-heading? (and markdown? (= parent page))
+                  (let [markdown-top-heading? (and markdown?
+                                                   (= parent page)
+                                                   (not unordered)
+                                                   heading-level)
                         [prefix spaces-tabs]
                         (cond
                           (= format :org)

+ 6 - 3
src/test/frontend/db/query_dsl_test.cljs

@@ -355,7 +355,8 @@ last-modified-at:: 1609084800002"}]]
       {:query '([?b :block/marker ?marker]
                 [(contains? #{"NOW" "LATER"} ?marker)]
                 (or (and [?b :block/path-refs [:block/name "page 1"]])
-                    (and [?b :block/path-refs [:block/name "page 2"]])))
+                    (and [?b :block/path-refs [:block/name "page 2"]])
+                    [?b]))
        :count 3})
 
     (are [x y] (= (q-count x) y)
@@ -366,7 +367,8 @@ last-modified-at:: 1609084800002"}]]
                  [(contains? #{"NOW" "LATER"} ?marker)]
                  (or
                   (and [?b :block/path-refs [:block/name "page 1"]])
-                  (and [?b :block/path-refs [:block/name "page 2"]]))))
+                  (and [?b :block/path-refs [:block/name "page 2"]])
+                  [?b])))
        :count 34})
 
     ;; FIXME: not working
@@ -388,7 +390,8 @@ last-modified-at:: 1609084800002"}]]
                 [(contains? #{"NOW" "LATER" "DONE"} ?marker)]
                 (or
                  (and [?b :block/path-refs [:block/name "page 1"]])
-                 (and (not [?b :block/path-refs [:block/name "page 1"]]))))
+                 (and (not [?b :block/path-refs [:block/name "page 1"]]))
+                 [?b]))
        :count 5}))
 
   ;; (testing "sort-by (created-at defaults to desc)"