go_import_excel_pg/internal/repository/store.go

44 lines
774 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
2024-02-23 14:10:40 +07:00
ImportPatientTx(ctx context.Context, params ImportDataPasienParams, createdBy int32, fasyankesId int32) error
2024-02-22 10:43:39 +07:00
}
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)
}