mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
♻️ Refactor Go to err != nil, err == nil (#12385)
This commit is contained in:
parent
473a159ef2
commit
b100721fee
147 changed files with 1661 additions and 1659 deletions
|
|
@ -36,7 +36,7 @@ import (
|
|||
func QueryEmptyContentEmbedBlocks() (ret []*Block) {
|
||||
stmt := "SELECT * FROM blocks WHERE type = 'query_embed' AND content = ''"
|
||||
rows, err := query(stmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", stmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ func QueryEmptyContentEmbedBlocks() (ret []*Block) {
|
|||
func queryBlockHashes(rootID string) (ret map[string]string) {
|
||||
stmt := "SELECT id, hash FROM blocks WHERE root_id = ?"
|
||||
rows, err := query(stmt, rootID)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", stmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -60,7 +60,7 @@ func queryBlockHashes(rootID string) (ret map[string]string) {
|
|||
ret = map[string]string{}
|
||||
for rows.Next() {
|
||||
var id, hash string
|
||||
if err = rows.Scan(&id, &hash); nil != err {
|
||||
if err = rows.Scan(&id, &hash); err != nil {
|
||||
logging.LogErrorf("query scan field failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
|
@ -72,7 +72,7 @@ func queryBlockHashes(rootID string) (ret map[string]string) {
|
|||
func QueryRootBlockByCondition(condition string) (ret []*Block) {
|
||||
sqlStmt := "SELECT *, length(hpath) - length(replace(hpath, '/', '')) AS lv FROM blocks WHERE type = 'd' AND " + condition + " ORDER BY box DESC,lv ASC LIMIT 128"
|
||||
rows, err := query(sqlStmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -80,7 +80,7 @@ func QueryRootBlockByCondition(condition string) (ret []*Block) {
|
|||
for rows.Next() {
|
||||
var block Block
|
||||
var sepCount int
|
||||
if err = rows.Scan(&block.ID, &block.ParentID, &block.RootID, &block.Hash, &block.Box, &block.Path, &block.HPath, &block.Name, &block.Alias, &block.Memo, &block.Tag, &block.Content, &block.FContent, &block.Markdown, &block.Length, &block.Type, &block.SubType, &block.IAL, &block.Sort, &block.Created, &block.Updated, &sepCount); nil != err {
|
||||
if err = rows.Scan(&block.ID, &block.ParentID, &block.RootID, &block.Hash, &block.Box, &block.Path, &block.HPath, &block.Name, &block.Alias, &block.Memo, &block.Tag, &block.Content, &block.FContent, &block.Markdown, &block.Length, &block.Type, &block.SubType, &block.IAL, &block.Sort, &block.Created, &block.Updated, &sepCount); err != nil {
|
||||
logging.LogErrorf("query scan field failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
|
@ -109,7 +109,7 @@ func queryBlockChildrenIDs(id string) (ret []string) {
|
|||
func queryBlockIDByParentID(parentID string) (ret []string) {
|
||||
sqlStmt := "SELECT id FROM blocks WHERE parent_id = ?"
|
||||
rows, err := query(sqlStmt, parentID)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ func QueryRecentUpdatedBlocks() (ret []*Block) {
|
|||
sqlStmt = "SELECT * FROM blocks WHERE type = 'd' ORDER BY updated DESC LIMIT 16"
|
||||
}
|
||||
rows, err := query(sqlStmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -151,7 +151,7 @@ func QueryBlockByNameOrAlias(rootID, text string) (ret *Block) {
|
|||
func QueryBlockAliases(rootID string) (ret []string) {
|
||||
sqlStmt := "SELECT alias FROM blocks WHERE root_id = ? AND alias != ''"
|
||||
rows, err := query(sqlStmt, rootID)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -191,7 +191,7 @@ func queryNames(searchIgnoreLines []string) (ret []string) {
|
|||
sqlStmt += buf.String()
|
||||
sqlStmt += " LIMIT ?"
|
||||
rows, err := query(sqlStmt, 10240)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -231,7 +231,7 @@ func queryAliases(searchIgnoreLines []string) (ret []string) {
|
|||
sqlStmt += buf.String()
|
||||
sqlStmt += " LIMIT ?"
|
||||
rows, err := query(sqlStmt, 10240)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -269,7 +269,7 @@ func queryDocIDsByTitle(title string, excludeIDs []string) (ret []string) {
|
|||
sqlStmt = "SELECT id FROM blocks WHERE type = 'd' AND content = ? AND id NOT IN " + notIn + " LIMIT ?"
|
||||
}
|
||||
rows, err := query(sqlStmt, title, 32)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -298,7 +298,7 @@ func queryDocTitles(searchIgnoreLines []string) (ret []string) {
|
|||
}
|
||||
sqlStmt += buf.String()
|
||||
rows, err := query(sqlStmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -330,7 +330,7 @@ func queryDocTitles(searchIgnoreLines []string) (ret []string) {
|
|||
func QueryBlockNamesByRootID(rootID string) (ret []string) {
|
||||
sqlStmt := "SELECT DISTINCT name FROM blocks WHERE root_id = ? AND name != ''"
|
||||
rows, err := query(sqlStmt, rootID)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -346,7 +346,7 @@ func QueryBlockNamesByRootID(rootID string) (ret []string) {
|
|||
func QueryBookmarkBlocksByKeyword(bookmark string) (ret []*Block) {
|
||||
sqlStmt := "SELECT * FROM blocks WHERE ial LIKE ?"
|
||||
rows, err := query(sqlStmt, "%bookmark=%")
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -362,7 +362,7 @@ func QueryBookmarkBlocksByKeyword(bookmark string) (ret []*Block) {
|
|||
func QueryBookmarkBlocks() (ret []*Block) {
|
||||
sqlStmt := "SELECT * FROM blocks WHERE ial LIKE ?"
|
||||
rows, err := query(sqlStmt, "%bookmark=%")
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -379,7 +379,7 @@ func QueryBookmarkLabels() (ret []string) {
|
|||
ret = []string{}
|
||||
sqlStmt := "SELECT * FROM blocks WHERE ial LIKE ?"
|
||||
rows, err := query(sqlStmt, "%bookmark=%")
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -410,7 +410,7 @@ func Query(stmt string, limit int) (ret []map[string]interface{}, err error) {
|
|||
// 考虑到 UNION 的使用场景不多,这里还是以支持 || 操作符为主
|
||||
p := sqlparser2.NewParser(strings.NewReader(stmt))
|
||||
parsedStmt2, err := p.ParseStatement()
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
if !strings.Contains(stmt, "||") {
|
||||
// 这个解析器无法处理 || 连接字符串操作符
|
||||
parsedStmt, err2 := sqlparser.Parse(stmt)
|
||||
|
|
@ -451,7 +451,7 @@ func Query(stmt string, limit int) (ret []map[string]interface{}, err error) {
|
|||
|
||||
ret = []map[string]interface{}{}
|
||||
rows, err := query(stmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogWarnf("sql query [%s] failed: %s", stmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -469,7 +469,7 @@ func Query(stmt string, limit int) (ret []map[string]interface{}, err error) {
|
|||
columnPointers[i] = &columns[i]
|
||||
}
|
||||
|
||||
if err = rows.Scan(columnPointers...); nil != err {
|
||||
if err = rows.Scan(columnPointers...); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -510,7 +510,7 @@ func getLimitClause(parsedStmt sqlparser.Statement, limit int) (ret *sqlparser.L
|
|||
|
||||
func queryRawStmt(stmt string, limit int) (ret []map[string]interface{}, err error) {
|
||||
rows, err := query(stmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "syntax error") {
|
||||
return
|
||||
}
|
||||
|
|
@ -519,7 +519,7 @@ func queryRawStmt(stmt string, limit int) (ret []map[string]interface{}, err err
|
|||
defer rows.Close()
|
||||
|
||||
cols, err := rows.Columns()
|
||||
if nil != err || nil == cols {
|
||||
if err != nil || nil == cols {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -532,7 +532,7 @@ func queryRawStmt(stmt string, limit int) (ret []map[string]interface{}, err err
|
|||
columnPointers[i] = &columns[i]
|
||||
}
|
||||
|
||||
if err = rows.Scan(columnPointers...); nil != err {
|
||||
if err = rows.Scan(columnPointers...); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -557,7 +557,7 @@ func SelectBlocksRawStmtNoParse(stmt string, limit int) (ret []*Block) {
|
|||
|
||||
func SelectBlocksRawStmt(stmt string, page, limit int) (ret []*Block) {
|
||||
parsedStmt, err := sqlparser.Parse(stmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
return selectBlocksRawStmt(stmt, limit)
|
||||
}
|
||||
|
||||
|
|
@ -603,7 +603,7 @@ func SelectBlocksRawStmt(stmt string, page, limit int) (ret []*Block) {
|
|||
stmt = strings.ReplaceAll(stmt, "\\\\*", "\\*")
|
||||
stmt = strings.ReplaceAll(stmt, "from dual", "")
|
||||
rows, err := query(stmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "syntax error") {
|
||||
return
|
||||
}
|
||||
|
|
@ -621,7 +621,7 @@ func SelectBlocksRawStmt(stmt string, page, limit int) (ret []*Block) {
|
|||
|
||||
func selectBlocksRawStmt(stmt string, limit int) (ret []*Block) {
|
||||
rows, err := query(stmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "syntax error") {
|
||||
return
|
||||
}
|
||||
|
|
@ -649,7 +649,7 @@ func selectBlocksRawStmt(stmt string, limit int) (ret []*Block) {
|
|||
|
||||
func scanBlockRows(rows *sql.Rows) (ret *Block) {
|
||||
var block Block
|
||||
if err := rows.Scan(&block.ID, &block.ParentID, &block.RootID, &block.Hash, &block.Box, &block.Path, &block.HPath, &block.Name, &block.Alias, &block.Memo, &block.Tag, &block.Content, &block.FContent, &block.Markdown, &block.Length, &block.Type, &block.SubType, &block.IAL, &block.Sort, &block.Created, &block.Updated); nil != err {
|
||||
if err := rows.Scan(&block.ID, &block.ParentID, &block.RootID, &block.Hash, &block.Box, &block.Path, &block.HPath, &block.Name, &block.Alias, &block.Memo, &block.Tag, &block.Content, &block.FContent, &block.Markdown, &block.Length, &block.Type, &block.SubType, &block.IAL, &block.Sort, &block.Created, &block.Updated); err != nil {
|
||||
logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())
|
||||
return
|
||||
}
|
||||
|
|
@ -660,7 +660,7 @@ func scanBlockRows(rows *sql.Rows) (ret *Block) {
|
|||
|
||||
func scanBlockRow(row *sql.Row) (ret *Block) {
|
||||
var block Block
|
||||
if err := row.Scan(&block.ID, &block.ParentID, &block.RootID, &block.Hash, &block.Box, &block.Path, &block.HPath, &block.Name, &block.Alias, &block.Memo, &block.Tag, &block.Content, &block.FContent, &block.Markdown, &block.Length, &block.Type, &block.SubType, &block.IAL, &block.Sort, &block.Created, &block.Updated); nil != err {
|
||||
if err := row.Scan(&block.ID, &block.ParentID, &block.RootID, &block.Hash, &block.Box, &block.Path, &block.HPath, &block.Name, &block.Alias, &block.Memo, &block.Tag, &block.Content, &block.FContent, &block.Markdown, &block.Length, &block.Type, &block.SubType, &block.IAL, &block.Sort, &block.Created, &block.Updated); err != nil {
|
||||
if sql.ErrNoRows != err {
|
||||
logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())
|
||||
}
|
||||
|
|
@ -684,7 +684,7 @@ func GetChildBlocks(parentID, condition string) (ret []*Block) {
|
|||
sqlStmt += " AND " + condition
|
||||
}
|
||||
rows, err := query(sqlStmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -704,7 +704,7 @@ func GetAllChildBlocks(rootIDs []string, condition string) (ret []*Block) {
|
|||
sqlStmt += " AND " + condition
|
||||
}
|
||||
rows, err := query(sqlStmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -732,7 +732,7 @@ func GetBlock(id string) (ret *Block) {
|
|||
|
||||
func GetRootUpdated() (ret map[string]string, err error) {
|
||||
rows, err := query("SELECT root_id, updated FROM `blocks` WHERE type = 'd'")
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
|
@ -749,7 +749,7 @@ func GetRootUpdated() (ret map[string]string, err error) {
|
|||
|
||||
func GetDuplicatedRootIDs(blocksTable string) (ret []string) {
|
||||
rows, err := query("SELECT DISTINCT root_id FROM `" + blocksTable + "` GROUP BY id HAVING COUNT(*) > 1")
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
|
@ -765,7 +765,7 @@ func GetDuplicatedRootIDs(blocksTable string) (ret []string) {
|
|||
func GetAllRootBlocks() (ret []*Block) {
|
||||
stmt := "SELECT * FROM blocks WHERE type = 'd'"
|
||||
rows, err := query(stmt)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", stmt, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -811,7 +811,7 @@ func GetBlocks(ids []string) (ret []*Block) {
|
|||
stmtBuilder.WriteString(")")
|
||||
sqlStmt := stmtBuilder.String()
|
||||
rows, err := query(sqlStmt, args...)
|
||||
if nil != err {
|
||||
if err != nil {
|
||||
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue