Browse Source

Fix Valuers by returning driver.ErrSkip if couldn't convert type internally (#709)

Fixes #708
Dave Stubbs 8 years ago
parent
commit
b816f3da15
3 changed files with 36 additions and 1 deletions
  1. 1 0
      AUTHORS
  2. 5 1
      connection_go18.go
  3. 30 0
      driver_test.go

+ 1 - 0
AUTHORS

@@ -71,6 +71,7 @@ Zhenye Xie <xiezhenye at gmail.com>
 # Organizations
 
 Barracuda Networks, Inc.
+Counting Ltd.
 Google Inc.
 Keybase Inc.
 Pivotal Inc.

+ 5 - 1
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
 }

+ 30 - 0
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