storage.go 2.5 KB

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