Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Oct 18, 2021
1 parent 5bfbccc commit d5c635a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
13 changes: 9 additions & 4 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@ func (g *Generator) GenerateModel(tableName string, opts ...model.MemberOpt) *ch

// GenerateModel catch table info from db, return a BaseStruct
func (g *Generator) GenerateModelAs(tableName string, modelName string, fieldOpts ...model.MemberOpt) *check.BaseStruct {
mc := model.DBConf{ModelPkg: g.Config.ModelPkgPath, TableName: tableName, ModelName: modelName,
SchemaNameOpts: g.dbNameOpts, MemberOpts: fieldOpts, Nullable: g.FieldNullable, WithIndexTag: g.FieldWithIndexTag}

s, err := check.GenBaseStructs(g.db, mc)
s, err := check.GenBaseStructs(g.db, model.DBConf{
ModelPkg: g.Config.ModelPkgPath,
TableName: tableName,
ModelName: modelName,
SchemaNameOpts: g.dbNameOpts,
MemberOpts: fieldOpts,
FieldNullable: g.FieldNullable,
FieldWithIndexTag: g.FieldWithIndexTag,
})
if err != nil {
g.db.Logger.Error(context.Background(), "generate struct from table fail: %s", err)
panic("generate struct fail")
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,6 @@ gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.12/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.14/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.15 h1:gAyaDoPw0lCyrSFWhBlahbUA1U4P5RViC1uIqoB+1Rk=
gorm.io/gorm v1.21.15/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.16 h1:YBIQLtP5PLfZQz59qfrq7xbrK7KWQ+JsXXCH/THlMqs=
gorm.io/gorm v1.21.16/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
Expand Down
13 changes: 8 additions & 5 deletions internal/check/gen_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,27 @@ const (

// GenBaseStructs generate db model by table name
func GenBaseStructs(db *gorm.DB, conf model.DBConf) (bases *BaseStruct, err error) {
modelName := conf.ModelName
tableName := conf.TableName
modelName, tableName := conf.ModelName, conf.TableName

if _, ok := db.Config.Dialector.(tests.DummyDialector); ok {
return nil, fmt.Errorf("UseDB() is necessary to generate model struct [%s] from database table [%s]", modelName, tableName)
}

if err = checkModelName(modelName); err != nil {
return nil, fmt.Errorf("model name %q is invalid: %w", modelName, err)
}

modelPkg := conf.ModelPkg
if modelPkg == "" {
modelPkg = DefaultModelPkg
}
modelPkg = filepath.Base(modelPkg)
dbName := conf.GetSchemaName(db)
columns, err := getTbColumns(db, dbName, tableName, conf.WithIndexTag)

columns, err := getTbColumns(db, conf.GetSchemaName(db), tableName, conf.FieldWithIndexTag)
if err != nil {
return nil, err
}

base := BaseStruct{
Source: model.TableName,
GenBaseStruct: true,
Expand All @@ -53,9 +55,10 @@ func GenBaseStructs(db *gorm.DB, conf model.DBConf) (bases *BaseStruct, err erro
S: strings.ToLower(modelName[0:1]),
StructInfo: parser.Param{Type: modelName, Package: modelPkg},
}

modifyOpts, filterOpts, createOpts := conf.SortOpt()
for _, field := range columns {
m := field.ToMember(conf.Nullable)
m := field.ToMember(conf.FieldNullable)

if filterMember(m, filterOpts) == nil {
continue
Expand Down
14 changes: 9 additions & 5 deletions internal/check/dal.go → internal/check/tb_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ 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"
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 =?"
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 {
Expand All @@ -32,6 +35,7 @@ func getTbColumns(db *gorm.DB, schemaName string, tableName string, indexTag boo
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 @@ -40,12 +44,12 @@ func getTbColumns(db *gorm.DB, schemaName string, tableName string, indexTag boo
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
}
Expand Down
12 changes: 7 additions & 5 deletions internal/model/db_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
)

type DBConf struct {
ModelPkg string
TableName string
ModelName string
ModelPkg string
TableName string
ModelName string

SchemaNameOpts []SchemaNameOpt
MemberOpts []MemberOpt
Nullable bool
WithIndexTag bool

FieldNullable bool
FieldWithIndexTag bool
}

func (cf *DBConf) SortOpt() (modifyOpts []MemberOpt, filterOpts []MemberOpt, createOpts []MemberOpt) {
Expand Down
5 changes: 2 additions & 3 deletions internal/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ var defaultMysqlSchemaNameOpt = SchemaNameOpt(func(db *gorm.DB) string {
return dbName[1:end]
})

type MemberOpt interface {
Self() func(*Member) *Member
}
type MemberOpt interface{ Self() func(*Member) *Member }

type ModifyMemberOpt func(*Member) *Member

func (o ModifyMemberOpt) Self() func(*Member) *Member { return o }
Expand Down

0 comments on commit d5c635a

Please sign in to comment.