finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
154
internal/foundry/models/db/language.go
Normal file
154
internal/foundry/models/db/language.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type SetupLanguage struct {
|
||||
ID string
|
||||
|
||||
Label string
|
||||
Modules []SetupLanguageModule
|
||||
}
|
||||
|
||||
func (l *SetupLanguage) Query(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO setup_language (setup_id, label)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
func (l *SetupLanguage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Label}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, _ := errgroup.WithContext(context.Background())
|
||||
InsertSliceParallel(group, tx, l.Modules, InsertId[string]{id: l.ID})
|
||||
|
||||
return group.Wait()
|
||||
}
|
||||
|
||||
func (l *SetupLanguage) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Label}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, _ := errgroup.WithContext(context.Background())
|
||||
InsertSliceParallel(group, tx, l.Modules, InsertId[string]{id: l.ID})
|
||||
|
||||
err = group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type SetupLanguageModule struct {
|
||||
ID string
|
||||
|
||||
Label string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (l SetupLanguageModule) Query(data *InsertId[string]) {
|
||||
data.query = `
|
||||
INSERT INTO setup_language_module (setup_language_id, id, label, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
func (l SetupLanguageModule) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.ID, l.Label, l.Path}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l SetupLanguageModule) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.ID, l.Label, l.Path}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
ID uint
|
||||
|
||||
Lang string
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (l *Language) Query(data *InsertId[string]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO language (%s, lang, name, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (l *Language) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Lang, l.Name, l.Path}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Language) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Lang, l.Name, l.Path}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user