asset_content.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "fmt"
  20. "strings"
  21. "github.com/siyuan-note/eventbus"
  22. )
  23. type AssetContent struct {
  24. ID string
  25. Name string
  26. Ext string
  27. Path string
  28. Size int64
  29. Updated int64
  30. Content string
  31. }
  32. const (
  33. AssetContentsFTSCaseInsensitiveInsert = "INSERT INTO asset_contents_fts_case_insensitive (id, name, ext, path, size, updated, content) VALUES %s"
  34. AssetContentsPlaceholder = "(?, ?, ?, ?, ?, ?, ?)"
  35. )
  36. func insertAssetContents(tx *sql.Tx, assetContents []*AssetContent, context map[string]interface{}) (err error) {
  37. if 1 > len(assetContents) {
  38. return
  39. }
  40. var bulk []*AssetContent
  41. for _, assetContent := range assetContents {
  42. bulk = append(bulk, assetContent)
  43. if 512 > len(bulk) {
  44. continue
  45. }
  46. if err = insertAssetContents0(tx, bulk, context); nil != err {
  47. return
  48. }
  49. bulk = []*AssetContent{}
  50. }
  51. if 0 < len(bulk) {
  52. if err = insertAssetContents0(tx, bulk, context); nil != err {
  53. return
  54. }
  55. }
  56. return
  57. }
  58. func insertAssetContents0(tx *sql.Tx, bulk []*AssetContent, context map[string]interface{}) (err error) {
  59. valueStrings := make([]string, 0, len(bulk))
  60. valueArgs := make([]interface{}, 0, len(bulk)*strings.Count(AssetContentsPlaceholder, "?"))
  61. for _, b := range bulk {
  62. valueStrings = append(valueStrings, AssetContentsPlaceholder)
  63. valueArgs = append(valueArgs, b.ID)
  64. valueArgs = append(valueArgs, b.Name)
  65. valueArgs = append(valueArgs, b.Ext)
  66. valueArgs = append(valueArgs, b.Path)
  67. valueArgs = append(valueArgs, b.Size)
  68. valueArgs = append(valueArgs, b.Updated)
  69. valueArgs = append(valueArgs, b.Content)
  70. }
  71. stmt := fmt.Sprintf(AssetContentsFTSCaseInsensitiveInsert, strings.Join(valueStrings, ","))
  72. if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
  73. return
  74. }
  75. eventbus.Publish(eventbus.EvtSQLInsertAssetContent, context)
  76. return
  77. }
  78. func deleteAssetContentsByPath(tx *sql.Tx, path string, context map[string]interface{}) (err error) {
  79. stmt := "DELETE FROM asset_contents_fts_case_insensitive WHERE path = ?"
  80. if err = execStmtTx(tx, stmt, path); nil != err {
  81. return
  82. }
  83. return
  84. }