diff --git a/driver/mysql/def.go b/driver/mysql/def.go index f116b2c..d888cca 100644 --- a/driver/mysql/def.go +++ b/driver/mysql/def.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/things-go/ens" + "github.com/things-go/ens/schema" "github.com/things-go/ens/utils" ) @@ -49,16 +50,46 @@ type Column struct { ColumnDefault *string `gorm:"column:COLUMN_DEFAULT"` // column default value.null mean not set. IsNullable string `gorm:"column:IS_NULLABLE"` // column null or not, YEW/NO DataType string `gorm:"column:DATA_TYPE"` // column data type(varchar) - CharacterMaximumLength int64 `gorm:"column:CHARACTER_MAXIMUM_LENGTH"` - CharacterOctetLength int64 `gorm:"column:CHARACTER_OCTET_LENGTH"` - NumericPrecision int64 `gorm:"column:NUMERIC_PRECISION"` - NumericScale int64 `gorm:"column:NUMERIC_SCALE"` + CharacterMaximumLength *int64 `gorm:"column:CHARACTER_MAXIMUM_LENGTH"` + CharacterOctetLength *int64 `gorm:"column:CHARACTER_OCTET_LENGTH"` + NumericPrecision *int64 `gorm:"column:NUMERIC_PRECISION"` + NumericScale *int64 `gorm:"column:NUMERIC_SCALE"` ColumnType string `gorm:"column:COLUMN_TYPE"` // column type(varchar(64)) ColumnKey string `gorm:"column:COLUMN_KEY"` // column key, PRI/MUL Extra string `gorm:"column:EXTRA"` // extra (auto_increment) ColumnComment string `gorm:"column:COLUMN_COMMENT"` // column comment } +func (c *Column) IntoSchemaColumn(indexes []*schema.ColumnIndex) *schema.Column { + col := &schema.Column{ + Name: c.ColumnName, + DataType: c.DataType, + ColumnType: c.ColumnType, + Unique: c.ColumnKey == columnKeyUnique, + Nullable: strings.EqualFold(c.IsNullable, nullableTrue), + Default: c.ColumnDefault, + IsPrimaryKey: c.ColumnKey == columnKeyPrimary, + AutoIncrement: c.Extra == extraAutoIncrement, + HasLength: false, + Length: 0, + HasPrecisionScale: false, + Precision: 0, + Scale: 0, + Comment: c.ColumnComment, + Indexes: indexes, + } + if c.CharacterMaximumLength != nil { + col.HasLength = true + col.Length = *c.CharacterMaximumLength + } + if c.NumericPrecision != nil && c.NumericScale != nil { + col.HasPrecisionScale = true + col.Precision = *c.NumericPrecision + col.Scale = *c.NumericScale + } + return col +} + func (c *Column) IntoSqlDefinition() string { nullable := strings.EqualFold(c.IsNullable, nullableTrue) isAutoIncrement := c.Extra == extraAutoIncrement diff --git a/driver/mysql/sql.go b/driver/mysql/sql.go index 9972b0f..c14c6c8 100644 --- a/driver/mysql/sql.go +++ b/driver/mysql/sql.go @@ -227,10 +227,10 @@ func fromSqlColumnDefinition(ordinalPosition int, col *sqlparser.ColumnDefinitio ColumnDefault: nil, IsNullable: "", DataType: colType.Type, - CharacterMaximumLength: 0, - CharacterOctetLength: 0, - NumericPrecision: 0, - NumericScale: 0, + CharacterMaximumLength: nil, + CharacterOctetLength: nil, + NumericPrecision: nil, + NumericScale: nil, ColumnType: "", ColumnKey: "", Extra: "", @@ -262,6 +262,16 @@ func fromSqlColumnDefinition(ordinalPosition int, col *sqlparser.ColumnDefinitio } return length } + if colType.Length != nil { + v := toInt(colType.Length) + column.CharacterMaximumLength = &v + } + if colType.Scale != nil { + v := toInt(colType.Length) + sv := toInt(colType.Scale) + column.NumericPrecision = &v + column.NumericScale = &sv + } isUnsigned := bool(colType.Unsigned) switch colType.Type { diff --git a/schema/column.go b/schema/column.go index 5a691c9..6ae3b2c 100644 --- a/schema/column.go +++ b/schema/column.go @@ -7,6 +7,15 @@ import ( "github.com/things-go/ens/utils" ) +type ColumnIndex struct { + KeyName string + PrimaryKey bool + Unique bool + IsComposite bool + Priority int + IndexType string +} + type Column struct { Name string DataType string // varchar @@ -22,7 +31,7 @@ type Column struct { Precision int64 Scale int64 Comment string - Indexes []*Index + Indexes []*ColumnIndex } // column, type, not null, authIncrement, default, [primaryKey|index], comment