Skip to content

Commit 7ebe0a5

Browse files
committed
Merge pull request #434 from pib/fix_float_with_placeholder
test and fix for MysSQL float parsing into float64 when placeholders are used Fixes #433
2 parents 1421caf + f47acb0 commit 7ebe0a5

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Lucas Liu <extrafliu at gmail.com>
3737
Luke Scott <luke at webconnex.com>
3838
Michael Woolnough <michael.woolnough at gmail.com>
3939
Nicola Peduzzi <thenikso at gmail.com>
40+
Paul Bonser <misterpib at gmail.com>
4041
Runrioter Wung <runrioter at gmail.com>
4142
Soroush Pour <me at soroushjp.com>
4243
Stan Putrya <root.vagner at gmail.com>

driver_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func TestInt(t *testing.T) {
365365
})
366366
}
367367

368-
func TestFloat(t *testing.T) {
368+
func TestFloat32(t *testing.T) {
369369
runTests(t, dsn, func(dbt *DBTest) {
370370
types := [2]string{"FLOAT", "DOUBLE"}
371371
in := float32(42.23)
@@ -388,6 +388,52 @@ func TestFloat(t *testing.T) {
388388
})
389389
}
390390

391+
func TestFloat64(t *testing.T) {
392+
runTests(t, dsn, func(dbt *DBTest) {
393+
types := [2]string{"FLOAT", "DOUBLE"}
394+
var expected float64 = 42.23
395+
var out float64
396+
var rows *sql.Rows
397+
for _, v := range types {
398+
dbt.mustExec("CREATE TABLE test (value " + v + ")")
399+
dbt.mustExec("INSERT INTO test VALUES (42.23)")
400+
rows = dbt.mustQuery("SELECT value FROM test")
401+
if rows.Next() {
402+
rows.Scan(&out)
403+
if expected != out {
404+
dbt.Errorf("%s: %g != %g", v, expected, out)
405+
}
406+
} else {
407+
dbt.Errorf("%s: no data", v)
408+
}
409+
dbt.mustExec("DROP TABLE IF EXISTS test")
410+
}
411+
})
412+
}
413+
414+
func TestFloat64Placeholder(t *testing.T) {
415+
runTests(t, dsn, func(dbt *DBTest) {
416+
types := [2]string{"FLOAT", "DOUBLE"}
417+
var expected float64 = 42.23
418+
var out float64
419+
var rows *sql.Rows
420+
for _, v := range types {
421+
dbt.mustExec("CREATE TABLE test (id int, value " + v + ")")
422+
dbt.mustExec("INSERT INTO test VALUES (1, 42.23)")
423+
rows = dbt.mustQuery("SELECT value FROM test WHERE id = ?", 1)
424+
if rows.Next() {
425+
rows.Scan(&out)
426+
if expected != out {
427+
dbt.Errorf("%s: %g != %g", v, expected, out)
428+
}
429+
} else {
430+
dbt.Errorf("%s: no data", v)
431+
}
432+
dbt.mustExec("DROP TABLE IF EXISTS test")
433+
}
434+
})
435+
}
436+
391437
func TestString(t *testing.T) {
392438
runTests(t, dsn, func(dbt *DBTest) {
393439
types := [6]string{"CHAR(255)", "VARCHAR(255)", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT"}

packets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
11491149
continue
11501150

11511151
case fieldTypeFloat:
1152-
dest[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4])))
1152+
dest[i] = float32(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4])))
11531153
pos += 4
11541154
continue
11551155

0 commit comments

Comments
 (0)