Skip to content

Commit

Permalink
Merge pull request #42 from Songmu/NamedValueChecker
Browse files Browse the repository at this point in the history
implement NamedValueChecker
  • Loading branch information
shogo82148 authored Jan 15, 2020
2 parents 9d74c1f + 533168e commit d681b4a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions conn_go19.go
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)
}
21 changes: 21 additions & 0 deletions stmt_go19.go
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)
}

0 comments on commit d681b4a

Please sign in to comment.