memo.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package sqlite
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/pkg/errors"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. storepb "github.com/usememos/memos/proto/gen/store"
  9. "github.com/usememos/memos/store"
  10. )
  11. func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
  12. fields := []string{"`uid`", "`creator_id`", "`content`", "`visibility`", "`payload`"}
  13. placeholder := []string{"?", "?", "?", "?", "?"}
  14. payload := "{}"
  15. if create.Payload != nil {
  16. payloadBytes, err := protojson.Marshal(create.Payload)
  17. if err != nil {
  18. return nil, err
  19. }
  20. payload = string(payloadBytes)
  21. }
  22. args := []any{create.UID, create.CreatorID, create.Content, create.Visibility, payload}
  23. stmt := "INSERT INTO `memo` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`, `row_status`"
  24. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  25. &create.ID,
  26. &create.CreatedTs,
  27. &create.UpdatedTs,
  28. &create.RowStatus,
  29. ); err != nil {
  30. return nil, err
  31. }
  32. return create, nil
  33. }
  34. func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
  35. where, args := []string{"1 = 1"}, []any{}
  36. if v := find.ID; v != nil {
  37. where, args = append(where, "`memo`.`id` = ?"), append(args, *v)
  38. }
  39. if v := find.UID; v != nil {
  40. where, args = append(where, "`memo`.`uid` = ?"), append(args, *v)
  41. }
  42. if v := find.CreatorID; v != nil {
  43. where, args = append(where, "`memo`.`creator_id` = ?"), append(args, *v)
  44. }
  45. if v := find.RowStatus; v != nil {
  46. where, args = append(where, "`memo`.`row_status` = ?"), append(args, *v)
  47. }
  48. if v := find.CreatedTsBefore; v != nil {
  49. where, args = append(where, "`memo`.`created_ts` < ?"), append(args, *v)
  50. }
  51. if v := find.CreatedTsAfter; v != nil {
  52. where, args = append(where, "`memo`.`created_ts` > ?"), append(args, *v)
  53. }
  54. if v := find.UpdatedTsBefore; v != nil {
  55. where, args = append(where, "`memo`.`updated_ts` < ?"), append(args, *v)
  56. }
  57. if v := find.UpdatedTsAfter; v != nil {
  58. where, args = append(where, "`memo`.`updated_ts` > ?"), append(args, *v)
  59. }
  60. if v := find.ContentSearch; len(v) != 0 {
  61. for _, s := range v {
  62. where, args = append(where, "`memo`.`content` LIKE ?"), append(args, fmt.Sprintf("%%%s%%", s))
  63. }
  64. }
  65. if v := find.VisibilityList; len(v) != 0 {
  66. placeholder := []string{}
  67. for _, visibility := range v {
  68. placeholder = append(placeholder, "?")
  69. args = append(args, visibility.String())
  70. }
  71. where = append(where, fmt.Sprintf("`memo`.`visibility` IN (%s)", strings.Join(placeholder, ",")))
  72. }
  73. if v := find.PayloadFind; v != nil {
  74. if v.Raw != nil {
  75. where, args = append(where, "`memo`.`payload` = ?"), append(args, *v.Raw)
  76. }
  77. if len(v.TagSearch) != 0 {
  78. for _, tag := range v.TagSearch {
  79. where, args = append(where, "(JSON_EXTRACT(`memo`.`payload`, '$.property.tags') LIKE ? OR JSON_EXTRACT(`memo`.`payload`, '$.property.tags') LIKE ?)"), append(args, fmt.Sprintf(`%%"%s"%%`, tag), fmt.Sprintf(`%%"%s/%%`, tag))
  80. }
  81. }
  82. if v.HasLink {
  83. where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasLink') IS TRUE")
  84. }
  85. if v.HasTaskList {
  86. where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') IS TRUE")
  87. }
  88. if v.HasCode {
  89. where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasCode') IS TRUE")
  90. }
  91. if v.HasIncompleteTasks {
  92. where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasIncompleteTasks') IS TRUE")
  93. }
  94. }
  95. if find.ExcludeComments {
  96. where = append(where, "`parent_id` IS NULL")
  97. }
  98. orderBy := []string{}
  99. if find.OrderByPinned {
  100. orderBy = append(orderBy, "`pinned` DESC")
  101. }
  102. order := "DESC"
  103. if find.OrderByTimeAsc {
  104. order = "ASC"
  105. }
  106. if find.OrderByUpdatedTs {
  107. orderBy = append(orderBy, "`updated_ts` "+order)
  108. } else {
  109. orderBy = append(orderBy, "`created_ts` "+order)
  110. }
  111. orderBy = append(orderBy, "`id` "+order)
  112. if find.Random {
  113. orderBy = []string{"RANDOM()"}
  114. }
  115. fields := []string{
  116. "`memo`.`id` AS `id`",
  117. "`memo`.`uid` AS `uid`",
  118. "`memo`.`creator_id` AS `creator_id`",
  119. "`memo`.`created_ts` AS `created_ts`",
  120. "`memo`.`updated_ts` AS `updated_ts`",
  121. "`memo`.`row_status` AS `row_status`",
  122. "`memo`.`visibility` AS `visibility`",
  123. "`memo`.`payload` AS `payload`",
  124. "IFNULL(`memo_organizer`.`pinned`, 0) AS `pinned`",
  125. "`memo_relation`.`related_memo_id` AS `parent_id`",
  126. }
  127. if !find.ExcludeContent {
  128. fields = append(fields, "`memo`.`content` AS `content`")
  129. }
  130. query := "SELECT " + strings.Join(fields, ", ") + "FROM `memo` " +
  131. "LEFT JOIN `memo_organizer` ON `memo`.`id` = `memo_organizer`.`memo_id` AND `memo`.`creator_id` = `memo_organizer`.`user_id` " +
  132. "LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` = \"COMMENT\" " +
  133. "WHERE " + strings.Join(where, " AND ") + " " +
  134. "ORDER BY " + strings.Join(orderBy, ", ")
  135. if find.Limit != nil {
  136. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  137. if find.Offset != nil {
  138. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  139. }
  140. }
  141. rows, err := d.db.QueryContext(ctx, query, args...)
  142. if err != nil {
  143. return nil, err
  144. }
  145. defer rows.Close()
  146. list := make([]*store.Memo, 0)
  147. for rows.Next() {
  148. var memo store.Memo
  149. var payloadBytes []byte
  150. dests := []any{
  151. &memo.ID,
  152. &memo.UID,
  153. &memo.CreatorID,
  154. &memo.CreatedTs,
  155. &memo.UpdatedTs,
  156. &memo.RowStatus,
  157. &memo.Visibility,
  158. &payloadBytes,
  159. &memo.Pinned,
  160. &memo.ParentID,
  161. }
  162. if !find.ExcludeContent {
  163. dests = append(dests, &memo.Content)
  164. }
  165. if err := rows.Scan(dests...); err != nil {
  166. return nil, err
  167. }
  168. payload := &storepb.MemoPayload{}
  169. if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
  170. return nil, errors.Wrap(err, "failed to unmarshal payload")
  171. }
  172. memo.Payload = payload
  173. list = append(list, &memo)
  174. }
  175. if err := rows.Err(); err != nil {
  176. return nil, err
  177. }
  178. return list, nil
  179. }
  180. func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
  181. set, args := []string{}, []any{}
  182. if v := update.UID; v != nil {
  183. set, args = append(set, "`uid` = ?"), append(args, *v)
  184. }
  185. if v := update.CreatedTs; v != nil {
  186. set, args = append(set, "`created_ts` = ?"), append(args, *v)
  187. }
  188. if v := update.UpdatedTs; v != nil {
  189. set, args = append(set, "`updated_ts` = ?"), append(args, *v)
  190. }
  191. if v := update.RowStatus; v != nil {
  192. set, args = append(set, "`row_status` = ?"), append(args, *v)
  193. }
  194. if v := update.Content; v != nil {
  195. set, args = append(set, "`content` = ?"), append(args, *v)
  196. }
  197. if v := update.Visibility; v != nil {
  198. set, args = append(set, "`visibility` = ?"), append(args, *v)
  199. }
  200. if v := update.Payload; v != nil {
  201. payloadBytes, err := protojson.Marshal(v)
  202. if err != nil {
  203. return err
  204. }
  205. set, args = append(set, "`payload` = ?"), append(args, string(payloadBytes))
  206. }
  207. args = append(args, update.ID)
  208. stmt := "UPDATE `memo` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  209. if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
  210. return err
  211. }
  212. return nil
  213. }
  214. func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
  215. where, args := []string{"`id` = ?"}, []any{delete.ID}
  216. stmt := "DELETE FROM `memo` WHERE " + strings.Join(where, " AND ")
  217. result, err := d.db.ExecContext(ctx, stmt, args...)
  218. if err != nil {
  219. return err
  220. }
  221. if _, err := result.RowsAffected(); err != nil {
  222. return err
  223. }
  224. return nil
  225. }