From c88bb03dd8737c05ce91731f655f0786b46408b6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 28 Jul 2021 23:05:57 +0800 Subject: [PATCH] Fix DBMetas returned unsigned tinyint --- integrations/types_test.go | 39 ++++++++++++++++++++++++++++++++ schemas/type.go | 46 +++++++++++++++++++++----------------- tags/parser.go | 4 ++++ tags/tag.go | 8 +++++++ 4 files changed, 77 insertions(+), 20 deletions(-) diff --git a/integrations/types_test.go b/integrations/types_test.go index 9d4e46a0..9f308a5b 100644 --- a/integrations/types_test.go +++ b/integrations/types_test.go @@ -493,6 +493,45 @@ func TestUnsignedUint32(t *testing.T) { assert.EqualValues(t, uint64(math.MaxUint32), v.Id) } +func TestUnsignedTinyInt(t *testing.T) { + type MyUnsignedTinyIntStruct struct { + Id uint8 `xorm:"unsigned tinyint"` + } + + assert.NoError(t, PrepareEngine()) + assertSync(t, new(MyUnsignedTinyIntStruct)) + + 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 TINYINT", tables[0].Columns()[0].SQLType.Name) + case schemas.POSTGRES: + assert.EqualValues(t, "INT", tables[0].Columns()[0].SQLType.Name) + case schemas.MSSQL: + assert.EqualValues(t, "INT", tables[0].Columns()[0].SQLType.Name) + default: + assert.False(t, true, "Unsigned is not implemented") + } + + cnt, err := testEngine.Insert(&MyUnsignedTinyIntStruct{ + Id: math.MaxUint8, + }) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + var v MyUnsignedTinyIntStruct + 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 func (d *MyDecimal) FromDB(data []byte) error { diff --git a/schemas/type.go b/schemas/type.go index d64251bf..d799db08 100644 --- a/schemas/type.go +++ b/schemas/type.go @@ -92,16 +92,19 @@ func (s *SQLType) IsXML() bool { // enumerates all the database column types var ( - Bit = "BIT" - UnsignedBit = "UNSIGNED BIT" - TinyInt = "TINYINT" - SmallInt = "SMALLINT" - MediumInt = "MEDIUMINT" - Int = "INT" - UnsignedInt = "UNSIGNED INT" - Integer = "INTEGER" - BigInt = "BIGINT" - UnsignedBigInt = "UNSIGNED BIGINT" + Bit = "BIT" + UnsignedBit = "UNSIGNED BIT" + TinyInt = "TINYINT" + UnsignedTinyInt = "UNSIGNED TINYINT" + SmallInt = "SMALLINT" + UnsignedSmallInt = "UNSIGNED SMALLINT" + MediumInt = "MEDIUMINT" + UnsignedMediumInt = "UNSIGNED MEDIUMINT" + Int = "INT" + UnsignedInt = "UNSIGNED INT" + Integer = "INTEGER" + BigInt = "BIGINT" + UnsignedBigInt = "UNSIGNED BIGINT" Enum = "ENUM" Set = "SET" @@ -158,16 +161,19 @@ var ( Array = "ARRAY" SqlTypes = map[string]int{ - Bit: NUMERIC_TYPE, - UnsignedBit: NUMERIC_TYPE, - TinyInt: NUMERIC_TYPE, - SmallInt: NUMERIC_TYPE, - MediumInt: NUMERIC_TYPE, - Int: NUMERIC_TYPE, - UnsignedInt: NUMERIC_TYPE, - Integer: NUMERIC_TYPE, - BigInt: NUMERIC_TYPE, - UnsignedBigInt: NUMERIC_TYPE, + Bit: NUMERIC_TYPE, + UnsignedBit: NUMERIC_TYPE, + TinyInt: NUMERIC_TYPE, + UnsignedTinyInt: NUMERIC_TYPE, + SmallInt: NUMERIC_TYPE, + UnsignedSmallInt: NUMERIC_TYPE, + MediumInt: NUMERIC_TYPE, + UnsignedMediumInt: NUMERIC_TYPE, + Int: NUMERIC_TYPE, + UnsignedInt: NUMERIC_TYPE, + Integer: NUMERIC_TYPE, + BigInt: NUMERIC_TYPE, + UnsignedBigInt: NUMERIC_TYPE, Enum: TEXT_TYPE, Set: TEXT_TYPE, diff --git a/tags/parser.go b/tags/parser.go index 7d8c3bd6..cf89211b 100644 --- a/tags/parser.go +++ b/tags/parser.go @@ -217,6 +217,10 @@ func (parser *Parser) parseFieldWithTags(table *schemas.Table, fieldIndex int, f if col.SQLType.Name == "" { col.SQLType = schemas.Type2SQLType(field.Type) } + if ctx.isUnsigned && col.SQLType.IsNumeric() && !strings.HasPrefix(col.SQLType.Name, "UNSIGNED") { + col.SQLType.Name = col.SQLType.Name + " UNSIGNED" + } + parser.dialect.SQLType(col) if col.Length == 0 { col.Length = col.SQLType.DefaultLength diff --git a/tags/tag.go b/tags/tag.go index 32cdb37c..4e1f1ce7 100644 --- a/tags/tag.go +++ b/tags/tag.go @@ -93,6 +93,7 @@ type Context struct { hasCacheTag bool hasNoCacheTag bool ignoreNext bool + isUnsigned bool } // Handler describes tag handler for XORM @@ -122,6 +123,7 @@ var ( "NOCACHE": NoCacheTagHandler, "COMMENT": CommentTagHandler, "EXTENDS": ExtendsTagHandler, + "UNSIGNED": UnsignedTagHandler, } ) @@ -268,6 +270,12 @@ func UniqueTagHandler(ctx *Context) error { return nil } +// UnsignedTagHandler represents the column is unsigned +func UnsignedTagHandler(ctx *Context) error { + ctx.isUnsigned = true + return nil +} + // CommentTagHandler add comment to column func CommentTagHandler(ctx *Context) error { if len(ctx.params) > 0 {