Skip to content
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

feat: support get prepare context #3

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (s driver.Stmt,
return st, err
}

return &stmt{Stmt: st, StmtHook: c.ConnHook.(StmtHook), query: query}, nil
return &stmt{Stmt: st, StmtHook: c.ConnHook.(StmtHook), query: query, prepareContext: ctx}, nil
}

func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (dd driver.Tx, err error) {
8 changes: 8 additions & 0 deletions stmt.go
Original file line number Diff line number Diff line change
@@ -11,14 +11,22 @@ var (
_ driver.StmtQueryContext = (*stmt)(nil)
)

type prepareContextKey struct{}

func PrepareContextFromContext(ctx context.Context) context.Context {
return ctx.Value(prepareContextKey{}).(context.Context)
}

type stmt struct {
driver.Stmt
query string
StmtHook
prepareContext context.Context
}

func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
query := s.query
ctx = context.WithValue(ctx, prepareContextKey{}, s.prepareContext)
ctx, args, err = s.BeforeStmtQueryContext(ctx, query, args, nil)
defer func() {
_, rows, err = s.AfterStmtQueryContext(ctx, query, args, rows, err)