An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: The database template column supports `queryBlocks` function https://github.com/siyuan-note/siyuan/issues/10077
Daniel108bdec5f699
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.goindex 8de0e42d31..14be072c3b 100644--- a/kernel/model/attribute_view.go+++ b/kernel/model/attribute_view.go@@ -604,7 +604,9 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av}goTpl := template.New("").Delims(".action{", "}")- goTpl = goTpl.Funcs(util.BuiltInTemplateFuncs())+ tplFuncMap := util.BuiltInTemplateFuncs()+ SQLTemplateFuncs(&tplFuncMap)+ goTpl = goTpl.Funcs(tplFuncMap)tpl, tplErr := goTpl.Parse(tplContent)if nil != tplErr {logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)diff --git a/kernel/model/template.go b/kernel/model/template.goindex f68ec071b5..f6d8d65196 100644--- a/kernel/model/template.go+++ b/kernel/model/template.go@@ -20,12 +20,14 @@ import ("bytes""errors""fmt"+ "github.com/araddon/dateparse""io/fs""os""path/filepath""sort""strings""text/template"+ "time""github.com/88250/gulu""github.com/88250/lute/ast"@@ -42,7 +44,9 @@ import (func RenderGoTemplate(templateContent string) (ret string, err error) {tmpl := template.New("")- tmpl = tmpl.Funcs(util.BuiltInTemplateFuncs())+ tplFuncMap := util.BuiltInTemplateFuncs()+ SQLTemplateFuncs(&tplFuncMap)+ tmpl = tmpl.Funcs(tplFuncMap)tpl, err := tmpl.Parse(templateContent)if nil != err {return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))@@ -216,8 +220,10 @@ func renderTemplate(p, id string, preview bool) (string, error) {}goTpl := template.New("").Delims(".action{", "}")- funcMap := util.BuiltInTemplateFuncs()- tpl, err := goTpl.Funcs(funcMap).Parse(gulu.Str.FromBytes(md))+ tplFuncMap := util.BuiltInTemplateFuncs()+ SQLTemplateFuncs(&tplFuncMap)+ goTpl = goTpl.Funcs(tplFuncMap)+ tpl, err := goTpl.Funcs(tplFuncMap).Parse(gulu.Str.FromBytes(md))if nil != err {return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))}@@ -388,3 +394,29 @@ func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) {block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)})}}++func SQLTemplateFuncs(templateFuncMap *template.FuncMap) {+ (*templateFuncMap)["queryBlocks"] = func(stmt string, args ...string) (retBlocks []*sql.Block) {+ for _, arg := range args {+ stmt = strings.Replace(stmt, "?", arg, 1)+ }+ retBlocks = sql.SelectBlocksRawStmt(stmt, 1, 512)+ return+ }+ (*templateFuncMap)["querySpans"] = func(stmt string, args ...string) (retSpans []*sql.Span) {+ for _, arg := range args {+ stmt = strings.Replace(stmt, "?", arg, 1)+ }+ retSpans = sql.SelectSpansRawStmt(stmt, 512)+ return+ }+ (*templateFuncMap)["parseTime"] = func(dateStr string) time.Time {+ now := time.Now()+ retTime, err := dateparse.ParseIn(dateStr, now.Location())+ if nil != err {+ logging.LogWarnf("parse date [%s] failed [%s], return current time instead", dateStr, err)+ return now+ }+ return retTime+ }+}diff --git a/kernel/treenode/node.go b/kernel/treenode/node.goindex c752dbb127..e5f836cb49 100644--- a/kernel/treenode/node.go+++ b/kernel/treenode/node.go@@ -963,8 +963,10 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av}goTpl := template.New("").Delims(".action{", "}")- funcMap := util.BuiltInTemplateFuncs()- tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)+ tplFuncMap := util.BuiltInTemplateFuncs()+ // 这里存在依赖问题所以不支持 SQLTemplateFuncs(&tplFuncMap)+ goTpl = goTpl.Funcs(tplFuncMap)+ tpl, tplErr := goTpl.Funcs(tplFuncMap).Parse(tplContent)if nil != tplErr {logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)return ""diff --git a/kernel/util/template.go b/kernel/util/template.goindex b5fc612b2b..a388c43f93 100644--- a/kernel/util/template.go+++ b/kernel/util/template.go@@ -17,16 +17,10 @@package utilimport (- "math"- "strings"- "text/template"- "time"-"github.com/Masterminds/sprig/v3"- "github.com/araddon/dateparse"- "github.com/siyuan-note/logging"- "github.com/siyuan-note/siyuan/kernel/sql""github.com/spf13/cast"+ "math"+ "text/template")func BuiltInTemplateFuncs() (ret template.FuncMap) {@@ -39,30 +33,6 @@ func BuiltInTemplateFuncs() (ret template.FuncMap) {ret["powf"] = powfret["log"] = logret["logf"] = logf-- ret["queryBlocks"] = func(stmt string, args ...string) (retBlocks []*sql.Block) {- for _, arg := range args {- stmt = strings.Replace(stmt, "?", arg, 1)- }- retBlocks = sql.SelectBlocksRawStmt(stmt, 1, 512)- return- }- ret["querySpans"] = func(stmt string, args ...string) (retSpans []*sql.Span) {- for _, arg := range args {- stmt = strings.Replace(stmt, "?", arg, 1)- }- retSpans = sql.SelectSpansRawStmt(stmt, 512)- return- }- ret["parseTime"] = func(dateStr string) time.Time {- now := time.Now()- retTime, err := dateparse.ParseIn(dateStr, now.Location())- if nil != err {- logging.LogWarnf("parse date [%s] failed [%s], return current time instead", dateStr, err)- return now- }- return retTime- }return}