♻️ Refactor Go to err != nil, err == nil (#12385)

This commit is contained in:
Oleksandr Redko 2024-09-04 04:40:50 +03:00 committed by GitHub
parent 473a159ef2
commit b100721fee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
147 changed files with 1661 additions and 1659 deletions

View file

@ -73,7 +73,7 @@ func docTitleImgAsset(root *ast.Node, boxLocalPath, docDirLocalPath string) *Ass
var err error
if lp := assetLocalPath(p, boxLocalPath, docDirLocalPath); "" != lp {
hash, err = util.GetEtag(lp)
if nil != err {
if err != nil {
logging.LogErrorf("calc asset [%s] hash failed: %s", lp, err)
return nil
}
@ -106,7 +106,7 @@ func QueryAssetByHash(hash string) (ret *Asset) {
sqlStmt := "SELECT * FROM assets WHERE hash = ?"
row := queryRow(sqlStmt, hash)
var asset Asset
if err := row.Scan(&asset.ID, &asset.BlockID, &asset.RootID, &asset.Box, &asset.DocPath, &asset.Path, &asset.Name, &asset.Title, &asset.Hash); nil != err {
if err := row.Scan(&asset.ID, &asset.BlockID, &asset.RootID, &asset.Box, &asset.DocPath, &asset.Path, &asset.Name, &asset.Title, &asset.Hash); err != nil {
if sql.ErrNoRows != err {
logging.LogErrorf("query scan field failed: %s", err)
}
@ -118,7 +118,7 @@ func QueryAssetByHash(hash string) (ret *Asset) {
func scanAssetRows(rows *sql.Rows) (ret *Asset) {
var asset Asset
if err := rows.Scan(&asset.ID, &asset.BlockID, &asset.RootID, &asset.Box, &asset.DocPath, &asset.Path, &asset.Name, &asset.Title, &asset.Hash); nil != err {
if err := rows.Scan(&asset.ID, &asset.BlockID, &asset.RootID, &asset.Box, &asset.DocPath, &asset.Path, &asset.Name, &asset.Title, &asset.Hash); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}

View file

@ -51,13 +51,13 @@ func insertAssetContents(tx *sql.Tx, assetContents []*AssetContent, context map[
continue
}
if err = insertAssetContents0(tx, bulk, context); nil != err {
if err = insertAssetContents0(tx, bulk, context); err != nil {
return
}
bulk = []*AssetContent{}
}
if 0 < len(bulk) {
if err = insertAssetContents0(tx, bulk, context); nil != err {
if err = insertAssetContents0(tx, bulk, context); err != nil {
return
}
}
@ -79,7 +79,7 @@ func insertAssetContents0(tx *sql.Tx, bulk []*AssetContent, context map[string]i
}
stmt := fmt.Sprintf(AssetContentsFTSCaseInsensitiveInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
if err = prepareExecInsertTx(tx, stmt, valueArgs); err != nil {
return
}
@ -89,7 +89,7 @@ func insertAssetContents0(tx *sql.Tx, bulk []*AssetContent, context map[string]i
func deleteAssetContentsByPath(tx *sql.Tx, path string, context map[string]interface{}) (err error) {
stmt := "DELETE FROM asset_contents_fts_case_insensitive WHERE path = ?"
if err = execStmtTx(tx, stmt, path); nil != err {
if err = execStmtTx(tx, stmt, path); err != nil {
return
}
return

View file

@ -33,7 +33,7 @@ func QueryAssetContentNoLimit(stmt string) (ret []map[string]interface{}, err er
func queryAssetContentRawStmt(stmt string, limit int) (ret []map[string]interface{}, err error) {
rows, err := queryAssetContent(stmt)
if nil != err {
if err != nil {
if strings.Contains(err.Error(), "syntax error") {
return
}
@ -42,7 +42,7 @@ func queryAssetContentRawStmt(stmt string, limit int) (ret []map[string]interfac
defer rows.Close()
cols, err := rows.Columns()
if nil != err || nil == cols {
if err != nil || nil == cols {
return
}
@ -55,7 +55,7 @@ func queryAssetContentRawStmt(stmt string, limit int) (ret []map[string]interfac
columnPointers[i] = &columns[i]
}
if err = rows.Scan(columnPointers...); nil != err {
if err = rows.Scan(columnPointers...); err != nil {
return
}
@ -76,7 +76,7 @@ func queryAssetContentRawStmt(stmt string, limit int) (ret []map[string]interfac
func SelectAssetContentsRawStmt(stmt string, page, limit int) (ret []*AssetContent) {
parsedStmt, err := sqlparser.Parse(stmt)
if nil != err {
if err != nil {
return selectAssetContentsRawStmt(stmt, limit)
}
@ -122,7 +122,7 @@ func SelectAssetContentsRawStmt(stmt string, page, limit int) (ret []*AssetConte
stmt = strings.ReplaceAll(stmt, "\\\\*", "\\*")
stmt = strings.ReplaceAll(stmt, "from dual", "")
rows, err := queryAssetContent(stmt)
if nil != err {
if err != nil {
if strings.Contains(err.Error(), "syntax error") {
return
}
@ -144,7 +144,7 @@ func SelectAssetContentsRawStmtNoParse(stmt string, limit int) (ret []*AssetCont
func selectAssetContentsRawStmt(stmt string, limit int) (ret []*AssetContent) {
rows, err := queryAssetContent(stmt)
if nil != err {
if err != nil {
if strings.Contains(err.Error(), "syntax error") {
return
}
@ -172,7 +172,7 @@ func selectAssetContentsRawStmt(stmt string, limit int) (ret []*AssetContent) {
func scanAssetContentRows(rows *sql.Rows) (ret *AssetContent) {
var ac AssetContent
if err := rows.Scan(&ac.ID, &ac.Name, &ac.Ext, &ac.Path, &ac.Size, &ac.Updated, &ac.Content); nil != err {
if err := rows.Scan(&ac.ID, &ac.Name, &ac.Ext, &ac.Path, &ac.Size, &ac.Updated, &ac.Content); err != nil {
logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())
return
}

View file

@ -408,7 +408,7 @@ func RenderTemplateCol(ial map[string]string, rowValues []*av.KeyValues, tplCont
SQLTemplateFuncs(&tplFuncMap)
goTpl = goTpl.Funcs(tplFuncMap)
tpl, err := goTpl.Parse(tplContent)
if nil != err {
if err != nil {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, err)
return
}
@ -489,7 +489,7 @@ func RenderTemplateCol(ial map[string]string, rowValues []*av.KeyValues, tplCont
}
}
if err = tpl.Execute(buf, dataModel); nil != err {
if err = tpl.Execute(buf, dataModel); err != nil {
logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
return
}
@ -575,7 +575,7 @@ func getAttributeViewContent(avID string,
}
attrView, err := av.ParseAttributeView(avID)
if nil != err {
if err != nil {
logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
return
}

View file

@ -57,16 +57,16 @@ type Block struct {
func updateRootContent(tx *sql.Tx, content, updated, id string) (err error) {
stmt := "UPDATE blocks SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, content, updated, id); nil != err {
if err = execStmtTx(tx, stmt, content, content, updated, id); err != nil {
return
}
stmt = "UPDATE blocks_fts SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, content, updated, id); nil != err {
if err = execStmtTx(tx, stmt, content, content, updated, id); err != nil {
return
}
if !caseSensitive {
stmt = "UPDATE blocks_fts_case_insensitive SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, content, updated, id); nil != err {
if err = execStmtTx(tx, stmt, content, content, updated, id); err != nil {
return
}
}
@ -77,18 +77,18 @@ func updateRootContent(tx *sql.Tx, content, updated, id string) (err error) {
func updateBlockContent(tx *sql.Tx, block *Block) (err error) {
stmt := "UPDATE blocks SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
if err = execStmtTx(tx, stmt, block.Content, block.ID); err != nil {
tx.Rollback()
return
}
stmt = "UPDATE blocks_fts SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
if err = execStmtTx(tx, stmt, block.Content, block.ID); err != nil {
tx.Rollback()
return
}
if !caseSensitive {
stmt = "UPDATE blocks_fts_case_insensitive SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
if err = execStmtTx(tx, stmt, block.Content, block.ID); err != nil {
tx.Rollback()
return
}
@ -117,18 +117,18 @@ func indexNode(tx *sql.Tx, id string) (err error) {
content := NodeStaticContent(node, nil, true, indexAssetPath, true, nil)
stmt := "UPDATE blocks SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, id); nil != err {
if err = execStmtTx(tx, stmt, content, id); err != nil {
tx.Rollback()
return
}
stmt = "UPDATE blocks_fts SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, id); nil != err {
if err = execStmtTx(tx, stmt, content, id); err != nil {
tx.Rollback()
return
}
if !caseSensitive {
stmt = "UPDATE blocks_fts_case_insensitive SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, id); nil != err {
if err = execStmtTx(tx, stmt, content, id); err != nil {
tx.Rollback()
return
}

View file

@ -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
}

View file

@ -38,10 +38,10 @@ type Ref struct {
}
func upsertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
if err = deleteRefsByPath(tx, tree.Box, tree.Path); nil != err {
if err = deleteRefsByPath(tx, tree.Box, tree.Path); err != nil {
return
}
if err = deleteFileAnnotationRefsByPath(tx, tree.Box, tree.Path); nil != err {
if err = deleteFileAnnotationRefsByPath(tx, tree.Box, tree.Path); err != nil {
return
}
err = insertRefs(tx, tree)
@ -49,10 +49,10 @@ func upsertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
}
func deleteRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
if err = deleteRefsByPath(tx, tree.Box, tree.Path); nil != err {
if err = deleteRefsByPath(tx, tree.Box, tree.Path); err != nil {
return
}
if err = deleteFileAnnotationRefsByPath(tx, tree.Box, tree.Path); nil != err {
if err = deleteFileAnnotationRefsByPath(tx, tree.Box, tree.Path); err != nil {
return
}
return
@ -60,10 +60,10 @@ func deleteRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
func insertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
refs, fileAnnotationRefs := refsFromTree(tree)
if err = insertBlockRefs(tx, refs); nil != err {
if err = insertBlockRefs(tx, refs); err != nil {
return
}
if err = insertFileAnnotationRefs(tx, fileAnnotationRefs); nil != err {
if err = insertFileAnnotationRefs(tx, fileAnnotationRefs); err != nil {
return
}
return err

View file

@ -31,7 +31,7 @@ import (
func GetRefDuplicatedDefRootIDs() (ret []string) {
rows, err := query("SELECT DISTINCT def_block_root_id FROM `refs` GROUP BY def_block_id, def_block_root_id, block_id HAVING COUNT(*) > 1")
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -75,7 +75,7 @@ func queryRefTexts(refSearchIgnoreLines []string) (ret []string) {
sqlStmt += buf.String()
sqlStmt += " LIMIT 10240"
rows, err := query(sqlStmt)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", sqlStmt, err)
return
}
@ -101,7 +101,7 @@ func QueryRefCount(defIDs []string) (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 {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -109,7 +109,7 @@ func QueryRefCount(defIDs []string) (ret map[string]int) {
for rows.Next() {
var id string
var cnt int
if err = rows.Scan(&id, &cnt); nil != err {
if err = rows.Scan(&id, &cnt); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -121,7 +121,7 @@ func QueryRefCount(defIDs []string) (ret map[string]int) {
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)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -129,7 +129,7 @@ func QueryRootChildrenRefCount(defRootID string) (ret map[string]int) {
for rows.Next() {
var id string
var cnt int
if err = rows.Scan(&id, &cnt); nil != err {
if err = rows.Scan(&id, &cnt); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -142,7 +142,7 @@ func QueryRootBlockRefCount() (ret map[string]int) {
ret = map[string]int{}
rows, err := query("SELECT def_block_root_id, COUNT(*) AS ref_cnt FROM refs GROUP BY def_block_root_id")
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -150,7 +150,7 @@ func QueryRootBlockRefCount() (ret map[string]int) {
for rows.Next() {
var id string
var cnt int
if err = rows.Scan(&id, &cnt); nil != err {
if err = rows.Scan(&id, &cnt); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -161,7 +161,7 @@ func QueryRootBlockRefCount() (ret map[string]int) {
func QueryDefRootBlocksByRefRootID(refRootID string) (ret []*Block) {
rows, err := query("SELECT * FROM blocks WHERE id IN (SELECT DISTINCT def_block_root_id FROM refs WHERE root_id = ?)", refRootID)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -179,7 +179,7 @@ func QueryRefRootBlocksByDefRootIDs(defRootIDs []string) (ret map[string][]*Bloc
stmt := "SELECT r.def_block_root_id, b.* FROM refs AS r, blocks AS b ON r.def_block_root_id IN ('" + strings.Join(defRootIDs, "','") + "')" + " AND b.id = r.root_id"
rows, err := query(stmt)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -187,7 +187,7 @@ func QueryRefRootBlocksByDefRootIDs(defRootIDs []string) (ret map[string][]*Bloc
for rows.Next() {
var block Block
var defRootID string
if err := rows.Scan(&defRootID, &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(&defRootID, &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
}
@ -261,14 +261,14 @@ func queryDefIDsByDefText(keyword string, excludeIDs []string) (ret []string) {
q = "SELECT DISTINCT(def_block_id) FROM refs WHERE content = ? AND def_block_id NOT IN " + notIn
}
rows, err := query(q, keyword)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
var id string
if err = rows.Scan(&id); nil != err {
if err = rows.Scan(&id); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -281,14 +281,14 @@ func queryDefIDsByNameAlias(keyword string, excludeIDs []string) (ret []string)
ret = []string{}
notIn := "('" + strings.Join(excludeIDs, "','") + "')"
rows, err := query("SELECT DISTINCT(id), name, alias FROM blocks WHERE (name = ? OR alias LIKE ?) AND id NOT IN "+notIn, keyword, "%"+keyword+"%")
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
var id, name, alias string
if err = rows.Scan(&id, &name, &alias); nil != err {
if err = rows.Scan(&id, &name, &alias); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -318,14 +318,14 @@ func queryDefIDsByNameAlias(keyword string, excludeIDs []string) (ret []string)
func QueryChildDefIDsByRootDefID(rootDefID string) (ret []string) {
ret = []string{}
rows, err := query("SELECT DISTINCT(def_block_id) FROM refs WHERE def_block_root_id = ?", rootDefID)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
var id string
if err = rows.Scan(&id); nil != err {
if err = rows.Scan(&id); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -343,14 +343,14 @@ func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs, refTexts []
} else {
rows, err = query("SELECT block_id, content FROM refs WHERE def_block_id = ?", defID)
}
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
var id, content string
if err = rows.Scan(&id, &content); nil != err {
if err = rows.Scan(&id, &content); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -376,7 +376,7 @@ func QueryRefsRecent(onlyDoc bool, typeFilter string, ignoreLines []string) (ret
}
stmt += " GROUP BY r.def_block_id ORDER BY r.id DESC LIMIT 32"
rows, err := query(stmt)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -410,7 +410,7 @@ func QueryRefsByDefID(defBlockID string, containChildren bool) (ret []*Ref) {
rows, err = query("SELECT * FROM refs WHERE def_block_id = ?", defBlockID)
}
}
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -425,7 +425,7 @@ func QueryRefsByDefID(defBlockID string, containChildren bool) (ret []*Ref) {
func QueryRefsByDefIDRefID(defBlockID, refBlockID string) (ret []*Ref) {
stmt := "SELECT * FROM refs WHERE def_block_id = ? AND block_id = ?"
rows, err := query(stmt, defBlockID, refBlockID)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -445,7 +445,7 @@ func DefRefs(condition string) (ret []map[*Block]*Block) {
}
rows, err := query(stmt)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -455,7 +455,7 @@ func DefRefs(condition string) (ret []map[*Block]*Block) {
var ref Block
var rel string
if err = rows.Scan(&ref.ID, &ref.ParentID, &ref.RootID, &ref.Hash, &ref.Box, &ref.Path, &ref.HPath, &ref.Name, &ref.Alias, &ref.Memo, &ref.Tag, &ref.Content, &ref.FContent, &ref.Markdown, &ref.Length, &ref.Type, &ref.SubType, &ref.IAL, &ref.Sort, &ref.Created, &ref.Updated,
&rel); nil != err {
&rel); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}
@ -463,7 +463,7 @@ func DefRefs(condition string) (ret []map[*Block]*Block) {
}
rows, err = query("SELECT def.* FROM blocks AS def, refs AS r WHERE def.id = r.def_block_id")
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -490,7 +490,7 @@ func DefRefs(condition string) (ret []map[*Block]*Block) {
func scanRefRows(rows *sql.Rows) (ret *Ref) {
var ref Ref
if err := rows.Scan(&ref.ID, &ref.DefBlockID, &ref.DefBlockParentID, &ref.DefBlockRootID, &ref.DefBlockPath, &ref.BlockID, &ref.RootID, &ref.Box, &ref.Path, &ref.Content, &ref.Markdown, &ref.Type); nil != err {
if err := rows.Scan(&ref.ID, &ref.DefBlockID, &ref.DefBlockParentID, &ref.DefBlockRootID, &ref.DefBlockPath, &ref.BlockID, &ref.RootID, &ref.Box, &ref.Path, &ref.Content, &ref.Markdown, &ref.Type); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}

View file

@ -53,7 +53,7 @@ func putBlockCache(block *Block) {
}
cloned := &Block{}
if err := copier.Copy(cloned, block); nil != err {
if err := copier.Copy(cloned, block); err != nil {
logging.LogErrorf("clone block failed: %v", err)
return
}

View file

@ -96,7 +96,7 @@ func InitDatabase(forceRebuild bool) (err error) {
closeDatabase()
if gulu.File.IsExist(util.DBPath) {
if err = removeDatabaseFile(); nil != err {
if err = removeDatabaseFile(); err != nil {
logging.LogErrorf("remove database file [%s] failed: %s", util.DBPath, err)
util.PushClearProgress()
err = nil
@ -112,111 +112,111 @@ func InitDatabase(forceRebuild bool) (err error) {
func initDBTables() {
_, err := db.Exec("DROP TABLE IF EXISTS stat")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [stat] failed: %s", err)
}
_, err = db.Exec("CREATE TABLE stat (key, value)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [stat] failed: %s", err)
}
setDatabaseVer()
_, err = db.Exec("DROP TABLE IF EXISTS blocks")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [blocks] failed: %s", err)
}
_, err = db.Exec("CREATE TABLE blocks (id, parent_id, root_id, hash, box, path, hpath, name, alias, memo, tag, content, fcontent, markdown, length, type, subtype, ial, sort, created, updated)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks] failed: %s", err)
}
_, err = db.Exec("CREATE INDEX idx_blocks_id ON blocks(id)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_blocks_id] failed: %s", err)
}
_, err = db.Exec("CREATE INDEX idx_blocks_parent_id ON blocks(parent_id)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_blocks_parent_id] failed: %s", err)
}
_, err = db.Exec("CREATE INDEX idx_blocks_root_id ON blocks(root_id)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_blocks_root_id] failed: %s", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS blocks_fts")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [blocks_fts] failed: %s", err)
}
_, err = db.Exec("CREATE VIRTUAL TABLE blocks_fts USING fts5(id UNINDEXED, parent_id UNINDEXED, root_id UNINDEXED, hash UNINDEXED, box UNINDEXED, path UNINDEXED, hpath, name, alias, memo, tag, content, fcontent, markdown UNINDEXED, length UNINDEXED, type UNINDEXED, subtype UNINDEXED, ial, sort UNINDEXED, created UNINDEXED, updated UNINDEXED, tokenize=\"siyuan\")")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks_fts] failed: %s", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS blocks_fts_case_insensitive")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [blocks_fts_case_insensitive] failed: %s", err)
}
_, err = db.Exec("CREATE VIRTUAL TABLE blocks_fts_case_insensitive USING fts5(id UNINDEXED, parent_id UNINDEXED, root_id UNINDEXED, hash UNINDEXED, box UNINDEXED, path UNINDEXED, hpath, name, alias, memo, tag, content, fcontent, markdown UNINDEXED, length UNINDEXED, type UNINDEXED, subtype UNINDEXED, ial, sort UNINDEXED, created UNINDEXED, updated UNINDEXED, tokenize=\"siyuan case_insensitive\")")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks_fts_case_insensitive] failed: %s", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS spans")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [spans] failed: %s", err)
}
_, err = db.Exec("CREATE TABLE spans (id, block_id, root_id, box, path, content, markdown, type, ial)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [spans] failed: %s", err)
}
_, err = db.Exec("CREATE INDEX idx_spans_root_id ON spans(root_id)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_spans_root_id] failed: %s", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS assets")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [assets] failed: %s", err)
}
_, err = db.Exec("CREATE TABLE assets (id, block_id, root_id, box, docpath, path, name, title, hash)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [assets] failed: %s", err)
}
_, err = db.Exec("CREATE INDEX idx_assets_root_id ON assets(root_id)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_assets_root_id] failed: %s", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS attributes")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [attributes] failed: %s", err)
}
_, err = db.Exec("CREATE TABLE attributes (id, name, value, type, block_id, root_id, box, path)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [attributes] failed: %s", err)
}
_, err = db.Exec("CREATE INDEX idx_attributes_root_id ON attributes(root_id)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_attributes_root_id] failed: %s", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS refs")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [refs] failed: %s", err)
}
_, err = db.Exec("CREATE TABLE refs (id, def_block_id, def_block_parent_id, def_block_root_id, def_block_path, block_id, root_id, box, path, content, markdown, type)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [refs] failed: %s", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS file_annotation_refs")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [refs] failed: %s", err)
}
_, err = db.Exec("CREATE TABLE file_annotation_refs (id, file_path, annotation_id, block_id, root_id, box, path, content, type)")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [refs] failed: %s", err)
}
}
@ -237,7 +237,7 @@ func initDBConnection() {
"&_case_sensitive_like=OFF"
var err error
db, err = sql.Open("sqlite3_extended", dsn)
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create database failed: %s", err)
}
db.SetMaxIdleConns(20)
@ -258,7 +258,7 @@ func InitHistoryDatabase(forceRebuild bool) {
}
historyDB.Close()
if err := os.RemoveAll(util.HistoryDBPath); nil != err {
if err := os.RemoveAll(util.HistoryDBPath); err != nil {
logging.LogErrorf("remove history database file [%s] failed: %s", util.HistoryDBPath, err)
return
}
@ -284,7 +284,7 @@ func initHistoryDBConnection() {
"&_case_sensitive_like=OFF"
var err error
historyDB, err = sql.Open("sqlite3_extended", dsn)
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create history database failed: %s", err)
}
historyDB.SetMaxIdleConns(3)
@ -295,7 +295,7 @@ func initHistoryDBConnection() {
func initHistoryDBTables() {
historyDB.Exec("DROP TABLE histories_fts_case_insensitive")
_, err := historyDB.Exec("CREATE VIRTUAL TABLE histories_fts_case_insensitive USING fts5(id UNINDEXED, type UNINDEXED, op UNINDEXED, title, content, path UNINDEXED, created UNINDEXED, tokenize=\"siyuan case_insensitive\")")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [histories_fts_case_insensitive] failed: %s", err)
}
}
@ -313,7 +313,7 @@ func InitAssetContentDatabase(forceRebuild bool) {
}
assetContentDB.Close()
if err := os.RemoveAll(util.AssetContentDBPath); nil != err {
if err := os.RemoveAll(util.AssetContentDBPath); err != nil {
logging.LogErrorf("remove assets database file [%s] failed: %s", util.AssetContentDBPath, err)
return
}
@ -339,7 +339,7 @@ func initAssetContentDBConnection() {
"&_case_sensitive_like=OFF"
var err error
assetContentDB, err = sql.Open("sqlite3_extended", dsn)
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create assets database failed: %s", err)
}
assetContentDB.SetMaxIdleConns(3)
@ -350,7 +350,7 @@ func initAssetContentDBConnection() {
func initAssetContentDBTables() {
assetContentDB.Exec("DROP TABLE asset_contents_fts_case_insensitive")
_, err := assetContentDB.Exec("CREATE VIRTUAL TABLE asset_contents_fts_case_insensitive USING fts5(id UNINDEXED, name, ext, path, size UNINDEXED, updated UNINDEXED, content, tokenize=\"siyuan case_insensitive\")")
if nil != err {
if err != nil {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [asset_contents_fts_case_insensitive] failed: %s", err)
}
}
@ -718,7 +718,7 @@ func buildSpanFromNode(n *ast.Node, tree *parse.Tree, rootID, boxID, p string) (
}
case ast.NodeInlineHTML, ast.NodeHTMLBlock, ast.NodeIFrame, ast.NodeWidget, ast.NodeAudio, ast.NodeVideo:
nodes, err := html.ParseFragment(bytes.NewReader(n.Tokens), &html.Node{Type: html.ElementNode})
if nil != err {
if err != nil {
logging.LogErrorf("parse HTML failed: %s", err)
walkStatus = ast.WalkContinue
return
@ -954,22 +954,22 @@ func heading(node *ast.Node) *ast.Node {
}
func deleteByBoxTx(tx *sql.Tx, box string) (err error) {
if err = deleteBlocksByBoxTx(tx, box); nil != err {
if err = deleteBlocksByBoxTx(tx, box); err != nil {
return
}
if err = deleteSpansByBoxTx(tx, box); nil != err {
if err = deleteSpansByBoxTx(tx, box); err != nil {
return
}
if err = deleteAssetsByBoxTx(tx, box); nil != err {
if err = deleteAssetsByBoxTx(tx, box); err != nil {
return
}
if err = deleteAttributesByBoxTx(tx, box); nil != err {
if err = deleteAttributesByBoxTx(tx, box); err != nil {
return
}
if err = deleteBlockRefsByBoxTx(tx, box); nil != err {
if err = deleteBlockRefsByBoxTx(tx, box); err != nil {
return
}
if err = deleteFileAnnotationRefsByBoxTx(tx, box); nil != err {
if err = deleteFileAnnotationRefsByBoxTx(tx, box); err != nil {
return
}
return
@ -989,13 +989,13 @@ func deleteBlocksByIDs(tx *sql.Tx, ids []string) (err error) {
var rowIDs []string
stmt := "SELECT ROWID FROM blocks WHERE id IN (" + strings.Join(ftsIDs, ",") + ")"
rows, err := tx.Query(stmt)
if nil != err {
if err != nil {
logging.LogErrorf("query block rowIDs failed: %s", err)
return
}
for rows.Next() {
var rowID int64
if err = rows.Scan(&rowID); nil != err {
if err = rows.Scan(&rowID); err != nil {
logging.LogErrorf("scan block rowID failed: %s", err)
rows.Close()
return
@ -1009,18 +1009,18 @@ func deleteBlocksByIDs(tx *sql.Tx, ids []string) (err error) {
}
stmt = "DELETE FROM blocks WHERE ROWID IN (" + strings.Join(rowIDs, ",") + ")"
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
stmt = "DELETE FROM blocks_fts WHERE ROWID IN (" + strings.Join(rowIDs, ",") + ")"
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
if !caseSensitive {
stmt = "DELETE FROM blocks_fts_case_insensitive WHERE ROWID IN (" + strings.Join(rowIDs, ",") + ")"
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
}
@ -1029,16 +1029,16 @@ func deleteBlocksByIDs(tx *sql.Tx, ids []string) (err error) {
func deleteBlocksByBoxTx(tx *sql.Tx, box string) (err error) {
stmt := "DELETE FROM blocks WHERE box = ?"
if err = execStmtTx(tx, stmt, box); nil != err {
if err = execStmtTx(tx, stmt, box); err != nil {
return
}
stmt = "DELETE FROM blocks_fts WHERE box = ?"
if err = execStmtTx(tx, stmt, box); nil != err {
if err = execStmtTx(tx, stmt, box); err != nil {
return
}
if !caseSensitive {
stmt = "DELETE FROM blocks_fts_case_insensitive WHERE box = ?"
if err = execStmtTx(tx, stmt, box); nil != err {
if err = execStmtTx(tx, stmt, box); err != nil {
return
}
}
@ -1096,7 +1096,7 @@ func deleteRefsByPathTx(tx *sql.Tx, box, path string) (err error) {
}
func deleteRefsByBoxTx(tx *sql.Tx, box string) (err error) {
if err = deleteFileAnnotationRefsByBoxTx(tx, box); nil != err {
if err = deleteFileAnnotationRefsByBoxTx(tx, box); err != nil {
return
}
return deleteBlockRefsByBoxTx(tx, box)
@ -1128,37 +1128,37 @@ func deleteFileAnnotationRefsByBoxTx(tx *sql.Tx, box string) (err error) {
func deleteByRootID(tx *sql.Tx, rootID string, context map[string]interface{}) (err error) {
stmt := "DELETE FROM blocks WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
stmt = "DELETE FROM blocks_fts WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
if !caseSensitive {
stmt = "DELETE FROM blocks_fts_case_insensitive WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
}
stmt = "DELETE FROM spans WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
stmt = "DELETE FROM assets WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
stmt = "DELETE FROM refs WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
stmt = "DELETE FROM file_annotation_refs WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
stmt = "DELETE FROM attributes WHERE root_id = ?"
if err = execStmtTx(tx, stmt, rootID); nil != err {
if err = execStmtTx(tx, stmt, rootID); err != nil {
return
}
ClearCache()
@ -1174,37 +1174,37 @@ func batchDeleteByRootIDs(tx *sql.Tx, rootIDs []string, context map[string]inter
ids := strings.Join(rootIDs, "','")
ids = "('" + ids + "')"
stmt := "DELETE FROM blocks WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
stmt = "DELETE FROM blocks_fts WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
if !caseSensitive {
stmt = "DELETE FROM blocks_fts_case_insensitive WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
}
stmt = "DELETE FROM spans WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
stmt = "DELETE FROM assets WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
stmt = "DELETE FROM refs WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
stmt = "DELETE FROM file_annotation_refs WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
stmt = "DELETE FROM attributes WHERE root_id IN " + ids
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}
ClearCache()
@ -1214,37 +1214,37 @@ func batchDeleteByRootIDs(tx *sql.Tx, rootIDs []string, context map[string]inter
func batchDeleteByPathPrefix(tx *sql.Tx, boxID, pathPrefix string) (err error) {
stmt := "DELETE FROM blocks WHERE box = ? AND path LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
stmt = "DELETE FROM blocks_fts WHERE box = ? AND path LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
if !caseSensitive {
stmt = "DELETE FROM blocks_fts_case_insensitive WHERE box = ? AND path LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
}
stmt = "DELETE FROM spans WHERE box = ? AND path LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
stmt = "DELETE FROM assets WHERE box = ? AND docpath LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
stmt = "DELETE FROM refs WHERE box = ? AND path LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
stmt = "DELETE FROM file_annotation_refs WHERE box = ? AND path LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
stmt = "DELETE FROM attributes WHERE box = ? AND path LIKE ?"
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); nil != err {
if err = execStmtTx(tx, stmt, boxID, pathPrefix+"%"); err != nil {
return
}
ClearCache()
@ -1253,16 +1253,16 @@ func batchDeleteByPathPrefix(tx *sql.Tx, boxID, pathPrefix string) (err error) {
func batchUpdateHPath(tx *sql.Tx, rootID, newHPath string, context map[string]interface{}) (err error) {
stmt := "UPDATE blocks SET hpath = ? WHERE root_id = ?"
if err = execStmtTx(tx, stmt, newHPath, rootID); nil != err {
if err = execStmtTx(tx, stmt, newHPath, rootID); err != nil {
return
}
stmt = "UPDATE blocks_fts SET hpath = ? WHERE root_id = ?"
if err = execStmtTx(tx, stmt, newHPath, rootID); nil != err {
if err = execStmtTx(tx, stmt, newHPath, rootID); err != nil {
return
}
if !caseSensitive {
stmt = "UPDATE blocks_fts_case_insensitive SET hpath = ? WHERE root_id = ?"
if err = execStmtTx(tx, stmt, newHPath, rootID); nil != err {
if err = execStmtTx(tx, stmt, newHPath, rootID); err != nil {
return
}
}
@ -1273,15 +1273,15 @@ func batchUpdateHPath(tx *sql.Tx, rootID, newHPath string, context map[string]in
}
func CloseDatabase() {
if err := closeDatabase(); nil != err {
if err := closeDatabase(); err != nil {
logging.LogErrorf("close database failed: %s", err)
return
}
if err := historyDB.Close(); nil != err {
if err := historyDB.Close(); err != nil {
logging.LogErrorf("close history database failed: %s", err)
return
}
if err := assetContentDB.Close(); nil != err {
if err := assetContentDB.Close(); err != nil {
logging.LogErrorf("close asset content database failed: %s", err)
return
}
@ -1307,7 +1307,7 @@ func query(query string, args ...interface{}) (*sql.Rows, error) {
}
func beginTx() (tx *sql.Tx, err error) {
if tx, err = db.Begin(); nil != err {
if tx, err = db.Begin(); err != nil {
logging.LogErrorf("begin tx failed: %s\n %s", err, logging.ShortStack())
if strings.Contains(err.Error(), "database is locked") {
os.Exit(logging.ExitCodeReadOnlyDatabase)
@ -1322,14 +1322,14 @@ func commitTx(tx *sql.Tx) (err error) {
return errors.New("tx is nil")
}
if err = tx.Commit(); nil != err {
if err = tx.Commit(); err != nil {
logging.LogErrorf("commit tx failed: %s\n %s", err, logging.ShortStack())
}
return
}
func beginHistoryTx() (tx *sql.Tx, err error) {
if tx, err = historyDB.Begin(); nil != err {
if tx, err = historyDB.Begin(); err != nil {
logging.LogErrorf("begin history tx failed: %s\n %s", err, logging.ShortStack())
if strings.Contains(err.Error(), "database is locked") {
os.Exit(logging.ExitCodeReadOnlyDatabase)
@ -1344,14 +1344,14 @@ func commitHistoryTx(tx *sql.Tx) (err error) {
return errors.New("tx is nil")
}
if err = tx.Commit(); nil != err {
if err = tx.Commit(); err != nil {
logging.LogErrorf("commit tx failed: %s\n %s", err, logging.ShortStack())
}
return
}
func beginAssetContentTx() (tx *sql.Tx, err error) {
if tx, err = assetContentDB.Begin(); nil != err {
if tx, err = assetContentDB.Begin(); err != nil {
logging.LogErrorf("begin asset content tx failed: %s\n %s", err, logging.ShortStack())
if strings.Contains(err.Error(), "database is locked") {
os.Exit(logging.ExitCodeReadOnlyDatabase)
@ -1366,7 +1366,7 @@ func commitAssetContentTx(tx *sql.Tx) (err error) {
return errors.New("tx is nil")
}
if err = tx.Commit(); nil != err {
if err = tx.Commit(); err != nil {
logging.LogErrorf("commit tx failed: %s\n %s", err, logging.ShortStack())
}
return
@ -1374,10 +1374,10 @@ func commitAssetContentTx(tx *sql.Tx) (err error) {
func prepareExecInsertTx(tx *sql.Tx, stmtSQL string, args []interface{}) (err error) {
stmt, err := tx.Prepare(stmtSQL)
if nil != err {
if err != nil {
return
}
if _, err = stmt.Exec(args...); nil != err {
if _, err = stmt.Exec(args...); err != nil {
logging.LogErrorf("exec database stmt [%s] failed: %s", stmtSQL, err)
return
}
@ -1385,7 +1385,7 @@ func prepareExecInsertTx(tx *sql.Tx, stmtSQL string, args []interface{}) (err er
}
func execStmtTx(tx *sql.Tx, stmt string, args ...interface{}) (err error) {
if _, err = tx.Exec(stmt, args...); nil != err {
if _, err = tx.Exec(stmt, args...); err != nil {
if strings.Contains(err.Error(), "database disk image is malformed") {
tx.Rollback()
closeDatabase()
@ -1446,15 +1446,15 @@ func ialAttr(ial, name string) (ret string) {
func removeDatabaseFile() (err error) {
err = os.RemoveAll(util.DBPath)
if nil != err {
if err != nil {
return
}
err = os.RemoveAll(util.DBPath + "-shm")
if nil != err {
if err != nil {
return
}
err = os.RemoveAll(util.DBPath + "-wal")
if nil != err {
if err != nil {
return
}
return

View file

@ -35,14 +35,14 @@ type FileAnnotationRef struct {
func QueryRefIDsByAnnotationID(annotationID string) (refIDs, refTexts []string) {
refIDs = []string{}
rows, err := query("SELECT block_id, content FROM file_annotation_refs WHERE annotation_id = ?", annotationID)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
var id, content string
if err = rows.Scan(&id, &content); nil != err {
if err = rows.Scan(&id, &content); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}

View file

@ -39,7 +39,7 @@ type History struct {
func QueryHistory(stmt string) (ret []map[string]interface{}, err error) {
ret = []map[string]interface{}{}
rows, err := queryHistory(stmt)
if nil != err {
if err != nil {
logging.LogWarnf("sql query [%s] failed: %s", stmt, err)
return
}
@ -57,7 +57,7 @@ func QueryHistory(stmt string) (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
}
@ -73,7 +73,7 @@ func QueryHistory(stmt string) (ret []map[string]interface{}, err error) {
func SelectHistoriesRawStmt(stmt string) (ret []*History) {
rows, err := historyDB.Query(stmt)
if nil != err {
if err != nil {
logging.LogWarnf("sql query [%s] failed: %s", stmt, err)
return
}
@ -88,7 +88,7 @@ func SelectHistoriesRawStmt(stmt string) (ret []*History) {
func scanHistoryRows(rows *sql.Rows) (ret *History) {
var history History
if err := rows.Scan(&history.ID, &history.Type, &history.Op, &history.Title, &history.Content, &history.Path, &history.Created); nil != err {
if err := rows.Scan(&history.ID, &history.Type, &history.Op, &history.Title, &history.Content, &history.Path, &history.Created); err != nil {
logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())
return
}
@ -106,7 +106,7 @@ func queryHistory(query string, args ...interface{}) (*sql.Rows, error) {
func deleteOutdatedHistories(tx *sql.Tx, before int64, context map[string]interface{}) (err error) {
stmt := "DELETE FROM histories_fts_case_insensitive WHERE CAST(created AS INTEGER) < ?"
if err = execStmtTx(tx, stmt, before); nil != err {
if err = execStmtTx(tx, stmt, before); err != nil {
return
}
return
@ -129,13 +129,13 @@ func insertHistories(tx *sql.Tx, histories []*History, context map[string]interf
continue
}
if err = insertHistories0(tx, bulk, context); nil != err {
if err = insertHistories0(tx, bulk, context); err != nil {
return
}
bulk = []*History{}
}
if 0 < len(bulk) {
if err = insertHistories0(tx, bulk, context); nil != err {
if err = insertHistories0(tx, bulk, context); err != nil {
return
}
}
@ -157,7 +157,7 @@ func insertHistories0(tx *sql.Tx, bulk []*History, context map[string]interface{
}
stmt := fmt.Sprintf(HistoriesFTSCaseInsensitiveInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
if err = prepareExecInsertTx(tx, stmt, valueArgs); err != nil {
return
}

View file

@ -136,20 +136,20 @@ func FlushQueue() {
}
tx, err := beginTx()
if nil != err {
if err != nil {
return
}
groupOpsCurrent[op.action]++
context["current"] = groupOpsCurrent[op.action]
context["total"] = groupOpsTotal[op.action]
if err = execOp(op, tx, context); nil != err {
if err = execOp(op, tx, context); err != nil {
tx.Rollback()
logging.LogErrorf("queue operation [%s] failed: %s", op.action, err)
continue
}
if err = commitTx(tx); nil != err {
if err = commitTx(tx); err != nil {
logging.LogErrorf("commit tx failed: %s", err)
continue
}
@ -188,7 +188,7 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (e
err = batchDeleteByRootIDs(tx, op.removeTreeIDs, context)
case "rename":
err = batchUpdateHPath(tx, op.renameTree.ID, op.renameTree.HPath, context)
if nil != err {
if err != nil {
break
}
err = updateRootContent(tx, path.Base(op.renameTree.HPath), op.renameTree.Root.IALAttr("updated"), op.renameTree.ID)

View file

@ -72,7 +72,7 @@ func FlushAssetContentQueue() {
}
tx, err := beginAssetContentTx()
if nil != err {
if err != nil {
return
}
@ -80,14 +80,14 @@ func FlushAssetContentQueue() {
context["current"] = groupOpsCurrent[op.action]
context["total"] = groupOpsTotal[op.action]
if err = execAssetContentOp(op, tx, context); nil != err {
if err = execAssetContentOp(op, tx, context); err != nil {
tx.Rollback()
logging.LogErrorf("queue operation failed: %s", err)
eventbus.Publish(util.EvtSQLAssetContentRebuild)
return
}
if err = commitAssetContentTx(tx); nil != err {
if err = commitAssetContentTx(tx); err != nil {
logging.LogErrorf("commit tx failed: %s", err)
return
}

View file

@ -72,7 +72,7 @@ func FlushHistoryQueue() {
}
tx, err := beginHistoryTx()
if nil != err {
if err != nil {
return
}
@ -80,14 +80,14 @@ func FlushHistoryQueue() {
context["current"] = groupOpsCurrent[op.action]
context["total"] = groupOpsTotal[op.action]
if err = execHistoryOp(op, tx, context); nil != err {
if err = execHistoryOp(op, tx, context); err != nil {
tx.Rollback()
logging.LogErrorf("queue operation failed: %s", err)
eventbus.Publish(util.EvtSQLHistoryRebuild)
return
}
if err = commitHistoryTx(tx); nil != err {
if err = commitHistoryTx(tx); err != nil {
logging.LogErrorf("commit tx failed: %s", err)
return
}

View file

@ -39,7 +39,7 @@ type Span struct {
func SelectSpansRawStmt(stmt string, limit int) (ret []*Span) {
parsedStmt, err := sqlparser.Parse(stmt)
if nil != err {
if err != nil {
//logging.LogErrorf("select [%s] failed: %s", stmt, err)
return
}
@ -65,7 +65,7 @@ func SelectSpansRawStmt(stmt string, limit int) (ret []*Span) {
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
}
@ -83,7 +83,7 @@ func SelectSpansRawStmt(stmt string, limit int) (ret []*Span) {
func QueryTagSpansByLabel(label string) (ret []*Span) {
stmt := "SELECT * FROM spans WHERE type LIKE '%tag%' AND content LIKE '%" + label + "%' GROUP BY block_id"
rows, err := query(stmt)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -99,7 +99,7 @@ func QueryTagSpansByKeyword(keyword string, limit int) (ret []*Span) {
stmt := "SELECT * FROM spans WHERE type LIKE '%tag%' AND content LIKE '%" + keyword + "%' GROUP BY markdown"
stmt += " LIMIT " + strconv.Itoa(limit)
rows, err := query(stmt)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -117,7 +117,7 @@ func QueryTagSpans(p string) (ret []*Span) {
stmt += " AND path = '" + p + "'"
}
rows, err := query(stmt)
if nil != err {
if err != nil {
logging.LogErrorf("sql query failed: %s", err)
return
}
@ -131,7 +131,7 @@ func QueryTagSpans(p string) (ret []*Span) {
func scanSpanRows(rows *sql.Rows) (ret *Span) {
var span Span
if err := rows.Scan(&span.ID, &span.BlockID, &span.RootID, &span.Box, &span.Path, &span.Content, &span.Markdown, &span.Type, &span.IAL); nil != err {
if err := rows.Scan(&span.ID, &span.BlockID, &span.RootID, &span.Box, &span.Path, &span.Content, &span.Markdown, &span.Type, &span.IAL); err != nil {
logging.LogErrorf("query scan field failed: %s", err)
return
}

View file

@ -33,7 +33,7 @@ func getDatabaseVer() (ret string) {
key := "siyuan_database_ver"
stmt := "SELECT value FROM stat WHERE `key` = ?"
row := db.QueryRow(stmt, key)
if err := row.Scan(&ret); nil != err {
if err := row.Scan(&ret); err != nil {
if !strings.Contains(err.Error(), "no such table") {
logging.LogErrorf("query database version failed: %s", err)
}
@ -44,10 +44,10 @@ func getDatabaseVer() (ret string) {
func setDatabaseVer() {
key := "siyuan_database_ver"
tx, err := beginTx()
if nil != err {
if err != nil {
return
}
if err = putStat(tx, key, util.DatabaseVer); nil != err {
if err = putStat(tx, key, util.DatabaseVer); err != nil {
return
}
commitTx(tx)
@ -55,7 +55,7 @@ func setDatabaseVer() {
func putStat(tx *sql.Tx, key, value string) (err error) {
stmt := "DELETE FROM stat WHERE `key` = '" + key + "'"
if err = execStmtTx(tx, stmt); nil != err {
if err = execStmtTx(tx, stmt); err != nil {
return
}

View file

@ -69,13 +69,13 @@ func insertBlocks(tx *sql.Tx, blocks []*Block, context map[string]interface{}) (
continue
}
if err = insertBlocks0(tx, bulk, context); nil != err {
if err = insertBlocks0(tx, bulk, context); err != nil {
return
}
bulk = []*Block{}
}
if 0 < len(bulk) {
if err = insertBlocks0(tx, bulk, context); nil != err {
if err = insertBlocks0(tx, bulk, context); err != nil {
return
}
}
@ -115,7 +115,7 @@ func insertBlocks0(tx *sql.Tx, bulk []*Block, context map[string]interface{}) (e
}
stmt := fmt.Sprintf(BlocksInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
if err = prepareExecInsertTx(tx, stmt, valueArgs); err != nil {
return
}
hashBuf.WriteString("blocks")
@ -124,13 +124,13 @@ func insertBlocks0(tx *sql.Tx, bulk []*Block, context map[string]interface{}) (e
//eventbus.Publish(eventbus.EvtSQLInsertBlocks, context, current, total, len(bulk), evtHash)
stmt = fmt.Sprintf(BlocksFTSInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
if err = prepareExecInsertTx(tx, stmt, valueArgs); err != nil {
return
}
if !caseSensitive {
stmt = fmt.Sprintf(BlocksFTSCaseInsensitiveInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
if err = prepareExecInsertTx(tx, stmt, valueArgs); err != nil {
return
}
}
@ -152,13 +152,13 @@ func insertAttributes(tx *sql.Tx, attributes []*Attribute) (err error) {
continue
}
if err = insertAttribute0(tx, bulk); nil != err {
if err = insertAttribute0(tx, bulk); err != nil {
return
}
bulk = []*Attribute{}
}
if 0 < len(bulk) {
if err = insertAttribute0(tx, bulk); nil != err {
if err = insertAttribute0(tx, bulk); err != nil {
return
}
}
@ -200,13 +200,13 @@ func insertAssets(tx *sql.Tx, assets []*Asset) (err error) {
continue
}
if err = insertAsset0(tx, bulk); nil != err {
if err = insertAsset0(tx, bulk); err != nil {
return
}
bulk = []*Asset{}
}
if 0 < len(bulk) {
if err = insertAsset0(tx, bulk); nil != err {
if err = insertAsset0(tx, bulk); err != nil {
return
}
}
@ -249,13 +249,13 @@ func insertSpans(tx *sql.Tx, spans []*Span) (err error) {
continue
}
if err = insertSpans0(tx, bulk); nil != err {
if err = insertSpans0(tx, bulk); err != nil {
return
}
bulk = []*Span{}
}
if 0 < len(bulk) {
if err = insertSpans0(tx, bulk); nil != err {
if err = insertSpans0(tx, bulk); err != nil {
return
}
}
@ -298,13 +298,13 @@ func insertBlockRefs(tx *sql.Tx, refs []*Ref) (err error) {
continue
}
if err = insertRefs0(tx, bulk); nil != err {
if err = insertRefs0(tx, bulk); err != nil {
return
}
bulk = []*Ref{}
}
if 0 < len(bulk) {
if err = insertRefs0(tx, bulk); nil != err {
if err = insertRefs0(tx, bulk); err != nil {
return
}
}
@ -352,13 +352,13 @@ func insertFileAnnotationRefs(tx *sql.Tx, refs []*FileAnnotationRef) (err error)
continue
}
if err = insertFileAnnotationRefs0(tx, bulk); nil != err {
if err = insertFileAnnotationRefs0(tx, bulk); err != nil {
return
}
bulk = []*FileAnnotationRef{}
}
if 0 < len(bulk) {
if err = insertFileAnnotationRefs0(tx, bulk); nil != err {
if err = insertFileAnnotationRefs0(tx, bulk); err != nil {
return
}
}
@ -425,28 +425,28 @@ func upsertTree(tx *sql.Tx, tree *parse.Tree, context map[string]interface{}) (e
toRemoves = append(toRemoves, b.ID)
}
if err = deleteBlocksByIDs(tx, toRemoves); nil != err {
if err = deleteBlocksByIDs(tx, toRemoves); err != nil {
return
}
if err = deleteSpansByRootID(tx, tree.ID); nil != err {
if err = deleteSpansByRootID(tx, tree.ID); err != nil {
return
}
if err = deleteAssetsByRootID(tx, tree.ID); nil != err {
if err = deleteAssetsByRootID(tx, tree.ID); err != nil {
return
}
if err = deleteAttributesByRootID(tx, tree.ID); nil != err {
if err = deleteAttributesByRootID(tx, tree.ID); err != nil {
return
}
if err = deleteRefsByPathTx(tx, tree.Box, tree.Path); nil != err {
if err = deleteRefsByPathTx(tx, tree.Box, tree.Path); err != nil {
return
}
if err = deleteFileAnnotationRefsByPathTx(tx, tree.Box, tree.Path); nil != err {
if err = deleteFileAnnotationRefsByPathTx(tx, tree.Box, tree.Path); err != nil {
return
}
refs, fileAnnotationRefs := refsFromTree(tree)
if err = insertTree0(tx, tree, context, blocks, spans, assets, attributes, refs, fileAnnotationRefs); nil != err {
if err = insertTree0(tx, tree, context, blocks, spans, assets, attributes, refs, fileAnnotationRefs); err != nil {
return
}
return err
@ -463,30 +463,30 @@ func insertTree0(tx *sql.Tx, tree *parse.Tree, context map[string]interface{},
}
}
if err = insertBlocks(tx, blocks, context); nil != err {
if err = insertBlocks(tx, blocks, context); err != nil {
return
}
if err = insertBlockRefs(tx, refs); nil != err {
if err = insertBlockRefs(tx, refs); err != nil {
return
}
if err = insertFileAnnotationRefs(tx, fileAnnotationRefs); nil != err {
if err = insertFileAnnotationRefs(tx, fileAnnotationRefs); err != nil {
return
}
if 0 < len(spans) {
// 移除文档标签,否则会重复添加 https://github.com/siyuan-note/siyuan/issues/3723
if err = deleteSpansByRootID(tx, tree.Root.ID); nil != err {
if err = deleteSpansByRootID(tx, tree.Root.ID); err != nil {
return
}
if err = insertSpans(tx, spans); nil != err {
if err = insertSpans(tx, spans); err != nil {
return
}
}
if err = insertAssets(tx, assets); nil != err {
if err = insertAssets(tx, assets); err != nil {
return
}
if err = insertAttributes(tx, attributes); nil != err {
if err = insertAttributes(tx, attributes); err != nil {
return
}
return
@ -511,17 +511,17 @@ func getIndexIgnoreLines() (ret []string) {
IndexIgnoreCached = true
indexIgnorePath := filepath.Join(util.DataDir, ".siyuan", "indexignore")
err := os.MkdirAll(filepath.Dir(indexIgnorePath), 0755)
if nil != err {
if err != nil {
return
}
if !gulu.File.IsExist(indexIgnorePath) {
if err = gulu.File.WriteFileSafer(indexIgnorePath, nil, 0644); nil != err {
if err = gulu.File.WriteFileSafer(indexIgnorePath, nil, 0644); err != nil {
logging.LogErrorf("create indexignore [%s] failed: %s", indexIgnorePath, err)
return
}
}
data, err := os.ReadFile(indexIgnorePath)
if nil != err {
if err != nil {
logging.LogErrorf("read indexignore [%s] failed: %s", indexIgnorePath, err)
return
}