diff --git a/AUTHORS b/AUTHORS index 1ace1fa45..c648e1835 100644 --- a/AUTHORS +++ b/AUTHORS @@ -46,6 +46,7 @@ Kamil Dziedzic Kevin Malachowski Lennart Rudolph Leonardo YongUk Kim +Linh Tran Tuan Lion Yang Luca Looz Lucas Liu diff --git a/connection_go18.go b/connection_go18.go index 65cc63ef2..1306b70b7 100644 --- a/connection_go18.go +++ b/connection_go18.go @@ -197,10 +197,6 @@ func (mc *mysqlConn) startWatcher() { } func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) { - value, err := converter{}.ConvertValue(nv.Value) - if err != nil { - return driver.ErrSkip - } - nv.Value = value + nv.Value, err = converter{}.ConvertValue(nv.Value) return } diff --git a/driver_test.go b/driver_test.go index 392e752a3..224a24c53 100644 --- a/driver_test.go +++ b/driver_test.go @@ -529,6 +529,55 @@ func TestValuer(t *testing.T) { }) } +type testValuerWithValidation struct { + value string +} + +func (tv testValuerWithValidation) Value() (driver.Value, error) { + if len(tv.value) == 0 { + return nil, fmt.Errorf("Invalid string valuer. Value must not be empty") + } + + return tv.value, nil +} + +func TestValuerWithValidation(t *testing.T) { + runTests(t, dsn, func(dbt *DBTest) { + in := testValuerWithValidation{"a_value"} + var out string + var rows *sql.Rows + + dbt.mustExec("CREATE TABLE testValuer (value VARCHAR(255)) CHARACTER SET utf8") + dbt.mustExec("INSERT INTO testValuer VALUES (?)", in) + + rows = dbt.mustQuery("SELECT value FROM testValuer") + defer rows.Close() + + if rows.Next() { + rows.Scan(&out) + if in.value != out { + dbt.Errorf("Valuer: %v != %s", in, out) + } + } else { + dbt.Errorf("Valuer: no data") + } + + if _, err := dbt.db.Exec("INSERT INTO testValuer VALUES (?)", testValuerWithValidation{""}); err == nil { + dbt.Errorf("Failed to check valuer error") + } + + if _, err := dbt.db.Exec("INSERT INTO testValuer VALUES (?)", nil); err != nil { + dbt.Errorf("Failed to check nil") + } + + if _, err := dbt.db.Exec("INSERT INTO testValuer VALUES (?)", map[string]bool{}); err == nil { + dbt.Errorf("Failed to check not valuer") + } + + dbt.mustExec("DROP TABLE IF EXISTS testValuer") + }) +} + type timeTests struct { dbtype string tlayout string diff --git a/statement.go b/statement.go index 4870a307c..98e57bcd8 100644 --- a/statement.go +++ b/statement.go @@ -137,6 +137,12 @@ func (c converter) ConvertValue(v interface{}) (driver.Value, error) { return v, nil } + if v != nil { + if valuer, ok := v.(driver.Valuer); ok { + return valuer.Value() + } + } + rv := reflect.ValueOf(v) switch rv.Kind() { case reflect.Ptr: