storage.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package mysql
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/usememos/memos/store"
  6. )
  7. func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
  8. fields := []string{"`name`", "`type`", "`config`"}
  9. placeholder := []string{"?", "?", "?"}
  10. args := []any{create.Name, create.Type, create.Config}
  11. if create.ID != 0 {
  12. fields = append(fields, "`id`")
  13. placeholder = append(placeholder, "?")
  14. args = append(args, create.ID)
  15. }
  16. stmt := "INSERT INTO `storage` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
  17. result, err := d.db.ExecContext(ctx, stmt, args...)
  18. if err != nil {
  19. return nil, err
  20. }
  21. id, err := result.LastInsertId()
  22. if err != nil {
  23. return nil, err
  24. }
  25. create.ID = int32(id)
  26. return create, nil
  27. }
  28. func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
  29. where, args := []string{"1 = 1"}, []any{}
  30. if find.ID != nil {
  31. where, args = append(where, "`id` = ?"), append(args, *find.ID)
  32. }
  33. rows, err := d.db.QueryContext(ctx, "SELECT `id`, `name`, `type`, `config` FROM `storage` WHERE "+strings.Join(where, " AND ")+" ORDER BY `id` DESC",
  34. args...,
  35. )
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer rows.Close()
  40. list := []*store.Storage{}
  41. for rows.Next() {
  42. storage := &store.Storage{}
  43. if err := rows.Scan(
  44. &storage.ID,
  45. &storage.Name,
  46. &storage.Type,
  47. &storage.Config,
  48. ); err != nil {
  49. return nil, err
  50. }
  51. list = append(list, storage)
  52. }
  53. if err := rows.Err(); err != nil {
  54. return nil, err
  55. }
  56. return list, nil
  57. }
  58. func (d *DB) GetStorage(ctx context.Context, find *store.FindStorage) (*store.Storage, error) {
  59. list, err := d.ListStorages(ctx, find)
  60. if err != nil {
  61. return nil, err
  62. }
  63. if len(list) == 0 {
  64. return nil, nil
  65. }
  66. return list[0], nil
  67. }
  68. func (d *DB) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
  69. set, args := []string{}, []any{}
  70. if update.Name != nil {
  71. set = append(set, "`name` = ?")
  72. args = append(args, *update.Name)
  73. }
  74. if update.Config != nil {
  75. set = append(set, "`config` = ?")
  76. args = append(args, *update.Config)
  77. }
  78. args = append(args, update.ID)
  79. stmt := "UPDATE `storage` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  80. _, err := d.db.ExecContext(ctx, stmt, args...)
  81. if err != nil {
  82. return nil, err
  83. }
  84. storage := &store.Storage{}
  85. stmt = "SELECT `id`, `name`, `type`, `config` FROM `storage` WHERE `id` = ?"
  86. if err := d.db.QueryRowContext(ctx, stmt, update.ID).Scan(
  87. &storage.ID,
  88. &storage.Name,
  89. &storage.Type,
  90. &storage.Config,
  91. ); err != nil {
  92. return nil, err
  93. }
  94. return storage, nil
  95. }
  96. func (d *DB) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
  97. stmt := "DELETE FROM `storage` WHERE `id` = ?"
  98. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  99. if err != nil {
  100. return err
  101. }
  102. if _, err := result.RowsAffected(); err != nil {
  103. return err
  104. }
  105. return nil
  106. }