Skip to content

Commit

Permalink
feat: add *sql.DB connector that uses database context (#6366)
Browse files Browse the repository at this point in the history
* feat: add SQLConnector

* rename
  • Loading branch information
lzakharov committed Jun 5, 2023
1 parent 5eaccaa commit 661781a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ func (db *DB) AddError(err error) error {
func (db *DB) DB() (*sql.DB, error) {
connPool := db.ConnPool

if connector, ok := connPool.(GetDBConnectorWithContext); ok && connector != nil {
return connector.GetDBConnWithContext(db)
}

if dbConnector, ok := connPool.(GetDBConnector); ok && dbConnector != nil {
if sqldb, err := dbConnector.GetDBConn(); sqldb != nil || err != nil {
return sqldb, err
Expand Down
6 changes: 6 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ type GetDBConnector interface {
GetDBConn() (*sql.DB, error)
}

// GetDBConnectorWithContext represents SQL db connector which takes into
// account the current database context
type GetDBConnectorWithContext interface {
GetDBConnWithContext(db *DB) (*sql.DB, error)
}

// Rows rows interface
type Rows interface {
Columns() ([]string, error)
Expand Down

0 comments on commit 661781a

Please sign in to comment.