finish setup data insertion, add world insertion
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type Macro struct {
|
||||
ID string
|
||||
|
||||
@@ -14,3 +23,53 @@ type Macro struct {
|
||||
Stats Stats
|
||||
Ownership []OwnershipString
|
||||
}
|
||||
|
||||
func (m *Macro) Query(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO macro (game_id, id, command, name, type, img, author, scope, folder, sort)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
|
||||
}
|
||||
|
||||
func (m *Macro) InsertObjects(tx *sqlx.Tx) error {
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
relId := InsertId[string]{id: m.ID, fieldName: "macro_id"}
|
||||
InsertWithCtxParallel(group, ctx, tx, m.Stats, relId)
|
||||
InsertSliceParallel(group, tx, m.Ownership, relId)
|
||||
|
||||
err := group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Macro) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, m.ID, m.Command, m.Name, m.Type, m.Img, m.Author, m.Scope, m.Folder, m.Sort}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Macro) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, m.ID, m.Command, m.Name, m.Type, m.Img, m.Author, m.Scope, m.Folder, m.Sort}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user