storage.go 2.6 KB

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