-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Songmu/NamedValueChecker
implement NamedValueChecker
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,25 @@ | ||
// +build go1.9 | ||
|
||
package proxy | ||
|
||
import "database/sql/driver" | ||
|
||
// copied from sql/driver/convert.go | ||
// defaultCheckNamedValue wraps the default ColumnConverter to have the same | ||
// function signature as the CheckNamedValue in the driver.NamedValueChecker | ||
// interface. | ||
func defaultCheckNamedValue(nv *driver.NamedValue) (err error) { | ||
nv.Value, err = driver.DefaultParameterConverter.ConvertValue(nv.Value) | ||
return err | ||
} | ||
|
||
// CheckNamedValue for implementing NamedValueChecker | ||
// This function may be unnecessary because `proxy.Stmt` already implements `NamedValueChecker`, | ||
// but it is implemented just in case. | ||
func (conn *Conn) CheckNamedValue(nv *driver.NamedValue) (err error) { | ||
if nvc, ok := conn.Conn.(driver.NamedValueChecker); ok { | ||
return nvc.CheckNamedValue(nv) | ||
} | ||
// fallback to default | ||
return defaultCheckNamedValue(nv) | ||
} |
This file contains 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,21 @@ | ||
// +build go1.9 | ||
|
||
package proxy | ||
|
||
import "database/sql/driver" | ||
|
||
// CheckNamedValue for implementing NamedValueChecker | ||
func (stmt *Stmt) CheckNamedValue(nv *driver.NamedValue) (err error) { | ||
if nvc, ok := stmt.Stmt.(driver.NamedValueChecker); ok { | ||
return nvc.CheckNamedValue(nv) | ||
} | ||
// When converting data in sql/driver/convert.go, it is checked first whether the `stmt` | ||
// implements `NamedValueChecker`, and then checks if `conn` implements NamedValueChecker. | ||
// In the case of "go-sql-proxy", the `proxy.Stmt` "implements" `CheckNamedValue` here, | ||
// so we also check both `stmt` and `conn` inside here. | ||
if nvc, ok := stmt.Conn.Conn.(driver.NamedValueChecker); ok { | ||
return nvc.CheckNamedValue(nv) | ||
} | ||
// fallback to default | ||
return defaultCheckNamedValue(nv) | ||
} |