From 0906de605302f1e3ec8b8b2d7f960974eaa01dc9 Mon Sep 17 00:00:00 2001 From: Mauro Leggieri Date: Wed, 2 Aug 2023 07:57:29 -0300 Subject: [PATCH] Updated dependencies and added comments. --- go.mod | 16 ++++++---------- go.sum | 34 +++++++++++++++------------------- internal.go | 10 +++++++--- postgres.go | 7 +++++++ postgres_test.go | 3 ++- rowgetter.go | 2 ++ rowsgetter.go | 3 +++ transaction.go | 1 + 8 files changed, 43 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index c34c680..06419cb 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 659c79d..3167826 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal.go b/internal.go index 82c53a8..16cc26a 100644 --- a/internal.go +++ b/internal.go @@ -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, @@ -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) + } } } diff --git a/postgres.go b/postgres.go index b0e8811..8642a7c 100644 --- a/postgres.go +++ b/postgres.go @@ -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 { @@ -27,6 +31,7 @@ type Database struct { } } +// Options defines the database connection options. type Options struct { Host string `json:"host"` Port uint16 `json:"port"` @@ -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 ( diff --git a/postgres_test.go b/postgres_test.go index 6b80325..980dfc8 100644 --- a/postgres_test.go +++ b/postgres_test.go @@ -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() diff --git a/rowgetter.go b/rowgetter.go index cbe92b9..642e4ab 100644 --- a/rowgetter.go +++ b/rowgetter.go @@ -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 } diff --git a/rowsgetter.go b/rowsgetter.go index b556567..b86a16d 100644 --- a/rowsgetter.go +++ b/rowsgetter.go @@ -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 } diff --git a/transaction.go b/transaction.go index f722558..48b4b01 100644 --- a/transaction.go +++ b/transaction.go @@ -8,6 +8,7 @@ import ( // ----------------------------------------------------------------------------- +// Tx encloses a transation object. type Tx struct { db *Database tx pgx.Tx