Skip to content

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

:art: The database template field supports using other template fields https://github.com/siyuan-note/siyuan/issues/15517

Daniel6e6522d56a80
diff --git a/kernel/av/value.go b/kernel/av/value.go
index 83fc42d883..2c5380661b 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -80,7 +80,7 @@ func (value *Value) String(format bool) string {
if nil == value.Text {
return ""
}
- return strings.TrimSpace(value.Text.Content)
+ return value.Text.Content
case KeyTypeNumber:
if nil == value.Number {
return ""
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index faf5bc9ff6..0f8537f044 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -1485,6 +1485,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
}
}
+ kv.Values[0].Rollup.Contents = nil
for _, bID := range relVal.Relation.BlockIDs {
destVal := destAv.GetValue(kv.Key.Rollup.KeyID, bID)
if nil != furtherCollection && av.KeyTypeTemplate == destKey.Type {
diff --git a/kernel/model/export.go b/kernel/model/export.go
index cebbdec7d2..9dc71de65e 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -3438,7 +3438,7 @@ func getAttrViewTable(attrView *av.AttributeView, view *av.View, query string) (
}
depth := 1
- ret = sql.RenderAttributeViewTable(attrView, view, query, &depth, map[string]*av.AttributeView{}, map[string]av.Collection{})
+ ret = sql.RenderAttributeViewTable(attrView, view, query, &depth, map[string]*av.AttributeView{})
return
}
diff --git a/kernel/sql/av.go b/kernel/sql/av.go
index 8a617a67f8..015377f50c 100644
--- a/kernel/sql/av.go
+++ b/kernel/sql/av.go
@@ -73,14 +73,13 @@ func RenderGroupView(attrView *av.AttributeView, view, groupView *av.View, query
func RenderView(attrView *av.AttributeView, view *av.View, query string) (ret av.Viewable) {
depth := 1
renderedAttrViews := map[string]*av.AttributeView{}
- renderedTemplateKeyCollections := map[string]av.Collection{}
renderedAttrViews[attrView.ID] = attrView
- ret = renderView(attrView, view, query, &depth, renderedAttrViews, renderedTemplateKeyCollections)
+ ret = renderView(attrView, view, query, &depth, renderedAttrViews)
return
}
func renderView(attrView *av.AttributeView, view *av.View, query string,
- depth *int, renderedAttrViews map[string]*av.AttributeView, renderedTemplateKeyCollections map[string]av.Collection) (ret av.Viewable) {
+ depth *int, renderedAttrViews map[string]*av.AttributeView) (ret av.Viewable) {
if 7 < *depth {
return
}
@@ -88,9 +87,9 @@ func renderView(attrView *av.AttributeView, view *av.View, query string,
*depth++
switch view.LayoutType {
case av.LayoutTypeTable:
- ret = RenderAttributeViewTable(attrView, view, query, depth, renderedAttrViews, renderedTemplateKeyCollections)
+ ret = RenderAttributeViewTable(attrView, view, query, depth, renderedAttrViews)
case av.LayoutTypeGallery:
- ret = RenderAttributeViewGallery(attrView, view, query, depth, renderedAttrViews, renderedTemplateKeyCollections)
+ ret = RenderAttributeViewGallery(attrView, view, query, depth, renderedAttrViews)
}
return
}
@@ -280,7 +279,7 @@ func filterNotFoundAttrViewItems(keyValuesMap *map[string][]*av.KeyValues) {
var toCheckBlockIDs []string
for blockID, keyValues := range *keyValuesMap {
blockValue := getBlockValue(keyValues)
- if nil == blockValue {
+ if nil == blockValue || nil == blockValue.Block {
notFound = append(notFound, blockID)
continue
}
@@ -289,7 +288,7 @@ func filterNotFoundAttrViewItems(keyValuesMap *map[string][]*av.KeyValues) {
continue
}
- if nil != blockValue.Block && "" == blockValue.Block.ID {
+ if "" == blockValue.Block.ID {
notFound = append(notFound, blockID)
continue
}
@@ -329,8 +328,8 @@ func fillAttributeViewBaseValue(baseValue *av.BaseValue, fieldID, itemID string,
}
}
-func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string, items map[string][]*av.KeyValues,
- depth *int, renderedAttrViews map[string]*av.AttributeView, renderedTemplateKeyCollections map[string]av.Collection) {
+func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string,
+ depth *int, renderedAttrViews map[string]*av.AttributeView) {
for _, item := range collection.GetItems() {
for _, value := range item.GetValues() {
itemID := item.GetID()
@@ -378,20 +377,19 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
}
var furtherCollection av.Collection
- var ok bool
if av.KeyTypeTemplate == destKey.Type {
- if furtherCollection, ok = renderedTemplateKeyCollections[destKey.ID]; !ok {
- renderedTemplateKeyCollections[destKey.ID] = collection
-
- // 渲染目标视图,这样才能汇总渲染后的模板字段值
- viewable := renderView(destAv, destAv.Views[0], "", depth, renderedAttrViews, renderedTemplateKeyCollections)
- if nil != viewable {
- furtherCollection = viewable.(av.Collection)
- renderedTemplateKeyCollections[destKey.ID] = furtherCollection
- }
+
+ // 渲染目标视图,这样才能汇总渲染后的模板字段值
+ viewable := renderView(destAv, destAv.Views[0], "", depth, renderedAttrViews)
+ if nil != viewable {
+ furtherCollection = viewable.(av.Collection)
+ } else {
+ fillAttributeViewTemplateValues(destAv, destAv.Views[0], collection, ials)
+ furtherCollection = collection
}
}
+ value.Rollup.Contents = nil
for _, blockID := range relVal.Relation.BlockIDs {
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
if nil != furtherCollection && av.KeyTypeTemplate == destKey.Type {
@@ -415,12 +413,6 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
}
value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
-
- // 将汇总字段的值保存到 rowsValues 中,后续渲染模板字段的时候会用到,下同
- // Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
- keyValues := items[itemID]
- keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: value.ID, KeyID: rollupKey.ID, BlockID: itemID, Type: av.KeyTypeRollup, Rollup: value.Rollup}}})
- items[itemID] = keyValues
case av.KeyTypeRelation: // 渲染关联
value.Relation.Contents = nil
relKey, _ := attrView.GetKey(value.KeyID)
@@ -447,10 +439,6 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
}
}
}
-
- keyValues := items[itemID]
- keyValues = append(keyValues, &av.KeyValues{Key: relKey, Values: []*av.Value{{ID: value.ID, KeyID: relKey.ID, BlockID: itemID, Type: av.KeyTypeRelation, Relation: value.Relation}}})
- items[itemID] = keyValues
case av.KeyTypeCreated: // 渲染创建时间
ial := map[string]string{}
block := item.GetBlockValue()
@@ -472,11 +460,6 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
} else {
value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
}
-
- keyValues := items[itemID]
- createdKey, _ := attrView.GetKey(value.KeyID)
- keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: value.ID, KeyID: createdKey.ID, BlockID: itemID, Type: av.KeyTypeCreated, Created: value.Created}}})
- items[itemID] = keyValues
case av.KeyTypeUpdated: // 渲染更新时间
ial := map[string]string{}
block := item.GetBlockValue()
@@ -499,17 +482,13 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
}
}
-
- keyValues := items[itemID]
- updatedKey, _ := attrView.GetKey(value.KeyID)
- keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: value.ID, KeyID: updatedKey.ID, BlockID: itemID, Type: av.KeyTypeUpdated, Updated: value.Updated}}})
- items[itemID] = keyValues
}
}
}
}
-func fillAttributeViewTemplateValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string, items map[string][]*av.KeyValues) (err error) {
+func fillAttributeViewTemplateValues(attrView *av.AttributeView, view *av.View, collection av.Collection, ials map[string]map[string]string) (err error) {
+ items := generateAttrViewItems(attrView, view)
existTemplateField := false
for _, kVals := range attrView.KeyValues {
if av.KeyTypeTemplate == kVals.Key.Type {
@@ -552,6 +531,23 @@ func fillAttributeViewTemplateValues(attrView *av.AttributeView, collection av.C
return
}
+func fillAttributeViewKeyValues(attrView *av.AttributeView, collection av.Collection) {
+ fieldValues := map[string][]*av.Value{}
+ for _, card := range collection.GetItems() {
+ for _, val := range card.GetValues() {
+ keyID := val.KeyID
+ fieldValues[keyID] = append(fieldValues[keyID], val)
+ }
+ }
+ for keyID, values := range fieldValues {
+ keyValues, _ := attrView.GetKeyValues(keyID)
+ keyValues.Values = nil
+ for _, val := range values {
+ keyValues.Values = append(keyValues.Values, val)
+ }
+ }
+}
+
func FillAttributeViewNilValue(value *av.Value, typ av.KeyType) {
value.Type = typ
switch typ {
diff --git a/kernel/sql/av_gallery.go b/kernel/sql/av_gallery.go
index f2ac61929e..2a08358589 100644
--- a/kernel/sql/av_gallery.go
+++ b/kernel/sql/av_gallery.go
@@ -19,7 +19,7 @@ import (
)
func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query string,
- depth *int, renderedAttrViews map[string]*av.AttributeView, renderedTemplateKeyCollections map[string]av.Collection) (ret *av.Gallery) {
+ depth *int, renderedAttrViews map[string]*av.AttributeView) (ret *av.Gallery) {
ret = &av.Gallery{
BaseInstance: av.NewViewBaseInstance(view),
CoverFrom: view.Gallery.CoverFrom,
@@ -111,14 +111,17 @@ func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query
ret.Cards = append(ret.Cards, &galleryCard)
}
+ // 回填补全数据
+ fillAttributeViewKeyValues(attrView, ret)
+
// 批量获取块属性以提升性能
ials := BatchGetBlockAttrsWitTrees(ialIDs, boundTrees)
// 渲染自动生成的字段值,比如关联、汇总、创建时间和更新时间
- fillAttributeViewAutoGeneratedValues(attrView, ret, ials, cardsValues, depth, renderedAttrViews, renderedTemplateKeyCollections)
+ fillAttributeViewAutoGeneratedValues(attrView, ret, ials, depth, renderedAttrViews)
// 最后单独渲染模板字段,这样模板就可以使用汇总、关联、创建时间和更新时间的值了
- renderTemplateErr := fillAttributeViewTemplateValues(attrView, ret, ials, cardsValues)
+ renderTemplateErr := fillAttributeViewTemplateValues(attrView, view, ret, ials)
if nil != renderTemplateErr {
util.PushErrMsg(fmt.Sprintf(util.Langs[util.Lang][44], util.EscapeHTML(renderTemplateErr.Error())), 30000)
}
diff --git a/kernel/sql/av_table.go b/kernel/sql/av_table.go
index b7cbcddc94..138a8832f4 100644
--- a/kernel/sql/av_table.go
+++ b/kernel/sql/av_table.go
@@ -23,7 +23,7 @@ import (
)
func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query string,
- depth *int, renderedAttrViews map[string]*av.AttributeView, renderedTemplateKeyCollections map[string]av.Collection) (ret *av.Table) {
+ depth *int, renderedAttrViews map[string]*av.AttributeView) (ret *av.Table) {
ret = &av.Table{
BaseInstance: av.NewViewBaseInstance(view),
Columns: []*av.TableColumn{},
@@ -97,6 +97,9 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
ret.Rows = append(ret.Rows, &tableRow)
}
+ // 回填补全数据
+ fillAttributeViewKeyValues(attrView, ret)
+
// 批量获取块属性以提升性能
var ialIDs []string
for _, row := range ret.Rows {
@@ -108,10 +111,10 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
ials := BatchGetBlockAttrs(ialIDs)
// 渲染自动生成的字段值,比如关联、汇总、创建时间和更新时间
- fillAttributeViewAutoGeneratedValues(attrView, ret, ials, rowsValues, depth, renderedAttrViews, renderedTemplateKeyCollections)
+ fillAttributeViewAutoGeneratedValues(attrView, ret, ials, depth, renderedAttrViews)
// 最后单独渲染模板字段,这样模板就可以使用汇总、关联、创建时间和更新时间的值了
- renderTemplateErr := fillAttributeViewTemplateValues(attrView, ret, ials, rowsValues)
+ renderTemplateErr := fillAttributeViewTemplateValues(attrView, view, ret, ials)
if nil != renderTemplateErr {
util.PushErrMsg(fmt.Sprintf(util.Langs[util.Lang][44], util.EscapeHTML(renderTemplateErr.Error())), 30000)
}