Skip to content

Commit

Permalink
feat(pgdriver): add Error.StatementTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Oct 12, 2021
1 parent 51c1702 commit 8a7934d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 21 additions & 0 deletions driver/pgdriver/driver_test.go
Expand Up @@ -224,13 +224,34 @@ func TestConnParams(t *testing.T) {
"search_path": "foo",
}),
))
defer db.Close()

var searchPath string
err := db.QueryRow("SHOW search_path").Scan(&searchPath)
require.NoError(t, err)
require.Equal(t, "foo", searchPath)
}

func TestStatementTimeout(t *testing.T) {
ctx := context.Background()

db := sqlDB()
defer db.Close()

cn, err := db.Conn(ctx)
require.NoError(t, err)

_, err = cn.ExecContext(ctx, "SET statement_timeout = 100")
require.NoError(t, err)

_, err = cn.ExecContext(ctx, "SELECT pg_sleep(1)")
require.Error(t, err)

pgerr, ok := err.(pgdriver.Error)
require.True(t, ok)
require.True(t, pgerr.StatementTimeout())
}

func sqlDB() *sql.DB {
db, err := sql.Open("pg", dsn())
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion driver/pgdriver/error.go
Expand Up @@ -21,7 +21,7 @@ func (err Error) Field(k byte) string {
return err.m[k]
}

// IntegrityViolation reports whether an error is a part of
// IntegrityViolation reports whether the error is a part of
// Integrity Constraint Violation class of errors.
//
// https://www.postgresql.org/docs/current/static/errcodes-appendix.html
Expand All @@ -34,6 +34,11 @@ func (err Error) IntegrityViolation() bool {
}
}

// StatementTimeout reports whether the error is a statement timeout error.
func (err Error) StatementTimeout() bool {
return err.Field('C') == "57014"
}

func (err Error) Error() string {
return fmt.Sprintf("%s #%s %s",
err.Field('S'), err.Field('C'), err.Field('M'))
Expand Down

0 comments on commit 8a7934d

Please sign in to comment.