Browse Source

feat: mix query dsl and datalog clauses

Tienson Qin 1 year ago
parent
commit
7cd208a51c

+ 12 - 7
src/main/frontend/components/query/builder.cljs

@@ -123,11 +123,11 @@
    [:div.flex.flex-row
    [:div.flex.flex-row
     [:div.font-medium.mt-2 "Between: "]
     [:div.font-medium.mt-2 "Between: "]
     (datepicker :start "Start date"
     (datepicker :start "Start date"
-      (merge opts {:auto-focus true
-                   :on-select (fn []
-                                (when-let [^js end-input (js/document.querySelector ".query-builder-datepicker[data-key=end]")]
-                                  (when (string/blank? (.-value end-input))
-                                    (.focus end-input))))}))
+                (merge opts {:auto-focus true
+                             :on-select (fn []
+                                          (when-let [^js end-input (js/document.querySelector ".query-builder-datepicker[data-key=end]")]
+                                            (when (string/blank? (.-value end-input))
+                                              (.focus end-input))))}))
     (datepicker :end "End date" opts)]
     (datepicker :end "End date" opts)]
    [:p.pt-2
    [:p.pt-2
     (ui/button "Submit"
     (ui/button "Submit"
@@ -445,6 +445,9 @@
   [clause]
   [clause]
   (let [f (first clause)]
   (let [f (first clause)]
     (cond
     (cond
+      (string/starts-with? (str f) "?") ; variable
+      (str clause)
+
       (string? clause)
       (string? clause)
       (str "Search: " clause)
       (str "Search: " clause)
 
 
@@ -583,7 +586,8 @@
   [*tree *find loc clauses]
   [*tree *find loc clauses]
   (when (seq clauses)
   (when (seq clauses)
     [:div.query-builder-clause
     [:div.query-builder-clause
-     (let [kind (keyword (first clauses))]
+     (let [operator (first clauses)
+           kind (keyword operator)]
        (if (query-builder/operators-set kind)
        (if (query-builder/operators-set kind)
          [:div.operator-clause.flex.flex-row.items-center {:data-level (count loc)}
          [:div.operator-clause.flex.flex-row.items-center {:data-level (count loc)}
           [:div.clause-bracket "("]
           [:div.clause-bracket "("]
@@ -627,7 +631,8 @@
     ""
     ""
     (if (or (common-util/wrapped-by-parens? q-str)
     (if (or (common-util/wrapped-by-parens? q-str)
             (common-util/wrapped-by-quotes? q-str)
             (common-util/wrapped-by-quotes? q-str)
-            (page-ref/page-ref? q-str))
+            (page-ref/page-ref? q-str)
+            (string/starts-with? q-str "[?"))
       q-str
       q-str
       (str "\"" q-str "\""))))
       (str "\"" q-str "\""))))
 
 

+ 1 - 1
src/main/frontend/components/query/result.cljs

@@ -30,7 +30,7 @@
                  (not (re-matches template/template-re (string/trim q))))
                  (not (re-matches template/template-re (string/trim q))))
             nil
             nil
 
 
-            (re-matches #"\".*\"" q) ; full-text search
+            (re-matches #"^\".*\"$" q) ; full-text search
             (p/let [blocks (search/block-search repo (string/trim form) {:limit 30})]
             (p/let [blocks (search/block-search repo (string/trim form) {:limit 30})]
               (when (seq blocks)
               (when (seq blocks)
                 (let [result (->> blocks
                 (let [result (->> blocks

+ 21 - 5
src/main/frontend/db/query_dsl.cljs

@@ -471,6 +471,15 @@
   {:query (list 'block-content '?b e)
   {:query (list 'block-content '?b e)
    :rules [:block-content]})
    :rules [:block-content]})
 
 
+(defn- datalog-clause?
+  [e]
+  (and
+   (coll? e)
+   (or
+    (list? (first e))
+    (and (= 3 (count e))
+         (string/starts-with? (str (first e)) "?")))))
+
 (defn build-query
 (defn build-query
   "This fn converts a form/list in a query e.g. `(operator arg1 arg2)` to its datalog
   "This fn converts a form/list in a query e.g. `(operator arg1 arg2)` to its datalog
   equivalent. This fn is called recursively on sublists for boolean operators
   equivalent. This fn is called recursively on sublists for boolean operators
@@ -485,7 +494,10 @@ Some bindings in this fn:
   ([e {:keys [sort-by blocks? sample] :as env :or {blocks? (atom nil)}} level]
   ([e {:keys [sort-by blocks? sample] :as env :or {blocks? (atom nil)}} level]
    ; {:post [(or (nil? %) (map? %))]}
    ; {:post [(or (nil? %) (map? %))]}
    (let [fe (first e)
    (let [fe (first e)
-         fe (when fe (symbol (string/lower-case (name fe))))
+         fe (when fe
+              (if (list? fe)
+                fe
+                (symbol (string/lower-case (name fe)))))
          page-ref? (page-ref/page-ref? e)]
          page-ref? (page-ref/page-ref? e)]
      (when (or (and page-ref?
      (when (or (and page-ref?
                     (not (contains? #{'page-property 'page-tags} (:current-filter env))))
                     (not (contains? #{'page-property 'page-tags} (:current-filter env))))
@@ -496,6 +508,9 @@ Some bindings in this fn:
        (nil? e)
        (nil? e)
        nil
        nil
 
 
+       (and (:db-graph? env) (datalog-clause? e))
+       {:query [e]}
+
        page-ref?
        page-ref?
        (build-page-ref e)
        (build-page-ref e)
 
 
@@ -704,16 +719,17 @@ Some bindings in this fn:
    (query repo query-string {}))
    (query repo query-string {}))
   ([repo query-string query-opts]
   ([repo query-string query-opts]
    (when (and (string? query-string) (not= "\"\"" query-string))
    (when (and (string? query-string) (not= "\"\"" query-string))
-     (let [{query* :query :keys [rules sort-by blocks? sample]} (parse-query query-string {:cards? (:cards? query-opts)})
+     (let [db-graph? (config/db-based-graph? repo)
+           {query* :query :keys [rules sort-by blocks? sample]} (parse-query query-string {:cards? (:cards? query-opts)})
            query* (if (:cards? query-opts)
            query* (if (:cards? query-opts)
                     (let [card-id (:db/id (db-utils/entity :logseq.class/Card))]
                     (let [card-id (:db/id (db-utils/entity :logseq.class/Card))]
                       (util/concat-without-nil
                       (util/concat-without-nil
                        [['?b :block/tags card-id]]
                        [['?b :block/tags card-id]]
                        (if (coll? (first query*)) query* [query*])))
                        (if (coll? (first query*)) query* [query*])))
-                    query*)]
+                    query*)
+           blocks? (if db-graph? true blocks?)]
        (when-let [query' (some-> query* (query-wrapper {:blocks? blocks?
        (when-let [query' (some-> query* (query-wrapper {:blocks? blocks?
-                                                        :block-attrs (when (config/db-based-graph? repo)
-                                                                       db-block-attrs)}))]
+                                                        :block-attrs (when db-graph? db-block-attrs)}))]
          (let [random-samples (if @sample
          (let [random-samples (if @sample
                                 (fn [col]
                                 (fn [col]
                                   (take @sample (shuffle col)))
                                   (take @sample (shuffle col)))