Fetch the repository succeeded.
This action will force synchronization from gorm/gen, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
package check
import (
"context"
"errors"
"gorm.io/gorm"
"gitee.com/whilew/gen/internal/model"
)
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 " +
"WHERE TABLE_SCHEMA = ? AND TABLE_NAME =?"
)
type ITableInfo interface {
GetTbColumns(schemaName string, tableName string) (result []*model.Column, err error)
GetTbIndex(schemaName string, tableName string) (result []*model.Index, err error)
}
func getITableInfo(db *gorm.DB) ITableInfo {
return &mysqlTableInfo{db: db}
}
func getTbColumns(db *gorm.DB, schemaName string, tableName string, indexTag bool) (result []*model.Column, err error) {
if db == nil {
return nil, errors.New("gorm db is nil")
}
mt := getITableInfo(db)
result, err = mt.GetTbColumns(schemaName, tableName)
if err != nil {
return nil, err
}
if !indexTag || len(result) == 0 {
return result, nil
}
index, err := mt.GetTbIndex(schemaName, tableName)
if err != nil { //ignore find index err
db.Logger.Warn(context.Background(), "GetTbIndex for %s,err=%s", tableName, err.Error())
return result, nil
}
if len(index) == 0 {
return result, nil
}
im := model.GroupByColumn(index)
for _, c := range result {
c.Indexes = im[c.ColumnName]
}
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。