Files
Foundry-Scrapping-API/internal/foundry/temp_models/db/files.go

105 lines
1.8 KiB
Go

package db
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
)
type Files struct {
ID uint
Storages []FilesStorage
}
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(data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
syncDB := NewSyncDB()
defer close(syncDB.errChan)
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 {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id}
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
syncDB := NewSyncDB()
defer close(syncDB.errChan)
InsertSliceParallel(syncDB, tx, f.Storages, &InsertId[uint]{id: f.ID})
return syncDB.Wait()
}
type FilesStorage struct {
ID uint
Storage string
}
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(data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
return nil
}
func (f FilesStorage) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, f.Storage}
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
return nil
}