memo_relation.go 2.3 KB

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