finish setup insert method, refactor sql migration file
This commit is contained in:
@@ -2,18 +2,11 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Language struct {
|
||||
ID uint
|
||||
|
||||
Lang string
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
type SetupLanguage struct {
|
||||
ID string
|
||||
|
||||
@@ -21,38 +14,47 @@ type SetupLanguage struct {
|
||||
Modules []SetupLanguageModule
|
||||
}
|
||||
|
||||
func (l SetupLanguage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
const query = `
|
||||
func (l *SetupLanguage) Query(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO setup_language (setup_id, label)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
func (l *SetupLanguage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Label}
|
||||
|
||||
err := tx.QueryRowx(query, args...).Scan(&l.ID)
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
InsertSlice(tx, l.Modules, &InsertId[string]{id: l.ID})
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
InsertSliceParallel(syncDB, tx, l.Modules, &InsertId[string]{id: l.ID})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l SetupLanguage) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
const query = `
|
||||
INSERT INTO setup_language (setup_id, label)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`
|
||||
func (l *SetupLanguage) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Label}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, query, args...).Scan(&l.ID)
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
InsertSlice(tx, l.Modules, &InsertId[string]{id: l.ID})
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
InsertSliceParallel(syncDB, tx, l.Modules, &InsertId[string]{id: l.ID})
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -64,15 +66,21 @@ type SetupLanguageModule struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (l SetupLanguageModule) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
const query = `
|
||||
INSERT INTO setup_language_module (setup_language_id, label, path)
|
||||
VALUES ($1, $2, $3)
|
||||
func (l SetupLanguageModule) Query(data *InsertId[string]) {
|
||||
data.query = `
|
||||
INSERT INTO setup_language_module (setup_language_id, id, label, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Label, l.Path}
|
||||
func (l SetupLanguageModule) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
err := tx.QueryRowx(query, args...).Scan(&l.ID)
|
||||
args := []any{data.id, l.ID, l.Label, l.Path}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -81,14 +89,58 @@ func (l SetupLanguageModule) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
}
|
||||
|
||||
func (l SetupLanguageModule) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
const query = `
|
||||
INSERT INTO setup_language_module (setup_language_id, label, path)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id`
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Label, l.Path}
|
||||
args := []any{data.id, l.ID, l.Label, l.Path}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, query, args...).Scan(&l.ID)
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
ID uint
|
||||
|
||||
Lang string
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (l *Language) Query(data *InsertId[string]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO language (%s, lang, name, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (l *Language) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Lang, l.Name, l.Path}
|
||||
|
||||
err := tx.QueryRow(data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Language) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, l.Lang, l.Name, l.Path}
|
||||
|
||||
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&l.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user