78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
type Actor struct {
|
|
ID string
|
|
|
|
Img string
|
|
Name string
|
|
Type string
|
|
Folder string
|
|
Sort int
|
|
PrototypeToken *Token
|
|
Stats Stats
|
|
Items []*Item
|
|
Ownership []OwnershipString
|
|
}
|
|
|
|
func (a *Actor) Query(data *InsertId[uint]) {
|
|
data.query = fmt.Sprintf(`
|
|
INSERT INTO actor (%s, id, img, name, type, folder, sort)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)`, data.fieldName)
|
|
}
|
|
|
|
func (a *Actor) InsertObjects(tx *sqlx.Tx) error {
|
|
group, ctx := errgroup.WithContext(context.Background())
|
|
|
|
relId := InsertId[string]{id: a.ID, fieldName: "actor_id"}
|
|
InsertWithCtxParallel(group, ctx, tx, a.PrototypeToken, relId)
|
|
InsertWithCtxParallel(group, ctx, tx, a.Stats, relId)
|
|
InsertSliceParallel(group, tx, a.Ownership, relId)
|
|
InsertSliceParallel(group, tx, a.Items, relId)
|
|
|
|
err := group.Wait()
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *Actor) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, a.ID, a.Img, a.Name, a.Type, a.Folder, a.Sort}
|
|
|
|
_, err := tx.Exec(data.query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return a.InsertObjects(tx)
|
|
}
|
|
|
|
func (a *Actor) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, a.ID, a.Img, a.Name, a.Type, a.Folder, a.Sort}
|
|
|
|
_, err := tx.ExecContext(ctx, data.query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return a.InsertObjects(tx)
|
|
}
|