44 lines
738 B
Go
44 lines
738 B
Go
|
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)
|
||
|
}
|