Skip to content

Allow driver.Connector wrapping #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danielrs
Copy link

Depending on the driver we might have easy access to a configurable driver.Connector, but not a configurable driver.Driver. This change adds a couple new functions so we can wrap a driver.Connector instead of a driver; in my case, it's useful when used together with jackc/pgx:

pgxConnConfig, err := pgx.ParseConfig(dsn)
if err != nil {
    return nil, errors.WithStack(err)
}

driver := sqlmw.WrapDriver(pgxstdlib.GetDefaultDriver(), &sqlInterceptor{ApplicationName: applicationName})

connector := sqlmw.WrapConnector(
    &driver,
    pgxstdlib.GetConnector(
    	*pgxConnConfig,
    	pgxstdlib.OptionAfterConnect(func(ctx context.Context, pgxConn *pgx.Conn) error {
    		pgxConn.TypeMap().RegisterType(&pgtype.Type{Name: "timestamp", OID: pgtype.TimestampOID, Codec: &pgtype.TimestampCodec{ScanLocation: time.UTC}})
    		pgxConn.TypeMap().RegisterType(&pgtype.Type{Name: "timestamptz", OID: pgtype.TimestamptzOID, Codec: &pgtype.TimestamptzCodec{ScanLocation: time.UTC}})
    		return nil
    	}),
    ),
)

db := sql.OpenDB(connector)

danielrs added 2 commits June 18, 2025 15:11
Some SQL drivers will provide a configurable connector, this new
function should make it compatible with interceptors.
Basically make sure we don't change the signature of the `.Driver(...)`
function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant