finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
131
internal/foundry/models/db/relationships.go
Normal file
131
internal/foundry/models/db/relationships.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type Relationships struct {
|
||||
ID uint
|
||||
|
||||
Systems []RelationshipsData
|
||||
Requires []RelationshipsData
|
||||
Recommends []RelationshipsData
|
||||
Conflicts []RelationshipsData
|
||||
}
|
||||
|
||||
func (r Relationships) Query(data *InsertId[string]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO relationships (%s)
|
||||
VALUES ($1)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (r Relationships) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, _ := errgroup.WithContext(context.Background())
|
||||
|
||||
InsertSliceParallel(group, tx, r.Systems, InsertId[uint]{id: r.ID, tableName: "relationships_systems"})
|
||||
InsertSliceParallel(group, tx, r.Requires, InsertId[uint]{id: r.ID, tableName: "relationships_requires"})
|
||||
InsertSliceParallel(group, tx, r.Recommends, InsertId[uint]{id: r.ID, tableName: "relationships_recommends"})
|
||||
InsertSliceParallel(group, tx, r.Conflicts, InsertId[uint]{id: r.ID, tableName: "relationships_conflicts"})
|
||||
|
||||
err = group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Relationships) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, _ := errgroup.WithContext(context.Background())
|
||||
|
||||
InsertSliceParallel(group, tx, r.Systems, InsertId[uint]{id: r.ID, tableName: "relationships_systems"})
|
||||
InsertSliceParallel(group, tx, r.Requires, InsertId[uint]{id: r.ID, tableName: "relationships_requires"})
|
||||
InsertSliceParallel(group, tx, r.Recommends, InsertId[uint]{id: r.ID, tableName: "relationships_recommends"})
|
||||
InsertSliceParallel(group, tx, r.Conflicts, InsertId[uint]{id: r.ID, tableName: "relationships_conflicts"})
|
||||
|
||||
err = group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type RelationshipsData struct {
|
||||
ID uint
|
||||
|
||||
Key string
|
||||
Type string
|
||||
Manifest string
|
||||
Compatibility Compatibility
|
||||
}
|
||||
|
||||
func (r RelationshipsData) Query(data *InsertId[uint]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO %s (relationships_id, key_, type, manifest)
|
||||
VALUES ($1, $2, $3, $4)`, data.tableName)
|
||||
}
|
||||
|
||||
func (r RelationshipsData) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.Key, r.Type, r.Manifest}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
InsertWithCtxParallel(group, ctx, tx, r.Compatibility, InsertId[string]{id: strconv.FormatUint(uint64(r.ID), 10), fieldName: fmt.Sprintf("%s_id", data.tableName)})
|
||||
|
||||
return group.Wait()
|
||||
}
|
||||
|
||||
func (r RelationshipsData) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.Key, r.Type, r.Manifest}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
InsertWithCtxParallel(group, ctx, tx, r.Compatibility, InsertId[string]{id: strconv.FormatUint(uint64(r.ID), 10), fieldName: fmt.Sprintf("%s_id", data.tableName)})
|
||||
|
||||
return group.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user