-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add context support #586
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
Closed
Closed
Add context support #586
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94e3ba7
Add context support
oscarzhao 669fc71
Use context.DeadlineExceeded error
2abb4ae
Add context support for reads
691ec98
Rework write deadline logic
6d7e804
README improvements
ce38c4f
Add compile-time interface tests and fix method signatures
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// +build go1.8 | ||
|
||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package mysql | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
) | ||
|
||
// Ping implements driver.Pinger interface | ||
func (mc *mysqlConn) Ping(ctx context.Context) error { | ||
if mc.netConn == nil { | ||
errLog.Print(ErrInvalidConn) | ||
return driver.ErrBadConn | ||
} | ||
if err := mc.writeCommandPacket(ctx, comPing); err != nil { | ||
errLog.Print(err) | ||
return err | ||
} | ||
|
||
if _, err := mc.readResultOK(ctx); err != nil { | ||
errLog.Print(err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// BeginTx implements driver.ConnBeginTx interface | ||
func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { | ||
return mc.beginTx(ctx, txOptions(opts)) | ||
} | ||
|
||
func (mc *mysqlConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { | ||
return mc.prepareContext(ctx, query) | ||
} | ||
|
||
// QueryContext implements driver.QueryerContext interface | ||
func (mc *mysqlConn) QueryContext(ctx context.Context, query string, args []driver.Value) (driver.Rows, error) { | ||
return mc.queryContext(ctx, query, args) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// +build !go1.8 | ||
|
||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package mysql | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// txOptions is defined for compatibility with Go 1.8's driver.TxOptions struct. | ||
type txOptions struct { | ||
Isolation int | ||
ReadOnly bool | ||
} | ||
|
||
// mysqlContext is a copy of context.Context from Go 1.7 and later. | ||
type mysqlContext interface { | ||
Deadline() (deadline time.Time, ok bool) | ||
|
||
Done() <-chan struct{} | ||
|
||
Err() error | ||
|
||
Value(key interface{}) interface{} | ||
} | ||
|
||
// emptyCtx is copied from Go 1.7's context package. | ||
type emptyCtx int | ||
|
||
func (*emptyCtx) Deadline() (deadline time.Time, ok bool) { | ||
return | ||
} | ||
|
||
func (*emptyCtx) Done() <-chan struct{} { | ||
return nil | ||
} | ||
|
||
func (*emptyCtx) Err() error { | ||
return nil | ||
} | ||
|
||
func (*emptyCtx) Value(key interface{}) interface{} { | ||
return nil | ||
} | ||
|
||
func (e *emptyCtx) String() string { | ||
return "context.Background" | ||
} | ||
|
||
var background = new(emptyCtx) | ||
|
||
func backgroundCtx() mysqlContext { | ||
return background | ||
} | ||
|
||
var deadlineExceeded = deadlineExceededError{} | ||
|
||
// deadlineExceededError is copied from Go 1.7's context package. | ||
type deadlineExceededError struct{} | ||
|
||
func (deadlineExceededError) Error() string { return "context deadline exceeded" } | ||
func (deadlineExceededError) Timeout() bool { return true } | ||
func (deadlineExceededError) Temporary() bool { return true } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The interface defined in
sql/driver
isExecContext(ctx context.Context, query string, args []NamedValue) (Result, error)
note the
driver.Value
is changed todriver.NamedValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's why there should be simple initialization tests, to check if our driver implements the respective interfaces, i.e.
var _ driver.Interface = mysql.Implementation{}
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example of what I mean: e1cf7db