Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions tools/gentool/README.ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

将Gen作为二进制的方式进行安装



## 安装

```shell
Expand All @@ -13,40 +11,47 @@
## 使用方式

```shell

gentool -h

Usage of gentool:
gentool -h

Usage of gentool:
-c string
is path for gen.yml
-db string
input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html] (default "mysql")
input mysql|postgres|sqlite|sqlserver|clickhouse. consult[https://gorm.io/docs/connecting_to_the_database.html] (default "mysql")
-dsn string
consult[https://gorm.io/docs/connecting_to_the_database.html]
-fieldNullable
generate with pointer when field is nullable
-fieldCoverable
generate with pointer when field has default value
-fieldNullable
generate with pointer when field is nullable
-fieldSignable
detect integer field's unsigned type, adjust generated data type
-fieldWithIndexTag
generate field with gorm index tag
-fieldWithTypeTag
generate field with gorm column type tag
-modelPkgName string
generated model code's package name
-onlyModel
only generate models (without query file)
-outFile string
query code file name, default: gen.go
-outPath string
specify a directory for output (default "./dao/query")
-tables string
enter the required data table or leave it blank
-onlyModel
only generate models (without query file)
-withDefaultQuery
create default query in generated code
-withQueryInterface
generate code with exported interface object
-withUnitTest
generate unit test for query code
-fieldSignable
detect integer field's unsigned type, adjust generated data type

-withoutContext
generate code without context constrain
```

#### c

default ""
可以指定配置文件gen.yml的路径。
用配置文件来代替命令行。
Expand Down Expand Up @@ -126,6 +131,23 @@ Value : False / True

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

#### withDefaultQuery

默认: false

为 true 时,生成 query 类的默认实例变量。

#### withQueryInterface

默认: false

为 true 时,生成 query 类的相应接口。

#### withoutContext

默认: false

为 true 时,生成的 query 类不接受 context 参数。

### 使用示例

Expand Down
47 changes: 35 additions & 12 deletions tools/gentool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,52 @@ Install GEN as a binary tool
## usage

```shell

gentool -h

Usage of gentool:
gentool -h

Usage of gentool:
-c string
is path for gen.yml
-db string
input mysql|postgres|sqlite|sqlserver|clickhouse. consult[https://gorm.io/docs/connecting_to_the_database.html] (default "mysql")
-dsn string
consult[https://gorm.io/docs/connecting_to_the_database.html]
-fieldNullable
generate with pointer when field is nullable
-fieldCoverable
generate with pointer when field has default value
-fieldNullable
generate with pointer when field is nullable
-fieldSignable
detect integer field's unsigned type, adjust generated data type
-fieldWithIndexTag
generate field with gorm index tag
-fieldWithTypeTag
generate field with gorm column type tag
-modelPkgName string
generated model code's package name
-onlyModel
only generate models (without query file)
-outFile string
query code file name, default: gen.go
-outPath string
specify a directory for output (default "./dao/query")
-tables string
enter the required data table or leave it blank
-onlyModel
only generate models (without query file)
-withDefaultQuery
create default query in generated code
-withQueryInterface
generate code with exported interface object
-withUnitTest
generate unit test for query code
-fieldSignable
detect integer field's unsigned type, adjust generated data type

-withoutContext
generate code without context constrain
```

#### c

default ""
Is path for gen.yml
Replace the command line with a configuration file
The command line is the highest priority


#### db

default:mysql
Expand Down Expand Up @@ -120,7 +127,23 @@ Value : False / True

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

#### withDefaultQuery

Default: false

create default query in generated code

#### withQueryInterface

Default: false

generate code with exported interface object

#### withoutContext

Default: false

generate code without context constrain

### example

Expand Down
6 changes: 6 additions & 0 deletions tools/gentool/gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ database:
fieldWithTypeTag : false
# detect integer field's unsigned type, adjust generated data type
fieldSignable : false
# create default query in generated code
withDefaultQuery: false
# generate code without context constrain
withoutContext: false
# generate code with exported interface object
withQueryInterface: false
60 changes: 45 additions & 15 deletions tools/gentool/gentool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gen"
"gorm.io/gorm"

"gorm.io/gen"
)

// DBType database type
Expand All @@ -34,19 +35,22 @@ 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
FieldCoverable bool `yaml:"fieldCoverable"` // generate with pointer when field has default value
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
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
FieldCoverable bool `yaml:"fieldCoverable"` // generate with pointer when field has default value
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
WithDefaultQuery bool `yaml:"withDefaultQuery"` // create default query in generated code
WithoutContext bool `yaml:"withoutContext"` // generate code without context constrain
WithQueryInterface bool `yaml:"withQueryInterface"` // generate code with exported interface object
}

func (c *CmdParams) revise() *CmdParams {
Expand Down Expand Up @@ -154,9 +158,13 @@ func argParse() *CmdParams {
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")
WithDefaultQuery := flag.Bool("withDefaultQuery", false, "create default query in generated code")
WithoutContext := flag.Bool("withoutContext", false, "generate code without context constrain")
WithQueryInterface := flag.Bool("withQueryInterface", false, "generate code with exported interface object")

flag.Parse()

if *genPath != "" { //use yml config
if *genPath != "" { // use yml config
return parseCmdFromYaml(*genPath)
}

Expand Down Expand Up @@ -201,6 +209,16 @@ func argParse() *CmdParams {
if *fieldSignable {
cmdParse.FieldSignable = *fieldSignable
}
if *WithDefaultQuery {
cmdParse.WithDefaultQuery = true
}
if *WithoutContext {
cmdParse.WithoutContext = true
}
if *WithQueryInterface {
cmdParse.WithQueryInterface = true
}

return &cmdParse
}

Expand All @@ -216,6 +234,17 @@ func main() {
log.Fatalln("connect db server fail:", err)
}

var generateMode gen.GenerateMode
if config.WithDefaultQuery {
generateMode |= gen.WithDefaultQuery
}
if config.WithoutContext {
generateMode |= gen.WithoutContext
}
if config.WithQueryInterface {
generateMode |= gen.WithQueryInterface
}

g := gen.NewGenerator(gen.Config{
OutPath: config.OutPath,
OutFile: config.OutFile,
Expand All @@ -226,6 +255,7 @@ func main() {
FieldWithIndexTag: config.FieldWithIndexTag,
FieldWithTypeTag: config.FieldWithTypeTag,
FieldSignable: config.FieldSignable,
Mode: generateMode,
})

g.UseDB(db)
Expand Down
Loading