Skip to content

Commit

Permalink
fix: fix column index
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Jul 16, 2023
1 parent a3de779 commit bcdf644
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
39 changes: 35 additions & 4 deletions driver/mysql/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/things-go/ens"
"github.com/things-go/ens/schema"
"github.com/things-go/ens/utils"
)

Expand Down Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions driver/mysql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit bcdf644

Please sign in to comment.