From 0e81889e30976534839798a7c9e8e05245b66f07 Mon Sep 17 00:00:00 2001 From: Paul Bonser Date: Wed, 2 Mar 2016 18:44:17 -0600 Subject: [PATCH 1/2] test and fix for MysSQL float parsing into float64 when placeholders are used --- driver_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++- packets.go | 2 +- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/driver_test.go b/driver_test.go index cd2a9168e..8275f23f1 100644 --- a/driver_test.go +++ b/driver_test.go @@ -365,7 +365,7 @@ func TestInt(t *testing.T) { }) } -func TestFloat(t *testing.T) { +func TestFloat32(t *testing.T) { runTests(t, dsn, func(dbt *DBTest) { types := [2]string{"FLOAT", "DOUBLE"} in := float32(42.23) @@ -388,6 +388,52 @@ func TestFloat(t *testing.T) { }) } +func TestFloat64(t *testing.T) { + runTests(t, dsn, func(dbt *DBTest) { + types := [2]string{"FLOAT", "DOUBLE"} + var expected float64 = 42.23 + var out float64 + var rows *sql.Rows + for _, v := range types { + dbt.mustExec("CREATE TABLE test (value " + v + ")") + dbt.mustExec("INSERT INTO test VALUES (42.23)") + rows = dbt.mustQuery("SELECT value FROM test") + if rows.Next() { + rows.Scan(&out) + if expected != out { + dbt.Errorf("%s: %g != %g", v, expected, out) + } + } else { + dbt.Errorf("%s: no data", v) + } + dbt.mustExec("DROP TABLE IF EXISTS test") + } + }) +} + +func TestFloat64Placeholder(t *testing.T) { + runTests(t, dsn, func(dbt *DBTest) { + types := [2]string{"FLOAT", "DOUBLE"} + var expected float64 = 42.23 + var out float64 + var rows *sql.Rows + for _, v := range types { + dbt.mustExec("CREATE TABLE test (id int, value " + v + ")") + dbt.mustExec("INSERT INTO test VALUES (1, 42.23)") + rows = dbt.mustQuery("SELECT value FROM test WHERE id = ?", 1) + if rows.Next() { + rows.Scan(&out) + if expected != out { + dbt.Errorf("%s: %g != %g", v, expected, out) + } + } else { + dbt.Errorf("%s: no data", v) + } + dbt.mustExec("DROP TABLE IF EXISTS test") + } + }) +} + func TestString(t *testing.T) { runTests(t, dsn, func(dbt *DBTest) { types := [6]string{"CHAR(255)", "VARCHAR(255)", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT"} diff --git a/packets.go b/packets.go index d5ea888fd..8d9166578 100644 --- a/packets.go +++ b/packets.go @@ -1149,7 +1149,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error { continue case fieldTypeFloat: - dest[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4]))) + dest[i] = float32(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4]))) pos += 4 continue From f47acb0bcb0de44571aa4d663513be115aae388d Mon Sep 17 00:00:00 2001 From: Paul Bonser Date: Wed, 2 Mar 2016 18:46:41 -0600 Subject: [PATCH 2/2] Add myself to AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 986f017cb..a5cc5779a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -36,6 +36,7 @@ Lucas Liu Luke Scott Michael Woolnough Nicola Peduzzi +Paul Bonser Runrioter Wung Soroush Pour Stan Putrya