|
|
@@ -3,14 +3,30 @@
|
|
|
(:require [clojure.walk :as walk]
|
|
|
[frontend.extensions.sci :as sci]
|
|
|
[frontend.handler.common :as common-handler]
|
|
|
- [frontend.handler.db-based.property.util :as db-pu]
|
|
|
[goog.string :as gstring]
|
|
|
[goog.string.format]
|
|
|
[frontend.state :as state]
|
|
|
- [frontend.config :as config]))
|
|
|
+ [frontend.config :as config]
|
|
|
+ [datascript.core :as d]
|
|
|
+ [logseq.db.frontend.property :as db-property]
|
|
|
+ [frontend.db.conn :as db-conn]))
|
|
|
+
|
|
|
+(defn- properties-by-name
|
|
|
+ "Given a block from a query result, returns a map of its properties indexed by
|
|
|
+ property names"
|
|
|
+ [db block]
|
|
|
+ (->> (db-property/properties block)
|
|
|
+ (map (fn [[k v]]
|
|
|
+ [(:block/title (d/entity db k))
|
|
|
+ ;; For now just support cardinality :one
|
|
|
+ (when-not (set? v)
|
|
|
+ (some->> (:db/id v)
|
|
|
+ (d/entity db)
|
|
|
+ db-property/property-value-content))]))
|
|
|
+ (into {})))
|
|
|
|
|
|
(defn- normalize-query-function
|
|
|
- [ast repo result]
|
|
|
+ [ast* repo result]
|
|
|
(let [ast (walk/prewalk
|
|
|
(fn [f]
|
|
|
(if (and (list? f)
|
|
|
@@ -25,26 +41,21 @@
|
|
|
(first f)
|
|
|
(list 'map (second f) 'result)))
|
|
|
f))
|
|
|
- ast)]
|
|
|
+ ast*)
|
|
|
+ db-based-graph? (config/db-based-graph? repo)
|
|
|
+ ;; These keyword aliases should be the same as those used in the query-table for sorting
|
|
|
+ special-file-graph-keywords
|
|
|
+ {:block :block/title
|
|
|
+ :page :block/name
|
|
|
+ :created-at :block/created-at
|
|
|
+ :updated-at :block/updated-at}]
|
|
|
(walk/postwalk
|
|
|
(fn [f]
|
|
|
(cond
|
|
|
(keyword? f)
|
|
|
- ;; These keyword aliases should be the same as those used in the query-table for sorting
|
|
|
- (case f
|
|
|
- :block
|
|
|
- :block/title
|
|
|
-
|
|
|
- :page
|
|
|
- :block/name
|
|
|
-
|
|
|
- :created-at
|
|
|
- :block/created-at
|
|
|
-
|
|
|
- :updated-at
|
|
|
- :block/updated-at
|
|
|
-
|
|
|
- (let [prop-key (if (config/db-based-graph? repo) (name f) f)
|
|
|
+ (if-let [kw (and (not db-based-graph?) (get special-file-graph-keywords f))]
|
|
|
+ kw
|
|
|
+ (let [prop-key (if db-based-graph? (name f) f)
|
|
|
vals (map #(get-in % [:block/properties prop-key]) result)
|
|
|
int? (some integer? vals)]
|
|
|
`(~'fn [~'b]
|
|
|
@@ -65,9 +76,12 @@
|
|
|
(mapcat val query-result*)
|
|
|
query-result*)
|
|
|
repo (state/get-current-repo)
|
|
|
+ db (db-conn/get-db repo)
|
|
|
query-result' (if (config/db-based-graph? repo)
|
|
|
- (map #(assoc % :block/properties (db-pu/properties-by-name repo %)) query-result)
|
|
|
- query-result)
|
|
|
+ (->> query-result
|
|
|
+ (map #(d/entity db (:db/id %)))
|
|
|
+ (map #(hash-map :block/properties (properties-by-name db %))))
|
|
|
+ query-result)
|
|
|
fn-string (-> (gstring/format "(fn [result] %s)" (first arguments))
|
|
|
(common-handler/safe-read-string "failed to parse function")
|
|
|
(normalize-query-function repo query-result')
|