fix errors on insertion to game, fix db creation of game
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type Ring struct {
|
||||
ID uint
|
||||
|
||||
@@ -9,6 +19,57 @@ type Ring struct {
|
||||
Subject Subject
|
||||
}
|
||||
|
||||
func (r *Ring) Query(data *InsertId[uint]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO ring (%s, enabled, effects)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (r *Ring) InsertObjects(tx *sqlx.Tx) error {
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
relId := InsertId[uint]{id: r.ID, fieldName: "ring_id"}
|
||||
InsertWithCtxParallel(group, ctx, tx, r.RingColors, relId)
|
||||
InsertWithCtxParallel(group, ctx, tx, r.Subject, relId)
|
||||
|
||||
err := group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Ring) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.Enabled, r.Effects}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Ring) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.Enabled, r.Effects}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type RingColors struct {
|
||||
ID uint
|
||||
|
||||
@@ -16,9 +77,83 @@ type RingColors struct {
|
||||
Background string
|
||||
}
|
||||
|
||||
func (r RingColors) Query(data *InsertId[uint]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO ring_colors (%s, ring, background)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (r RingColors) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.Ring, r.Background}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r RingColors) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.Ring, r.Background}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Subject struct {
|
||||
ID uint
|
||||
|
||||
Scale int
|
||||
Texture string
|
||||
}
|
||||
|
||||
func (s Subject) Query(data *InsertId[uint]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO ring_colors (%s, scale, texture)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (s Subject) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, s.Scale, s.Texture}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&s.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Subject) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, s.Scale, s.Texture}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&s.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user