Skip to content

Commit ccf5eaf

Browse files
author
PaienNate
committed
fix(db): 采用传参方式避免读写DB混合,并修改一些文字描述
1 parent 31f5ef5 commit ccf5eaf

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

internal/db/searchnode.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"gorm.io/gorm"
1313
)
1414

15-
func whereInParent(parent string) *gorm.DB {
15+
func whereInParent(db *gorm.DB, parent string) *gorm.DB {
1616
if parent == "/" {
17-
return rwDb.R().Where("1 = 1")
17+
return db.Where("1 = 1")
1818
}
19-
return rwDb.R().Where(fmt.Sprintf("%s LIKE ?", columnName("parent")),
19+
return db.Where(fmt.Sprintf("%s LIKE ?", columnName("parent")),
2020
fmt.Sprintf("%s/%%", parent)).
2121
Or(fmt.Sprintf("%s = ?", columnName("parent")), parent)
2222
}
@@ -31,7 +31,7 @@ func BatchCreateSearchNodes(nodes *[]model.SearchNode) error {
3131

3232
func DeleteSearchNodesByParent(path string) error {
3333
path = utils.FixAndCleanPath(path)
34-
err := rwDb.W().Where(whereInParent(path)).Delete(&model.SearchNode{}).Error
34+
err := rwDb.W().Where(whereInParent(rwDb.W(), path)).Delete(&model.SearchNode{}).Error
3535
if err != nil {
3636
return err
3737
}
@@ -61,14 +61,14 @@ func SearchNode(req model.SearchReq, useFullText bool) ([]model.SearchNode, int6
6161
for _, keyword := range strings.Fields(req.Keywords) {
6262
keywordsClause = keywordsClause.Where("name LIKE ?", fmt.Sprintf("%%%s%%", keyword))
6363
}
64-
searchDB = rwDb.R().Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).Where(keywordsClause)
64+
searchDB = rwDb.R().Model(&model.SearchNode{}).Where(whereInParent(rwDb.R(), req.Parent)).Where(keywordsClause)
6565
} else {
6666
switch conf.Conf.Database.Type {
6767
case "mysql":
68-
searchDB = rwDb.R().Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).
68+
searchDB = rwDb.R().Model(&model.SearchNode{}).Where(whereInParent(rwDb.R(), req.Parent)).
6969
Where("MATCH (name) AGAINST (? IN BOOLEAN MODE)", "'*"+req.Keywords+"*'")
7070
case "postgres":
71-
searchDB = rwDb.R().Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).
71+
searchDB = rwDb.R().Model(&model.SearchNode{}).Where(whereInParent(rwDb.R(), req.Parent)).
7272
Where("to_tsvector(name) @@ to_tsquery(?)", strings.Join(strings.Fields(req.Keywords), " & "))
7373
}
7474
}

internal/model/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
// A Connection struct abstracts the access to the underlying database connections
99
// It is used for reading from a database connection and writing to a database connection.
10-
// The writing is assumed is always in the context of a database transaction.
10+
// The writing is always assumed to be in the context of a database transaction.
1111
type Connection struct {
1212
// Read is a read connection used for fast access to the underlying database transaction
1313
Read *gorm.DB

0 commit comments

Comments
 (0)