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

@@ -2,10 +2,13 @@ package db
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"github.com/jmoiron/sqlx"
"golang.org/x/sync/errgroup"
)
type Folder struct {
@@ -26,18 +29,21 @@ func (f *Folder) Query(data *InsertId[string]) {
}
func (f *Folder) InsertObjects(tx *sqlx.Tx) error {
relId := &InsertId[string]{
relId := InsertId[string]{
id: strconv.FormatUint(uint64(f.ID), 10),
fieldName: "folder_id",
tableName: "folder_packs",
}
syncDB := NewSyncDB()
defer close(syncDB.errChan)
group, _ := errgroup.WithContext(context.Background())
InsertSimpleSliceParallel(syncDB, tx, f.Packs, relId)
InsertSliceParallel(syncDB, tx, f.Folders, relId)
InsertSimpleSliceParallel(group, tx, f.Packs, &relId)
InsertSliceParallel(group, tx, f.Folders, relId)
return syncDB.Wait()
err := group.Wait()
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
return nil
}
func (f *Folder) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
@@ -47,7 +53,7 @@ func (f *Folder) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
args := []any{data.id, f.Name, f.Sorting, f.Color}
err := tx.QueryRow(data.query, args...).Scan(&f.ID)
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
@@ -62,7 +68,7 @@ func (f *Folder) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[stri
args := []any{data.id, f.Name, f.Sorting, f.Color}
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&f.ID)
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
@@ -81,5 +87,54 @@ type WorldFolder struct {
Color string
Sort int
Stats Stats
// Flags any `json:"flags,omitempty"`
}
func (w *WorldFolder) Query(data *InsertId[uint]) {
data.query = fmt.Sprintf(`
INSERT INTO world_folder (%s, id, name, type, folder, sorting, description, color, sort)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id`, data.fieldName)
}
func (w *WorldFolder) InsertObjects(tx *sqlx.Tx) error {
group, ctx := errgroup.WithContext(context.Background())
relId := InsertId[string]{id: w.ID, fieldName: "world_folder_id"}
InsertWithCtxParallel(group, ctx, tx, w.Stats, relId)
err := group.Wait()
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
return nil
}
func (w *WorldFolder) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, w.ID, w.Name, w.Type, w.Folder, w.Sorting, w.Description, w.Color, w.Sort}
_, err := tx.Exec(data.query, args...)
if err != nil {
return err
}
return w.InsertObjects(tx)
}
func (w *WorldFolder) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, w.ID, w.Name, w.Type, w.Folder, w.Sorting, w.Description, w.Color, w.Sort}
_, err := tx.ExecContext(ctx, data.query, args...)
if err != nil {
return err
}
return w.InsertObjects(tx)
}