storage.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package postgres
  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. args := []any{create.Name, create.Type, create.Config}
  10. stmt := "INSERT INTO storage (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id"
  11. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  12. &create.ID,
  13. ); err != nil {
  14. return nil, err
  15. }
  16. storage := create
  17. return storage, nil
  18. }
  19. func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
  20. where, args := []string{"1 = 1"}, []any{}
  21. if find.ID != nil {
  22. where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
  23. }
  24. rows, err := d.db.QueryContext(ctx, `
  25. SELECT
  26. id,
  27. name,
  28. type,
  29. config
  30. FROM storage
  31. WHERE `+strings.Join(where, " AND ")+`
  32. ORDER BY id DESC`,
  33. args...,
  34. )
  35. if err != nil {
  36. return nil, err
  37. }
  38. defer rows.Close()
  39. list := []*store.Storage{}
  40. for rows.Next() {
  41. storage := &store.Storage{}
  42. if err := rows.Scan(
  43. &storage.ID,
  44. &storage.Name,
  45. &storage.Type,
  46. &storage.Config,
  47. ); err != nil {
  48. return nil, err
  49. }
  50. list = append(list, storage)
  51. }
  52. if err := rows.Err(); err != nil {
  53. return nil, err
  54. }
  55. return list, nil
  56. }
  57. func (d *DB) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
  58. set, args := []string{}, []any{}
  59. if update.Name != nil {
  60. set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
  61. }
  62. if update.Config != nil {
  63. set, args = append(set, "config = "+placeholder(len(args)+1)), append(args, *update.Config)
  64. }
  65. stmt := `UPDATE storage SET ` + strings.Join(set, ", ") + ` WHERE id = ` + placeholder(len(args)+1) + ` RETURNING id, name, type, config`
  66. args = append(args, update.ID)
  67. storage := &store.Storage{}
  68. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  69. &storage.ID,
  70. &storage.Name,
  71. &storage.Type,
  72. &storage.Config,
  73. ); err != nil {
  74. return nil, err
  75. }
  76. return storage, nil
  77. }
  78. func (d *DB) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
  79. stmt := `DELETE FROM storage WHERE id = $1`
  80. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  81. if err != nil {
  82. return err
  83. }
  84. if _, err := result.RowsAffected(); err != nil {
  85. return err
  86. }
  87. return nil
  88. }