Zuoqiu-Yingyi / widget-query

一个将思源笔记数据库查询结果以表格样式渲染的挂件 | A widget that renders the query results of the Siyuan Notes database in tabular style.
GNU Affero General Public License v3.0
53 stars 7 forks source link

渲染问题探讨 #12

Closed banjuer closed 2 years ago

banjuer commented 2 years ago

感谢提供的非常棒的插件.

思源版本: V1.8.5

有个问题请教一下: 我想实现的效果, 统计所有page的待办事项, 汇总已完成, 待处理和总共几个指标, 同时渲染页面名称为链接, 下面是我的sql:

with checklist as(
    select hpath, count(*) todo, 0 done from blocks where type='i' and markdown like '%[ ]%' group by hpath
    union all
    select hpath,0 todo, count(*) done from blocks where type='i' and markdown like '%[x]%' group by hpath
)
,collect as (
    select hpath,sum(todo) todo,sum(done) done,sum(todo) + sum(done) total from checklist group by hpath
)
select collect.*,blocks.id __ref__id, '(('||blocks.id||' "'||blocks.content||'"))' __ref__name, '(('||blocks.id||' "'||blocks.content||'"))' name,blocks.content from collect left join blocks on collect.hpath=blocks.hpath and blocks.sort=0 order by todo desc;

我的疑问是:

不知道是哪里做的不对, 还请指教一下.

另外一个小建议: 建议sql有个默认limit, 有时候还没来得及写limit就已经开始渲染了, 导致整个页面非常卡.

Zuoqiu-Yingyi commented 2 years ago

@banjuer

  1. 自定义查询的渲染方案从 script/module/config.js#L55 处定义, 前缀为 __ref__ 的字段默认的渲染方案针对的是各内容块的块ID, 因此仅支持 __ref__id 这类渲染方案
  2. 如果想自定义渲染方案, 可以自定义上述渲染处理方法, 块引用的格式为 ((块id "锚文本")), 自定义方案可以参考 自定义配置
  3. 如果设置默认 limit, 那么应该如何取消默认限制呢❓
  4. 下面是一个实现所述功能的渲染方法(未测试), 可以参考一下
handler: (row, key) => { // 其他查询结果默认处理方法, row 是查询结果的一条记录, key 是字段名
    if (key === '__ref__name') {
        return `((${row['__ref__id']} "${row[key]}"))`;
    }
    switch (true) {
        case key.startsWith(config.query.prefix.ref):
            return `((${row[key]} "${row[key]}"))`;
        case key.startsWith(config.query.prefix.link):
            return `[${row[key]}](${row[key]})`;
        case key.startsWith(config.query.prefix.pre):
            return markdown2span(row[key]);
        case key.startsWith(config.query.prefix.date):
            return dateFormat(row[key]);
        case key.startsWith(config.query.prefix.time):
            return timeFormat(row[key]);
        case key.startsWith(config.query.prefix.datetime):
            return timestampFormat(row[key]);
        case key.startsWith(config.query.prefix.raw):
        default:
            return `\`${row[key]}\``;
    }
}
banjuer commented 2 years ago

@Zuoqiu-Yingyi 感谢支持, 已解决.

回复3: 3.如果设置默认 limit, 那么应该如何取消默认限制呢❓

没做过前端不是很懂, 我理解的可以简单检查下用户sql. 如果以limit number 结尾则不管, 否则加上默认limit限制条件, 如下伪码:

splits = sql.trim().split('limit')
if len(splits) <= 1 or splits[len(splits)-1].trim() is nan:
    sql = sql + " limit 10"
Zuoqiu-Yingyi commented 2 years ago

@banjuer 3 中我是指如果在挂件中为用户添加上了 LIMIT, 那么用户想要查询数据库中所有符合条件的记录时就不能取消限制了, 只能通过设置一个较大的限制覆盖默认的限制

banjuer commented 2 years ago

我觉得还是要限制, 不然太卡了。可惜思源不提供挂件设置选项

Zuoqiu-Yingyi commented 2 years ago

@banjuer 我添加一个默认关闭的查询限制功能吧

Zuoqiu-Yingyi commented 2 years ago

@banjuer 我添加一个默认关闭的查询限制功能吧

Release Query v0.1.6 release · Zuoqiu-Yingyi/widget-query

Zuoqiu-Yingyi commented 2 years ago

@all-contributors please add @banjuer for ideas

allcontributors[bot] commented 2 years ago

@Zuoqiu-Yingyi

I've put up a pull request to add @banjuer! :tada: