resource.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package sqlite
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/pkg/errors"
  8. "google.golang.org/protobuf/encoding/protojson"
  9. storepb "github.com/usememos/memos/proto/gen/store"
  10. "github.com/usememos/memos/store"
  11. )
  12. func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
  13. fields := []string{"`uid`", "`filename`", "`blob`", "`type`", "`size`", "`creator_id`", "`memo_id`", "`storage_type`", "`reference`", "`payload`"}
  14. placeholder := []string{"?", "?", "?", "?", "?", "?", "?", "?", "?", "?"}
  15. storageType := ""
  16. if create.StorageType != storepb.ResourceStorageType_RESOURCE_STORAGE_TYPE_UNSPECIFIED {
  17. storageType = create.StorageType.String()
  18. }
  19. payloadString := "{}"
  20. if create.Payload != nil {
  21. bytes, err := protojson.Marshal(create.Payload)
  22. if err != nil {
  23. return nil, errors.Wrap(err, "failed to marshal resource payload")
  24. }
  25. payloadString = string(bytes)
  26. }
  27. args := []any{create.UID, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID, create.MemoID, storageType, create.Reference, payloadString}
  28. stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`"
  29. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
  30. return nil, err
  31. }
  32. return create, nil
  33. }
  34. func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
  35. where, args := []string{"1 = 1"}, []any{}
  36. if v := find.ID; v != nil {
  37. where, args = append(where, "`id` = ?"), append(args, *v)
  38. }
  39. if v := find.UID; v != nil {
  40. where, args = append(where, "`uid` = ?"), append(args, *v)
  41. }
  42. if v := find.CreatorID; v != nil {
  43. where, args = append(where, "`creator_id` = ?"), append(args, *v)
  44. }
  45. if v := find.Filename; v != nil {
  46. where, args = append(where, "`filename` = ?"), append(args, *v)
  47. }
  48. if v := find.FilenameSearch; v != nil {
  49. where, args = append(where, "`filename` LIKE ?"), append(args, fmt.Sprintf("%%%s%%", *v))
  50. }
  51. if v := find.MemoID; v != nil {
  52. where, args = append(where, "`memo_id` = ?"), append(args, *v)
  53. }
  54. if find.HasRelatedMemo {
  55. where = append(where, "`memo_id` IS NOT NULL")
  56. }
  57. if find.StorageType != nil {
  58. where, args = append(where, "`storage_type` = ?"), append(args, find.StorageType.String())
  59. }
  60. fields := []string{"`id`", "`uid`", "`filename`", "`type`", "`size`", "`creator_id`", "`created_ts`", "`updated_ts`", "`memo_id`", "`storage_type`", "`reference`", "`payload`"}
  61. if find.GetBlob {
  62. fields = append(fields, "`blob`")
  63. }
  64. query := fmt.Sprintf("SELECT %s FROM `resource` WHERE %s ORDER BY `updated_ts` DESC", strings.Join(fields, ", "), strings.Join(where, " AND "))
  65. if find.Limit != nil {
  66. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  67. if find.Offset != nil {
  68. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  69. }
  70. }
  71. rows, err := d.db.QueryContext(ctx, query, args...)
  72. if err != nil {
  73. return nil, err
  74. }
  75. defer rows.Close()
  76. list := make([]*store.Resource, 0)
  77. for rows.Next() {
  78. resource := store.Resource{}
  79. var memoID sql.NullInt32
  80. var storageType string
  81. var payloadBytes []byte
  82. dests := []any{
  83. &resource.ID,
  84. &resource.UID,
  85. &resource.Filename,
  86. &resource.Type,
  87. &resource.Size,
  88. &resource.CreatorID,
  89. &resource.CreatedTs,
  90. &resource.UpdatedTs,
  91. &memoID,
  92. &storageType,
  93. &resource.Reference,
  94. &payloadBytes,
  95. }
  96. if find.GetBlob {
  97. dests = append(dests, &resource.Blob)
  98. }
  99. if err := rows.Scan(dests...); err != nil {
  100. return nil, err
  101. }
  102. if memoID.Valid {
  103. resource.MemoID = &memoID.Int32
  104. }
  105. resource.StorageType = storepb.ResourceStorageType(storepb.ResourceStorageType_value[storageType])
  106. payload := &storepb.ResourcePayload{}
  107. if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
  108. return nil, err
  109. }
  110. resource.Payload = payload
  111. list = append(list, &resource)
  112. }
  113. if err := rows.Err(); err != nil {
  114. return nil, err
  115. }
  116. return list, nil
  117. }
  118. func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) error {
  119. set, args := []string{}, []any{}
  120. if v := update.UID; v != nil {
  121. set, args = append(set, "`uid` = ?"), append(args, *v)
  122. }
  123. if v := update.UpdatedTs; v != nil {
  124. set, args = append(set, "`updated_ts` = ?"), append(args, *v)
  125. }
  126. if v := update.Filename; v != nil {
  127. set, args = append(set, "`filename` = ?"), append(args, *v)
  128. }
  129. if v := update.MemoID; v != nil {
  130. set, args = append(set, "`memo_id` = ?"), append(args, *v)
  131. }
  132. if v := update.Reference; v != nil {
  133. set, args = append(set, "`reference` = ?"), append(args, *v)
  134. }
  135. if v := update.Payload; v != nil {
  136. bytes, err := protojson.Marshal(v)
  137. if err != nil {
  138. return errors.Wrap(err, "failed to marshal resource payload")
  139. }
  140. set, args = append(set, "`payload` = ?"), append(args, string(bytes))
  141. }
  142. args = append(args, update.ID)
  143. stmt := "UPDATE `resource` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  144. result, err := d.db.ExecContext(ctx, stmt, args...)
  145. if err != nil {
  146. return errors.Wrap(err, "failed to update resource")
  147. }
  148. if _, err := result.RowsAffected(); err != nil {
  149. return err
  150. }
  151. return nil
  152. }
  153. func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
  154. stmt := "DELETE FROM `resource` WHERE `id` = ?"
  155. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  156. if err != nil {
  157. return err
  158. }
  159. if _, err := result.RowsAffected(); err != nil {
  160. return err
  161. }
  162. return nil
  163. }