Parcourir la source

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

Daniel il y a 1 an
Parent
commit
46c82acf6c
5 fichiers modifiés avec 72 ajouts et 24 suppressions
  1. 12 4
      kernel/av/av.go
  2. 5 3
      kernel/av/table.go
  3. 52 16
      kernel/model/attribute_view.go
  4. 1 1
      kernel/model/transaction.go
  5. 2 0
      kernel/treenode/node.go

+ 12 - 4
kernel/av/av.go

@@ -89,12 +89,10 @@ type Key struct {
 	Template string `json:"template"` // 模板内容
 
 	// 关联列
-	RelationAvID      string `json:"relationAvID"`      // 关联的属性视图 ID
-	IsBiRelation      bool   `json:"isBiRelation"`      // 是否双向关联
-	BackRelationKeyID string `json:"backRelationKeyID"` // 双向关联时回链关联列的 ID
+	Relation *Relation `json:"relation,omitempty"` // 关联信息
 
 	// 汇总列
-	RollupKeyID string `json:"rollupKeyID"` // 汇总列 ID
+	Rollup *Rollup `json:"rollup,omitempty"` // 汇总信息
 }
 
 func NewKey(id, name, icon string, keyType KeyType) *Key {
@@ -106,6 +104,16 @@ func NewKey(id, name, icon string, keyType KeyType) *Key {
 	}
 }
 
+type Rollup struct {
+	KeyID string `json:"keyID"` // 汇总列 ID
+}
+
+type Relation struct {
+	AvID      string `json:"avID"`      // 关联的属性视图 ID
+	IsTwoWay  bool   `json:"isTwoWay"`  // 是否双向关联
+	BackKeyID string `json:"backKeyID"` // 双向关联时回链关联列的 ID
+}
+
 type KeySelectOption struct {
 	Name  string `json:"name"`
 	Color string `json:"color"`

+ 5 - 3
kernel/av/table.go

@@ -651,9 +651,11 @@ type TableColumn struct {
 
 	// 以下是某些列类型的特有属性
 
-	Options      []*KeySelectOption `json:"options,omitempty"` // 选项列表
-	NumberFormat NumberFormat       `json:"numberFormat"`      // 列数字格式化
-	Template     string             `json:"template"`          // 模板内容
+	Options      []*KeySelectOption `json:"options,omitempty"`  // 选项列表
+	NumberFormat NumberFormat       `json:"numberFormat"`       // 列数字格式化
+	Template     string             `json:"template"`           // 模板内容
+	Relation     *Relation          `json:"relation,omitempty"` // 关联列
+	Rollup       *Rollup            `json:"rollup,omitempty"`   // 汇总列
 }
 
 type TableCell struct {

+ 52 - 16
kernel/model/attribute_view.go

@@ -553,6 +553,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
 			Options:      key.Options,
 			NumberFormat: key.NumberFormat,
 			Template:     key.Template,
+			Relation:     key.Relation,
+			Rollup:       key.Rollup,
 			Wrap:         col.Wrap,
 			Hidden:       col.Hidden,
 			Width:        col.Width,
@@ -728,32 +730,66 @@ func (tx *Transaction) doUpdateAttrViewColRelation(operation *Operation) (ret *T
 }
 
 func updateAttributeViewColRelation(operation *Operation) (err error) {
-	err = updateAttributeViewColRelation0(operation.AvID, operation.KeyID, operation.ID, operation.IsBiRelation, operation.BackRelationKeyID, operation.Name)
+	// operation.AvID 源 avID
+	// operation.ID 目标 avID
+	// operation.KeyID 源 av 关联列 ID
+	// operation.IsTwoWay 是否双向关联
+	// operation.BackRelationKeyID 双向关联的目标关联列 ID
+	// operation.Name 双向关联的目标关联列名称
+
+	srcAv, err := av.ParseAttributeView(operation.AvID)
 	if nil != err {
 		return
 	}
 
-	if operation.IsBiRelation {
-		err = updateAttributeViewColRelation0(operation.ID, operation.BackRelationKeyID, operation.AvID, operation.IsBiRelation, operation.KeyID, operation.Name)
-	}
-	return
-}
-
-func updateAttributeViewColRelation0(avID, relKeyID, destAvID string, isBiRel bool, backRelKeyID, backRelKeyName string) (err error) {
-	attrView, err := av.ParseAttributeView(avID)
+	destAv, err := av.ParseAttributeView(operation.ID)
 	if nil != err {
 		return
 	}
 
-	for _, keyValues := range attrView.KeyValues {
-		if keyValues.Key.ID == relKeyID {
-			keyValues.Key.RelationAvID = destAvID
-			keyValues.Key.IsBiRelation = isBiRel
-			keyValues.Key.BackRelationKeyID = backRelKeyID
-			err = av.SaveAttributeView(attrView)
-			return
+	isSameAv := srcAv.ID == destAv.ID
+
+	for _, keyValues := range srcAv.KeyValues {
+		if keyValues.Key.ID == operation.KeyID {
+			keyValues.Key.Relation = &av.Relation{
+				AvID:      operation.ID,
+				IsTwoWay:  operation.IsTwoWay,
+				BackKeyID: operation.BackRelationKeyID,
+			}
+			break
 		}
 	}
+
+	destAdded := false
+	for _, keyValues := range destAv.KeyValues {
+		if keyValues.Key.ID == operation.BackRelationKeyID {
+			keyValues.Key.Relation = &av.Relation{
+				AvID:      operation.AvID,
+				IsTwoWay:  operation.IsTwoWay,
+				BackKeyID: operation.KeyID,
+			}
+			destAdded = true
+			break
+		}
+	}
+	if !destAdded {
+		destAv.KeyValues = append(destAv.KeyValues, &av.KeyValues{
+			Key: &av.Key{
+				ID:       operation.BackRelationKeyID,
+				Name:     operation.Name,
+				Type:     av.KeyTypeRelation,
+				Relation: &av.Relation{AvID: operation.AvID, IsTwoWay: operation.IsTwoWay, BackKeyID: operation.KeyID},
+			},
+		})
+	}
+
+	err = av.SaveAttributeView(srcAv)
+	if nil != err {
+		return
+	}
+	if !isSameAv {
+		err = av.SaveAttributeView(destAv)
+	}
 	return
 }
 

+ 1 - 1
kernel/model/transaction.go

@@ -1191,7 +1191,7 @@ type Operation struct {
 	Format            string   `json:"format"`            // 属性视图列格式化
 	KeyID             string   `json:"keyID"`             // 属性视列 ID
 	RowID             string   `json:"rowID"`             // 属性视图行 ID
-	IsBiRelation      bool     `json:"isBiRelation"`      // 属性视图关联列是否是双向关系
+	IsTwoWay          bool     `json:"isTwoWay"`          // 属性视图关联列是否是双向关系
 	BackRelationKeyID string   `json:"backRelationKeyID"` // 属性视图关联列回链关联列的 ID
 }
 

+ 2 - 0
kernel/treenode/node.go

@@ -614,6 +614,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
 			Options:      key.Options,
 			NumberFormat: key.NumberFormat,
 			Template:     key.Template,
+			Relation:     key.Relation,
+			Rollup:       key.Rollup,
 			Wrap:         col.Wrap,
 			Hidden:       col.Hidden,
 			Width:        col.Width,