🎨 基于内容块属性动态建立对应数据库表结构 https://github.com/siyuan-note/siyuan/issues/7521

This commit is contained in:
Liang Ding 2023-03-02 11:32:39 +08:00
parent 35081c1e9b
commit 7df8700205
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
8 changed files with 189 additions and 44 deletions

View file

@ -19,6 +19,7 @@ package av
import (
"database/sql"
"github.com/88250/lute/ast"
"path/filepath"
"strings"
@ -30,9 +31,9 @@ import (
// AttributeView 描述了属性视图的结构。
type AttributeView struct {
ID string `json:"id"` // 属性视图 ID
Columns []Column `json:"columns"` // 表格列名
Rows [][]string `json:"rows"` // 表格行记录
ID string `json:"id"` // 属性视图 ID
Columns []Column `json:"columns"` // 表格列名
Rows [][]Cell `json:"rows"` // 表格行记录
Type AttributeViewType `json:"type"` // 属性视图类型
Projections []string `json:"projections"` // 显示的列名SELECT *
@ -47,6 +48,18 @@ const (
AttributeViewTypeTable AttributeViewType = "table" // 属性视图类型 - 表格
)
func NewAttributeView() *AttributeView {
return &AttributeView{
ID: ast.NewNodeID(),
Columns: []Column{NewColumnBlock()},
Rows: [][]Cell{},
Type: AttributeViewTypeTable,
Projections: []string{},
Filters: []*AttributeViewFilter{},
Sorts: []*AttributeViewSort{},
}
}
func (av *AttributeView) GetColumnNames() (ret []string) {
ret = []string{}
for _, column := range av.Columns {
@ -88,46 +101,48 @@ const (
SortOrderDesc SortOrder = "DESC"
)
// SyncAttributeViewTableFromJSON 从 JSON 文件同步属性视图表,用于数据同步后将属性视图 JSON 文件同步到数据库。
func SyncAttributeViewTableFromJSON(tableID string) (err error) {
avJSONPath := getAttributeViewJSONPath(tableID)
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
avJSONPath := getAttributeViewJSONPath(avID)
if !gulu.File.IsExist(avJSONPath) {
ret = NewAttributeView()
return
}
data, err := filelock.ReadFile(avJSONPath)
if nil != err {
logging.LogErrorf("read attribute view table failed: %s", err)
logging.LogErrorf("read attribute view [%s] failed: %s", avID, err)
return
}
var attributeView AttributeView
if err = gulu.JSON.UnmarshalJSON(data, &attributeView); nil != err {
logging.LogErrorf("unmarshal attribute view table failed: %s", err)
ret = &AttributeView{}
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
return
}
return
}
// SyncAttributeViewTableToJSON 同步属性视图表到 JSON 文件,用于将数据库中的属性视图持久化到 JSON 文件中。
func SyncAttributeViewTableToJSON(av *AttributeView) (err error) {
func SaveAttributeView(av *AttributeView) (err error) {
data, err := gulu.JSON.MarshalJSON(av)
if nil != err {
logging.LogErrorf("marshal attribute view table [%s] failed: %s", av.ID, err)
logging.LogErrorf("marshal attribute view [%s] failed: %s", av.ID, err)
return
}
avJSONPath := getAttributeViewJSONPath(av.ID)
if err = filelock.WriteFile(avJSONPath, data); nil != err {
logging.LogErrorf("save attribute view table [%s] failed: %s", av.ID, err)
logging.LogErrorf("save attribute view [%s] failed: %s", av.ID, err)
return
}
return
}
func getAttributeViewJSONPath(tableID string) string {
return filepath.Join(util.DataDir, "storage", "av", tableID+".json")
func getAttributeViewJSONPath(avID string) string {
return filepath.Join(util.DataDir, "storage", "av", avID+".json")
}
func dropAttributeViewTableColumn(db *sql.DB, tableID string, column string) (err error) {
_, err = db.Exec("ALTER TABLE `av_" + tableID + "` DROP COLUMN `" + column + "`")
func dropAttributeViewTableColumn(db *sql.DB, avID string, column string) (err error) {
_, err = db.Exec("ALTER TABLE `av_" + avID + "` DROP COLUMN `" + column + "`")
if nil != err {
logging.LogErrorf("drop column [%s] failed: %s", column, err)
return
@ -135,8 +150,8 @@ func dropAttributeViewTableColumn(db *sql.DB, tableID string, column string) (er
return
}
func addAttributeViewTableColumn(db *sql.DB, tableID string, column string) (err error) {
_, err = db.Exec("ALTER TABLE `av_" + tableID + "` ADD COLUMN `" + column + "`")
func addAttributeViewTableColumn(db *sql.DB, avID string, column string) (err error) {
_, err = db.Exec("ALTER TABLE `av_" + avID + "` ADD COLUMN `" + column + "`")
if nil != err {
logging.LogErrorf("add column [%s] failed: %s", column, err)
return
@ -144,19 +159,19 @@ func addAttributeViewTableColumn(db *sql.DB, tableID string, column string) (err
return
}
func dropAttributeViewTable(db *sql.DB, tableID string) (err error) {
_, err = db.Exec("DROP TABLE IF EXISTS `av_" + tableID + "`")
func dropAttributeViewTable(db *sql.DB, avID string) (err error) {
_, err = db.Exec("DROP TABLE IF EXISTS `av_" + avID + "`")
if nil != err {
logging.LogErrorf("drop table [%s] failed: %s", tableID, err)
logging.LogErrorf("drop table [%s] failed: %s", avID, err)
return
}
return
}
func createAttributeViewTable(db *sql.DB, tableID string, column []string) (err error) {
_, err = db.Exec("CREATE TABLE IF NOT EXISTS `av_" + tableID + "` (id, " + strings.Join(column, ", ") + ")")
func createAttributeViewTable(db *sql.DB, avID string, column []string) (err error) {
_, err = db.Exec("CREATE TABLE IF NOT EXISTS `av_" + avID + "` (id, " + strings.Join(column, ", ") + ")")
if nil != err {
logging.LogErrorf("create table [%s] failed: %s", tableID, err)
logging.LogErrorf("create table [%s] failed: %s", avID, err)
return
}
return

35
kernel/av/cell.go Normal file
View file

@ -0,0 +1,35 @@
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package av
type Cell interface {
// Resolve 根据参数解析单元格的值。
Resolve(input interface{}) string
}
type CellBlock struct {
Value string `json:"value"`
}
func NewCellBlock(blockID string) *CellBlock {
return &CellBlock{Value: blockID}
}
func (c *CellBlock) Resolve(blockID interface{}) string {
return blockID.(string)
}

View file

@ -26,14 +26,26 @@ type Column interface {
Name() string
// Type 用于获取列类型。
Type() string
Type() ColumnType
}
type ColumnType string
const (
ColumnTypeBlock ColumnType = "block"
ColumnTypeDate ColumnType = "date"
ColumnTypeNumber ColumnType = "number"
ColumnTypeRelation ColumnType = "relation"
ColumnTypeRollup ColumnType = "rollup"
ColumnTypeSelect ColumnType = "select"
ColumnTypeText ColumnType = "text"
)
// BaseColumn 描述了属性视图的基础结构。
type BaseColumn struct {
BaseID string `json:"id"` // 列 ID
BaseName string `json:"name"` // 列名
BaseType string `json:"type"` // 列类型
BaseID string `json:"id"` // 列 ID
BaseName string `json:"name"` // 列名
BaseType ColumnType `json:"type"` // 列类型
}
func (c *BaseColumn) ID() string {
@ -44,13 +56,6 @@ func (c *BaseColumn) Name() string {
return c.BaseName
}
func (c *BaseColumn) Type() string {
func (c *BaseColumn) Type() ColumnType {
return c.BaseType
}
// ColumnValueResolver 描述了属性视图的列值解析器。
type ColumnValueResolver interface {
// Resolve 用于解析列值。
Resolve() string
}

33
kernel/av/column_block.go Normal file
View file

@ -0,0 +1,33 @@
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package av
import "github.com/88250/lute/ast"
type ColumnBlock struct {
*BaseColumn
}
func NewColumnBlock() *ColumnBlock {
return &ColumnBlock{
BaseColumn: &BaseColumn{
BaseID: ast.NewNodeID(),
BaseName: "Block",
BaseType: ColumnTypeBlock,
},
}
}