Browse Source

fix: closed value query in dsl

Tienson Qin 1 year ago
parent
commit
f26c002c3e
2 changed files with 39 additions and 17 deletions
  1. 15 11
      src/main/frontend/db/async.cljs
  2. 24 6
      src/main/frontend/db/query_dsl.cljs

+ 15 - 11
src/main/frontend/db/async.cljs

@@ -95,17 +95,21 @@
       (->> result
            (map (fn [[prop-type v]] [prop-type (if (coll? v) v [v])]))
            (mapcat (fn [[prop-type vals]]
-                     (case prop-type
-                       :default
-                       ;; Remove multi-block properties as there isn't a supported approach to query them yet
-                       (map str (remove uuid? vals))
-                       (:page :date)
-                       (map #(page-ref/->page-ref (:block/original-name (db-utils/entity graph [:block/uuid %])))
-                            vals)
-                       :number
-                       vals
-                       ;; Checkboxes returned as strings as builder doesn't display boolean values correctly
-                       (map str vals))))
+                     (let [result (case prop-type
+                                    :default
+                                    ;; Remove multi-block properties as there isn't a supported approach to query them yet
+                                    (map str (remove uuid? vals))
+                                    (:page :date)
+                                    (map #(:block/original-name (db-utils/entity graph [:block/uuid %]))
+                                         vals)
+                                    :number
+                                    vals
+                                    ;; Checkboxes returned as strings as builder doesn't display boolean values correctly
+                                    (map str vals))]
+                       (map (fn [value]
+                              (if (uuid? value)
+                                (get-in (db-utils/entity graph [:block/uuid value]) [:block/schema :value])
+                                value)) result))))
            ;; Remove blanks as they match on everything
            (remove string/blank?)
            (distinct)

+ 24 - 6
src/main/frontend/db/query_dsl.cljs

@@ -20,7 +20,9 @@
             [logseq.common.util :as common-util]
             [frontend.util.text :as text-util]
             [frontend.util :as util]
-            [frontend.config :as config]))
+            [frontend.config :as config]
+            [frontend.state :as state]
+            [logseq.db.frontend.property :as db-property]))
 
 
 ;; Query fields:
@@ -255,6 +257,13 @@
       (string/trim result)
       result)))
 
+(defn- ->keyword-property
+  [property-name]
+  (let [repo (state/get-current-repo)]
+    (if (config/db-based-graph? repo)
+      (str property-name)
+      (keyword property-name))))
+
 (defn- build-property-two-arg
   [e]
   (let [k (string/replace (name (nth e 1)) "_" "-")
@@ -262,14 +271,23 @@
         v (if-not (nil? v)
             (parse-property-value (str v))
             v)
-        v (if (coll? v) (first v) v)]
-    {:query (list 'property '?b (keyword k) v)
+        v (if (coll? v) (first v) v)
+        property (db-property/get-property (conn/get-db) k)
+        values (get-in property [:block/schema :values])
+        v' (if (seq values)             ; closed values
+             (some #(when-let [closed-value (get-in (db-utils/entity [:block/uuid %]) [:block/schema :value])]
+                      (if (= v closed-value)
+                        %
+                        v))
+                   values)
+             v)]
+    {:query (list 'property '?b (->keyword-property k) v')
      :rules [:property]}))
 
 (defn- build-property-one-arg
   [e]
   (let [k (string/replace (name (nth e 1)) "_" "-")]
-    {:query (list 'has-property '?b (keyword k))
+    {:query (list 'has-property '?b (->keyword-property k))
      :rules [:has-property]}))
 
 (defn- build-property [e]
@@ -307,9 +325,9 @@
     (if (some? v)
       (let [v' (parse-property-value (str v))
             val (if (coll? v') (first v') v')]
-        {:query (list 'page-property '?p (keyword k) val)
+        {:query (list 'page-property '?p (->keyword-property k) val)
          :rules [:page-property]})
-      {:query (list 'has-page-property '?p (keyword k))
+      {:query (list 'has-page-property '?p (->keyword-property k))
        :rules [:has-page-property]})))
 
 (defn- build-page-tags