2025-06-08 16:12:37 +08:00
//
// 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 sql
import (
"fmt"
"github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/util"
)
func RenderAttributeViewTable ( attrView * av . AttributeView , view * av . View , query string ) ( ret * av . Table ) {
ret = & av . Table {
2025-06-08 17:22:03 +08:00
BaseInstance : & av . BaseInstance {
ID : view . ID ,
Icon : view . Icon ,
Name : view . Name ,
Desc : view . Desc ,
HideAttrViewName : view . HideAttrViewName ,
2025-06-29 11:11:20 +08:00
Filters : view . Filters ,
Sorts : view . Sorts ,
2025-06-29 15:53:56 +08:00
Group : view . Group ,
2025-06-30 12:50:50 +08:00
ShowIcon : view . Table . ShowIcon ,
WrapField : view . Table . WrapField ,
2025-06-08 17:22:03 +08:00
} ,
Columns : [ ] * av . TableColumn { } ,
Rows : [ ] * av . TableRow { } ,
2025-06-08 16:12:37 +08:00
}
// 组装列
for _ , col := range view . Table . Columns {
key , getErr := attrView . GetKey ( col . ID )
if nil != getErr {
// 找不到字段则在视图中删除
removeMissingField ( attrView , view , col . ID )
continue
}
ret . Columns = append ( ret . Columns , & av . TableColumn {
2025-06-08 17:22:03 +08:00
BaseInstanceField : & av . BaseInstanceField {
2025-06-08 17:24:24 +08:00
ID : key . ID ,
Name : key . Name ,
Type : key . Type ,
Icon : key . Icon ,
2025-06-30 12:50:50 +08:00
Wrap : col . Wrap ,
2025-06-08 17:24:24 +08:00
Hidden : col . Hidden ,
Desc : key . Desc ,
Options : key . Options ,
NumberFormat : key . NumberFormat ,
Template : key . Template ,
Relation : key . Relation ,
Rollup : key . Rollup ,
Date : key . Date ,
2025-06-08 17:22:03 +08:00
} ,
2025-06-08 17:24:24 +08:00
Width : col . Width ,
Pin : col . Pin ,
Calc : col . Calc ,
2025-06-08 16:12:37 +08:00
} )
}
2025-07-01 22:42:48 +08:00
rowsValues := generateAttrViewItems ( attrView , view ) // 生成行
filterNotFoundAttrViewItems ( & rowsValues ) // 过滤掉不存在的行
2025-06-08 16:12:37 +08:00
// 生成行单元格
2025-06-08 22:41:03 +08:00
for rowID , rowValues := range rowsValues {
2025-06-08 16:12:37 +08:00
var tableRow av . TableRow
for _ , col := range ret . Columns {
var tableCell * av . TableCell
2025-06-08 22:41:03 +08:00
for _ , keyValues := range rowValues {
2025-06-08 16:12:37 +08:00
if keyValues . Key . ID == col . ID {
tableCell = & av . TableCell {
2025-06-08 17:22:03 +08:00
BaseValue : & av . BaseValue {
ID : keyValues . Values [ 0 ] . ID ,
Value : keyValues . Values [ 0 ] ,
ValueType : col . Type ,
} ,
2025-06-08 16:12:37 +08:00
}
break
}
}
if nil == tableCell {
tableCell = & av . TableCell {
2025-06-08 17:22:03 +08:00
BaseValue : & av . BaseValue {
ID : ast . NewNodeID ( ) ,
ValueType : col . Type ,
} ,
2025-06-08 16:12:37 +08:00
}
}
tableRow . ID = rowID
2025-06-11 11:34:43 +08:00
fillAttributeViewBaseValue ( tableCell . BaseValue , col . ID , rowID , col . NumberFormat , col . Template )
2025-06-08 16:12:37 +08:00
tableRow . Cells = append ( tableRow . Cells , tableCell )
}
ret . Rows = append ( ret . Rows , & tableRow )
}
// 批量获取块属性以提升性能
var ialIDs [ ] string
for _ , row := range ret . Rows {
block := row . GetBlockValue ( )
if nil != block && ! block . IsDetached {
ialIDs = append ( ialIDs , row . ID )
}
}
ials := BatchGetBlockAttrs ( ialIDs )
// 渲染自动生成的列值,比如关联列、汇总列、创建时间列和更新时间列
avCache := map [ string ] * av . AttributeView { }
avCache [ attrView . ID ] = attrView
for _ , row := range ret . Rows {
2025-06-11 17:39:32 +08:00
for _ , cell := range row . Cells {
fillAttributeViewAutoGeneratedValues ( attrView , ials , cell . Value , row , rowsValues , & avCache )
2025-06-08 16:12:37 +08:00
}
}
// 最后单独渲染模板列,这样模板列就可以使用汇总、关联、创建时间和更新时间列的值了
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
var renderTemplateErr error
for _ , row := range ret . Rows {
for _ , cell := range row . Cells {
2025-06-12 10:30:43 +08:00
err := fillAttributeViewTemplateValue ( cell . Value , row , attrView , ials , rowsValues )
if nil != err {
renderTemplateErr = err
2025-06-08 16:12:37 +08:00
}
}
}
if nil != renderTemplateErr {
util . PushErrMsg ( fmt . Sprintf ( util . Langs [ util . Lang ] [ 44 ] , util . EscapeHTML ( renderTemplateErr . Error ( ) ) ) , 30000 )
}
2025-06-12 17:14:54 +08:00
filterByQuery ( query , ret )
2025-07-02 17:02:40 +08:00
manualSort ( view , ret )
2025-06-08 16:12:37 +08:00
return
}