From 78377e682582cd4ab55117eac476934203fe14dd Mon Sep 17 00:00:00 2001 From: Christoph Pirkl Date: Thu, 27 Jun 2024 14:54:04 +0200 Subject: [PATCH] Restrict conversion to whole numbers --- pkg/connection/result_set.go | 7 ++++++- pkg/connection/result_set_test.go | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/connection/result_set.go b/pkg/connection/result_set.go index a0d4f37..1956f22 100644 --- a/pkg/connection/result_set.go +++ b/pkg/connection/result_set.go @@ -5,6 +5,7 @@ import ( "database/sql" "database/sql/driver" "io" + "math" "reflect" "sync" @@ -111,7 +112,7 @@ func (results *QueryResults) getColumnValue(columnIndex int) driver.Value { // See https://github.com/exasol/exasol-driver-go/issues/113 for details. func convertValue(value any, columnType types.SqlQueryColumnType) driver.Value { if isIntegerColumn(columnType) { - if floatValue, ok := value.(float64); ok { + if floatValue, ok := value.(float64); ok && isIntegerValue(floatValue) { return int64(floatValue) } } @@ -122,6 +123,10 @@ func isIntegerColumn(columnType types.SqlQueryColumnType) bool { return columnType.Type == "DECIMAL" && columnType.Scale != nil && *columnType.Scale == 0 } +func isIntegerValue(value float64) bool { + return value == math.Trunc(value) +} + func (results *QueryResults) fetchNextRowChunk() error { chunk := &types.SqlQueryResponseResultSetData{} err := results.con.Send(context.Background(), &types.FetchCommand{ diff --git a/pkg/connection/result_set_test.go b/pkg/connection/result_set_test.go index d8a7dbb..26212e5 100644 --- a/pkg/connection/result_set_test.go +++ b/pkg/connection/result_set_test.go @@ -290,8 +290,10 @@ func (suite *ResultSetTestSuite) TestConvertValue() { columnType types.SqlQueryColumnType expectedValue driver.Value }{ - {float64(1.1), decimalTypeZeroScale, int64(1)}, // Only this combination will convert the value - {1.1, decimalTypeZeroScale, int64(1)}, + {float64(1), decimalTypeZeroScale, int64(1)}, // Only this combination will convert the value + {float64(-1), decimalTypeZeroScale, int64(-1)}, // Only this combination will convert the value + {float64(10000000000), decimalTypeZeroScale, int64(10000000000)}, // Only this combination will convert the value + {1.1, decimalTypeZeroScale, float64(1.1)}, {float32(1.1), decimalTypeZeroScale, float32(1.1)}, {"string", decimalTypeZeroScale, "string"}, {true, decimalTypeZeroScale, true},