mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
refactor: migrate storage to driver
This commit is contained in:
@ -36,4 +36,10 @@ type Driver interface {
|
|||||||
UpsertTag(ctx context.Context, upsert *Tag) (*Tag, error)
|
UpsertTag(ctx context.Context, upsert *Tag) (*Tag, error)
|
||||||
ListTags(ctx context.Context, find *FindTag) ([]*Tag, error)
|
ListTags(ctx context.Context, find *FindTag) ([]*Tag, error)
|
||||||
DeleteTag(ctx context.Context, delete *DeleteTag) error
|
DeleteTag(ctx context.Context, delete *DeleteTag) error
|
||||||
|
|
||||||
|
CreateStorage(ctx context.Context, create *Storage) (*Storage, error)
|
||||||
|
ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error)
|
||||||
|
GetStorage(ctx context.Context, find *FindStorage) (*Storage, error)
|
||||||
|
UpdateStorage(ctx context.Context, update *UpdateStorage) (*Storage, error)
|
||||||
|
DeleteStorage(ctx context.Context, delete *DeleteStorage) error
|
||||||
}
|
}
|
||||||
|
133
store/sqlite/storage.go
Normal file
133
store/sqlite/storage.go
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
package sqlite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (d *Driver) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
|
||||||
|
stmt := `
|
||||||
|
INSERT INTO storage (
|
||||||
|
name,
|
||||||
|
type,
|
||||||
|
config
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
RETURNING id
|
||||||
|
`
|
||||||
|
if err := d.db.QueryRowContext(ctx, stmt, create.Name, create.Type, create.Config).Scan(
|
||||||
|
&create.ID,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
storage := create
|
||||||
|
return storage, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Driver) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
|
||||||
|
where, args := []string{"1 = 1"}, []any{}
|
||||||
|
if find.ID != nil {
|
||||||
|
where, args = append(where, "id = ?"), append(args, *find.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := d.db.QueryContext(ctx, `
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
type,
|
||||||
|
config
|
||||||
|
FROM storage
|
||||||
|
WHERE `+strings.Join(where, " AND ")+`
|
||||||
|
ORDER BY id DESC`,
|
||||||
|
args...,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
list := []*store.Storage{}
|
||||||
|
for rows.Next() {
|
||||||
|
storage := &store.Storage{}
|
||||||
|
if err := rows.Scan(
|
||||||
|
&storage.ID,
|
||||||
|
&storage.Name,
|
||||||
|
&storage.Type,
|
||||||
|
&storage.Config,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
list = append(list, storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Driver) GetStorage(ctx context.Context, find *store.FindStorage) (*store.Storage, error) {
|
||||||
|
list, err := d.ListStorages(ctx, find)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(list) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return list[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Driver) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
|
||||||
|
set, args := []string{}, []any{}
|
||||||
|
if update.Name != nil {
|
||||||
|
set = append(set, "name = ?")
|
||||||
|
args = append(args, *update.Name)
|
||||||
|
}
|
||||||
|
if update.Config != nil {
|
||||||
|
set = append(set, "config = ?")
|
||||||
|
args = append(args, *update.Config)
|
||||||
|
}
|
||||||
|
args = append(args, update.ID)
|
||||||
|
|
||||||
|
stmt := `
|
||||||
|
UPDATE storage
|
||||||
|
SET ` + strings.Join(set, ", ") + `
|
||||||
|
WHERE id = ?
|
||||||
|
RETURNING
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
type,
|
||||||
|
config
|
||||||
|
`
|
||||||
|
storage := &store.Storage{}
|
||||||
|
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||||
|
&storage.ID,
|
||||||
|
&storage.Name,
|
||||||
|
&storage.Type,
|
||||||
|
&storage.Config,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return storage, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Driver) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
|
||||||
|
stmt := `
|
||||||
|
DELETE FROM storage
|
||||||
|
WHERE id = ?
|
||||||
|
`
|
||||||
|
result, err := d.db.ExecContext(ctx, stmt, delete.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := result.RowsAffected(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
106
store/storage.go
106
store/storage.go
@ -2,7 +2,6 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
@ -27,66 +26,11 @@ type DeleteStorage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) CreateStorage(ctx context.Context, create *Storage) (*Storage, error) {
|
func (s *Store) CreateStorage(ctx context.Context, create *Storage) (*Storage, error) {
|
||||||
stmt := `
|
return s.driver.CreateStorage(ctx, create)
|
||||||
INSERT INTO storage (
|
|
||||||
name,
|
|
||||||
type,
|
|
||||||
config
|
|
||||||
)
|
|
||||||
VALUES (?, ?, ?)
|
|
||||||
RETURNING id
|
|
||||||
`
|
|
||||||
if err := s.db.QueryRowContext(ctx, stmt, create.Name, create.Type, create.Config).Scan(
|
|
||||||
&create.ID,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
storage := create
|
|
||||||
return storage, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error) {
|
func (s *Store) ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error) {
|
||||||
where, args := []string{"1 = 1"}, []any{}
|
return s.driver.ListStorages(ctx, find)
|
||||||
if find.ID != nil {
|
|
||||||
where, args = append(where, "id = ?"), append(args, *find.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err := s.db.QueryContext(ctx, `
|
|
||||||
SELECT
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
type,
|
|
||||||
config
|
|
||||||
FROM storage
|
|
||||||
WHERE `+strings.Join(where, " AND ")+`
|
|
||||||
ORDER BY id DESC`,
|
|
||||||
args...,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
list := []*Storage{}
|
|
||||||
for rows.Next() {
|
|
||||||
storage := &Storage{}
|
|
||||||
if err := rows.Scan(
|
|
||||||
&storage.ID,
|
|
||||||
&storage.Name,
|
|
||||||
&storage.Type,
|
|
||||||
&storage.Config,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
list = append(list, storage)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return list, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*Storage, error) {
|
func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*Storage, error) {
|
||||||
@ -102,51 +46,9 @@ func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*Storage, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) UpdateStorage(ctx context.Context, update *UpdateStorage) (*Storage, error) {
|
func (s *Store) UpdateStorage(ctx context.Context, update *UpdateStorage) (*Storage, error) {
|
||||||
set, args := []string{}, []any{}
|
return s.driver.UpdateStorage(ctx, update)
|
||||||
if update.Name != nil {
|
|
||||||
set = append(set, "name = ?")
|
|
||||||
args = append(args, *update.Name)
|
|
||||||
}
|
|
||||||
if update.Config != nil {
|
|
||||||
set = append(set, "config = ?")
|
|
||||||
args = append(args, *update.Config)
|
|
||||||
}
|
|
||||||
args = append(args, update.ID)
|
|
||||||
|
|
||||||
stmt := `
|
|
||||||
UPDATE storage
|
|
||||||
SET ` + strings.Join(set, ", ") + `
|
|
||||||
WHERE id = ?
|
|
||||||
RETURNING
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
type,
|
|
||||||
config
|
|
||||||
`
|
|
||||||
storage := &Storage{}
|
|
||||||
if err := s.db.QueryRowContext(ctx, stmt, args...).Scan(
|
|
||||||
&storage.ID,
|
|
||||||
&storage.Name,
|
|
||||||
&storage.Type,
|
|
||||||
&storage.Config,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return storage, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) DeleteStorage(ctx context.Context, delete *DeleteStorage) error {
|
func (s *Store) DeleteStorage(ctx context.Context, delete *DeleteStorage) error {
|
||||||
stmt := `
|
return s.driver.DeleteStorage(ctx, delete)
|
||||||
DELETE FROM storage
|
|
||||||
WHERE id = ?
|
|
||||||
`
|
|
||||||
result, err := s.db.ExecContext(ctx, stmt, delete.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := result.RowsAffected(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user