finish setup data insertion, add world insertion

This commit is contained in:
lbenedar
2026-04-22 18:18:43 +03:00
parent 21abe68858
commit f46ff8333e
66 changed files with 2257 additions and 676 deletions

View File

@@ -1,5 +1,15 @@
package db
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
"golang.org/x/sync/errgroup"
)
type Setting struct {
ID string
@@ -7,3 +17,52 @@ type Setting struct {
Value string
Stats Stats
}
func (s *Setting) Query(data *InsertId[uint]) {
data.query = fmt.Sprintf(`
INSERT INTO setting (%s, id, key_, value)
VALUES ($1, $2, $3, $4)`, data.fieldName)
}
func (s *Setting) InsertObjects(tx *sqlx.Tx) error {
group, ctx := errgroup.WithContext(context.Background())
relId := InsertId[string]{id: s.ID, fieldName: "setting_id"}
InsertWithCtxParallel(group, ctx, tx, s.Stats, relId)
err := group.Wait()
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
return nil
}
func (s *Setting) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, s.ID, s.Key, s.Value}
_, err := tx.Exec(data.query, args...)
if err != nil {
return err
}
return s.InsertObjects(tx)
}
func (s *Setting) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, s.ID, s.Key, s.Value}
_, err := tx.ExecContext(ctx, data.query, args...)
if err != nil {
return err
}
return s.InsertObjects(tx)
}