memo.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/pkg/errors"
  8. "github.com/usememos/memos/store"
  9. )
  10. func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
  11. fields := []string{"resource_name", "creator_id", "content", "visibility"}
  12. args := []any{create.ResourceName, create.CreatorID, create.Content, create.Visibility}
  13. stmt := "INSERT INTO memo (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts, row_status"
  14. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  15. &create.ID,
  16. &create.CreatedTs,
  17. &create.UpdatedTs,
  18. &create.RowStatus,
  19. ); err != nil {
  20. return nil, err
  21. }
  22. return create, nil
  23. }
  24. func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
  25. where, args := []string{"1 = 1"}, []any{}
  26. if v := find.ID; v != nil {
  27. where, args = append(where, "memo.id = "+placeholder(len(args)+1)), append(args, *v)
  28. }
  29. if v := find.ResourceName; v != nil {
  30. where, args = append(where, "memo.resource_name = "+placeholder(len(args)+1)), append(args, *v)
  31. }
  32. if v := find.CreatorID; v != nil {
  33. where, args = append(where, "memo.creator_id = "+placeholder(len(args)+1)), append(args, *v)
  34. }
  35. if v := find.RowStatus; v != nil {
  36. where, args = append(where, "memo.row_status = "+placeholder(len(args)+1)), append(args, *v)
  37. }
  38. if v := find.CreatedTsBefore; v != nil {
  39. where, args = append(where, "memo.created_ts < "+placeholder(len(args)+1)), append(args, *v)
  40. }
  41. if v := find.CreatedTsAfter; v != nil {
  42. where, args = append(where, "memo.created_ts > "+placeholder(len(args)+1)), append(args, *v)
  43. }
  44. if v := find.UpdatedTsBefore; v != nil {
  45. where, args = append(where, "memo.updated_ts < "+placeholder(len(args)+1)), append(args, *v)
  46. }
  47. if v := find.UpdatedTsAfter; v != nil {
  48. where, args = append(where, "memo.updated_ts > "+placeholder(len(args)+1)), append(args, *v)
  49. }
  50. if v := find.ContentSearch; len(v) != 0 {
  51. for _, s := range v {
  52. where, args = append(where, "memo.content LIKE "+placeholder(len(args)+1)), append(args, fmt.Sprintf("%%%s%%", s))
  53. }
  54. }
  55. if v := find.VisibilityList; len(v) != 0 {
  56. holders := []string{}
  57. for _, visibility := range v {
  58. holders = append(holders, placeholder(len(args)+1))
  59. args = append(args, visibility.String())
  60. }
  61. where = append(where, fmt.Sprintf("memo.visibility in (%s)", strings.Join(holders, ", ")))
  62. }
  63. if find.ExcludeComments {
  64. where = append(where, "memo_relation.related_memo_id IS NULL")
  65. }
  66. orders := []string{}
  67. if find.OrderByPinned {
  68. orders = append(orders, "pinned DESC")
  69. }
  70. if find.OrderByUpdatedTs {
  71. orders = append(orders, "updated_ts DESC")
  72. } else {
  73. orders = append(orders, "created_ts DESC")
  74. }
  75. orders = append(orders, "id DESC")
  76. fields := []string{
  77. `memo.id AS id`,
  78. `memo.resource_name AS resource_name`,
  79. `memo.creator_id AS creator_id`,
  80. `memo.created_ts AS created_ts`,
  81. `memo.updated_ts AS updated_ts`,
  82. `memo.row_status AS row_status`,
  83. `memo.visibility AS visibility`,
  84. `COALESCE(memo_organizer.pinned, 0) AS pinned`,
  85. `memo_relation.related_memo_id AS parent_id`,
  86. }
  87. if !find.ExcludeContent {
  88. fields = append(fields, `memo.content AS content`)
  89. }
  90. query := `SELECT ` + strings.Join(fields, ", ") + `
  91. FROM memo
  92. LEFT JOIN memo_organizer ON memo.id = memo_organizer.memo_id AND memo.creator_id = memo_organizer.user_id
  93. LEFT JOIN memo_relation ON memo.id = memo_relation.memo_id AND memo_relation.type = 'COMMENT'
  94. WHERE ` + strings.Join(where, " AND ") + `
  95. ORDER BY ` + strings.Join(orders, ", ")
  96. if find.Limit != nil {
  97. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  98. if find.Offset != nil {
  99. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  100. }
  101. }
  102. rows, err := d.db.QueryContext(ctx, query, args...)
  103. if err != nil {
  104. return nil, err
  105. }
  106. defer rows.Close()
  107. list := make([]*store.Memo, 0)
  108. for rows.Next() {
  109. var memo store.Memo
  110. dests := []any{
  111. &memo.ID,
  112. &memo.ResourceName,
  113. &memo.CreatorID,
  114. &memo.CreatedTs,
  115. &memo.UpdatedTs,
  116. &memo.RowStatus,
  117. &memo.Visibility,
  118. &memo.Pinned,
  119. &memo.ParentID,
  120. }
  121. if !find.ExcludeContent {
  122. dests = append(dests, &memo.Content)
  123. }
  124. if err := rows.Scan(dests...); err != nil {
  125. return nil, err
  126. }
  127. list = append(list, &memo)
  128. }
  129. if err := rows.Err(); err != nil {
  130. return nil, err
  131. }
  132. return list, nil
  133. }
  134. func (d *DB) GetMemo(ctx context.Context, find *store.FindMemo) (*store.Memo, error) {
  135. list, err := d.ListMemos(ctx, find)
  136. if err != nil {
  137. return nil, err
  138. }
  139. if len(list) == 0 {
  140. return nil, nil
  141. }
  142. memo := list[0]
  143. return memo, nil
  144. }
  145. func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
  146. set, args := []string{}, []any{}
  147. if v := update.ResourceName; v != nil {
  148. set, args = append(set, "resource_name = "+placeholder(len(args)+1)), append(args, *v)
  149. }
  150. if v := update.CreatedTs; v != nil {
  151. set, args = append(set, "created_ts = "+placeholder(len(args)+1)), append(args, *v)
  152. }
  153. if v := update.UpdatedTs; v != nil {
  154. set, args = append(set, "updated_ts = "+placeholder(len(args)+1)), append(args, *v)
  155. }
  156. if v := update.RowStatus; v != nil {
  157. set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, *v)
  158. }
  159. if v := update.Content; v != nil {
  160. set, args = append(set, "content = "+placeholder(len(args)+1)), append(args, *v)
  161. }
  162. if v := update.Visibility; v != nil {
  163. set, args = append(set, "visibility = "+placeholder(len(args)+1)), append(args, *v)
  164. }
  165. stmt := `UPDATE memo SET ` + strings.Join(set, ", ") + ` WHERE id = ` + placeholder(len(args)+1)
  166. args = append(args, update.ID)
  167. if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
  168. return err
  169. }
  170. return nil
  171. }
  172. func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
  173. where, args := []string{"id = " + placeholder(1)}, []any{delete.ID}
  174. stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
  175. result, err := d.db.ExecContext(ctx, stmt, args...)
  176. if err != nil {
  177. return errors.Wrap(err, "failed to delete memo")
  178. }
  179. if _, err := result.RowsAffected(); err != nil {
  180. return err
  181. }
  182. return nil
  183. }
  184. func vacuumMemo(ctx context.Context, tx *sql.Tx) error {
  185. stmt := `DELETE FROM memo WHERE creator_id NOT IN (SELECT id FROM "user")`
  186. _, err := tx.ExecContext(ctx, stmt)
  187. return err
  188. }