Skip to content

Commit

Permalink
adjust generated data type
Browse files Browse the repository at this point in the history
  • Loading branch information
wulorn committed Jul 10, 2022
1 parent 6b318c4 commit 741fbf2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
7 changes: 7 additions & 0 deletions tools/gentool/README.ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
only generate models (without query file)
-withUnitTest
generate unit test for query code
-fieldSignable
detect integer field's unsigned type, adjust generated data type

```

Expand Down Expand Up @@ -112,6 +114,11 @@ eg :

生成单元测试。

#### fieldSignable

Value : False / True

基于数据表定义的数据类型,生成对应的数据类型


### 使用示例
Expand Down
8 changes: 8 additions & 0 deletions tools/gentool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Install GEN as a binary tool
only generate models (without query file)
-withUnitTest
generate unit test for query code
-fieldSignable
detect integer field's unsigned type, adjust generated data type

```
#### c
Expand Down Expand Up @@ -106,6 +108,12 @@ Value : False / True

Generate unit test.

#### fieldSignable

Value : False / True

detect integer field's unsigned type, adjust generated data type



### example
Expand Down
10 changes: 8 additions & 2 deletions tools/gentool/gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ database:
dsn : "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
# input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
db : "mysql"
# enter the required data table or leave it blank.You can input : orders,users,goods
tables : ""
# enter the required data table or leave it blank.You can input :
# tables :
# - orders
# - users
# - goods
tables :
# specify a directory for output
outPath : "./dao/query"
# query code file name, default: gen.go
Expand All @@ -20,3 +24,5 @@ database:
fieldWithIndexTag : false
# generate field with gorm column type tag
fieldWithTypeTag : false
# detect integer field's unsigned type, adjust generated data type
fieldSignable : false
36 changes: 21 additions & 15 deletions tools/gentool/gentool.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ const (

// CmdParams is command line parameters
type CmdParams struct {
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
Tables string `yaml:"tables"` // enter the required data table or leave it blank
OnlyModel bool `yaml:"onlyModel"` // only generate model
OutPath string `yaml:"outPath"` // specify a directory for output
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
ModelPkgName string `yaml:"modelPkgName"` // generated model code's package name
FieldNullable bool `yaml:"fieldNullable"` // generate with pointer when field is nullable
FieldWithIndexTag bool `yaml:"fieldWithIndexTag"` // generate field with gorm index tag
FieldWithTypeTag bool `yaml:"fieldWithTypeTag"` // generate field with gorm column type tag
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
Tables []string `yaml:"tables"` // enter the required data table or leave it blank
OnlyModel bool `yaml:"onlyModel"` // only generate model
OutPath string `yaml:"outPath"` // specify a directory for output
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
ModelPkgName string `yaml:"modelPkgName"` // generated model code's package name
FieldNullable bool `yaml:"fieldNullable"` // generate with pointer when field is nullable
FieldWithIndexTag bool `yaml:"fieldWithIndexTag"` // generate field with gorm index tag
FieldWithTypeTag bool `yaml:"fieldWithTypeTag"` // generate field with gorm column type tag
FieldSignable bool `yaml:"fieldSignable"` // detect integer field's unsigned type, adjust generated data type
}

// YamlConfig is yaml config struct
Expand Down Expand Up @@ -73,16 +74,16 @@ func connectDB(t DBType, dsn string) (*gorm.DB, error) {
}

// genModels is gorm/gen generated models
func genModels(g *gen.Generator, db *gorm.DB, tables string) (models []interface{}, err error) {
func genModels(g *gen.Generator, db *gorm.DB, tables []string) (models []interface{}, err error) {
var tablesList []string
if tables == "" {
if len(tables) == 0 {
// Execute tasks for all tables in the database
tablesList, err = db.Migrator().GetTables()
if err != nil {
return nil, fmt.Errorf("GORM migrator get all tables fail: %w", err)
}
} else {
tablesList = strings.Split(tables, ",")
tablesList = tables
}

// Execute some data table tasks
Expand Down Expand Up @@ -122,6 +123,7 @@ func argParse() *CmdParams {
fieldNullable := flag.Bool("fieldNullable", false, "generate with pointer when field is nullable")
fieldWithIndexTag := flag.Bool("fieldWithIndexTag", false, "generate field with gorm index tag")
fieldWithTypeTag := flag.Bool("fieldWithTypeTag", false, "generate field with gorm column type tag")
fieldSignable := flag.Bool("fieldSignable", false, "detect integer field's unsigned type, adjust generated data type")
flag.Parse()
var cmdParse CmdParams
if *genPath != "" {
Expand All @@ -137,7 +139,7 @@ func argParse() *CmdParams {
cmdParse.DB = *db
}
if *tableList != "" {
cmdParse.Tables = *tableList
cmdParse.Tables = strings.Split(*tableList, ",")
}
if *onlyModel {
cmdParse.OnlyModel = true
Expand All @@ -163,6 +165,9 @@ func argParse() *CmdParams {
if *fieldWithTypeTag {
cmdParse.FieldWithTypeTag = *fieldWithTypeTag
}
if *fieldSignable {
cmdParse.FieldSignable = *fieldSignable
}
return &cmdParse
}

Expand All @@ -185,6 +190,7 @@ func main() {
FieldNullable: config.FieldNullable,
FieldWithIndexTag: config.FieldWithIndexTag,
FieldWithTypeTag: config.FieldWithTypeTag,
FieldSignable: config.FieldSignable,
})

g.UseDB(db)
Expand Down

0 comments on commit 741fbf2

Please sign in to comment.