Ver Fonte

split db-version query rules

rcmerci há 2 anos atrás
pai
commit
0c4e9bf4d4

+ 29 - 19
deps/db/src/logseq/db/rules.cljc

@@ -109,16 +109,18 @@
    '[(has-property ?b ?prop)
      [?b :block/properties ?bp]
      [(missing? $ ?b :block/name)]
-     (or-join [?bp]
-              [(get ?bp ?prop)]
-              ;; for db version, :block/properties looks like: {<uuid> <vals>, ...}
-              ;; keys are uuid-string instead of property name
-              (and [(name ?prop) ?prop-name-str]
-                   [?prop-b :block/name ?prop-name-str]
-                   [?prop-b :block/type "property"]
-                   [?prop-b :block/uuid ?prop-uuid]
-                   [(str ?prop-uuid) ?prop-uuid-str]
-                   [(get ?bp ?prop-uuid-str)]))]
+     [(get ?bp ?prop)]]
+
+   :has-property-db-version
+   '[(has-property ?b ?prop)
+     [?b :block/properties ?bp]
+     [(missing? $ ?b :block/name)]
+     [(name ?prop) ?prop-name-str]
+     [?prop-b :block/name ?prop-name-str]
+     [?prop-b :block/type "property"]
+     [?prop-b :block/uuid ?prop-uuid]
+     [(str ?prop-uuid) ?prop-uuid-str]
+     [(get ?bp ?prop-uuid-str)]]
 
    :block-content
    '[(block-content ?b ?query)
@@ -139,15 +141,23 @@
    '[(property ?b ?key ?val)
      [?b :block/properties ?prop]
      [(missing? $ ?b :block/name)]
-     (or-join [?v ?prop]
-              [(get ?prop ?key) ?v]
-              ;; for db version
-              (and [(name ?key) ?key-str]
-                   [?prop-b :block/name ?key-str]
-                   [?prop-b :block/type "property"]
-                   [?prop-b :block/uuid ?prop-uuid]
-                   [(str ?prop-uuid) ?prop-uuid-str]
-                   [(get ?prop ?prop-uuid-str) ?v]))
+     [(get ?prop ?key) ?v]
+     [(str ?val) ?str-val]
+     (or [(= ?v ?val)]
+         [(contains? ?v ?val)]
+         ;; For integer pages that aren't strings
+         [(contains? ?v ?str-val)])]
+
+   :property-db-version
+   '[(property ?b ?key ?val)
+     [?b :block/properties ?prop]
+     [(missing? $ ?b :block/name)]
+     [(name ?key) ?key-str]
+     [?prop-b :block/name ?key-str]
+     [?prop-b :block/type "property"]
+     [?prop-b :block/uuid ?prop-uuid]
+     [(str ?prop-uuid) ?prop-uuid-str]
+     [(get ?prop ?prop-uuid-str) ?v]
      [(str ?val) ?str-val]
      (or [(= ?v ?val)]
          [(contains? ?v ?val)]

+ 4 - 2
src/main/frontend/components/query.cljs

@@ -13,7 +13,8 @@
             [frontend.extensions.sci :as sci]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.editor.property :as editor-property]
-            [logseq.graph-parser.util :as gp-util]))
+            [logseq.graph-parser.util :as gp-util]
+            [frontend.config :as config]))
 
 (defn built-in-custom-query?
   [title]
@@ -162,7 +163,8 @@
         view-fn (if (keyword? view) (get-in (state/sub-config) [:query/views view]) view)
         view-f (and view-fn (sci/eval-string (pr-str view-fn)))
         dsl-page-query? (and dsl-query?
-                             (false? (:blocks? (query-dsl/parse-query query))))
+                             (false? (:blocks? (query-dsl/parse-query
+                                                query (config/db-based-graph? (state/get-current-repo))))))
         ;; FIXME: This isn't getting set for full-text searches
         full-text-search? (and dsl-query?
                                (util/electron?)

+ 3 - 2
src/main/frontend/components/query/builder.cljs

@@ -16,7 +16,8 @@
             [rum.core :as rum]
             [clojure.string :as string]
             [logseq.graph-parser.util :as gp-util]
-            [logseq.graph-parser.util.page-ref :as page-ref]))
+            [logseq.graph-parser.util.page-ref :as page-ref]
+            [frontend.config :as config]))
 
 (rum/defc page-block-selector
   [*find]
@@ -455,7 +456,7 @@
              (assoc state ::tree *tree)))
    :will-mount (fn [state]
                  (let [q-str (first (:rum/args state))
-                       parsed-query (query-dsl/parse-query q-str)
+                       parsed-query (query-dsl/parse-query q-str (config/db-based-graph? (state/get-current-repo)))
                        blocks-query? (:blocks? parsed-query)
                        find-mode (cond
                                    blocks-query?

+ 3 - 2
src/main/frontend/config.cljs

@@ -368,8 +368,9 @@
 
 (defn db-based-graph?
   [s]
-  (and (string? s)
-       (string/starts-with? s db-version-prefix)))
+  (boolean
+   (and (string? s)
+        (string/starts-with? s db-version-prefix))))
 
 (defn get-local-asset-absolute-path
   [s]

+ 18 - 16
src/main/frontend/db/query_dsl.cljs

@@ -16,7 +16,8 @@
             [logseq.graph-parser.util.page-ref :as page-ref]
             [logseq.graph-parser.util :as gp-util]
             [frontend.util.text :as text-util]
-            [frontend.util :as util]))
+            [frontend.util :as util]
+            [frontend.config :as config]))
 
 
 ;; Query fields:
@@ -252,7 +253,7 @@
       result)))
 
 (defn- build-property-two-arg
-  [e]
+  [e db-version?]
   (let [k (string/replace (name (nth e 1)) "_" "-")
         v (nth e 2)
         v (if-not (nil? v)
@@ -260,21 +261,21 @@
             v)
         v (if (coll? v) (first v) v)]
     {:query (list 'property '?b (keyword k) v)
-     :rules [:property]}))
+     :rules [(if db-version? :property-db-version :property)]}))
 
 (defn- build-property-one-arg
-  [e]
+  [e db-version?]
   (let [k (string/replace (name (nth e 1)) "_" "-")]
     {:query (list 'has-property '?b (keyword k))
-     :rules [:has-property]}))
+     :rules [(if db-version? :has-property-db-version :has-property)]}))
 
-(defn- build-property [e]
+(defn- build-property [e db-version?]
   (cond
     (= 3 (count e))
-    (build-property-two-arg e)
+    (build-property-two-arg e db-version?)
 
     (= 2 (count e))
-    (build-property-one-arg e)))
+    (build-property-one-arg e db-version?)))
 
 (defn- build-task
   [e]
@@ -385,7 +386,7 @@ Some bindings in this fn:
 * fe - the query operator e.g. `property`"
   ([e env]
    (build-query e (assoc env :vars (atom {})) 0))
-  ([e {:keys [sort-by blocks? sample] :as env :or {blocks? (atom nil)}} level]
+  ([e {:keys [sort-by blocks? sample db-version?] :as env :or {blocks? (atom nil)}} level]
    ; {:post [(or (nil? %) (map? %))]}
    (let [fe (first e)
          fe (when fe (symbol (string/lower-case (name fe))))
@@ -412,7 +413,7 @@ Some bindings in this fn:
        (build-between e)
 
        (= 'property fe)
-       (build-property e)
+       (build-property e db-version?)
 
        ;; task is the new name and todo is the old one
        (or (= 'todo fe) (= 'task fe))
@@ -529,7 +530,7 @@ Some bindings in this fn:
 
 (def custom-readers {:readers {'tag (fn [x] (page-ref/->page-ref x))}})
 (defn parse
-  [s]
+  [s db-version?]
   (when (and (string? s)
              (not (string/blank? s)))
     (let [s (if (= \# (first s)) (page-ref/->page-ref (subs s 1)) s)
@@ -543,7 +544,8 @@ Some bindings in this fn:
           {result :query rules :rules}
           (when form (build-query form {:sort-by sort-by
                                         :blocks? blocks?
-                                        :sample sample}))
+                                        :sample sample
+                                        :db-version? db-version?}))
           result' (when (seq result)
                     (let [key (if (coll? (first result))
                                 ;; Only queries for this branch are not's like:
@@ -576,9 +578,9 @@ Some bindings in this fn:
       (conj q where))))
 
 (defn parse-query
-  [q]
+  [q db-version?]
   (let [q' (template/resolve-dynamic-template! q)]
-    (parse q')))
+    (parse q' db-version?)))
 
 (defn pre-transform-query
   [q]
@@ -591,7 +593,7 @@ Some bindings in this fn:
    (query repo query-string {}))
   ([repo query-string query-opts]
    (when (and (string? query-string) (not= "\"\"" query-string))
-     (let [{:keys [query rules sort-by blocks? sample]} (parse-query query-string)]
+     (let [{:keys [query rules sort-by blocks? sample]} (parse-query query-string (config/db-based-graph? repo))]
        (when-let [query' (some-> query (query-wrapper {:blocks? blocks?}))]
          (let [sort-by (or sort-by identity)
                random-samples (if @sample
@@ -613,7 +615,7 @@ Some bindings in this fn:
   [repo query-m query-opts]
   (when (seq (:query query-m))
     (let [query-string (template/resolve-dynamic-template! (pr-str (:query query-m)))
-          {:keys [query sort-by blocks? rules]} (parse query-string)]
+          {:keys [query sort-by blocks? rules]} (parse query-string (config/db-based-graph? repo))]
       (when-let [query' (some-> query (query-wrapper {:blocks? blocks?}))]
         (query-react/react-query repo
                            (merge

+ 3 - 2
src/main/frontend/extensions/srs.cljs

@@ -29,7 +29,8 @@
             [rum.core :as rum]
             [frontend.modules.shortcut.core :as shortcut]
             [medley.core :as medley]
-            [frontend.context.i18n :refer [t]]))
+            [frontend.context.i18n :refer [t]]
+            [frontend.config :as config]))
 
 ;;; ================================================================
 ;;; Commentary
@@ -269,7 +270,7 @@
                                                    (string/starts-with? query-string "["))
                                          (page-ref/->page-ref (string/trim query-string))
                                          query-string)
-                          {:keys [query sort-by rules]} (query-dsl/parse query-string)
+                          {:keys [query sort-by rules]} (query-dsl/parse query-string (config/db-based-graph? repo))
                           query* (util/concat-without-nil
                                   [['?b :block/refs '?br] ['?br :block/name card-hash-tag]]
                                   (if (coll? (first query)) query [query]))]

+ 3 - 3
src/main/frontend/handler/editor.cljs

@@ -3232,7 +3232,7 @@
 
 (defn- valid-dsl-query-block?
   "Whether block has a valid dsl query."
-  [block]
+  [repo block]
   (->> (:block/macros (db/entity (:db/id block)))
        (some (fn [macro]
                (when-let [query-body (and
@@ -3240,7 +3240,7 @@
                                       (first (:logseq.macro-arguments (:block/properties macro))))]
                  (seq (:query
                        (try
-                         (query-dsl/parse-query query-body)
+                         (query-dsl/parse-query query-body (config/db-based-graph? repo))
                          (catch :default _e
                            nil)))))))))
 
@@ -3263,7 +3263,7 @@
    (when block-id
      (if-let [block (db-model/query-block-by-uuid block-id)]
        (or (db-model/has-children? block-id)
-           (valid-dsl-query-block? block)
+           (valid-dsl-query-block? (state/get-current-repo) block)
            (valid-custom-query-block? block)
            (and
             (:outliner/block-title-collapse-enabled? (state/get-config))