resource.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.MemoID; v != nil {
  49. where, args = append(where, "`memo_id` = ?"), append(args, *v)
  50. }
  51. if find.HasRelatedMemo {
  52. where = append(where, "`memo_id` IS NOT NULL")
  53. }
  54. if find.StorageType != nil {
  55. where, args = append(where, "`storage_type` = ?"), append(args, find.StorageType.String())
  56. }
  57. fields := []string{"`id`", "`uid`", "`filename`", "`type`", "`size`", "`creator_id`", "`created_ts`", "`updated_ts`", "`memo_id`", "`storage_type`", "`reference`", "`payload`"}
  58. if find.GetBlob {
  59. fields = append(fields, "`blob`")
  60. }
  61. query := fmt.Sprintf("SELECT %s FROM `resource` WHERE %s ORDER BY `updated_ts` DESC", strings.Join(fields, ", "), strings.Join(where, " AND "))
  62. if find.Limit != nil {
  63. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  64. if find.Offset != nil {
  65. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  66. }
  67. }
  68. rows, err := d.db.QueryContext(ctx, query, args...)
  69. if err != nil {
  70. return nil, err
  71. }
  72. defer rows.Close()
  73. list := make([]*store.Resource, 0)
  74. for rows.Next() {
  75. resource := store.Resource{}
  76. var memoID sql.NullInt32
  77. var storageType string
  78. var payloadBytes []byte
  79. dests := []any{
  80. &resource.ID,
  81. &resource.UID,
  82. &resource.Filename,
  83. &resource.Type,
  84. &resource.Size,
  85. &resource.CreatorID,
  86. &resource.CreatedTs,
  87. &resource.UpdatedTs,
  88. &memoID,
  89. &storageType,
  90. &resource.Reference,
  91. &payloadBytes,
  92. }
  93. if find.GetBlob {
  94. dests = append(dests, &resource.Blob)
  95. }
  96. if err := rows.Scan(dests...); err != nil {
  97. return nil, err
  98. }
  99. if memoID.Valid {
  100. resource.MemoID = &memoID.Int32
  101. }
  102. resource.StorageType = storepb.ResourceStorageType(storepb.ResourceStorageType_value[storageType])
  103. payload := &storepb.ResourcePayload{}
  104. if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
  105. return nil, err
  106. }
  107. resource.Payload = payload
  108. list = append(list, &resource)
  109. }
  110. if err := rows.Err(); err != nil {
  111. return nil, err
  112. }
  113. return list, nil
  114. }
  115. func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) error {
  116. set, args := []string{}, []any{}
  117. if v := update.UID; v != nil {
  118. set, args = append(set, "`uid` = ?"), append(args, *v)
  119. }
  120. if v := update.UpdatedTs; v != nil {
  121. set, args = append(set, "`updated_ts` = ?"), append(args, *v)
  122. }
  123. if v := update.Filename; v != nil {
  124. set, args = append(set, "`filename` = ?"), append(args, *v)
  125. }
  126. if v := update.MemoID; v != nil {
  127. set, args = append(set, "`memo_id` = ?"), append(args, *v)
  128. }
  129. if v := update.Reference; v != nil {
  130. set, args = append(set, "`reference` = ?"), append(args, *v)
  131. }
  132. if v := update.Payload; v != nil {
  133. bytes, err := protojson.Marshal(v)
  134. if err != nil {
  135. return errors.Wrap(err, "failed to marshal resource payload")
  136. }
  137. set, args = append(set, "`payload` = ?"), append(args, string(bytes))
  138. }
  139. args = append(args, update.ID)
  140. stmt := "UPDATE `resource` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  141. result, err := d.db.ExecContext(ctx, stmt, args...)
  142. if err != nil {
  143. return errors.Wrap(err, "failed to update resource")
  144. }
  145. if _, err := result.RowsAffected(); err != nil {
  146. return err
  147. }
  148. return nil
  149. }
  150. func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
  151. stmt := "DELETE FROM `resource` WHERE `id` = ?"
  152. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  153. if err != nil {
  154. return err
  155. }
  156. if _, err := result.RowsAffected(); err != nil {
  157. return err
  158. }
  159. return nil
  160. }