瀏覽代碼

fix: query builder can query private built-in properties

like before
Gabriel Horner 11 月之前
父節點
當前提交
cc2d6df37b

+ 2 - 2
deps/db/bb.edn

@@ -31,8 +31,8 @@
            (concat (mapcat val rules/rules)
                    ;; TODO: Update linter to handle false positive on ?str-val for :property
                    (rules/extract-rules (dissoc rules/query-dsl-rules :property))
-                   ;; TODO: Update linter to handle false positive on :task, :priority, :property and :page-property
-                   (rules/extract-rules (dissoc rules/db-query-dsl-rules :task :priority :property :page-property)))))}}
+                   ;; TODO: Update linter to handle false positive on :task, :priority, :property and :property-property
+                   (rules/extract-rules (dissoc rules/db-query-dsl-rules :task :priority :property :private-property)))))}}
 
  :tasks/config
  {:large-vars

+ 24 - 0
deps/db/src/logseq/db/frontend/rules.cljc

@@ -171,6 +171,13 @@
       [(get ?prop-schema :public? true) ?public]
       [(= true ?public)]]
 
+    ;; Same as has-property except it returns public and private properties like :block/title
+    :has-private-property
+    '[(has-private-property ?b ?prop)
+      [?b ?prop _]
+      [?prop-e :db/ident ?prop]
+      [?prop-e :block/type "property"]]
+
     :property
     '[(property ?b ?prop ?val)
       [?prop-e :db/ident ?prop]
@@ -190,6 +197,23 @@
         (or [?pv :block/title ?val]
             [?pv :property.value/content ?val])))]
 
+    ;; Same as property except it returns public and private properties like :block/title
+    :private-property
+    '[(private-property ?b ?prop ?val)
+      [?prop-e :db/ident ?prop]
+      [?prop-e :block/type "property"]
+      [?b ?prop ?pv]
+      (or
+       ;; non-ref value
+       (and
+        [(missing? $ ?prop-e :db/valueType)]
+        [?b ?prop ?val])
+       ;; ref value
+       (and
+        [?prop-e :db/valueType :db.type/ref]
+        (or [?pv :block/title ?val]
+            [?pv :property.value/content ?val])))]
+
     :task
     '[(task ?b ?statuses)
       ;; and needed to avoid binding error

+ 17 - 15
src/main/frontend/components/query/builder.cljs

@@ -140,11 +140,10 @@
                                  (reset! *between-dates {}))))))]])
 
 (rum/defc property-select
-  [*mode *property]
+  [*mode *property *private-property?]
   (let [[properties set-properties!] (rum/use-state nil)
-        [include-built-in? set-include-built-in!] (rum/use-state false)
         properties (cond->> properties
-                     (not include-built-in?)
+                     (not @*private-property?)
                      (remove ldb/built-in?))]
     (rum/use-effect!
      (fn []
@@ -156,11 +155,11 @@
      [:div.flex.flex-row.justify-between.gap-1.items-center.px-1.pb-1.border-b
       [:label.opacity-50.cursor.select-none.text-sm
        {:for "built-in"}
-       "Show built in properties"]
+       "Show built-in properties"]
       (shui/checkbox
        {:id "built-in"
-        :value include-built-in?
-        :on-checked-change #(set-include-built-in! (not include-built-in?))})]
+        :value @*private-property?
+        :on-checked-change #(reset! *private-property? (not @*private-property?))})]
      (select (map #(hash-map :db/ident (:db/ident %)
                              :value (:block/title %))
                   properties)
@@ -172,7 +171,7 @@
 
 (rum/defc property-value-select-inner
   < rum/reactive db-mixins/query
-  [repo *property *find *tree opts loc values {:keys [db-graph? ref-property? property-type]}]
+  [repo *property *private-property? *find *tree opts loc values {:keys [db-graph? ref-property? property-type]}]
   (let [;; FIXME: lazy load property values consistently on first call
         ;; Guard against non ref properties like :logseq.property/icon
         _ (when (and db-graph? ref-property?)
@@ -192,7 +191,7 @@
     (select values''
             (fn [{:keys [original-value]}]
               (let [k (cond
-                        db-graph? :property
+                        db-graph? (if @*private-property? :private-property :property)
                         (= @*find :page) :page-property
                         :else :property)
                     x (if (= original-value "Select all")
@@ -202,7 +201,7 @@
                 (append-tree! *tree opts loc x))))))
 
 (rum/defc property-value-select
-  [repo *property *find *tree opts loc]
+  [repo *property *private-property? *find *tree opts loc]
   (let [db-graph? (sqlite-util/db-based-graph? repo)
         property-type (when db-graph? (get-in (db/entity repo @*property) [:block/schema :type]))
         ref-property? (and db-graph? (contains? db-property-type/all-ref-property-types property-type))
@@ -217,7 +216,7 @@
              (db-async/<get-block repo db-id :children? false)))
          (set-values! result)))
      [@*property])
-    (property-value-select-inner repo *property *find *tree opts loc values
+    (property-value-select-inner repo *property *private-property? *find *tree opts loc values
                                  {:db-graph? db-graph?
                                   :ref-property? ref-property?
                                   :property-type property-type})))
@@ -242,14 +241,15 @@
   [state *find *tree loc clause opts]
   (let [*mode (::mode state)
         *property (::property state)
+        *private-property? (::private-property? state)
         repo (state/get-current-repo)]
     [:div
      (case @*mode
        "property"
-       (property-select *mode *property)
+       (property-select *mode *property *private-property?)
 
        "property-value"
-       (property-value-select repo *property *find *tree opts loc)
+       (property-value-select repo *property *private-property? *find *tree opts loc)
 
        "sample"
        (select (range 1 101)
@@ -319,6 +319,7 @@
   [state *find *tree loc clause opts]
   (let [*mode (::mode state)
         *property (::property state)
+        *private-property? (::private-property? state)
         repo (state/get-current-repo)]
     [:div
      (case @*mode
@@ -332,10 +333,10 @@
        (tags repo *tree opts loc)
 
        "property"
-       (property-select *mode *property)
+       (property-select *mode *property *private-property?)
 
        "property-value"
-       (property-value-select repo *property *find *tree opts loc)
+       (property-value-select repo *property *private-property? *find *tree opts loc)
 
        "sample"
        (select (range 1 101)
@@ -404,6 +405,7 @@
                  state)}
   (rum/local nil ::mode)                ; pick mode
   (rum/local nil ::property)
+  (rum/local false ::private-property?)
   [state *find *tree loc clause opts]
   (let [*mode (::mode state)
         db-based? (config/db-based-graph? (state/get-current-repo))
@@ -482,7 +484,7 @@
         :else
         (str "#" (second (second clause))))
 
-      (contains? #{:property :page-property} (keyword f))
+      (contains? #{:property :private-property :page-property} (keyword f))
       (str (if (and (config/db-based-graph? (state/get-current-repo))
                     (qualified-keyword? (second clause)))
              (:block/title (db/entity (second clause)))

+ 20 - 12
src/main/frontend/db/query_dsl.cljs

@@ -265,7 +265,7 @@
   (db-based-build-between-three-arg (concat e ['now])))
 
 (defn- build-between
-  [e db-graph?]
+  [e {:keys [db-graph?]}]
   (cond
     (= 3 (count e))
     (let [k (get-timestamp-property e)]
@@ -330,18 +330,24 @@
         ::no-property-found)))
 
 (defn- build-property-two-arg
-  [e {:keys [db-graph?]}]
+  [e {:keys [db-graph? private-property?]}]
   (let [k (if db-graph? (->db-keyword-property (nth e 1)) (->file-keyword-property (nth e 1)))
         v (nth e 2)
         v' (if db-graph? (->db-property-value k v) (->file-property-value v))]
-    {:query (list 'property '?b k v')
-     :rules [:property]}))
+    (if private-property?
+      {:query (list 'private-property '?b k v')
+       :rules [:private-property]}
+      {:query (list 'property '?b k v')
+       :rules [:property]})))
 
 (defn- build-property-one-arg
-  [e {:keys [db-graph?]}]
+  [e {:keys [db-graph? private-property?]}]
   (let [k (if db-graph? (->db-keyword-property (nth e 1)) (->file-keyword-property (nth e 1)))]
-    {:query (list 'has-property '?b k)
-     :rules [:has-property]}))
+    (if private-property?
+      {:query (list 'has-private-property '?b k)
+       :rules [:has-private-property]}
+      {:query (list 'has-property '?b k)
+       :rules [:has-property]})))
 
 (defn- build-property [e env]
   (cond
@@ -391,7 +397,7 @@
        :rules [:has-page-property]})))
 
 (defn- build-tags
-  [db-graph? e]
+  [e {:keys [db-graph?]}]
   (let [tags (if (coll? (first (rest e)))
                (first (rest e))
                (rest e))
@@ -507,7 +513,7 @@ Some bindings in this fn:
             (:db-graph? env)
             (and page-ref?
                  (not (contains? #{'page-property 'page-tags} (:current-filter env))))
-            (contains? #{'between 'property 'todo 'task 'priority 'page} fe)
+            (contains? #{'between 'property 'private-property 'todo 'task 'priority 'page} fe)
             (and (not page-ref?) (string? e)))
        (reset! blocks? true))
      (cond
@@ -528,11 +534,14 @@ Some bindings in this fn:
        (build-and-or-not e env level fe)
 
        (= 'between fe)
-       (build-between e (:db-graph? env))
+       (build-between e env)
 
        (= 'property fe)
        (build-property e env)
 
+       (= 'private-property fe)
+       (build-property e (assoc env :private-property? true))
+
        ;; task is the new name and todo is the old one
        (or (= 'todo fe) (= 'task fe))
        (build-task e env)
@@ -553,7 +562,7 @@ Some bindings in this fn:
        (build-page-property e env)
 
        (= 'tags fe)
-       (build-tags (:db-graph? env) e)
+       (build-tags e env)
 
        (= 'page-tags fe)
        (build-page-tags e)
@@ -745,7 +754,6 @@ Some bindings in this fn:
                           #(sort-by % (fn [m prop] (get-in m [:block/properties prop])))
                           identity)
                transform-fn (comp sort-by' random-samples)]
-           (prn :debug :query query')
            (query-react/react-query repo
                                     {:query query'
                                      :query-string query-string

+ 1 - 1
src/main/frontend/handler/query/builder.cljs

@@ -163,7 +163,7 @@
     (into [(symbol :between)] (map ->page-ref (rest f)))
 
     ;; property key value
-    (and (vector? f) (= 3 (count f)) (contains? #{:page-property :property} (keyword (first f))))
+    (and (vector? f) (= 3 (count f)) (contains? #{:page-property :property :private-property} (keyword (first f))))
     (let [l (if (page-ref/page-ref? (str (last f)))
               (symbol (last f))
               (last f))]