Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: The database rollup field supports using the relation field https://github.com/siyuan-note/siyuan/issues/15851

Daniel07cfc5864241
diff --git a/app/src/protyle/render/av/rollup.ts b/app/src/protyle/render/av/rollup.ts
index 051529bbb5..330df98a48 100644
--- a/app/src/protyle/render/av/rollup.ts
+++ b/app/src/protyle/render/av/rollup.ts
@@ -81,7 +81,7 @@ const genSearchList = (element: Element, keyword: string, avId: string, isRelati
showMessage(window.siyuan.languages.selectRelation);
return;
}
- fetchPost(isRelation ? "/api/av/searchAttributeViewRelationKey" : "/api/av/searchAttributeViewNonRelationKey", {
+ fetchPost(isRelation ? "/api/av/searchAttributeViewRelationKey" : "/api/av/searchAttributeViewRollupDestKeys", {
avID: avId,
keyword
}, (response) => {
diff --git a/kernel/api/av.go b/kernel/api/av.go
index 0f1f16571f..61c9316510 100644
--- a/kernel/api/av.go
+++ b/kernel/api/av.go
@@ -567,6 +567,24 @@ func searchAttributeViewNonRelationKey(c *gin.Context) {
}
}
+func searchAttributeViewRollupDestKeys(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, _ := util.JsonArg(c, ret)
+ if nil == arg {
+ return
+ }
+
+ avID := arg["avID"].(string)
+ keyword := arg["keyword"].(string)
+
+ rollupDestKeys := model.SearchAttributeViewRollupDestKeys(avID, keyword)
+ ret.Data = map[string]interface{}{
+ "keys": rollupDestKeys,
+ }
+}
+
func searchAttributeViewRelationKey(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 8e1f8e0f52..1f60060658 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -448,6 +448,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/av/getAttributeView", model.CheckAuth, model.CheckReadonly, getAttributeView)
ginServer.Handle("POST", "/api/av/searchAttributeViewRelationKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, searchAttributeViewRelationKey)
ginServer.Handle("POST", "/api/av/searchAttributeViewNonRelationKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, searchAttributeViewNonRelationKey)
+ ginServer.Handle("POST", "/api/av/searchAttributeViewRollupDestKeys", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, searchAttributeViewRollupDestKeys)
ginServer.Handle("POST", "/api/av/getAttributeViewFilterSort", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, getAttributeViewFilterSort)
ginServer.Handle("POST", "/api/av/addAttributeViewKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, addAttributeViewKey)
ginServer.Handle("POST", "/api/av/removeAttributeViewKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, removeAttributeViewKey)
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 6f00eecf66..86c79dfeba 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -1311,6 +1311,26 @@ func SearchAttributeViewNonRelationKey(avID, keyword string) (ret []*av.Key) {
return
}
+func SearchAttributeViewRollupDestKeys(avID, keyword string) (ret []*av.Key) {
+ waitForSyncingStorages()
+
+ ret = []*av.Key{}
+ attrView, err := av.ParseAttributeView(avID)
+ if err != nil {
+ logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
+ return
+ }
+
+ for _, keyValues := range attrView.KeyValues {
+ if av.KeyTypeRollup != keyValues.Key.Type && av.KeyTypeLineNumber != keyValues.Key.Type {
+ if strings.Contains(strings.ToLower(keyValues.Key.Name), strings.ToLower(keyword)) {
+ ret = append(ret, keyValues.Key)
+ }
+ }
+ }
+ return
+}
+
func SearchAttributeViewRelationKey(avID, keyword string) (ret []*av.Key) {
waitForSyncingStorages()
@@ -1592,8 +1612,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
}
}
- // 先渲染主键、创建时间、更新时间
-
+ // 渲染主键、创建时间、更新时间
for _, kv := range keyValues {
switch kv.Key.Type {
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
@@ -1627,8 +1646,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
}
}
- // 再渲染关联和汇总
-
+ // 渲染关联和汇总
rollupFurtherCollections := sql.GetFurtherCollections(attrView, cachedAttrViews)
for _, kv := range keyValues {
switch kv.Key.Type {
@@ -1685,7 +1703,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
}
}
- // 最后渲染模板
+ // 渲染模板
templateKeys, _ := sql.GetTemplateKeysByResolutionOrder(attrView)
var renderTemplateErr error
for _, templateKey := range templateKeys {
diff --git a/kernel/sql/av.go b/kernel/sql/av.go
index bb2e2416ca..a376c2aab5 100644
--- a/kernel/sql/av.go
+++ b/kernel/sql/av.go
@@ -352,7 +352,7 @@ func fillAttributeViewBaseValue(baseValue *av.BaseValue, fieldID, itemID string,
}
func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string, depth *int, cachedAttrViews map[string]*av.AttributeView) {
- // 先渲染主键、创建时间、更新时间
+ // 渲染主键、创建时间、更新时间
for _, item := range collection.GetItems() {
for _, value := range item.GetValues() {
@@ -415,8 +415,42 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
}
}
- // 再渲染关联和汇总
+ // 渲染关联
+ for _, item := range collection.GetItems() {
+ for _, value := range item.GetValues() {
+ if av.KeyTypeRelation != value.Type {
+ continue
+ }
+
+ value.Relation.Contents = nil
+ relKey, _ := attrView.GetKey(value.KeyID)
+ if nil != relKey && nil != relKey.Relation {
+ destAv := cachedAttrViews[relKey.Relation.AvID]
+ if nil == destAv {
+ destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
+ if nil != destAv {
+ cachedAttrViews[relKey.Relation.AvID] = destAv
+ }
+ }
+ if nil != destAv {
+ blocks := map[string]*av.Value{}
+ blockValues := destAv.GetBlockKeyValues()
+ if nil != blockValues {
+ for _, blockValue := range blockValues.Values {
+ blocks[blockValue.BlockID] = blockValue
+ }
+ for _, blockID := range value.Relation.BlockIDs {
+ if val := blocks[blockID]; nil != val {
+ value.Relation.Contents = append(value.Relation.Contents, val)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ // 渲染汇总
rollupFurtherCollections := map[string]av.Collection{}
for _, field := range collection.GetFields() {
if av.KeyTypeRollup != field.GetType() {
@@ -451,7 +485,7 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
isSameAv := destAv.ID == attrView.ID
var furtherCollection av.Collection
- if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
+ if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type || av.KeyTypeRelation == destKey.Type)) {
viewable := renderView(destAv, destAv.Views[0], "", depth, cachedAttrViews)
if nil != viewable {
furtherCollection = viewable.(av.Collection)
@@ -465,70 +499,43 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
for _, item := range collection.GetItems() {
for _, value := range item.GetValues() {
- itemID := item.GetID()
-
- switch value.Type {
- case av.KeyTypeRollup: // 渲染汇总
- rollupKey, _ := attrView.GetKey(value.KeyID)
- if nil == rollupKey || nil == rollupKey.Rollup {
- break
- }
+ if av.KeyTypeRollup != value.Type {
+ continue
+ }
- relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
- if nil == relKey || nil == relKey.Relation {
- break
- }
+ rollupKey, _ := attrView.GetKey(value.KeyID)
+ if nil == rollupKey || nil == rollupKey.Rollup {
+ break
+ }
- relVal := attrView.GetValue(relKey.ID, itemID)
- if nil == relVal || nil == relVal.Relation {
- break
- }
+ relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
+ if nil == relKey || nil == relKey.Relation {
+ break
+ }
- destAv := cachedAttrViews[relKey.Relation.AvID]
- if nil == destAv {
- destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
- if nil != destAv {
- cachedAttrViews[relKey.Relation.AvID] = destAv
- }
- }
- if nil == destAv {
- break
- }
+ relVal := attrView.GetValue(relKey.ID, item.GetID())
+ if nil == relVal || nil == relVal.Relation {
+ break
+ }
- destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
- if nil == destKey {
- break
+ destAv := cachedAttrViews[relKey.Relation.AvID]
+ if nil == destAv {
+ destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
+ if nil != destAv {
+ cachedAttrViews[relKey.Relation.AvID] = destAv
}
+ }
+ if nil == destAv {
+ break
+ }
- furtherCollection := rollupFurtherCollections[rollupKey.ID]
- value.Rollup.BuildContents(destAv.KeyValues, destKey, relVal, rollupKey.Rollup.Calc, furtherCollection)
- case av.KeyTypeRelation: // 渲染关联
- value.Relation.Contents = nil
- relKey, _ := attrView.GetKey(value.KeyID)
- if nil != relKey && nil != relKey.Relation {
- destAv := cachedAttrViews[relKey.Relation.AvID]
- if nil == destAv {
- destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
- if nil != destAv {
- cachedAttrViews[relKey.Relation.AvID] = destAv
- }
- }
- if nil != destAv {
- blocks := map[string]*av.Value{}
- blockValues := destAv.GetBlockKeyValues()
- if nil != blockValues {
- for _, blockValue := range blockValues.Values {
- blocks[blockValue.BlockID] = blockValue
- }
- for _, blockID := range value.Relation.BlockIDs {
- if val := blocks[blockID]; nil != val {
- value.Relation.Contents = append(value.Relation.Contents, val)
- }
- }
- }
- }
- }
+ destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
+ if nil == destKey {
+ break
}
+
+ furtherCollection := rollupFurtherCollections[rollupKey.ID]
+ value.Rollup.BuildContents(destAv.KeyValues, destKey, relVal, rollupKey.Rollup.Calc, furtherCollection)
}
}
}
@@ -561,7 +568,7 @@ func GetFurtherCollections(attrView *av.AttributeView, cachedAttrViews map[strin
isSameAv := destAv.ID == attrView.ID
var furtherCollection av.Collection
- if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
+ if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type || av.KeyTypeRelation == destKey.Type)) {
viewable := RenderView(destAv, destAv.Views[0], "")
if nil != viewable {
furtherCollection = viewable.(av.Collection)