mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: allow drivers to support custom arg types
Previously all arguments were passed through driver.IsValid. This checked arguments against a few fundamental go types and prevented others from being passed in as arguments. The new interface driver.NamedValueChecker may be implemented by both driver.Stmt and driver.Conn. This allows this new interface to completely supersede the driver.ColumnConverter interface as it can be used for checking arguments known to a prepared statement and arbitrary query arguments. The NamedValueChecker may be skipped with driver.ErrSkip after all special cases are exhausted to use the default argument converter. In addition if driver.ErrRemoveArgument is returned the argument will not be passed to the query at all, useful for passing in driver specific per-query options. Add a canonical Out argument wrapper to be passed to OUTPUT parameters. This will unify checks that need to be written in the NameValueChecker. The statement number check is also moved to the argument converter so the NamedValueChecker may remove arguments passed to the query. Fixes #13567 Fixes #18079 Updates #18417 Updates #17834 Updates #16235 Updates #13067 Updates #19797 Change-Id: I89088bd9cca4596a48bba37bfd20d987453ef237 Reviewed-on: https://go-review.googlesource.com/38533 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
9044cb04f2
commit
a9bf3b2e19
6 changed files with 360 additions and 90 deletions
|
|
@ -58,9 +58,10 @@ type fakeDriver struct {
|
|||
type fakeDB struct {
|
||||
name string
|
||||
|
||||
mu sync.Mutex
|
||||
tables map[string]*table
|
||||
badConn bool
|
||||
mu sync.Mutex
|
||||
tables map[string]*table
|
||||
badConn bool
|
||||
allowAny bool
|
||||
}
|
||||
|
||||
type table struct {
|
||||
|
|
@ -352,12 +353,14 @@ func (c *fakeConn) Close() (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func checkSubsetTypes(args []driver.NamedValue) error {
|
||||
func checkSubsetTypes(allowAny bool, args []driver.NamedValue) error {
|
||||
for _, arg := range args {
|
||||
switch arg.Value.(type) {
|
||||
case int64, float64, bool, nil, []byte, string, time.Time:
|
||||
default:
|
||||
return fmt.Errorf("fakedb_test: invalid argument ordinal %[1]d: %[2]v, type %[2]T", arg.Ordinal, arg.Value)
|
||||
if !allowAny {
|
||||
return fmt.Errorf("fakedb_test: invalid argument ordinal %[1]d: %[2]v, type %[2]T", arg.Ordinal, arg.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
@ -373,7 +376,7 @@ func (c *fakeConn) ExecContext(ctx context.Context, query string, args []driver.
|
|||
// just to check that all the args are of the proper types.
|
||||
// ErrSkip is returned so the caller acts as if we didn't
|
||||
// implement this at all.
|
||||
err := checkSubsetTypes(args)
|
||||
err := checkSubsetTypes(c.db.allowAny, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -390,7 +393,7 @@ func (c *fakeConn) QueryContext(ctx context.Context, query string, args []driver
|
|||
// just to check that all the args are of the proper types.
|
||||
// ErrSkip is returned so the caller acts as if we didn't
|
||||
// implement this at all.
|
||||
err := checkSubsetTypes(args)
|
||||
err := checkSubsetTypes(c.db.allowAny, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -642,7 +645,7 @@ func (s *fakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (d
|
|||
return nil, driver.ErrBadConn
|
||||
}
|
||||
|
||||
err := checkSubsetTypes(args)
|
||||
err := checkSubsetTypes(s.c.db.allowAny, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -753,7 +756,7 @@ func (s *fakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (
|
|||
return nil, driver.ErrBadConn
|
||||
}
|
||||
|
||||
err := checkSubsetTypes(args)
|
||||
err := checkSubsetTypes(s.c.db.allowAny, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1004,6 +1007,12 @@ func (fakeDriverString) ConvertValue(v interface{}) (driver.Value, error) {
|
|||
return fmt.Sprintf("%v", v), nil
|
||||
}
|
||||
|
||||
type anyTypeConverter struct{}
|
||||
|
||||
func (anyTypeConverter) ConvertValue(v interface{}) (driver.Value, error) {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func converterForType(typ string) driver.ValueConverter {
|
||||
switch typ {
|
||||
case "bool":
|
||||
|
|
@ -1030,6 +1039,8 @@ func converterForType(typ string) driver.ValueConverter {
|
|||
return driver.Null{Converter: driver.DefaultParameterConverter}
|
||||
case "datetime":
|
||||
return driver.DefaultParameterConverter
|
||||
case "any":
|
||||
return anyTypeConverter{}
|
||||
}
|
||||
panic("invalid fakedb column type of " + typ)
|
||||
}
|
||||
|
|
@ -1056,6 +1067,8 @@ func colTypeToReflectType(typ string) reflect.Type {
|
|||
return reflect.TypeOf(NullFloat64{})
|
||||
case "datetime":
|
||||
return reflect.TypeOf(time.Time{})
|
||||
case "any":
|
||||
return reflect.TypeOf(new(interface{})).Elem()
|
||||
}
|
||||
panic("invalid fakedb column type of " + typ)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue