136 lines
3.5 KiB
Go
136 lines
3.5 KiB
Go
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}
|
|
|
|
mutex := GetMutex("relationships_insert")
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
|
|
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, _ := errgroup.WithContext(context.Background())
|
|
InsertWithCtxParallel(group, 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, _ := errgroup.WithContext(context.Background())
|
|
InsertWithCtxParallel(group, tx, r.Compatibility, InsertId[string]{id: strconv.FormatUint(uint64(r.ID), 10), fieldName: fmt.Sprintf("%s_id", data.tableName)})
|
|
|
|
return group.Wait()
|
|
}
|