From 57372b7f47afc328ffc6e507a36f87ed43e02aed Mon Sep 17 00:00:00 2001 From: Dave Stubbs Date: Thu, 16 Nov 2017 15:28:07 +0000 Subject: [PATCH] Fix Valuers by returning driver.ErrSkip if couldn't convert type internally Added a test that Valuer types are handled correctly. This test fails without the fix. CheckNamedValue code was included recently and breaks existing applications: https://github.com/go-sql-driver/mysql/pull/690 Reported in: https://github.com/go-sql-driver/mysql/issues/708 --- AUTHORS | 1 + connection_go18.go | 6 +++++- driver_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index c405b8912..1ace1fa45 100644 --- a/AUTHORS +++ b/AUTHORS @@ -71,6 +71,7 @@ Zhenye Xie # Organizations Barracuda Networks, Inc. +Counting Ltd. Google Inc. Keybase Inc. Pivotal Inc. diff --git a/connection_go18.go b/connection_go18.go index 1306b70b7..65cc63ef2 100644 --- a/connection_go18.go +++ b/connection_go18.go @@ -197,6 +197,10 @@ func (mc *mysqlConn) startWatcher() { } func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) { - nv.Value, err = converter{}.ConvertValue(nv.Value) + value, err := converter{}.ConvertValue(nv.Value) + if err != nil { + return driver.ErrSkip + } + nv.Value = value return } diff --git a/driver_test.go b/driver_test.go index f6965b191..392e752a3 100644 --- a/driver_test.go +++ b/driver_test.go @@ -499,6 +499,36 @@ func TestString(t *testing.T) { }) } +type testValuer struct { + value string +} + +func (tv testValuer) Value() (driver.Value, error) { + return tv.value, nil +} + +func TestValuer(t *testing.T) { + runTests(t, dsn, func(dbt *DBTest) { + in := testValuer{"a_value"} + var out string + var rows *sql.Rows + + dbt.mustExec("CREATE TABLE test (value VARCHAR(255)) CHARACTER SET utf8") + dbt.mustExec("INSERT INTO test VALUES (?)", in) + rows = dbt.mustQuery("SELECT value FROM test") + if rows.Next() { + rows.Scan(&out) + if in.value != out { + dbt.Errorf("Valuer: %v != %s", in, out) + } + } else { + dbt.Errorf("Valuer: no data") + } + + dbt.mustExec("DROP TABLE IF EXISTS test") + }) +} + type timeTests struct { dbtype string tlayout string