finish setup insert method, refactor sql migration file
This commit is contained in:
@@ -1,20 +1,16 @@
|
||||
package db
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type World struct {
|
||||
ID string
|
||||
|
||||
Socket bool
|
||||
Protected bool
|
||||
Exclusive bool
|
||||
PersistentStorage bool
|
||||
Locked bool
|
||||
Owned bool
|
||||
HasStorage bool
|
||||
Playtime int
|
||||
Availability int
|
||||
NextSession time.Time
|
||||
Title string
|
||||
Description string
|
||||
Version string
|
||||
@@ -24,15 +20,93 @@ type World struct {
|
||||
CoreVersion string
|
||||
SystemVersion string
|
||||
LastPlayed string
|
||||
Playtime int
|
||||
Availability int
|
||||
NextSession time.Time
|
||||
Socket bool
|
||||
Protected bool
|
||||
Exclusive bool
|
||||
PersistentStorage bool
|
||||
Locked bool
|
||||
Owned bool
|
||||
HasStorage bool
|
||||
Compatibility Compatibility
|
||||
Relationships Relationships
|
||||
Tags []string
|
||||
Scripts []string
|
||||
Esmodules []string
|
||||
Compatibility Compatibility
|
||||
Relationships Relationships
|
||||
Authors []Author
|
||||
Media []Media
|
||||
Styles []Style
|
||||
Languages []Language
|
||||
Packs []Pack
|
||||
PackFolders []Folder
|
||||
Authors []*Author
|
||||
Media []*Media
|
||||
Styles []*Style
|
||||
Languages []*Language
|
||||
Packs []*Pack
|
||||
PackFolders []*Folder
|
||||
}
|
||||
|
||||
func (w *World) Query(data *InsertId[string]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO world (%s, id, title, description, version, system, background, join_theme,
|
||||
core_version, system_version, last_played, playtime, availability, next_session, socket,
|
||||
protected, exclusive_, persistent_storage, locked, owned, has_storage)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)`,
|
||||
data.fieldName)
|
||||
}
|
||||
|
||||
func (w *World) InsertObjects(tx *sqlx.Tx) error {
|
||||
relId := &InsertId[string]{id: w.ID, fieldName: "world_id"}
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
|
||||
InsertWithCtxParallel(syncDB, tx, w.Compatibility, relId)
|
||||
InsertWithCtxParallel(syncDB, tx, w.Relationships, relId)
|
||||
|
||||
esModulesRelId := &InsertId[string]{id: w.ID, fieldName: "world_id", tableName: "es_modules"}
|
||||
InsertSimpleSliceParallel(syncDB, tx, w.Esmodules, esModulesRelId)
|
||||
scriptRelId := &InsertId[string]{id: w.ID, fieldName: "world_id", tableName: "scripts"}
|
||||
InsertSimpleSliceParallel(syncDB, tx, w.Scripts, scriptRelId)
|
||||
tagsRelId := &InsertId[string]{id: w.ID, fieldName: "world_id", tableName: "tags"}
|
||||
InsertSimpleSliceParallel(syncDB, tx, w.Tags, tagsRelId)
|
||||
|
||||
InsertSliceParallel(syncDB, tx, w.Authors, relId)
|
||||
InsertSliceParallel(syncDB, tx, w.Media, relId)
|
||||
InsertSliceParallel(syncDB, tx, w.Styles, relId)
|
||||
InsertSliceParallel(syncDB, tx, w.Languages, relId)
|
||||
InsertSliceParallel(syncDB, tx, w.Packs, relId)
|
||||
InsertSliceParallel(syncDB, tx, w.PackFolders, relId)
|
||||
|
||||
return syncDB.Wait()
|
||||
}
|
||||
|
||||
func (w *World) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, w.ID, w.Title, w.Description, w.Version, w.System, w.Background, w.JoinTheme,
|
||||
w.CoreVersion, w.SystemVersion, w.LastPlayed, w.Playtime, w.Availability, w.NextSession, w.Socket,
|
||||
w.Protected, w.Exclusive, w.PersistentStorage, w.Locked, w.Owned, w.HasStorage}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return w.InsertObjects(tx)
|
||||
}
|
||||
|
||||
func (w *World) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, w.ID, w.Title, w.Description, w.Version, w.System, w.Background, w.JoinTheme,
|
||||
w.CoreVersion, w.SystemVersion, w.LastPlayed, w.Playtime, w.Availability, w.NextSession, w.Socket,
|
||||
w.Protected, w.Exclusive, w.PersistentStorage, w.Locked, w.Owned, w.HasStorage}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return w.InsertObjects(tx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user