sqlmw icon indicating copy to clipboard operation
sqlmw copied to clipboard

Allow driver.Connector wrapping

Open danielrs opened this issue 7 months ago • 0 comments

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 avatar Jun 23 '25 17:06 danielrs