2023-03-02 09:12:28 +08:00
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
2023-03-02 09:27:07 +08:00
|
|
|
|
// Package av 包含了属性视图(Attribute View)相关的实现。
|
2023-03-02 09:12:28 +08:00
|
|
|
|
package av
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"database/sql"
|
2023-03-02 18:49:05 +08:00
|
|
|
|
"os"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
2023-03-03 10:49:45 +08:00
|
|
|
|
"github.com/88250/lute/ast"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
"github.com/siyuan-note/filelock"
|
|
|
|
|
|
"github.com/siyuan-note/logging"
|
|
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// AttributeView 描述了属性视图的结构。
|
|
|
|
|
|
type AttributeView struct {
|
2023-03-03 10:49:45 +08:00
|
|
|
|
Spec int `json:"spec"`
|
|
|
|
|
|
ID string `json:"id"` // 属性视图 ID
|
|
|
|
|
|
Columns []*Column `json:"columns"` // 表格列名
|
|
|
|
|
|
Rows []*Row `json:"rows"` // 表格行记录
|
2023-03-02 09:12:28 +08:00
|
|
|
|
|
2023-03-02 09:16:30 +08:00
|
|
|
|
Type AttributeViewType `json:"type"` // 属性视图类型
|
2023-03-02 09:12:28 +08:00
|
|
|
|
Projections []string `json:"projections"` // 显示的列名,SELECT *
|
|
|
|
|
|
Filters []*AttributeViewFilter `json:"filters"` // 过滤规则,WHERE ...
|
|
|
|
|
|
Sorts []*AttributeViewSort `json:"sorts"` // 排序规则,ORDER BY ...
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 09:16:30 +08:00
|
|
|
|
// AttributeViewType 描述了属性视图的类型。
|
|
|
|
|
|
type AttributeViewType string
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
AttributeViewTypeTable AttributeViewType = "table" // 属性视图类型 - 表格
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-03-02 18:57:20 +08:00
|
|
|
|
func NewAttributeView(id string) *AttributeView {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
return &AttributeView{
|
2023-03-03 10:28:33 +08:00
|
|
|
|
Spec: 0,
|
2023-03-02 18:57:20 +08:00
|
|
|
|
ID: id,
|
2023-03-15 20:15:56 +08:00
|
|
|
|
Columns: []*Column{{ID: ast.NewNodeID(), Name: "Block", Type: ColumnTypeBlock}},
|
2023-03-03 10:26:02 +08:00
|
|
|
|
Rows: []*Row{},
|
2023-03-02 11:32:39 +08:00
|
|
|
|
Type: AttributeViewTypeTable,
|
|
|
|
|
|
Projections: []string{},
|
|
|
|
|
|
Filters: []*AttributeViewFilter{},
|
|
|
|
|
|
Sorts: []*AttributeViewSort{},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 09:12:28 +08:00
|
|
|
|
func (av *AttributeView) GetColumnNames() (ret []string) {
|
|
|
|
|
|
ret = []string{}
|
|
|
|
|
|
for _, column := range av.Columns {
|
2023-03-03 10:49:45 +08:00
|
|
|
|
ret = append(ret, column.Name)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-03 10:49:45 +08:00
|
|
|
|
func (av *AttributeView) InsertColumn(index int, column *Column) {
|
2023-03-02 15:03:33 +08:00
|
|
|
|
if 0 > index || len(av.Columns) == index {
|
|
|
|
|
|
av.Columns = append(av.Columns, column)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
av.Columns = append(av.Columns[:index+1], av.Columns[index:]...)
|
|
|
|
|
|
av.Columns[index] = column
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 09:12:28 +08:00
|
|
|
|
type AttributeViewFilter struct {
|
2023-03-02 09:33:21 +08:00
|
|
|
|
Column string `json:"column"`
|
|
|
|
|
|
Operator FilterOperator `json:"operator"`
|
|
|
|
|
|
Value string `json:"value"`
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 09:33:21 +08:00
|
|
|
|
type FilterOperator string
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
FilterOperatorEq FilterOperator = "="
|
|
|
|
|
|
FilterOperatorNe FilterOperator = "!="
|
|
|
|
|
|
FilterOperatorGt FilterOperator = ">"
|
|
|
|
|
|
FilterOperatorGe FilterOperator = ">="
|
|
|
|
|
|
FilterOperatorLt FilterOperator = "<"
|
|
|
|
|
|
FilterOperatorLe FilterOperator = "<="
|
|
|
|
|
|
FilterOperatorIn FilterOperator = "IN"
|
|
|
|
|
|
FilterOperatorNotIn FilterOperator = "NOT IN"
|
|
|
|
|
|
FilterOperatorLike FilterOperator = "LIKE"
|
|
|
|
|
|
FilterOperatorNotLike FilterOperator = "NOT LIKE"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-03-02 09:12:28 +08:00
|
|
|
|
type AttributeViewSort struct {
|
2023-03-02 09:33:21 +08:00
|
|
|
|
Column string `json:"column"`
|
|
|
|
|
|
Order SortOrder `json:"order"`
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 09:33:21 +08:00
|
|
|
|
type SortOrder string
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
SortOrderAsc SortOrder = "ASC"
|
|
|
|
|
|
SortOrderDesc SortOrder = "DESC"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
|
2023-03-03 10:49:45 +08:00
|
|
|
|
avJSONPath := getAttributeViewDataPath(avID)
|
2023-03-02 11:32:39 +08:00
|
|
|
|
if !gulu.File.IsExist(avJSONPath) {
|
2023-03-02 18:57:20 +08:00
|
|
|
|
ret = NewAttributeView(avID)
|
2023-03-02 11:32:39 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 09:12:28 +08:00
|
|
|
|
data, err := filelock.ReadFile(avJSONPath)
|
|
|
|
|
|
if nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("read attribute view [%s] failed: %s", avID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
ret = &AttributeView{}
|
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
|
|
|
|
|
|
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func SaveAttributeView(av *AttributeView) (err error) {
|
2023-03-02 18:49:05 +08:00
|
|
|
|
data, err := gulu.JSON.MarshalIndentJSON(av, "", "\t")
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("marshal attribute view [%s] failed: %s", av.ID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-03 10:49:45 +08:00
|
|
|
|
avJSONPath := getAttributeViewDataPath(av.ID)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if err = filelock.WriteFile(avJSONPath, data); nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("save attribute view [%s] failed: %s", av.ID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-03 10:49:45 +08:00
|
|
|
|
func getAttributeViewDataPath(avID string) (ret string) {
|
2023-03-02 18:49:05 +08:00
|
|
|
|
av := filepath.Join(util.DataDir, "storage", "av")
|
|
|
|
|
|
ret = filepath.Join(av, avID+".json")
|
|
|
|
|
|
if !gulu.File.IsDir(av) {
|
|
|
|
|
|
if err := os.MkdirAll(av, 0755); nil != err {
|
|
|
|
|
|
logging.LogErrorf("create attribute view dir failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func dropAttributeViewTableColumn(db *sql.DB, avID string, column string) (err error) {
|
|
|
|
|
|
_, err = db.Exec("ALTER TABLE `av_" + avID + "` DROP COLUMN `" + column + "`")
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
|
logging.LogErrorf("drop column [%s] failed: %s", column, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func addAttributeViewTableColumn(db *sql.DB, avID string, column string) (err error) {
|
|
|
|
|
|
_, err = db.Exec("ALTER TABLE `av_" + avID + "` ADD COLUMN `" + column + "`")
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
|
logging.LogErrorf("add column [%s] failed: %s", column, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func dropAttributeViewTable(db *sql.DB, avID string) (err error) {
|
|
|
|
|
|
_, err = db.Exec("DROP TABLE IF EXISTS `av_" + avID + "`")
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("drop table [%s] failed: %s", avID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
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, ", ") + ")")
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("create table [%s] failed: %s", avID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|