2023-09-28 22:09:52 +08:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
|
2023-10-08 18:30:24 +08:00
|
|
|
fields := []string{"`name`", "`type`", "`config`"}
|
|
|
|
placeholder := []string{"?", "?", "?"}
|
|
|
|
args := []any{create.Name, create.Type, create.Config}
|
|
|
|
|
|
|
|
if create.ID != 0 {
|
|
|
|
fields = append(fields, "`id`")
|
|
|
|
placeholder = append(placeholder, "?")
|
|
|
|
args = append(args, create.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt := "INSERT INTO `storage` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
|
|
|
|
result, err := d.db.ExecContext(ctx, stmt, args...)
|
2023-09-28 22:09:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := result.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
create.ID = int32(id)
|
|
|
|
return create, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
|
2023-09-28 22:09:52 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
if find.ID != nil {
|
2023-10-07 22:56:12 +08:00
|
|
|
where, args = append(where, "`id` = ?"), append(args, *find.ID)
|
2023-09-28 22:09:52 +08:00
|
|
|
}
|
|
|
|
|
2023-10-07 22:56:12 +08:00
|
|
|
rows, err := d.db.QueryContext(ctx, "SELECT `id`, `name`, `type`, `config` FROM `storage` WHERE "+strings.Join(where, " AND ")+" ORDER BY `id` DESC",
|
2023-09-28 22:09:52 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
|
2023-09-28 22:09:52 +08:00
|
|
|
set, args := []string{}, []any{}
|
|
|
|
if update.Name != nil {
|
2023-10-07 22:56:12 +08:00
|
|
|
set = append(set, "`name` = ?")
|
2023-09-28 22:09:52 +08:00
|
|
|
args = append(args, *update.Name)
|
|
|
|
}
|
|
|
|
if update.Config != nil {
|
2023-10-07 22:56:12 +08:00
|
|
|
set = append(set, "`config` = ?")
|
2023-09-28 22:09:52 +08:00
|
|
|
args = append(args, *update.Config)
|
|
|
|
}
|
|
|
|
args = append(args, update.ID)
|
|
|
|
|
2023-10-07 22:56:12 +08:00
|
|
|
stmt := "UPDATE `storage` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
|
2023-09-28 22:09:52 +08:00
|
|
|
_, err := d.db.ExecContext(ctx, stmt, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := &store.Storage{}
|
2023-10-07 22:56:12 +08:00
|
|
|
stmt = "SELECT `id`, `name`, `type`, `config` FROM `storage` WHERE `id` = ?"
|
2023-09-28 22:09:52 +08:00
|
|
|
if err := d.db.QueryRowContext(ctx, stmt, update.ID).Scan(
|
|
|
|
&storage.ID,
|
|
|
|
&storage.Name,
|
|
|
|
&storage.Type,
|
|
|
|
&storage.Config,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
|
2023-10-07 22:56:12 +08:00
|
|
|
stmt := "DELETE FROM `storage` WHERE `id` = ?"
|
2023-09-28 22:09:52 +08:00
|
|
|
result, err := d.db.ExecContext(ctx, stmt, delete.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := result.RowsAffected(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|