go_import_excel_pg/internal/repository/store.go

44 lines
738 B
Go
Raw Normal View History

2024-02-22 10:43:39 +07:00
package repository
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
type Store interface {
Querier
ImportPatientTx(ctx context.Context, params ImportDataPasienParams) error
}
type SQLStore struct {
*Queries
pool *pgxpool.Pool
}
func NewStore(pool *pgxpool.Pool) Store {
return &SQLStore{
pool: pool,
Queries: New(pool),
}
}
// TRANSACTION QUERY FUNCTION
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error {
tx, err := store.pool.Begin(ctx)
if err != nil {
return err
}
q := NewTx(tx)
err = fn(q)
if err != nil {
if rbErr := tx.Rollback(ctx); rbErr != nil {
return fmt.Errorf("tx err: %v, rb err : %v", err, rbErr)
}
return err
}
return tx.Commit(ctx)
}