fix: create storage without some attributes (#2358)

This commit is contained in:
Athurg Gooth 2023-10-08 18:30:24 +08:00 committed by GitHub
parent 7680be1a2f
commit 287f1beb90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View file

@ -8,8 +8,18 @@ import (
)
func (d *DB) 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)
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...)
if err != nil {
return nil, err
}

View file

@ -8,16 +8,18 @@ import (
)
func (d *DB) 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(
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, ", ") + ") RETURNING `id`"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID,
); err != nil {
return nil, err