memo_relation.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package mysql
  2. import (
  3. "context"
  4. "database/sql"
  5. "strings"
  6. "github.com/usememos/memos/store"
  7. )
  8. func (d *DB) UpsertMemoRelation(ctx context.Context, create *store.MemoRelation) (*store.MemoRelation, error) {
  9. stmt := "INSERT INTO `memo_relation` (`memo_id`, `related_memo_id`, `type`) VALUES (?, ?, ?)"
  10. _, err := d.db.ExecContext(
  11. ctx,
  12. stmt,
  13. create.MemoID,
  14. create.RelatedMemoID,
  15. create.Type,
  16. )
  17. if err != nil {
  18. return nil, err
  19. }
  20. memoRelation := store.MemoRelation{
  21. MemoID: create.MemoID,
  22. RelatedMemoID: create.RelatedMemoID,
  23. Type: create.Type,
  24. }
  25. return &memoRelation, nil
  26. }
  27. func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
  28. where, args := []string{"TRUE"}, []any{}
  29. if find.MemoID != nil {
  30. where, args = append(where, "`memo_id` = ?"), append(args, find.MemoID)
  31. }
  32. if find.RelatedMemoID != nil {
  33. where, args = append(where, "`related_memo_id` = ?"), append(args, find.RelatedMemoID)
  34. }
  35. if find.Type != nil {
  36. where, args = append(where, "`type` = ?"), append(args, find.Type)
  37. }
  38. rows, err := d.db.QueryContext(ctx, "SELECT `memo_id`, `related_memo_id`, `type` FROM `memo_relation` WHERE "+strings.Join(where, " AND "), args...)
  39. if err != nil {
  40. return nil, err
  41. }
  42. defer rows.Close()
  43. list := []*store.MemoRelation{}
  44. for rows.Next() {
  45. memoRelation := &store.MemoRelation{}
  46. if err := rows.Scan(
  47. &memoRelation.MemoID,
  48. &memoRelation.RelatedMemoID,
  49. &memoRelation.Type,
  50. ); err != nil {
  51. return nil, err
  52. }
  53. list = append(list, memoRelation)
  54. }
  55. if err := rows.Err(); err != nil {
  56. return nil, err
  57. }
  58. return list, nil
  59. }
  60. func (d *DB) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
  61. where, args := []string{"TRUE"}, []any{}
  62. if delete.MemoID != nil {
  63. where, args = append(where, "`memo_id` = ?"), append(args, delete.MemoID)
  64. }
  65. if delete.RelatedMemoID != nil {
  66. where, args = append(where, "`related_memo_id` = ?"), append(args, delete.RelatedMemoID)
  67. }
  68. if delete.Type != nil {
  69. where, args = append(where, "`type` = ?"), append(args, delete.Type)
  70. }
  71. stmt := "DELETE FROM `memo_relation` WHERE " + strings.Join(where, " AND ")
  72. result, err := d.db.ExecContext(ctx, stmt, args...)
  73. if err != nil {
  74. return err
  75. }
  76. if _, err = result.RowsAffected(); err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. func vacuumMemoRelations(ctx context.Context, tx *sql.Tx) error {
  82. if _, err := tx.ExecContext(ctx, "DELETE FROM `memo_relation` WHERE `memo_id` NOT IN (SELECT `id` FROM `memo`) OR `related_memo_id` NOT IN (SELECT `id` FROM `memo`)"); err != nil {
  83. return err
  84. }
  85. return nil
  86. }