finish setup insert method, refactor sql migration file
This commit is contained in:
@@ -3,7 +3,6 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
@@ -14,48 +13,49 @@ type Files struct {
|
||||
Storages []FilesStorage
|
||||
}
|
||||
|
||||
func (f *Files) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
query := fmt.Sprintf(`
|
||||
func (f *Files) Query(data *InsertId[uint]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO files (%s)
|
||||
VALUES ($1)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (f *Files) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id}
|
||||
|
||||
err := tx.QueryRowx(query, args...).Scan(&f.ID)
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
syncDB := SyncDbOperations{
|
||||
wg: sync.WaitGroup{},
|
||||
errChan: make(chan error),
|
||||
}
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
|
||||
InsertSliceParallel[uint](&syncDB, tx, f.Storages, &InsertId[uint]{id: f.ID})
|
||||
InsertSliceParallel(syncDB, tx, f.Storages, &InsertId[uint]{id: f.ID})
|
||||
|
||||
return syncDB.Wait()
|
||||
}
|
||||
|
||||
func (f *Files) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO files (%s)
|
||||
VALUES ($1)
|
||||
RETURNING id`, data.fieldName)
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, query, args...).Scan(&f.ID)
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
syncDB := SyncDbOperations{
|
||||
wg: sync.WaitGroup{},
|
||||
errChan: make(chan error),
|
||||
}
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
|
||||
InsertSliceParallel(&syncDB, tx, f.Storages, &InsertId[uint]{id: f.ID})
|
||||
InsertSliceParallel(syncDB, tx, f.Storages, &InsertId[uint]{id: f.ID})
|
||||
|
||||
return syncDB.Wait()
|
||||
}
|
||||
@@ -66,15 +66,21 @@ type FilesStorage struct {
|
||||
Storage string
|
||||
}
|
||||
|
||||
func (f FilesStorage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
query := `
|
||||
INSERT INTO files (files_id, storage)
|
||||
func (f FilesStorage) Query(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO files_storage (files_id, storage)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
func (f FilesStorage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, f.Storage}
|
||||
|
||||
err := tx.QueryRowx(query, args...).Scan(&f.ID)
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -83,14 +89,13 @@ func (f FilesStorage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
}
|
||||
|
||||
func (f FilesStorage) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
const query = `
|
||||
INSERT INTO featured_content (files_id, storage)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, f.Storage}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, query, args...).Scan(&f.ID)
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user