Skip to content
Merged
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
16 changes: 6 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ module github.com/randlabs/go-postgres

go 1.19

require (
github.com/jackc/pgx/v5 v5.2.0
github.com/randlabs/go-exterror v1.0.0
)
require github.com/jackc/pgx/v5 v5.4.2

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/puddle/v2 v2.1.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
golang.org/x/text v0.3.8 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.11.0 // indirect
)
34 changes: 15 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgx/v5 v5.2.0 h1:NdPpngX0Y6z6XDFKqmFQaE+bCtkqzvQIOt1wvBlAqs8=
github.com/jackc/pgx/v5 v5.2.0/go.mod h1:Ptn7zmohNsWEsdxRawMzk3gaKma2obW+NWTnKa0S4nk=
github.com/jackc/puddle/v2 v2.1.2 h1:0f7vaaXINONKTsxYDn4otOAiJanX/BMeAtY//BXqzlg=
github.com/jackc/puddle/v2 v2.1.2/go.mod h1:2lpufsF5mRHO6SuZkm0fNYxM6SWHfvyFj62KwNzgels=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.4.2 h1:u1gmGDwbdRUZiwisBm/Ky2M14uQyUP65bG8+20nnyrg=
github.com/jackc/pgx/v5 v5.4.2/go.mod h1:q6iHT8uDNXWiFNOlRqJzBTaSH3+2xCXkokxHZC5qWFY=
github.com/jackc/puddle/v2 v2.2.0 h1:RdcDk92EJBuBS55nQMMYFXTxwstHug4jkhT5pq8VxPk=
github.com/jackc/puddle/v2 v2.2.0/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/randlabs/go-exterror v1.0.0 h1:KJT5iMslnIK3acMQELSoKTmGbjgBReVryo/VCYu/qw0=
github.com/randlabs/go-exterror v1.0.0/go.mod h1:4XHfj2DJ9E7JTpuS1tBrr40y2WNQEIwAb5/flSqY7Lo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 h1:ZrnxWX62AgTKOSagEqxvb3ffipvEDX2pl7E1TdqLqIc=
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10 changes: 7 additions & 3 deletions internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var errNoRows = &NoRowsError{}

// -----------------------------------------------------------------------------

// Gets a connection from the pool and initiates a transaction
// Gets a connection from the pool and initiates a transaction.
func (db *Database) getTx(ctx context.Context) (pgx.Tx, error) {
tx, err := db.pool.BeginTx(ctx, pgx.TxOptions{
IsoLevel: pgx.ReadCommitted, //pgx.Serializable,
Expand All @@ -40,12 +40,16 @@ func (db *Database) processError(err error) error {
if err != nil && IsDatabaseError(err) && err != errNoRows {
if db.err.last == nil {
db.err.last = err
db.err.handler(err)
if db.err.handler != nil {
db.err.handler(err)
}
}
} else {
if db.err.last != nil {
db.err.last = nil
db.err.handler(nil)
if db.err.handler != nil {
db.err.handler(nil)
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import (

// -----------------------------------------------------------------------------

// WithinTxCallback defines a callback called in the context of the initiated transaction.
type WithinTxCallback = func(ctx context.Context, tx Tx) error

// CopyCallback defines a callback that is called for each record being copied to the database
type CopyCallback func(ctx context.Context, idx int) ([]interface{}, error)

// -----------------------------------------------------------------------------

// Database represents a PostgreSQL database accessor.
type Database struct {
pool *pgxpool.Pool
err struct {
Expand All @@ -27,6 +31,7 @@ type Database struct {
}
}

// Options defines the database connection options.
type Options struct {
Host string `json:"host"`
Port uint16 `json:"port"`
Expand All @@ -37,8 +42,10 @@ type Options struct {
SSLMode SSLMode
}

// ErrorHandler defines a custom error handler.
type ErrorHandler func(err error)

// SSLMode states if secure communication with the server is optional or mandatory.
type SSLMode int

const (
Expand Down
3 changes: 2 additions & 1 deletion postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func TestPostgres(t *testing.T) {
if err != nil {
t.Fatalf("%v", err.Error())
}
// We comment the next line because we want to do a clean database pool shutdown on errors
// We comment the next defer line because we want to do a clean database pool shutdown on errors and
// calling fatal exits the process.
// defer db.Close()

ctx := context.Background()
Expand Down
2 changes: 2 additions & 0 deletions rowgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

// -----------------------------------------------------------------------------

// Row defines a returned record.
type Row interface {
// Scan saves the content of the current row in the destination variables.
Scan(dest ...interface{}) error
}

Expand Down
3 changes: 3 additions & 0 deletions rowsgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (

// -----------------------------------------------------------------------------

// ScanRowsCallback defines a callback that is called on each row returned by the executed query.
type ScanRowsCallback = func(ctx context.Context, row Row) (bool, error)

// Rows defines a set of returned records.
type Rows interface {
// Do calls the provided callback for each row returned by the executed query.
Do(callback ScanRowsCallback) error
}

Expand Down
1 change: 1 addition & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// -----------------------------------------------------------------------------

// Tx encloses a transation object.
type Tx struct {
db *Database
tx pgx.Tx
Expand Down