Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"log"

"github.com/alexeyco/pig"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
)

func main() {
Expand Down Expand Up @@ -59,7 +59,7 @@ import (
"log"

"github.com/alexeyco/pig"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
)

func main() {
Expand Down Expand Up @@ -90,7 +90,7 @@ import (
"log"

"github.com/alexeyco/pig"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
)

type Thing struct {
Expand Down Expand Up @@ -128,7 +128,7 @@ import (
"time"

"github.com/alexeyco/pig"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
)

func main() {
Expand Down
10 changes: 5 additions & 5 deletions ex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package pig
import (
"context"

"github.com/georgysavva/scany/pgxscan"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/pkg/errors"
)

type executable interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
Query(context.Context, string, ...any) (pgx.Rows, error)
}

// Ex to execute queries.
Expand Down
21 changes: 15 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
module github.com/alexeyco/pig
module github.com/otetz/pig

go 1.16
go 1.23.0

require (
github.com/georgysavva/scany v0.2.8
github.com/jackc/pgconn v1.8.1
github.com/jackc/pgx/v4 v4.11.0
github.com/pashagolub/pgxmock v0.0.0-20210329125448-d2e8b19a1746
github.com/georgysavva/scany/v2 v2.1.4
github.com/jackc/pgx/v5 v5.7.5
github.com/pashagolub/pgxmock/v4 v4.8.0
github.com/pkg/errors v0.9.1
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/text v0.24.0 // indirect
)
518 changes: 32 additions & 486 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/alexeyco/pig"
"github.com/otetz/pig"
)

type key int
Expand Down
6 changes: 3 additions & 3 deletions pig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package pig
import (
"context"

"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)

// Conn connection interface.
type Conn interface {
BeginFunc(context.Context, func(pgx.Tx) error) error
BeginTx(context.Context, pgx.TxOptions) (pgx.Tx, error)
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
}
Expand Down
12 changes: 5 additions & 7 deletions pig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"testing"
"time"

"github.com/alexeyco/pig"
"github.com/jackc/pgx/v4"
"github.com/pashagolub/pgxmock"
"github.com/jackc/pgx/v5"
"github.com/pashagolub/pgxmock/v4"
"github.com/pkg/errors"

"github.com/otetz/pig"
)

func connect(t *testing.T) pgxmock.PgxConnIface {
Expand Down Expand Up @@ -102,6 +103,7 @@ func TestPig_Query(t *testing.T) {
defer func() { _ = conn.Close(context.Background()) }()

conn.ExpectExec("DELETE FROM things WHERE id = $1").
WithArgs(123).
WillReturnError(errExpected)

rowsAffected, err := pig.New(conn).
Expand Down Expand Up @@ -269,7 +271,6 @@ func TestPig_Tx(t *testing.T) {
conn.ExpectExec("DELETE FROM things WHERE id = $1").
WithArgs(123).
WillReturnResult(pgxmock.NewResult("DELETE", 1))
conn.ExpectCommit()
conn.ExpectRollback()

err := pig.New(conn).
Expand Down Expand Up @@ -299,7 +300,6 @@ func TestPig_Tx(t *testing.T) {
WithArgs(123).
WillReturnError(errExpected)
conn.ExpectRollback()
conn.ExpectRollback()

err := pig.New(conn).
Tx().
Expand Down Expand Up @@ -330,7 +330,6 @@ func TestPig_Tx(t *testing.T) {
conn.ExpectExec("DELETE FROM things WHERE id = $1").
WithArgs(123).
WillReturnResult(pgxmock.NewResult("DELETE", 1))
conn.ExpectCommit()
conn.ExpectRollback()

err := pig.New(conn).
Expand Down Expand Up @@ -391,7 +390,6 @@ func TestPig_Tx(t *testing.T) {
conn.ExpectExec("DELETE FROM things WHERE id = $1").
WithArgs(123).
WillReturnResult(pgxmock.NewResult("DELETE", 1))
conn.ExpectCommit()
conn.ExpectRollback()

err := pig.New(conn).
Expand Down
43 changes: 30 additions & 13 deletions tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pig

import (
"github.com/jackc/pgx/v4"
"context"

"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
)

Expand All @@ -18,23 +20,38 @@ type Tx struct {

// Exec to execute transaction.
func (tx *Tx) Exec(handler Handler) error {
err := tx.conn.BeginFunc(tx.options.Context, func(txx pgx.Tx) error {
if tx.options.TransactionTimeout > 0 {
if _, err := txx.Exec(tx.options.Context, transactionTimeoutQuery, tx.options.TransactionTimeout); err != nil {
return errors.Wrap(err, "pig: set transaction timeout")
txx, err := tx.conn.BeginTx(tx.options.Context, pgx.TxOptions{})
if err != nil {
return errors.Wrap(err, "pig: begin transaction")
}

defer func() {
switch err {
case nil:
err = txx.Commit(context.Background())
if err != nil {
_ = txx.Rollback(context.Background())
}
default:
_ = txx.Rollback(context.Background())
}
}()

if tx.options.StatementTimeout > 0 {
if _, err := txx.Exec(tx.options.Context, statementTimeoutQuery, tx.options.StatementTimeout); err != nil {
return errors.Wrap(err, "pig: set statement timeout")
}
if tx.options.TransactionTimeout > 0 {
if _, err = txx.Exec(tx.options.Context, transactionTimeoutQuery, tx.options.TransactionTimeout); err != nil {
return errors.Wrap(err, "pig: set transaction timeout")
}
}

if tx.options.StatementTimeout > 0 {
if _, err = txx.Exec(tx.options.Context, statementTimeoutQuery, tx.options.StatementTimeout); err != nil {
return errors.Wrap(err, "pig: set statement timeout")
}
}

return handler(&Ex{
ex: txx,
options: tx.options,
})
err = handler(&Ex{
ex: txx,
options: tx.options,
})

return errors.WithStack(err)
Expand Down