memo_relation.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package postgres
  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 := `
  10. INSERT INTO memo_relation (
  11. memo_id,
  12. related_memo_id,
  13. type
  14. )
  15. VALUES (` + placeholders(3) + `)
  16. RETURNING memo_id, related_memo_id, type
  17. `
  18. memoRelation := &store.MemoRelation{}
  19. if err := d.db.QueryRowContext(
  20. ctx,
  21. stmt,
  22. create.MemoID,
  23. create.RelatedMemoID,
  24. create.Type,
  25. ).Scan(
  26. &memoRelation.MemoID,
  27. &memoRelation.RelatedMemoID,
  28. &memoRelation.Type,
  29. ); err != nil {
  30. return nil, err
  31. }
  32. return memoRelation, nil
  33. }
  34. func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
  35. where, args := []string{"1 = 1"}, []any{}
  36. if find.MemoID != nil {
  37. where, args = append(where, "memo_id = "+placeholder(len(args)+1)), append(args, find.MemoID)
  38. }
  39. if find.RelatedMemoID != nil {
  40. where, args = append(where, "related_memo_id = "+placeholder(len(args)+1)), append(args, find.RelatedMemoID)
  41. }
  42. if find.Type != nil {
  43. where, args = append(where, "type = "+placeholder(len(args)+1)), append(args, find.Type)
  44. }
  45. rows, err := d.db.QueryContext(ctx, `
  46. SELECT
  47. memo_id,
  48. related_memo_id,
  49. type
  50. FROM memo_relation
  51. WHERE `+strings.Join(where, " AND "), args...)
  52. if err != nil {
  53. return nil, err
  54. }
  55. defer rows.Close()
  56. list := []*store.MemoRelation{}
  57. for rows.Next() {
  58. memoRelation := &store.MemoRelation{}
  59. if err := rows.Scan(
  60. &memoRelation.MemoID,
  61. &memoRelation.RelatedMemoID,
  62. &memoRelation.Type,
  63. ); err != nil {
  64. return nil, err
  65. }
  66. list = append(list, memoRelation)
  67. }
  68. if err := rows.Err(); err != nil {
  69. return nil, err
  70. }
  71. return list, nil
  72. }
  73. func (d *DB) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
  74. where, args := []string{"1 = 1"}, []any{}
  75. if delete.MemoID != nil {
  76. where, args = append(where, "memo_id = "+placeholder(len(args)+1)), append(args, delete.MemoID)
  77. }
  78. if delete.RelatedMemoID != nil {
  79. where, args = append(where, "related_memo_id = "+placeholder(len(args)+1)), append(args, delete.RelatedMemoID)
  80. }
  81. if delete.Type != nil {
  82. where, args = append(where, "type = "+placeholder(len(args)+1)), append(args, delete.Type)
  83. }
  84. stmt := `DELETE FROM memo_relation WHERE ` + strings.Join(where, " AND ")
  85. result, err := d.db.ExecContext(ctx, stmt, args...)
  86. if err != nil {
  87. return err
  88. }
  89. if _, err = result.RowsAffected(); err != nil {
  90. return err
  91. }
  92. return nil
  93. }
  94. func vacuumMemoRelations(ctx context.Context, tx *sql.Tx) error {
  95. if _, err := tx.ExecContext(ctx, `
  96. DELETE FROM memo_relation
  97. WHERE memo_id NOT IN (SELECT id FROM memo) OR related_memo_id NOT IN (SELECT id FROM memo)
  98. `); err != nil {
  99. return err
  100. }
  101. return nil
  102. }