Browse Source

:art: Support modifying database column types Fix https://github.com/siyuan-note/siyuan/issues/9513

Daniel 1 year ago
parent
commit
a57c258fce
2 changed files with 61 additions and 0 deletions
  1. 2 0
      kernel/model/attribute_view.go
  2. 59 0
      kernel/treenode/node.go

+ 2 - 0
kernel/model/attribute_view.go

@@ -415,6 +415,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
 				tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeUpdated}
 			}
 
+			treenode.FillAttributeViewTableCellNilValue(tableCell, rowID, col.ID)
+
 			tableRow.Cells = append(tableRow.Cells, tableCell)
 		}
 		ret.Rows = append(ret.Rows, &tableRow)

+ 59 - 0
kernel/treenode/node.go

@@ -663,6 +663,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
 				tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeUpdated}
 			}
 
+			FillAttributeViewTableCellNilValue(tableCell, rowID, col.ID)
+
 			tableRow.Cells = append(tableRow.Cells, tableCell)
 		}
 		ret.Rows = append(ret.Rows, &tableRow)
@@ -722,6 +724,63 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
 	return
 }
 
+func FillAttributeViewTableCellNilValue(tableCell *av.TableCell, rowID, colID string) {
+	if nil == tableCell.Value {
+		tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: colID, BlockID: rowID, Type: tableCell.ValueType}
+	}
+	tableCell.Value.Type = tableCell.ValueType
+	switch tableCell.ValueType {
+	case av.KeyTypeText:
+		if nil == tableCell.Value.Text {
+			tableCell.Value.Text = &av.ValueText{}
+		}
+	case av.KeyTypeNumber:
+		if nil == tableCell.Value.Number {
+			tableCell.Value.Number = &av.ValueNumber{}
+		}
+	case av.KeyTypeDate:
+		if nil == tableCell.Value.Date {
+			tableCell.Value.Date = &av.ValueDate{}
+		}
+	case av.KeyTypeSelect:
+		if 1 > len(tableCell.Value.MSelect) {
+			tableCell.Value.MSelect = []*av.ValueSelect{}
+		}
+	case av.KeyTypeMSelect:
+		if 1 > len(tableCell.Value.MSelect) {
+			tableCell.Value.MSelect = []*av.ValueSelect{}
+		}
+	case av.KeyTypeURL:
+		if nil == tableCell.Value.URL {
+			tableCell.Value.URL = &av.ValueURL{}
+		}
+	case av.KeyTypeEmail:
+		if nil == tableCell.Value.Email {
+			tableCell.Value.Email = &av.ValueEmail{}
+		}
+	case av.KeyTypePhone:
+		if nil == tableCell.Value.Phone {
+			tableCell.Value.Phone = &av.ValuePhone{}
+		}
+	case av.KeyTypeMAsset:
+		if 1 > len(tableCell.Value.MAsset) {
+			tableCell.Value.MAsset = []*av.ValueAsset{}
+		}
+	case av.KeyTypeTemplate:
+		if nil == tableCell.Value.Template {
+			tableCell.Value.Template = &av.ValueTemplate{}
+		}
+	case av.KeyTypeCreated:
+		if nil == tableCell.Value.Created {
+			tableCell.Value.Created = &av.ValueCreated{}
+		}
+	case av.KeyTypeUpdated:
+		if nil == tableCell.Value.Updated {
+			tableCell.Value.Updated = &av.ValueUpdated{}
+		}
+	}
+}
+
 func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av.KeyValues) string {
 	if "" == ial["id"] {
 		block := getRowBlockValue(rowValues)