From 9288e523b23f2fd8522e5eb7847e707f647daffe Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 9 Oct 2023 12:14:11 +0800 Subject: [PATCH] :art: Add created and updated type column to database https://github.com/siyuan-note/siyuan/issues/9371 --- kernel/model/attribute_view.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 5f62f9f5fea..7fd04e592ed 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -127,8 +127,13 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) { } } - if av.KeyTypeTemplate == kValues.Key.Type { + switch kValues.Key.Type { + case av.KeyTypeTemplate: kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: ""}}) + case av.KeyTypeCreated: + kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeCreated}) + case av.KeyTypeUpdated: + kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeUpdated}) } if 0 < len(kValues.Values) { @@ -136,13 +141,33 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) { } } - // 渲染模板列 + // 渲染自动生成的列值,比如模板列、创建时间列和更新时间列 for _, kv := range keyValues { - if av.KeyTypeTemplate == kv.Key.Type { + switch kv.Key.Type { + case av.KeyTypeTemplate: if 0 < len(kv.Values) { ial := GetBlockAttrs(blockID) kv.Values[0].Template.Content = renderTemplateCol(ial, kv.Key.Template, keyValues) } + case av.KeyTypeCreated: + createdStr := blockID[:len("20060102150405")] + created, parseErr := time.Parse("20060102150405", createdStr) + if nil == parseErr { + kv.Values[0].Created = av.NewFormattedValueCreated(created.UnixMilli(), 0, av.CreatedFormatNone) + } else { + logging.LogWarnf("parse created [%s] failed: %s", createdStr, parseErr) + kv.Values[0].Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone) + } + case av.KeyTypeUpdated: + ial := GetBlockAttrs(blockID) + updatedStr := ial["updated"] + updated, parseErr := time.Parse("20060102150405", updatedStr) + if nil == parseErr { + kv.Values[0].Updated = av.NewFormattedValueUpdated(updated.UnixMilli(), 0, av.UpdatedFormatNone) + } else { + logging.LogWarnf("parse updated [%s] failed: %s", updatedStr, parseErr) + kv.Values[0].Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone) + } } }