From 059515f52bfea4932c3616cdc294801152a2bd6a Mon Sep 17 00:00:00 2001 From: go-jet Date: Sat, 14 Nov 2020 12:14:08 +0100 Subject: [PATCH] QRM: Convert lossless decimal types first to string. --- go.mod | 1 + go.sum | 12 +++++++++-- qrm/utill.go | 4 ++-- tests/postgres/sample_test.go | 39 ++++++++++++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 665b9bd..bc0d574 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/go-jet/jet/v2 go 1.11 require ( + github.com/ericlagergren/decimal v0.0.0-20191206042408-88212e6cfca9 github.com/go-sql-driver/mysql v1.5.0 github.com/google/go-cmp v0.5.0 github.com/google/uuid v1.1.1 diff --git a/go.sum b/go.sum index 9321721..17146cf 100644 --- a/go.sum +++ b/go.sum @@ -1,23 +1,31 @@ +github.com/apmckinlay/gsuneido v0.0.0-20190404155041-0b6cd442a18f/go.mod h1:JU2DOj5Fc6rol0yaT79Csr47QR0vONGwJtBNGRD7jmc= +github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-jet/jet v1.0.0 h1:ENxUe/6lH82qLykIGAdZIlskZrpTeNfxjHz4VHtkVmA= -github.com/go-jet/jet v2.3.0+incompatible h1:Yg7JSERDC0f9x3dHUBMA2cxe9/qC6qlozDDO/s38USU= +github.com/ericlagergren/decimal v0.0.0-20191206042408-88212e6cfca9 h1:mMVotm9OVwoOS2IFGRRS5AfMTFWhtf8wj34JEYh47/k= +github.com/ericlagergren/decimal v0.0.0-20191206042408-88212e6cfca9/go.mod h1:ZWP59etEywfyMG2lAqnoi3t8uoiZCiTmLtwt6iESIsQ= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY= github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.5.0 h1:042Buzk+NhDI+DeSAA62RwJL8VAuZUMQZUjCsRz1Mug= github.com/pkg/profile v1.5.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/qrm/utill.go b/qrm/utill.go index 5d8455d..233c44a 100644 --- a/qrm/utill.go +++ b/qrm/utill.go @@ -255,7 +255,7 @@ func setReflectValue(source, destination reflect.Value) { } } - panic("jet: can't set " + source.Type().String() + " to " + destination.Type().String()) + //panic("jet: can't set " + source.Type().String() + " to " + destination.Type().String()) } func createScanValue(columnTypes []*sql.ColumnType) []interface{} { @@ -308,7 +308,7 @@ func newScanType(columnType *sql.ColumnType) reflect.Type { return nullStringType case "FLOAT4": return nullFloat32Type - case "FLOAT8", "NUMERIC", "DECIMAL", "FLOAT", "DOUBLE": + case "FLOAT8", "FLOAT", "DOUBLE": return nullFloat64Type case "BOOL": return nullBoolType diff --git a/tests/postgres/sample_test.go b/tests/postgres/sample_test.go index 327604f..7ce224c 100644 --- a/tests/postgres/sample_test.go +++ b/tests/postgres/sample_test.go @@ -1,13 +1,17 @@ package postgres import ( + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/require" + "github.com/go-jet/jet/v2/internal/testutils" . "github.com/go-jet/jet/v2/postgres" "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/model" . "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/table" - "github.com/google/uuid" - "github.com/stretchr/testify/require" - "testing" + + "github.com/ericlagergren/decimal/sql/postgres" ) func TestUUIDType(t *testing.T) { @@ -31,6 +35,35 @@ WHERE all_types.uuid = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'; requireLogged(t, query) } +func TestExactDecimals(t *testing.T) { + query := SELECT( + AllTypes.Numeric, + AllTypes.NumericPtr, + AllTypes.Decimal, + AllTypes.DecimalPtr, + ).FROM( + AllTypes, + ).WHERE(AllTypes.UUID.EQ(String("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"))) + + type AllTypes struct { + model.AllTypes + Numeric postgres.Decimal + NumericPtr postgres.Decimal + Decimal postgres.Decimal + DecimalPtr postgres.Decimal + } + + var result AllTypes + + err := query.Query(db, &result) + require.NoError(t, err) + + require.Equal(t, result.Decimal.V.String(), "1.11") + require.Equal(t, result.DecimalPtr.V.String(), "1.11") + require.Equal(t, result.Numeric.V.String(), "2.220") + require.Equal(t, result.NumericPtr.V.String(), "2.220") +} + func TestUUIDComplex(t *testing.T) { query := Person.INNER_JOIN(PersonPhone, PersonPhone.PersonID.EQ(Person.PersonID)). SELECT(Person.AllColumns, PersonPhone.AllColumns).