mirror of
https://github.com/usememos/memos.git
synced 2024-12-24 11:44:44 +03:00
c72f221fc0
* Rename checkDSN to checkDataDir * Add option to set DSN and db driver * Add mysql driver skeleton * Add mysql container in compose for debug * Add basic function for mysql driver * Cleanup go mod with tidy * Cleanup go.sum with tidy * Add DeleteUser support for mysql driver * Fix UpdateUser of mysql driver * Add DeleteTag support for mysql driver * Add DeleteResource support for mysql driver * Add UpdateMemo and DeleteMemo support for mysql driver * Add MemoRelation support for mysql driver * Add MemoOrganizer support for mysql driver * Add Idp support for mysql driver * Add Storage support for mysql driver * Add FindMemosVisibilityList support for mysql driver * Add Vacuum support for mysql driver * Add Migration support for mysql driver * Add Migration support for mysql driver * Fix ListMemo failed with referece * Change Activity.CreateTs type in MySQL * Change User.CreateTs type in MySQL * Fix by golangci-lint * Change Resource.CreateTs type in MySQL * Change MigrationHistory.CreateTs type in MySQL * Change Memo.CreateTs type in MySQL
138 lines
2.6 KiB
Go
138 lines
2.6 KiB
Go
package mysql
|
|
|
|
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 (?, ?, ?)
|
|
`
|
|
result, err := d.db.ExecContext(ctx, stmt, create.Name, create.Type, create.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
create.ID = int32(id)
|
|
return create, 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 = ?
|
|
`
|
|
_, err := d.db.ExecContext(ctx, stmt, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
storage := &store.Storage{}
|
|
stmt = `SELECT id,name,type,config FROM storage WHERE id = ?`
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|