2023-09-26 19:07:14 +08:00
|
|
|
package sqlite
|
2023-09-26 17:16:58 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2024-01-29 22:43:40 +08:00
|
|
|
func (d *DB) UpsertWorkspaceSetting(ctx context.Context, upsert *store.WorkspaceSetting) (*store.WorkspaceSetting, error) {
|
2023-09-26 17:16:58 +08:00
|
|
|
stmt := `
|
|
|
|
INSERT INTO system_setting (
|
|
|
|
name, value, description
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT(name) DO UPDATE
|
|
|
|
SET
|
|
|
|
value = EXCLUDED.value,
|
|
|
|
description = EXCLUDED.description
|
|
|
|
`
|
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upsert, nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 22:43:40 +08:00
|
|
|
func (d *DB) ListWorkspaceSettings(ctx context.Context, find *store.FindWorkspaceSetting) ([]*store.WorkspaceSetting, error) {
|
2023-09-26 17:16:58 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
if find.Name != "" {
|
|
|
|
where, args = append(where, "name = ?"), append(args, find.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
description
|
|
|
|
FROM system_setting
|
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
|
|
|
|
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2024-01-29 22:43:40 +08:00
|
|
|
list := []*store.WorkspaceSetting{}
|
2023-09-26 17:16:58 +08:00
|
|
|
for rows.Next() {
|
2024-01-29 22:43:40 +08:00
|
|
|
systemSettingMessage := &store.WorkspaceSetting{}
|
2023-09-26 17:16:58 +08:00
|
|
|
if err := rows.Scan(
|
|
|
|
&systemSettingMessage.Name,
|
|
|
|
&systemSettingMessage.Value,
|
|
|
|
&systemSettingMessage.Description,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list = append(list, systemSettingMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|