block_ref.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "github.com/88250/lute/parse"
  20. )
  21. type Ref struct {
  22. ID string
  23. DefBlockID string
  24. DefBlockParentID string
  25. DefBlockRootID string
  26. DefBlockPath string
  27. BlockID string
  28. RootID string
  29. Box string
  30. Path string
  31. Content string
  32. Markdown string
  33. Type string
  34. }
  35. func upsertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
  36. if err = deleteRefsByPath(tx, tree.Box, tree.Path); nil != err {
  37. return
  38. }
  39. if err = deleteFileAnnotationRefsByPath(tx, tree.Box, tree.Path); nil != err {
  40. return
  41. }
  42. err = insertRefs(tx, tree)
  43. return
  44. }
  45. func deleteRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
  46. if err = deleteRefsByPath(tx, tree.Box, tree.Path); nil != err {
  47. return
  48. }
  49. if err = deleteFileAnnotationRefsByPath(tx, tree.Box, tree.Path); nil != err {
  50. return
  51. }
  52. return
  53. }
  54. func insertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
  55. refs, fileAnnotationRefs := refsFromTree(tree)
  56. if err = insertBlockRefs(tx, refs); nil != err {
  57. return
  58. }
  59. if err = insertFileAnnotationRefs(tx, fileAnnotationRefs); nil != err {
  60. return
  61. }
  62. return err
  63. }