Unverified Commit 287f1beb authored by Athurg Gooth's avatar Athurg Gooth Committed by GitHub

fix: create storage without some attributes (#2358)

parent 7680be1a
...@@ -8,8 +8,18 @@ import ( ...@@ -8,8 +8,18 @@ import (
) )
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) { func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
stmt := "INSERT INTO `storage` (`name`, `type`, `config`) VALUES (?, ?, ?)" fields := []string{"`name`", "`type`", "`config`"}
result, err := d.db.ExecContext(ctx, stmt, create.Name, create.Type, create.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 { if err != nil {
return nil, err return nil, err
} }
......
...@@ -8,16 +8,18 @@ import ( ...@@ -8,16 +8,18 @@ import (
) )
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) { func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
stmt := ` fields := []string{"`name`", "`type`", "`config`"}
INSERT INTO storage ( placeholder := []string{"?", "?", "?"}
name, args := []any{create.Name, create.Type, create.Config}
type,
config if create.ID != 0 {
) fields = append(fields, "`id`")
VALUES (?, ?, ?) placeholder = append(placeholder, "?")
RETURNING id args = append(args, create.ID)
` }
if err := d.db.QueryRowContext(ctx, stmt, create.Name, create.Type, create.Config).Scan(
stmt := "INSERT INTO `storage` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID, &create.ID,
); err != nil { ); err != nil {
return nil, err return nil, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment