Skip to content

Commit c0a9d8b

Browse files
authored
Merge pull request #4 from randlabs/bug_fixes
Bug fixes
2 parents bbca366 + 4843cee commit c0a9d8b

File tree

8 files changed

+80
-15
lines changed

8 files changed

+80
-15
lines changed

copycallback.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type copyWithCallback struct {
1717

1818
// -----------------------------------------------------------------------------
1919

20-
func (c copyWithCallback) Next() bool {
20+
func (c *copyWithCallback) Next() bool {
2121
if c.err != nil || c.counter < 0 {
2222
return false
2323
}
@@ -37,7 +37,7 @@ func (c copyWithCallback) Next() bool {
3737
return true
3838
}
3939

40-
func (c copyWithCallback) Values() ([]interface{}, error) {
40+
func (c *copyWithCallback) Values() ([]interface{}, error) {
4141
if c.err != nil {
4242
return nil, c.err
4343
}
@@ -49,6 +49,6 @@ func (c copyWithCallback) Values() ([]interface{}, error) {
4949
return data, nil
5050
}
5151

52-
func (c copyWithCallback) Err() error {
52+
func (c *copyWithCallback) Err() error {
5353
return c.err
5454
}

errors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ import (
1111
type Error struct {
1212
message string
1313
err error // Err is the underlying error that occurred during the operation.
14+
Details *ErrorDetails
15+
}
16+
17+
type ErrorDetails struct {
18+
Severity string
19+
Code string
20+
Message string
21+
Detail string
22+
Hint string
23+
Position int32
24+
Where string
25+
SchemaName string
26+
TableName string
27+
ColumnName string
28+
DataTypeName string
29+
ConstraintName string
30+
File string
31+
Line int32
32+
Routine string
1433
}
1534

1635
// NoRowsError is the error we return if the query does not return any row.

helpers.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
package postgres
22

33
import (
4+
"errors"
45
"strings"
6+
7+
"github.com/jackc/pgx/v5/pgconn"
58
)
69

710
// -----------------------------------------------------------------------------
811

912
func newError(wrappedErr error, text string) *Error {
10-
e := &Error{
13+
var e *Error
14+
var pgErr *pgconn.PgError
15+
16+
if errors.As(wrappedErr, &e) {
17+
return e
18+
}
19+
e = &Error{
1120
message: text,
1221
err: wrappedErr,
1322
}
23+
24+
if errors.As(wrappedErr, &pgErr) {
25+
e.Details = &ErrorDetails{
26+
Severity: pgErr.Severity,
27+
Code: pgErr.Code,
28+
Message: pgErr.Message,
29+
Detail: pgErr.Detail,
30+
Hint: pgErr.Hint,
31+
Position: pgErr.Position,
32+
Where: pgErr.Where,
33+
SchemaName: pgErr.SchemaName,
34+
TableName: pgErr.TableName,
35+
ColumnName: pgErr.ColumnName,
36+
DataTypeName: pgErr.DataTypeName,
37+
ConstraintName: pgErr.ConstraintName,
38+
File: pgErr.File,
39+
Line: pgErr.Line,
40+
Routine: pgErr.Routine,
41+
}
42+
}
43+
44+
// Done
1445
return e
1546
}
1647

internal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (db *Database) getTx(ctx context.Context) (pgx.Tx, error) {
2121
DeferrableMode: pgx.NotDeferrable,
2222
})
2323
if err != nil {
24-
return nil, newError(err, "unable to start db transaction")
24+
return nil, newError(err, "unable to start transaction")
2525
}
2626

2727
//Done

postgres.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ func (db *Database) SetEventHandler(handler ErrorHandler) {
130130
// Exec executes an SQL statement on a new connection
131131
func (db *Database) Exec(ctx context.Context, sql string, args ...interface{}) (int64, error) {
132132
ct, err := db.pool.Exec(ctx, sql, args...)
133+
if err != nil {
134+
err = newError(err, "unable to execute command")
135+
}
133136
return ct.RowsAffected(), db.processError(err)
134137
}
135138

@@ -145,7 +148,7 @@ func (db *Database) Exec(ctx context.Context, sql string, args ...interface{}) (
145148
// 3. To avoid overflows on high uint64 values, store them in NUMERIC(24,0) fields.
146149
// 4. For time-only fields, date is set to Jan 1, 2000 by PGX in time.Time variables.
147150
func (db *Database) QueryRow(ctx context.Context, sql string, args ...interface{}) Row {
148-
return rowGetter{
151+
return &rowGetter{
149152
db: db,
150153
row: db.pool.QueryRow(ctx, sql, args...),
151154
}
@@ -154,7 +157,10 @@ func (db *Database) QueryRow(ctx context.Context, sql string, args ...interface{
154157
// QueryRows executes a SQL query on a new connection
155158
func (db *Database) QueryRows(ctx context.Context, sql string, args ...interface{}) Rows {
156159
rows, err := db.pool.Query(ctx, sql, args...)
157-
return rowsGetter{
160+
if err != nil {
161+
err = newError(err, "unable to scan row")
162+
}
163+
return &rowsGetter{
158164
db: db,
159165
ctx: ctx,
160166
rows: rows,
@@ -168,11 +174,14 @@ func (db *Database) Copy(ctx context.Context, tableName string, columnNames []st
168174
ctx,
169175
pgx.Identifier{tableName},
170176
columnNames,
171-
copyWithCallback{
177+
&copyWithCallback{
172178
ctx: ctx,
173179
callback: callback,
174180
},
175181
)
182+
if err != nil {
183+
err = newError(err, "unable to execute command")
184+
}
176185

177186
// Done
178187
return n, db.processError(err)

rowgetter.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ type rowGetter struct {
1919
row pgx.Row
2020
}
2121

22-
func (r rowGetter) Scan(dest ...interface{}) error {
22+
func (r *rowGetter) Scan(dest ...interface{}) error {
2323
err := r.row.Scan(dest...)
24+
if err != nil {
25+
err = newError(err, "unable to scan row")
26+
}
2427
return r.db.processError(err)
2528
}

rowsgetter.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ type rowsGetter struct {
2626
err error
2727
}
2828

29-
func (r rowsGetter) Do(callback ScanRowsCallback) error {
29+
func (r *rowsGetter) Do(callback ScanRowsCallback) error {
3030
if r.err == nil {
3131
// Scan returned rows
3232
for r.rows.Next() {
3333
cont, err := callback(r.ctx, r)
3434
if err != nil {
35-
r.err = err
35+
r.err = newError(err, "callback returned failure")
3636
break
3737
}
3838
if !cont {
@@ -46,7 +46,10 @@ func (r rowsGetter) Do(callback ScanRowsCallback) error {
4646
return r.db.processError(r.err)
4747
}
4848

49-
func (r rowsGetter) Scan(dest ...interface{}) error {
49+
func (r *rowsGetter) Scan(dest ...interface{}) error {
5050
err := r.rows.Scan(dest...)
51+
if err != nil {
52+
err = newError(err, "unable to scan row")
53+
}
5154
return r.db.processError(err)
5255
}

transaction.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (tx *Tx) Exec(ctx context.Context, sql string, args ...interface{}) (int64,
2929

3030
// QueryRow executes a SQL query within the transaction.
3131
func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) Row {
32-
return rowGetter{
32+
return &rowGetter{
3333
db: tx.db,
3434
row: tx.tx.QueryRow(ctx, sql, args...),
3535
}
@@ -38,7 +38,7 @@ func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) Row
3838
// QueryRows executes a SQL query within the transaction.
3939
func (tx *Tx) QueryRows(ctx context.Context, sql string, args ...interface{}) Rows {
4040
rows, err := tx.tx.Query(ctx, sql, args...)
41-
return rowsGetter{
41+
return &rowsGetter{
4242
db: tx.db,
4343
ctx: ctx,
4444
rows: rows,
@@ -52,7 +52,7 @@ func (tx *Tx) Copy(ctx context.Context, tableName string, columnNames []string,
5252
ctx,
5353
pgx.Identifier{tableName},
5454
columnNames,
55-
copyWithCallback{
55+
&copyWithCallback{
5656
ctx: ctx,
5757
callback: callback,
5858
},

0 commit comments

Comments
 (0)