sqlmw icon indicating copy to clipboard operation
sqlmw copied to clipboard

How do I get the library specified by the request sql from the intercept function?

Open 2w1nd opened this issue 1 year ago • 0 comments

func run(dsn string) {
        // install the wrapped driver
        sql.Register("postgres-mw", sqlmw.Driver(pq.Driver{}, new(sqlInterceptor)))
        db, err := sql.Open("postgres-mw", dsn)
        ...
}

type sqlInterceptor struct {
        sqlmw.NullInterceptor
}

func (in *sqlInterceptor) StmtQueryContext(ctx context.Context, conn driver.StmtQueryContext, query string, args []driver.NamedValue) (driver.Rows, error) {
        startedAt := time.Now()
        rows, err := conn.QueryContext(ctx, args)
        log.Debug("executed sql query", "duration", time.Since(startedAt), "query", query, "args", args, "err", err)
        return rows, err
}

Like this example, I want to get the library name of the query execution in the StmtQueryContext, how do I do that?I've tried doing a sql fetch like 'select database()' ahead of time。And interception functions such as StmtExecContext, ConnExecContext, ConnPrepareContext, etc., need to be obtained by different implementation methods

2w1nd avatar Mar 06 '24 10:03 2w1nd