Skip to content

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

:art: Add Rollup column to database table view https://github.com/siyuan-note/siyuan/issues/9958

Daniel93dc9f5c2256
diff --git a/kernel/av/av.go b/kernel/av/av.go
index fbf59218d8..348366ce65 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -105,8 +105,14 @@ func NewKey(id, name, icon string, keyType KeyType) *Key {
}
type Rollup struct {
- RelationKeyID string `json:"relationKeyID"` // 关联列 ID
- KeyID string `json:"keyID"` // 目标列 ID
+ RelationKeyID string `json:"relationKeyID"` // 关联列 ID
+ KeyID string `json:"keyID"` // 目标列 ID
+ Calc *RollupCalc `json:"calc"` // 计算方式
+}
+
+type RollupCalc struct {
+ Operator CalcOperator `json:"operator"`
+ Result *Value `json:"result"`
}
type Relation struct {
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 710eecaeab..581da4d3d8 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -774,6 +774,46 @@ func getRowBlockValue(keyValues []*av.KeyValues) (ret *av.Value) {
return
}
+func (tx *Transaction) doUpdateAttrViewColRollup(operation *Operation) (ret *TxErr) {
+ err := updateAttributeViewColRollup(operation)
+ if nil != err {
+ return &TxErr{code: TxErrWriteAttributeView, id: operation.AvID, msg: err.Error()}
+ }
+ return
+}
+
+func updateAttributeViewColRollup(operation *Operation) (err error) {
+ // operation.AvID 汇总列所在 av
+ // operation.ID 汇总列 ID
+ // operation.ParentID 汇总列基于的关联列 ID
+ // operation.KeyID 目标列 ID
+ // operation.Data 计算方式
+
+ attrView, err := av.ParseAttributeView(operation.AvID)
+ if nil != err {
+ return
+ }
+
+ rollUpKey, _ := attrView.GetKey(operation.ID)
+ if nil == rollUpKey {
+ return
+ }
+
+ rollUpKey.Rollup = &av.Rollup{
+ RelationKeyID: operation.ParentID,
+ KeyID: operation.KeyID,
+ }
+
+ if "" != operation.Data {
+ if err = gulu.JSON.UnmarshalJSON([]byte(operation.Data.(string)), &rollUpKey.Rollup.Calc); nil != err {
+ return
+ }
+ }
+
+ err = av.SaveAttributeView(attrView)
+ return
+}
+
func (tx *Transaction) doUpdateAttrViewColRelation(operation *Operation) (ret *TxErr) {
err := updateAttributeViewColRelation(operation)
if nil != err {
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index aaa101fd1e..4187e3e889 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -274,6 +274,8 @@ func performTx(tx *Transaction) (ret *TxErr) {
ret = tx.doSortAttrViewView(op)
case "updateAttrViewColRelation":
ret = tx.doUpdateAttrViewColRelation(op)
+ case "updateAttrViewColRollup":
+ ret = tx.doUpdateAttrViewColRollup(op)
}
if nil != ret {