finish setup insert method, refactor sql migration file
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Relationships struct {
|
||||
ID uint
|
||||
|
||||
@@ -9,11 +16,107 @@ type Relationships struct {
|
||||
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
|
||||
}
|
||||
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
|
||||
InsertSliceParallel(syncDB, tx, r.Systems, &InsertId[uint]{id: r.ID, tableName: "relationships_systems"})
|
||||
InsertSliceParallel(syncDB, tx, r.Requires, &InsertId[uint]{id: r.ID, tableName: "relationships_requires"})
|
||||
InsertSliceParallel(syncDB, tx, r.Recommends, &InsertId[uint]{id: r.ID, tableName: "relationships_recommends"})
|
||||
InsertSliceParallel(syncDB, tx, r.Conflicts, &InsertId[uint]{id: r.ID, tableName: "relationships_conflicts"})
|
||||
|
||||
return syncDB.Wait()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
|
||||
InsertSliceParallel(syncDB, tx, r.Systems, &InsertId[uint]{id: r.ID, tableName: "relationships_systems"})
|
||||
InsertSliceParallel(syncDB, tx, r.Requires, &InsertId[uint]{id: r.ID, tableName: "relationships_requires"})
|
||||
InsertSliceParallel(syncDB, tx, r.Recommends, &InsertId[uint]{id: r.ID, tableName: "relationships_recommends"})
|
||||
InsertSliceParallel(syncDB, tx, r.Conflicts, &InsertId[uint]{id: r.ID, tableName: "relationships_conflicts"})
|
||||
|
||||
return syncDB.Wait()
|
||||
}
|
||||
|
||||
type RelationshipsData struct {
|
||||
ID string
|
||||
|
||||
RelationshipsType string
|
||||
Type string
|
||||
Manifest string
|
||||
Compatibility Compatibility
|
||||
Type string
|
||||
Manifest string
|
||||
Compatibility Compatibility
|
||||
}
|
||||
|
||||
func (r RelationshipsData) Query(data *InsertId[uint]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO %s (relationships_id, id, type, manifest)
|
||||
VALUES ($1, $2, $3, $4, $5)`, data.tableName)
|
||||
}
|
||||
|
||||
func (r RelationshipsData) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.ID, r.Type, r.Manifest}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
InsertWithCtxParallel(syncDB, tx, r.Compatibility, &InsertId[string]{id: r.ID, fieldName: fmt.Sprintf("%s_id", data.tableName)})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r RelationshipsData) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, r.ID, r.Type, r.Manifest}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
InsertWithCtxParallel(syncDB, tx, r.Compatibility, &InsertId[string]{id: r.ID, fieldName: fmt.Sprintf("%s_id", data.tableName)})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user