resource.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package sqlite
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/usememos/memos/store"
  8. )
  9. func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
  10. fields := []string{"`filename`", "`blob`", "`external_link`", "`type`", "`size`", "`creator_id`", "`internal_path`"}
  11. placeholder := []string{"?", "?", "?", "?", "?", "?", "?"}
  12. args := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath}
  13. if create.ID != 0 {
  14. fields = append(fields, "`id`")
  15. placeholder = append(placeholder, "?")
  16. args = append(args, create.ID)
  17. }
  18. if create.CreatedTs != 0 {
  19. fields = append(fields, "`created_ts`")
  20. placeholder = append(placeholder, "?")
  21. args = append(args, create.CreatedTs)
  22. }
  23. if create.UpdatedTs != 0 {
  24. fields = append(fields, "`updated_ts`")
  25. placeholder = append(placeholder, "?")
  26. args = append(args, create.UpdatedTs)
  27. }
  28. if create.MemoID != nil {
  29. fields = append(fields, "`memo_id`")
  30. placeholder = append(placeholder, "?")
  31. args = append(args, *create.MemoID)
  32. }
  33. stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`"
  34. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
  35. return nil, err
  36. }
  37. return create, nil
  38. }
  39. func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
  40. where, args := []string{"1 = 1"}, []any{}
  41. if v := find.ID; v != nil {
  42. where, args = append(where, "id = ?"), append(args, *v)
  43. }
  44. if v := find.CreatorID; v != nil {
  45. where, args = append(where, "creator_id = ?"), append(args, *v)
  46. }
  47. if v := find.Filename; v != nil {
  48. where, args = append(where, "filename = ?"), append(args, *v)
  49. }
  50. if v := find.MemoID; v != nil {
  51. where, args = append(where, "memo_id = ?"), append(args, *v)
  52. }
  53. if find.HasRelatedMemo {
  54. where = append(where, "memo_id IS NOT NULL")
  55. }
  56. fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "memo_id"}
  57. if find.GetBlob {
  58. fields = append(fields, "blob")
  59. }
  60. query := fmt.Sprintf(`
  61. SELECT
  62. %s
  63. FROM resource
  64. WHERE %s
  65. GROUP BY id
  66. ORDER BY created_ts DESC
  67. `, strings.Join(fields, ", "), strings.Join(where, " AND "))
  68. if find.Limit != nil {
  69. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  70. if find.Offset != nil {
  71. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  72. }
  73. }
  74. rows, err := d.db.QueryContext(ctx, query, args...)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer rows.Close()
  79. list := make([]*store.Resource, 0)
  80. for rows.Next() {
  81. resource := store.Resource{}
  82. var memoID sql.NullInt32
  83. dests := []any{
  84. &resource.ID,
  85. &resource.Filename,
  86. &resource.ExternalLink,
  87. &resource.Type,
  88. &resource.Size,
  89. &resource.CreatorID,
  90. &resource.CreatedTs,
  91. &resource.UpdatedTs,
  92. &resource.InternalPath,
  93. &memoID,
  94. }
  95. if find.GetBlob {
  96. dests = append(dests, &resource.Blob)
  97. }
  98. if err := rows.Scan(dests...); err != nil {
  99. return nil, err
  100. }
  101. if memoID.Valid {
  102. resource.MemoID = &memoID.Int32
  103. }
  104. list = append(list, &resource)
  105. }
  106. if err := rows.Err(); err != nil {
  107. return nil, err
  108. }
  109. return list, nil
  110. }
  111. func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) (*store.Resource, error) {
  112. set, args := []string{}, []any{}
  113. if v := update.UpdatedTs; v != nil {
  114. set, args = append(set, "updated_ts = ?"), append(args, *v)
  115. }
  116. if v := update.Filename; v != nil {
  117. set, args = append(set, "filename = ?"), append(args, *v)
  118. }
  119. if v := update.InternalPath; v != nil {
  120. set, args = append(set, "internal_path = ?"), append(args, *v)
  121. }
  122. if v := update.MemoID; v != nil {
  123. set, args = append(set, "memo_id = ?"), append(args, *v)
  124. }
  125. if v := update.Blob; v != nil {
  126. set, args = append(set, "blob = ?"), append(args, v)
  127. }
  128. args = append(args, update.ID)
  129. fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path"}
  130. stmt := `
  131. UPDATE resource
  132. SET ` + strings.Join(set, ", ") + `
  133. WHERE id = ?
  134. RETURNING ` + strings.Join(fields, ", ")
  135. resource := store.Resource{}
  136. dests := []any{
  137. &resource.ID,
  138. &resource.Filename,
  139. &resource.ExternalLink,
  140. &resource.Type,
  141. &resource.Size,
  142. &resource.CreatorID,
  143. &resource.CreatedTs,
  144. &resource.UpdatedTs,
  145. &resource.InternalPath,
  146. }
  147. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(dests...); err != nil {
  148. return nil, err
  149. }
  150. return &resource, nil
  151. }
  152. func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
  153. stmt := `
  154. DELETE FROM resource
  155. WHERE id = ?
  156. `
  157. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  158. if err != nil {
  159. return err
  160. }
  161. if _, err := result.RowsAffected(); err != nil {
  162. return err
  163. }
  164. if err := d.Vacuum(ctx); err != nil {
  165. // Prevent linter warning.
  166. return err
  167. }
  168. return nil
  169. }
  170. func vacuumResource(ctx context.Context, tx *sql.Tx) error {
  171. stmt := `
  172. DELETE FROM
  173. resource
  174. WHERE
  175. creator_id NOT IN (
  176. SELECT
  177. id
  178. FROM
  179. user
  180. )`
  181. _, err := tx.ExecContext(ctx, stmt)
  182. if err != nil {
  183. return err
  184. }
  185. return nil
  186. }