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

Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
Daniel 2025-09-15 17:55:58 +08:00
parent 9b62385e86
commit 07cfc58642
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 112 additions and 68 deletions

View file

@ -81,7 +81,7 @@ const genSearchList = (element: Element, keyword: string, avId: string, isRelati
showMessage(window.siyuan.languages.selectRelation); showMessage(window.siyuan.languages.selectRelation);
return; return;
} }
fetchPost(isRelation ? "/api/av/searchAttributeViewRelationKey" : "/api/av/searchAttributeViewNonRelationKey", { fetchPost(isRelation ? "/api/av/searchAttributeViewRelationKey" : "/api/av/searchAttributeViewRollupDestKeys", {
avID: avId, avID: avId,
keyword keyword
}, (response) => { }, (response) => {

View file

@ -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) { func searchAttributeViewRelationKey(c *gin.Context) {
ret := gulu.Ret.NewResult() ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret) defer c.JSON(http.StatusOK, ret)

View file

@ -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/getAttributeView", model.CheckAuth, model.CheckReadonly, getAttributeView)
ginServer.Handle("POST", "/api/av/searchAttributeViewRelationKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, searchAttributeViewRelationKey) 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/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/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/addAttributeViewKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, addAttributeViewKey)
ginServer.Handle("POST", "/api/av/removeAttributeViewKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, removeAttributeViewKey) ginServer.Handle("POST", "/api/av/removeAttributeViewKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, removeAttributeViewKey)

View file

@ -1311,6 +1311,26 @@ func SearchAttributeViewNonRelationKey(avID, keyword string) (ret []*av.Key) {
return 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) { func SearchAttributeViewRelationKey(avID, keyword string) (ret []*av.Key) {
waitForSyncingStorages() waitForSyncingStorages()
@ -1592,8 +1612,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
} }
} }
// 先渲染主键、创建时间、更新时间 // 渲染主键、创建时间、更新时间
for _, kv := range keyValues { for _, kv := range keyValues {
switch kv.Key.Type { 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 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) rollupFurtherCollections := sql.GetFurtherCollections(attrView, cachedAttrViews)
for _, kv := range keyValues { for _, kv := range keyValues {
switch kv.Key.Type { switch kv.Key.Type {
@ -1685,7 +1703,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
} }
} }
// 最后渲染模板 // 渲染模板
templateKeys, _ := sql.GetTemplateKeysByResolutionOrder(attrView) templateKeys, _ := sql.GetTemplateKeysByResolutionOrder(attrView)
var renderTemplateErr error var renderTemplateErr error
for _, templateKey := range templateKeys { for _, templateKey := range templateKeys {

View file

@ -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) { 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 _, item := range collection.GetItems() {
for _, value := range item.GetValues() { 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{} rollupFurtherCollections := map[string]av.Collection{}
for _, field := range collection.GetFields() { for _, field := range collection.GetFields() {
if av.KeyTypeRollup != field.GetType() { if av.KeyTypeRollup != field.GetType() {
@ -451,7 +485,7 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
isSameAv := destAv.ID == attrView.ID isSameAv := destAv.ID == attrView.ID
var furtherCollection av.Collection 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) viewable := renderView(destAv, destAv.Views[0], "", depth, cachedAttrViews)
if nil != viewable { if nil != viewable {
furtherCollection = viewable.(av.Collection) furtherCollection = viewable.(av.Collection)
@ -465,70 +499,43 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
for _, item := range collection.GetItems() { for _, item := range collection.GetItems() {
for _, value := range item.GetValues() { for _, value := range item.GetValues() {
itemID := item.GetID() if av.KeyTypeRollup != value.Type {
continue
}
switch value.Type { rollupKey, _ := attrView.GetKey(value.KeyID)
case av.KeyTypeRollup: // 渲染汇总 if nil == rollupKey || nil == rollupKey.Rollup {
rollupKey, _ := attrView.GetKey(value.KeyID) break
if nil == rollupKey || nil == rollupKey.Rollup { }
break
}
relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID) relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
if nil == relKey || nil == relKey.Relation { if nil == relKey || nil == relKey.Relation {
break break
} }
relVal := attrView.GetValue(relKey.ID, itemID) relVal := attrView.GetValue(relKey.ID, item.GetID())
if nil == relVal || nil == relVal.Relation { if nil == relVal || nil == relVal.Relation {
break break
} }
destAv := cachedAttrViews[relKey.Relation.AvID] destAv := cachedAttrViews[relKey.Relation.AvID]
if nil == destAv { if nil == destAv {
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID) destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
if nil != destAv { if nil != destAv {
cachedAttrViews[relKey.Relation.AvID] = destAv cachedAttrViews[relKey.Relation.AvID] = destAv
}
}
if nil == destAv {
break
}
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)
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)
}
}
}
}
} }
} }
if nil == destAv {
break
}
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 isSameAv := destAv.ID == attrView.ID
var furtherCollection av.Collection 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], "") viewable := RenderView(destAv, destAv.Views[0], "")
if nil != viewable { if nil != viewable {
furtherCollection = viewable.(av.Collection) furtherCollection = viewable.(av.Collection)