130 lines
2.8 KiB
Go
130 lines
2.8 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
type Pack struct {
|
|
ID uint
|
|
|
|
Key string
|
|
Name string
|
|
Label string
|
|
Banner string
|
|
Path string
|
|
Type string
|
|
System string
|
|
PackageType string
|
|
PackageName string
|
|
Ownership Ownership
|
|
Index []*Index
|
|
Folders []*PackFolder
|
|
}
|
|
|
|
func (p *Pack) Query(data *InsertId[string]) {
|
|
data.query = fmt.Sprintf(`
|
|
INSERT INTO pack (%s, key_, name, label, banner, path, type, system, package_type, package_name)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
|
RETURNING id`, data.fieldName)
|
|
}
|
|
|
|
func (p *Pack) InsertObjects(tx *sqlx.Tx) error {
|
|
group, ctx := errgroup.WithContext(context.Background())
|
|
|
|
InsertWithCtxParallel(group, ctx, tx, p.Ownership,
|
|
InsertId[string]{id: strconv.FormatUint(uint64(p.ID), 10), fieldName: "pack_id"})
|
|
|
|
relId := InsertId[uint]{id: p.ID, fieldName: "pack_id"}
|
|
InsertSliceParallel(group, tx, p.Index, relId)
|
|
InsertSliceParallel(group, tx, p.Folders, relId)
|
|
|
|
err := group.Wait()
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Pack) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, p.Key, p.Name, p.Label, p.Banner, p.Path, p.Type, p.System, p.PackageType, p.PackageName}
|
|
|
|
err := tx.QueryRowx(data.query, args...).Scan(&p.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return p.InsertObjects(tx)
|
|
}
|
|
|
|
func (p *Pack) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, p.Key, p.Name, p.Label, p.Banner, p.Path, p.Type, p.System, p.PackageType, p.PackageName}
|
|
|
|
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&p.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return p.InsertObjects(tx)
|
|
}
|
|
|
|
type PackFolder struct {
|
|
ID string
|
|
|
|
Description string
|
|
Name string
|
|
Sorting string
|
|
Type string
|
|
Sort int
|
|
}
|
|
|
|
func (p *PackFolder) Query(data *InsertId[uint]) {
|
|
data.query = fmt.Sprintf(`
|
|
INSERT INTO pack_folder (%s, id, description, name, sorting, type, sort)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)`, data.fieldName)
|
|
}
|
|
|
|
func (p *PackFolder) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, p.ID, p.Description, p.Name, p.Sorting, p.Type, p.Sort}
|
|
|
|
_, err := tx.Exec(data.query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *PackFolder) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, p.ID, p.Description, p.Name, p.Sorting, p.Type, p.Sort}
|
|
|
|
_, err := tx.ExecContext(ctx, data.query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|