history.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package sql
  17. import (
  18. "database/sql"
  19. "errors"
  20. "fmt"
  21. "strings"
  22. "github.com/siyuan-note/eventbus"
  23. "github.com/siyuan-note/logging"
  24. )
  25. type History struct {
  26. ID string
  27. Type int
  28. Op string
  29. Title string
  30. Content string
  31. Created string
  32. Path string
  33. }
  34. func QueryHistory(stmt string) (ret []map[string]interface{}, err error) {
  35. ret = []map[string]interface{}{}
  36. rows, err := queryHistory(stmt)
  37. if nil != err {
  38. logging.LogWarnf("sql query [%s] failed: %s", stmt, err)
  39. return
  40. }
  41. defer rows.Close()
  42. cols, _ := rows.Columns()
  43. if nil == cols {
  44. return
  45. }
  46. for rows.Next() {
  47. columns := make([]interface{}, len(cols))
  48. columnPointers := make([]interface{}, len(cols))
  49. for i := range columns {
  50. columnPointers[i] = &columns[i]
  51. }
  52. if err = rows.Scan(columnPointers...); nil != err {
  53. return
  54. }
  55. m := make(map[string]interface{})
  56. for i, colName := range cols {
  57. val := columnPointers[i].(*interface{})
  58. m[colName] = *val
  59. }
  60. ret = append(ret, m)
  61. }
  62. return
  63. }
  64. func SelectHistoriesRawStmt(stmt string) (ret []*History) {
  65. rows, err := historyDB.Query(stmt)
  66. if nil != err {
  67. logging.LogWarnf("sql query [%s] failed: %s", stmt, err)
  68. return
  69. }
  70. defer rows.Close()
  71. for rows.Next() {
  72. if history := scanHistoryRows(rows); nil != history {
  73. ret = append(ret, history)
  74. }
  75. }
  76. return
  77. }
  78. func scanHistoryRows(rows *sql.Rows) (ret *History) {
  79. var history History
  80. if err := rows.Scan(&history.ID, &history.Type, &history.Op, &history.Title, &history.Content, &history.Path, &history.Created); nil != err {
  81. logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())
  82. return
  83. }
  84. ret = &history
  85. return
  86. }
  87. func queryHistory(query string, args ...interface{}) (*sql.Rows, error) {
  88. query = strings.TrimSpace(query)
  89. if "" == query {
  90. return nil, errors.New("statement is empty")
  91. }
  92. return historyDB.Query(query, args...)
  93. }
  94. func deleteOutdatedHistories(tx *sql.Tx, before string, context map[string]interface{}) (err error) {
  95. stmt := "DELETE FROM histories_fts_case_insensitive WHERE created < ?"
  96. if err = execStmtTx(tx, stmt, before); nil != err {
  97. return
  98. }
  99. return
  100. }
  101. const (
  102. HistoriesFTSCaseInsensitiveInsert = "INSERT INTO histories_fts_case_insensitive (id, type, op, title, content, path, created) VALUES %s"
  103. HistoriesPlaceholder = "(?, ?, ?, ?, ?, ?, ?)"
  104. )
  105. func insertHistories(tx *sql.Tx, histories []*History, context map[string]interface{}) (err error) {
  106. if 1 > len(histories) {
  107. return
  108. }
  109. var bulk []*History
  110. for _, history := range histories {
  111. bulk = append(bulk, history)
  112. if 512 > len(bulk) {
  113. continue
  114. }
  115. if err = insertHistories0(tx, bulk, context); nil != err {
  116. return
  117. }
  118. bulk = []*History{}
  119. }
  120. if 0 < len(bulk) {
  121. if err = insertHistories0(tx, bulk, context); nil != err {
  122. return
  123. }
  124. }
  125. return
  126. }
  127. func insertHistories0(tx *sql.Tx, bulk []*History, context map[string]interface{}) (err error) {
  128. valueStrings := make([]string, 0, len(bulk))
  129. valueArgs := make([]interface{}, 0, len(bulk)*strings.Count(HistoriesPlaceholder, "?"))
  130. for _, b := range bulk {
  131. valueStrings = append(valueStrings, HistoriesPlaceholder)
  132. valueArgs = append(valueArgs, b.ID)
  133. valueArgs = append(valueArgs, b.Type)
  134. valueArgs = append(valueArgs, b.Op)
  135. valueArgs = append(valueArgs, b.Title)
  136. valueArgs = append(valueArgs, b.Content)
  137. valueArgs = append(valueArgs, b.Path)
  138. valueArgs = append(valueArgs, b.Created)
  139. }
  140. stmt := fmt.Sprintf(HistoriesFTSCaseInsensitiveInsert, strings.Join(valueStrings, ","))
  141. if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
  142. return
  143. }
  144. eventbus.Publish(eventbus.EvtSQLInsertHistory, context)
  145. return
  146. }