Skip to content

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

:art: Improve database rollup template rendering https://github.com/siyuan-note/siyuan/issues/15722

Daniel7952fec7cbf7
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index eca441e86a..fe850f60e2 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -1536,6 +1536,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
@@ -1569,7 +1570,44 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
}
}
- // 再渲染汇总和关联
+ // 再渲染关联和汇总
+
+ rollupFurtherCollections := map[string]av.Collection{}
+ for _, kv := range keyValues {
+ if av.KeyTypeRollup != kv.Key.Type {
+ continue
+ }
+
+ relKey, _ := attrView.GetKey(kv.Key.Rollup.RelationKeyID)
+ if nil == relKey {
+ continue
+ }
+
+ destAv := attrViewCache[relKey.Relation.AvID]
+ if nil == destAv {
+ destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
+ if nil == destAv {
+ continue
+ }
+ attrViewCache[relKey.Relation.AvID] = destAv
+ }
+
+ destKey, _ := destAv.GetKey(kv.Key.Rollup.KeyID)
+ if nil == destKey {
+ continue
+ }
+ isSameAv := destAv.ID == attrView.ID
+
+ var furtherCollection av.Collection
+ if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
+ viewable := sql.RenderView(destAv, destAv.Views[0], "")
+ if nil != viewable {
+ furtherCollection = viewable.(av.Collection)
+ }
+ }
+ rollupFurtherCollections[kv.Key.ID] = furtherCollection
+ }
+
for _, kv := range keyValues {
switch kv.Key.Type {
case av.KeyTypeRollup:
@@ -1595,15 +1633,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
destKey, _ := destAv.GetKey(kv.Key.Rollup.KeyID)
if nil != destKey {
- isSameAv := destAv.ID == attrView.ID
- var furtherCollection av.Collection
- if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
- viewable := sql.RenderView(destAv, destAv.Views[0], "")
- if nil != viewable {
- furtherCollection = viewable.(av.Collection)
- }
- }
-
+ furtherCollection := rollupFurtherCollections[kv.Key.ID]
kv.Values[0].Rollup.BuildContents(keyValues, destKey, relVal, kv.Key.Rollup.Calc, furtherCollection)
}
}
diff --git a/kernel/sql/av.go b/kernel/sql/av.go
index 9746444003..ac992fc52c 100644
--- a/kernel/sql/av.go
+++ b/kernel/sql/av.go
@@ -338,6 +338,7 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
depth *int, cachedAttrViews map[string]*av.AttributeView) {
// 先渲染主键、创建时间、更新时间
+
for _, item := range collection.GetItems() {
for _, value := range item.GetValues() {
itemID := item.GetID()
@@ -399,7 +400,54 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
}
}
- // 再渲染汇总和关联
+ // 再渲染关联和汇总
+
+ rollupFurtherCollections := map[string]av.Collection{}
+ for _, field := range collection.GetFields() {
+ if av.KeyTypeRollup != field.GetType() {
+ continue
+ }
+
+ rollupKey, _ := attrView.GetKey(field.GetID())
+ if nil == rollupKey || nil == rollupKey.Rollup {
+ continue
+ }
+
+ relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
+ if nil == relKey || nil == relKey.Relation {
+ continue
+ }
+
+ 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 {
+ continue
+ }
+
+ destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
+ if nil == destKey {
+ continue
+ }
+
+ isSameAv := destAv.ID == attrView.ID
+ var furtherCollection av.Collection
+ if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
+ viewable := renderView(destAv, destAv.Views[0], "", depth, cachedAttrViews)
+ if nil != viewable {
+ furtherCollection = viewable.(av.Collection)
+ } else {
+ fillAttributeViewTemplateValues(destAv, destAv.Views[0], collection, ials)
+ furtherCollection = collection
+ }
+ }
+ rollupFurtherCollections[rollupKey.ID] = furtherCollection
+ }
+
for _, item := range collection.GetItems() {
for _, value := range item.GetValues() {
itemID := item.GetID()
@@ -437,18 +485,7 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
break
}
- isSameAv := destAv.ID == attrView.ID
- var furtherCollection av.Collection
- if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
- viewable := renderView(destAv, destAv.Views[0], "", depth, cachedAttrViews)
- if nil != viewable {
- furtherCollection = viewable.(av.Collection)
- } else {
- fillAttributeViewTemplateValues(destAv, destAv.Views[0], collection, ials)
- furtherCollection = collection
- }
- }
-
+ furtherCollection := rollupFurtherCollections[rollupKey.ID]
value.Rollup.BuildContents(destAv.KeyValues, destKey, relVal, rollupKey.Rollup.Calc, furtherCollection)
case av.KeyTypeRelation: // 渲染关联
value.Relation.Contents = nil