Browse Source

:art: 嵌入块查询结果中显示块引用计数 https://github.com/siyuan-note/siyuan/issues/7191

Liang Ding 2 years ago
parent
commit
35baf86b6e
2 changed files with 35 additions and 0 deletions
  1. 13 0
      kernel/model/block.go
  2. 22 0
      kernel/sql/block_ref_query.go

+ 13 - 0
kernel/model/block.go

@@ -19,6 +19,7 @@ package model
 import (
 	"errors"
 	"fmt"
+	"strconv"
 	"time"
 
 	"github.com/88250/lute"
@@ -466,6 +467,18 @@ func getEmbeddedBlock(embedBlockID string, trees map[string]*parse.Tree, sqlBloc
 		return
 	}
 
+	// 嵌入块查询结果中显示块引用计数 https://github.com/siyuan-note/siyuan/issues/7191
+	var defIDs []string
+	for _, n := range nodes {
+		defIDs = append(defIDs, n.ID)
+	}
+	refCount := sql.QueryRefCount(defIDs)
+	for _, n := range nodes {
+		if cnt := refCount[n.ID]; 0 < cnt {
+			n.SetIALAttr("refcount", strconv.Itoa(cnt))
+		}
+	}
+
 	luteEngine := NewLute()
 	luteEngine.RenderOptions.ProtyleContenteditable = false // 不可编辑
 	dom := renderBlockDOMByNodes(nodes, luteEngine)

+ 22 - 0
kernel/sql/block_ref_query.go

@@ -79,6 +79,28 @@ func queryRefTexts() (ret []string) {
 	return
 }
 
+func QueryRefCount(defIDs []string) (ret map[string]int) {
+	ret = map[string]int{}
+	ids := strings.Join(defIDs, "','")
+	ids = "('" + ids + "')"
+	rows, err := query("SELECT def_block_id, COUNT(*) AS ref_cnt FROM refs WHERE def_block_id IN " + ids + " GROUP BY def_block_id")
+	if nil != err {
+		logging.LogErrorf("sql query failed: %s", err)
+		return
+	}
+	defer rows.Close()
+	for rows.Next() {
+		var id string
+		var cnt int
+		if err = rows.Scan(&id, &cnt); nil != err {
+			logging.LogErrorf("query scan field failed: %s", err)
+			return
+		}
+		ret[id] = cnt
+	}
+	return
+}
+
 func QueryRootChildrenRefCount(defRootID string) (ret map[string]int) {
 	ret = map[string]int{}
 	rows, err := query("SELECT def_block_id, COUNT(*) AS ref_cnt FROM refs WHERE def_block_root_id = ? GROUP BY def_block_id", defRootID)