From 994c9915aa14dd664dd72063d0775c7bb34aac12 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 7 Jun 2021 16:16:41 +0800 Subject: [PATCH 1/3] test for unsigned int32 --- integrations/types_test.go | 59 +++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/integrations/types_test.go b/integrations/types_test.go index 91f334d9..539171d5 100644 --- a/integrations/types_test.go +++ b/integrations/types_test.go @@ -7,6 +7,7 @@ package integrations import ( "errors" "fmt" + "math" "math/big" "strconv" "testing" @@ -378,7 +379,7 @@ func TestCustomType2(t *testing.T) { fmt.Println(users) } -func TestUnsigned(t *testing.T) { +func TestUnsignedUint64(t *testing.T) { type MyUnsignedStruct struct { Id uint64 } @@ -403,6 +404,62 @@ func TestUnsigned(t *testing.T) { default: assert.False(t, true, "Unsigned is not implemented") } + + // Only MYSQL database supports unsigned bigint + if testEngine.Dialect().URI().DBType != schemas.MYSQL { + return + } + + cnt, err := testEngine.Insert(&MyUnsignedStruct{ + Id: math.MaxUint64, + }) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + var v MyUnsignedStruct + has, err := testEngine.Get(&v) + assert.NoError(t, err) + assert.True(t, has) + assert.EqualValues(t, uint64(math.MaxUint64), v.Id) +} + +func TestUnsignedUint32(t *testing.T) { + type MyUnsignedInt32Struct struct { + Id uint32 + } + + assert.NoError(t, PrepareEngine()) + assertSync(t, new(MyUnsignedInt32Struct)) + + tables, err := testEngine.DBMetas() + assert.NoError(t, err) + assert.EqualValues(t, 1, len(tables)) + assert.EqualValues(t, 1, len(tables[0].Columns())) + + switch testEngine.Dialect().URI().DBType { + case schemas.SQLITE: + assert.EqualValues(t, "INTEGER", tables[0].Columns()[0].SQLType.Name) + case schemas.MYSQL: + assert.EqualValues(t, "UNSIGNED INT", tables[0].Columns()[0].SQLType.Name) + case schemas.POSTGRES: + assert.EqualValues(t, "BIGINT", tables[0].Columns()[0].SQLType.Name) + case schemas.MSSQL: + assert.EqualValues(t, "BIGINT", tables[0].Columns()[0].SQLType.Name) + default: + assert.False(t, true, "Unsigned is not implemented") + } + + cnt, err := testEngine.Insert(&MyUnsignedInt32Struct{ + Id: math.MaxUint32, + }) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + var v MyUnsignedInt32Struct + has, err := testEngine.Get(&v) + assert.NoError(t, err) + assert.True(t, has) + assert.EqualValues(t, uint64(math.MaxUint32), v.Id) } type MyDecimal big.Int -- 2.40.1 From 83617929ac16a48e58be9c491206cb65d19c4b41 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 8 Jun 2021 23:16:16 +0800 Subject: [PATCH 2/3] Fix postgres uint32 --- dialects/postgres.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dialects/postgres.go b/dialects/postgres.go index 7a2cd87d..544c98e9 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -838,12 +838,12 @@ func (db *postgres) SQLType(c *schemas.Column) string { case schemas.Bit: res = schemas.Boolean return res - case schemas.MediumInt, schemas.Int, schemas.Integer, schemas.UnsignedInt: + case schemas.MediumInt, schemas.Int, schemas.Integer: if c.IsAutoIncrement { return schemas.Serial } return schemas.Integer - case schemas.BigInt, schemas.UnsignedBigInt: + case schemas.BigInt, schemas.UnsignedBigInt, schemas.UnsignedInt: if c.IsAutoIncrement { return schemas.BigSerial } -- 2.40.1 From f282523d4a20dc14962d41d1877c1af8a9061a53 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 9 Jun 2021 09:26:17 +0800 Subject: [PATCH 3/3] Fix mssql --- dialects/mssql.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dialects/mssql.go b/dialects/mssql.go index 15d1cd06..32e7ac50 100644 --- a/dialects/mssql.go +++ b/dialects/mssql.go @@ -284,7 +284,7 @@ func (db *mssql) SQLType(c *schemas.Column) string { case schemas.TimeStampz: res = "DATETIMEOFFSET" c.Length = 7 - case schemas.MediumInt, schemas.UnsignedInt: + case schemas.MediumInt: res = schemas.Int case schemas.Text, schemas.MediumText, schemas.TinyText, schemas.LongText, schemas.Json: res = db.defaultVarchar + "(MAX)" @@ -296,7 +296,7 @@ func (db *mssql) SQLType(c *schemas.Column) string { case schemas.TinyInt: res = schemas.TinyInt c.Length = 0 - case schemas.BigInt, schemas.UnsignedBigInt: + case schemas.BigInt, schemas.UnsignedBigInt, schemas.UnsignedInt: res = schemas.BigInt c.Length = 0 case schemas.NVarchar: -- 2.40.1