finish world initialization, set up init on startup and on shutdown, move all models to main directory

This commit is contained in:
lbenedar
2026-04-24 18:04:35 +03:00
parent 07025afefc
commit fd65631be1
125 changed files with 1534 additions and 2802 deletions

View File

@@ -0,0 +1,113 @@
package db
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
"golang.org/x/sync/errgroup"
)
type Files struct {
ID uint
Storages []FilesStorage
}
func (f *Files) Query(data *InsertId[uint]) {
data.query = fmt.Sprintf(`
INSERT INTO files (%s)
VALUES ($1)
RETURNING id`, data.fieldName)
}
func (f *Files) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id}
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
group, _ := errgroup.WithContext(context.Background())
InsertSliceParallel(group, tx, f.Storages, InsertId[uint]{id: f.ID})
err = group.Wait()
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
return nil
}
func (f *Files) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id}
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
group, _ := errgroup.WithContext(context.Background())
InsertSliceParallel(group, tx, f.Storages, InsertId[uint]{id: f.ID})
err = group.Wait()
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
return nil
}
type FilesStorage struct {
ID uint
Storage string
}
func (f FilesStorage) Query(data *InsertId[uint]) {
data.query = `
INSERT INTO files_storage (files_id, storage)
VALUES ($1, $2)
RETURNING id`
}
func (f FilesStorage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, f.Storage}
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
return nil
}
func (f FilesStorage) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, f.Storage}
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
return nil
}