Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Feb 25, 2022
1 parent 258d704 commit d9dfe85
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
30 changes: 3 additions & 27 deletions internal/check/tb_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (
)

const (
//query table structure
columnQuery = "SELECT COLUMN_NAME,COLUMN_COMMENT,DATA_TYPE,IS_NULLABLE,COLUMN_KEY,COLUMN_TYPE,COLUMN_DEFAULT,EXTRA " +
"FROM information_schema.COLUMNS " +
"WHERE TABLE_SCHEMA = ? AND TABLE_NAME =? " +
"ORDER BY ORDINAL_POSITION"

//query table index
indexQuery = "SELECT TABLE_NAME,COLUMN_NAME,INDEX_NAME,SEQ_IN_INDEX,NON_UNIQUE " +
"FROM information_schema.STATISTICS " +
Expand All @@ -37,6 +31,7 @@ func getTblColumns(db *gorm.DB, schemaName string, tableName string, indexTag bo
if db == nil {
return nil, errors.New("gorm db is nil")
}

mt := getITableInfo(db)
result, err = mt.GetTbColumns(schemaName, tableName)
if err != nil {
Expand All @@ -61,20 +56,6 @@ func getTblColumns(db *gorm.DB, schemaName string, tableName string, indexTag bo
return result, nil
}

type mysqlTableInfo struct {
db *gorm.DB
}

//GetTbColumns Mysql struct
func (t *mysqlTableInfo) GetTbColumns(schemaName string, tableName string) (result []*model.Column, err error) {
return result, t.db.Raw(columnQuery, schemaName, tableName).Scan(&result).Error
}

//GetTbIndex Mysql index
func (t *mysqlTableInfo) GetTbIndex(schemaName string, tableName string) (result []*model.Index, err error) {
return result, t.db.Raw(indexQuery, schemaName, tableName).Scan(&result).Error
}

type defaultTableInfo struct {
db *gorm.DB
}
Expand All @@ -85,21 +66,16 @@ func (t *defaultTableInfo) GetTbColumns(schemaName string, tableName string) (re
if err != nil {
return nil, err
}
us := true
if t.db.Dialector.Name() == "mysql" {
us = false
}
for _, column := range types {
result = append(result, &model.Column{ColumnType: column, TableName: tableName, UseScanType: us})
result = append(result, &model.Column{ColumnType: column, TableName: tableName, UseScanType: t.db.Dialector.Name() != "mysql"})
}

return result, nil
}

//GetTbIndex index
func (t *defaultTableInfo) GetTbIndex(schemaName string, tableName string) (result []*model.Index, err error) {
if dn := t.db.Dialector.Name(); dn != "mysql" {
return nil, errors.New(fmt.Sprintf("not support %s", dn))
return nil, fmt.Errorf("%s dose not support index", dn)
}
return result, t.db.Raw(indexQuery, schemaName, tableName).Scan(&result).Error
}
6 changes: 3 additions & 3 deletions internal/model/tbl_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
// Column table column's info
type Column struct {
gorm.ColumnType
TableName string `gorm:"column:TABLE_NAME"`
Indexes []*Index `gorm:"-"`
UseScanType bool
TableName string `gorm:"column:TABLE_NAME"`
Indexes []*Index `gorm:"-"`
UseScanType bool `gorm:"-"`
dataTypeMap map[string]func(detailType string) (dataType string) `gorm:"-"`
jsonTagNS func(columnName string) string `gorm:"-"`
newTagNS func(columnName string) string `gorm:"-"`
Expand Down

0 comments on commit d9dfe85

Please sign in to comment.