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 System struct {
|
||||
ID string
|
||||
|
||||
@@ -10,29 +17,101 @@ type System struct {
|
||||
Bugs string
|
||||
Changelog string
|
||||
Version string
|
||||
Socket bool
|
||||
Manifest string
|
||||
Download string
|
||||
Protected bool
|
||||
Exclusive bool
|
||||
PersistentStorage bool
|
||||
Background string
|
||||
PrimaryTokenAttribute string
|
||||
Availability int
|
||||
Socket bool
|
||||
Protected bool
|
||||
Exclusive bool
|
||||
PersistentStorage bool
|
||||
Locked bool
|
||||
Owned bool
|
||||
HasStorage bool
|
||||
Esmodules []string
|
||||
Scripts []string
|
||||
Tags []string
|
||||
Compatibility Compatibility
|
||||
Relationships Relationships
|
||||
DocumentTypes DocumentTypes
|
||||
Grid Grid
|
||||
Authors []Author
|
||||
Media []Media
|
||||
Packs []Pack
|
||||
Styles []Style
|
||||
Languages []Language
|
||||
PackFolders []Folder
|
||||
Grid *Grid
|
||||
Esmodules []string
|
||||
Scripts []string
|
||||
Tags []string
|
||||
Authors []*Author
|
||||
Media []*Media
|
||||
Packs []*Pack
|
||||
Styles []*Style
|
||||
Languages []*Language
|
||||
PackFolders []*Folder
|
||||
}
|
||||
|
||||
func (s *System) Query(data *InsertId[string]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO system (%s, id, title, description, url, license, bugs, changelog, version, manifest,
|
||||
download, background, primary_token_attribute, availability, 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 (s *System) InsertObjects(tx *sqlx.Tx) error {
|
||||
relId := &InsertId[string]{id: s.ID, fieldName: "system_id"}
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
|
||||
InsertWithCtxParallel(syncDB, tx, s.Compatibility, relId)
|
||||
InsertWithCtxParallel(syncDB, tx, s.Relationships, relId)
|
||||
InsertWithCtxParallel(syncDB, tx, s.DocumentTypes, relId)
|
||||
InsertWithCtxParallel(syncDB, tx, s.Grid, relId)
|
||||
|
||||
esModulesRelId := &InsertId[string]{id: s.ID, fieldName: "system_id", tableName: "es_modules"}
|
||||
InsertSimpleSliceParallel(syncDB, tx, s.Esmodules, esModulesRelId)
|
||||
scriptRelId := &InsertId[string]{id: s.ID, fieldName: "system_id", tableName: "scripts"}
|
||||
InsertSimpleSliceParallel(syncDB, tx, s.Scripts, scriptRelId)
|
||||
tagsRelId := &InsertId[string]{id: s.ID, fieldName: "system_id", tableName: "tags"}
|
||||
InsertSimpleSliceParallel(syncDB, tx, s.Tags, tagsRelId)
|
||||
|
||||
InsertSliceParallel(syncDB, tx, s.Authors, relId)
|
||||
InsertSliceParallel(syncDB, tx, s.Media, relId)
|
||||
InsertSliceParallel(syncDB, tx, s.Styles, relId)
|
||||
InsertSliceParallel(syncDB, tx, s.Languages, relId)
|
||||
InsertSliceParallel(syncDB, tx, s.Packs, relId)
|
||||
InsertSliceParallel(syncDB, tx, s.PackFolders, relId)
|
||||
|
||||
return syncDB.Wait()
|
||||
}
|
||||
|
||||
func (s *System) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, s.ID, s.Title, s.Description, s.URL, s.License, s.Bugs,
|
||||
s.Changelog, s.Version, s.Manifest, s.Download, s.Background,
|
||||
s.PrimaryTokenAttribute, s.Availability, s.Socket, s.Protected, s.Exclusive,
|
||||
s.PersistentStorage, s.Locked, s.Owned, s.HasStorage}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.InsertObjects(tx)
|
||||
}
|
||||
|
||||
func (s *System) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, s.ID, s.Title, s.Description, s.URL, s.License, s.Bugs,
|
||||
s.Changelog, s.Version, s.Manifest, s.Download, s.Background,
|
||||
s.PrimaryTokenAttribute, s.Availability, s.Socket, s.Protected, s.Exclusive,
|
||||
s.PersistentStorage, s.Locked, s.Owned, s.HasStorage}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.InsertObjects(tx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user