From f10e21cb1581b1046445a4f7b3d4442a7ad49ffd Mon Sep 17 00:00:00 2001 From: Lucifer Lai Date: Mon, 4 Jan 2021 01:47:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=AF=B9mysql=20unsi?= =?UTF-8?q?gned=E7=9A=84=E6=95=B4=E5=9E=8B=E5=AD=97=E6=AE=B5=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=8C=E5=B9=B6=E5=B0=86created,created=5F?= =?UTF-8?q?at,updated,updated=5Fat,deleted,deleted=5Fat=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E4=B8=BA=E6=96=B0=E5=A2=9E=EF=BC=8C=E6=9B=B4=E6=96=B0=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=9A=84=E6=97=B6=E9=97=B4=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=A0=87=E8=AF=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dialects/mysql.go | 15 +++++++++++++-- schemas/type.go | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/dialects/mysql.go b/dialects/mysql.go index 32e18a17..fad8c325 100644 --- a/dialects/mysql.go +++ b/dialects/mysql.go @@ -389,9 +389,12 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName } if colType == "FLOAT UNSIGNED" { colType = "FLOAT" - } - if colType == "DOUBLE UNSIGNED" { + } else if colType == "DOUBLE UNSIGNED" { colType = "DOUBLE" + } else { + if strings.Index(colType, "UNSIGNED") > 0 { + colType = strings.Replace(colType, " UNSIGNED", "_UNSIGNED", -1) + } } col.Length = len1 col.Length2 = len2 @@ -419,6 +422,14 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName col.Default = "'" + col.Default + "'" } } + switch strings.ToLower(col.Name) { + case "created_at", "created": + col.IsCreated = true + case "updated_at", "updated": + col.IsUpdated = true + case "deleted_at", "deleted": + col.IsDeleted = true + } cols[col.Name] = col colSeq = append(colSeq, col.Name) } diff --git a/schemas/type.go b/schemas/type.go index 89459a4d..bd8b4a1c 100644 --- a/schemas/type.go +++ b/schemas/type.go @@ -76,6 +76,13 @@ var ( Int = "INT" Integer = "INTEGER" BigInt = "BIGINT" + // 新增对mysql unsigned 的支撑 + UTinyInt = "TINYINT_UNSIGNED" + USmallInt = "SMALLINT_UNSIGNED" + UMediumInt = "MEDIUMINT_UNSIGNED" + UInt = "INT_UNSIGNED" + UBigInt = "BIGINT_UNSIGNED" + // -- end Enum = "ENUM" Set = "SET" @@ -139,6 +146,12 @@ var ( Integer: NUMERIC_TYPE, BigInt: NUMERIC_TYPE, + UTinyInt: NUMERIC_TYPE, + USmallInt: NUMERIC_TYPE, + UMediumInt: NUMERIC_TYPE, + UInt: NUMERIC_TYPE, + UBigInt: NUMERIC_TYPE, + Enum: TEXT_TYPE, Set: TEXT_TYPE, Json: TEXT_TYPE, @@ -273,10 +286,22 @@ var ( // Type2SQLType generate SQLType acorrding Go's type func Type2SQLType(t reflect.Type) (st SQLType) { switch k := t.Kind(); k { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32: + case reflect.Int, reflect.Int32: st = SQLType{Int, 0, 0} - case reflect.Int64, reflect.Uint64: + case reflect.Int16: + st = SQLType{SmallInt, 0, 0} + case reflect.Uint16: + st = SQLType{USmallInt, 0, 0} + case reflect.Uint: + st = SQLType{UInt, 0, 0} + case reflect.Int8: + st = SQLType{TinyInt, 0, 0} + case reflect.Uint8: + st = SQLType{UTinyInt, 0, 0} + case reflect.Int64: st = SQLType{BigInt, 0, 0} + case reflect.Uint64: + st = SQLType{UBigInt, 0, 0} case reflect.Float32: st = SQLType{Float, 0, 0} case reflect.Float64: @@ -312,8 +337,18 @@ func Type2SQLType(t reflect.Type) (st SQLType) { func SQLType2Type(st SQLType) reflect.Type { name := strings.ToUpper(st.Name) switch name { - case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, Serial: - return reflect.TypeOf(1) + case SmallInt: + return reflect.TypeOf(int16(1)) + case USmallInt: + return reflect.TypeOf(uint16(1)) + case MediumInt, Int, Integer, Serial: + return reflect.TypeOf(int32(1)) + case Bit, TinyInt: + return reflect.TypeOf(int8(1)) + case UTinyInt: + return reflect.TypeOf(uint8(1)) + case UBigInt: + return reflect.TypeOf(uint64(1)) case BigInt, BigSerial: return reflect.TypeOf(int64(1)) case Float, Real: