Skip to content

proposal: database/sql: support creating a *sql.DB directly from a driver.Driver and dsn. #20268

Closed
@james-lawrence

Description

@james-lawrence

sometimes drivers expose functionality that doesn't make sense to handle via the DSN and would be painful/impossible to setup, using PGX as an example:

splitting sql.Open into two methods solves the above issues with minimal changes to the runtime.

func Open(driverName, dataSourceName string) (*DB, error) {
	driversMu.RLock()
	driveri, ok := drivers[driverName]
	driversMu.RUnlock()
	if !ok {
		return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
	}

	return NewFromDriver(driveri, dataSourceName), nil
}

func NewFromDriver(driveri driver.Driver, dataSourceName string) *DB {
	db := &DB{
		driver:       driveri,
		dsn:          dataSourceName,
		openerCh:     make(chan struct{}, connectionRequestQueueSize),
		lastPut:      make(map[*driverConn]string),
		connRequests: make(map[uint64]chan connRequest),
	}
	go db.connectionOpener()
	return db
}

for reference here is the current open code:

func Open(driverName, dataSourceName string) (*DB, error) {
	driversMu.RLock()
	driveri, ok := drivers[driverName]
	driversMu.RUnlock()
	if !ok {
		return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
	}

	db := &DB{
		driver:       driveri,
		dsn:          dataSourceName,
		openerCh:     make(chan struct{}, connectionRequestQueueSize),
		lastPut:      make(map[*driverConn]string),
		connRequests: make(map[uint64]chan connRequest),
	}

	go db.connectionOpener()
	return db, nil
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions