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,10 +1,70 @@
package db
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
"golang.org/x/sync/errgroup"
)
type Journal struct {
ID string
Name string
Sort int
Pages []JournalPage
Pages []*JournalPage
Ownership []OwnershipString
}
func (j *Journal) Query(data *InsertId[uint]) {
data.query = fmt.Sprintf(`
INSERT INTO journal (%s, id, name, sort)
VALUES ($1, $2, $3, $4)`, data.fieldName)
}
func (j *Journal) InsertObjects(tx *sqlx.Tx) error {
group, _ := errgroup.WithContext(context.Background())
relId := InsertId[string]{id: j.ID, fieldName: "journal_id"}
InsertSliceParallel(group, tx, j.Pages, relId)
InsertSliceParallel(group, tx, j.Ownership, relId)
err := group.Wait()
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
return nil
}
func (j *Journal) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, j.ID, j.Name, j.Sort}
_, err := tx.Exec(data.query, args...)
if err != nil {
return err
}
return j.InsertObjects(tx)
}
func (j *Journal) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, j.ID, j.Name, j.Sort}
_, err := tx.ExecContext(ctx, data.query, args...)
if err != nil {
return err
}
return j.InsertObjects(tx)
}