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

This commit is contained in:
Daniel 2023-12-24 21:47:10 +08:00
parent be4ebe776d
commit 93dc9f5c22
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 50 additions and 2 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {