Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:white_check_mark: Cover raw query limits across database routes https://github.com/siyuan-note/siyuan/pull/21035

Danielc5d8bbe5ca11
diff --git a/kernel/sql/query_limit_test.go b/kernel/sql/query_limit_test.go
index 4d3adb42c5..ce073362ff 100644
--- a/kernel/sql/query_limit_test.go
+++ b/kernel/sql/query_limit_test.go
@@ -5,7 +5,9 @@
package sql
import (
+ "context"
gosql "database/sql"
+ "fmt"
"math"
"testing"
)
@@ -71,18 +73,26 @@ func TestRawStatementQueriesHonourRowLimit(t *testing.T) {
}
testDB.SetMaxOpenConns(1)
t.Cleanup(func() { _ = testDB.Close() })
- previousDB := db
- db = testDB
- t.Cleanup(func() { db = previousDB })
+ previousDB, previousAssetDB := db, assetContentDB
+ db, assetContentDB = testDB, testDB
+ t.Cleanup(func() { db, assetContentDB = previousDB, previousAssetDB })
if _, err = testDB.Exec("CREATE TABLE items (n INTEGER, content TEXT); INSERT INTO items VALUES (1, 'a'), (2, 'a'), (3, 'a'), (4, 'a'), (5, 'a')"); err != nil {
t.Fatal(err)
}
for _, limit := range []int{1, 3, 5} {
- rows, queryErr := queryRawStmtArgs("SELECT n FROM items ORDER BY n", nil, limit)
+ rows, queryErr := queryRawStmtArgs("SELECT n FROM items WHERE n >= ? ORDER BY n", []any{1}, limit)
if queryErr != nil || len(rows) != limit {
t.Fatalf("queryRawStmtArgs limit=%d: got %d rows, err=%v", limit, len(rows), queryErr)
}
+ assetRows, assetErr := queryAssetContentRawStmt("SELECT n FROM items WHERE n >= ? ORDER BY n", []any{1}, limit)
+ if assetErr != nil || len(assetRows) != limit {
+ t.Fatalf("queryAssetContentRawStmt limit=%d: got %d rows, err=%v", limit, len(assetRows), assetErr)
+ }
+ assets := selectAssetContentsRawStmt("SELECT n, content, '', content, 0, 0, content FROM items WHERE n >= ? ORDER BY n", []any{1}, limit)
+ if len(assets) != limit {
+ t.Fatalf("selectAssetContentsRawStmt limit=%d: got %d rows", limit, len(assets))
+ }
}
// 语句自带 LIMIT 时由语句决定行数,本层不再截断。
@@ -92,6 +102,61 @@ func TestRawStatementQueriesHonourRowLimit(t *testing.T) {
}
}
+func TestBlockRawStatementRowLimitsInBothDatabases(t *testing.T) {
+ testDB, boxID := useEncryptedQueryTestDB(t)
+ previousDB := db
+ db = testDB
+ t.Cleanup(func() { db = previousDB })
+ for i := 1; i <= 5; i++ {
+ insertEncryptedQueryTestBlock(t, testDB, fmt.Sprintf("row-%d", i), "", "root", "p")
+ }
+ const statement = "SELECT * FROM blocks ORDER BY id"
+ for _, scenario := range []struct {
+ name string
+ run func(string, int) (int, error)
+ }{
+ {"global block args", func(stmt string, limit int) (int, error) {
+ rows := SelectBlocksRawStmtArgs(stmt, nil, limit)
+ return len(rows), nil
+ }},
+ {"global block no parse", func(stmt string, limit int) (int, error) {
+ return len(SelectBlocksRawStmtNoParse(stmt, limit)), nil
+ }},
+ {"encrypted block args", func(stmt string, limit int) (int, error) {
+ rows, err := SelectBlocksRawStmtArgsInBoxContext(context.Background(), stmt, nil, limit, boxID)
+ return len(rows), err
+ }},
+ {"encrypted block no parse", func(stmt string, limit int) (int, error) {
+ return len(SelectBlocksRawStmtNoParseInBox(stmt, limit, boxID)), nil
+ }},
+ {"encrypted map", func(stmt string, limit int) (int, error) {
+ rows, err := queryRawStmtForBox(boxID, stmt, limit)
+ return len(rows), err
+ }},
+ {"encrypted map args", func(stmt string, limit int) (int, error) {
+ rows, err := queryRawStmtArgsForBox(boxID, stmt, nil, limit)
+ return len(rows), err
+ }},
+ {"encrypted map args context", func(stmt string, limit int) (int, error) {
+ rows, err := queryRawStmtArgsForBoxContext(context.Background(), boxID, stmt, nil, limit)
+ return len(rows), err
+ }},
+ } {
+ t.Run(scenario.name, func(t *testing.T) {
+ for _, limit := range []int{1, 3, 5, 6} {
+ count, err := scenario.run(statement, limit)
+ if err != nil || count != min(limit, 5) {
+ t.Fatalf("limit %d: count=%d, err=%v", limit, count, err)
+ }
+ }
+ count, err := scenario.run(statement+" LIMIT 4", 1)
+ if err != nil || count != 4 {
+ t.Fatalf("explicit SQL limit must take precedence: count=%d, err=%v", count, err)
+ }
+ })
+ }
+}
+
func TestQueryWithLimitInfo(t *testing.T) {
testDB, err := gosql.Open("sqlite3_extended", ":memory:")
if err != nil {