Gen Tool

Gen Tool 是單一二進位檔,沒有依賴關係,可用於從資料庫產生結構。

安裝

go install gorm.io/gen/tools/gentool@latest

使用

gentool -h

Usage of gentool:
-c string
config file path
-db string
input mysql or postgres or sqlite or sqlserver. consult[https://gorm.dev.org.tw/docs/connecting_to_the_database.html] (default "mysql")
-dsn string
consult[https://gorm.dev.org.tw/docs/connecting_to_the_database.html]
-fieldNullable
generate with pointer when field is nullable
-fieldWithIndexTag
generate field with gorm index tag
-fieldWithTypeTag
generate field with gorm column type tag
-modelPkgName string
generated model code's package name
-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)
-withUnitTest
generate unit test for query code
-fieldSignable
detect integer field's unsigned type, adjust generated data type

c

設定檔名稱,預設值為「」,命令列選項的優先權高於設定檔。

db

指定驅動程式方言,預設值為「mysql」,請參閱:https://gorm.dev.org.tw/docs/connecting_to_the_database.html

dsn

用於連線資料庫的 DSN,請參閱:https://gorm.dev.org.tw/docs/connecting_to_the_database.html

fieldNullable

當欄位為可為空時,產生指標。

fieldWithIndexTag

產生具有 gorm 索引標籤的欄位。

fieldWithTypeTag

產生具有 gorm 欄位類型標籤的欄位。

modelPkgName

產生模型程式碼的套件名稱。

outFile

產生查詢程式碼檔名稱,預設值:gen.go

outPath

指定輸出目錄(預設為「./dao/query」)

tables

指定要從中產生代碼的資料表,預設為所有資料表。

例如

--tables="orders"       # generate from `orders`

--tables="orders,users" # generate from `orders` and `users`

--tables=""             # generate from all tables

產生一些資料表代碼。

withUnitTest

產生單元測試,預設值為 false,選項:false / true

fieldSignable

使用可簽署的資料類型作為欄位類型,預設值為 false,選項:false / true

Example

gentool -dsn "user:pwd@tcp(localhost:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "orders,doctor"

gentool -c "./gen.tool"
version: "0.1"
database:
# consult[https://gorm.dev.org.tw/docs/connecting_to_the_database.html]"
dsn : "username:password@tcp(address:port)/db?charset=utf8mb4&parseTime=true&loc=Local"
# input mysql or postgres or sqlite or sqlserver. consult[https://gorm.dev.org.tw/docs/connecting_to_the_database.html]
db : "mysql"
# enter the required data table or leave it blank.You can input : orders,users,goods
tables : "user"
# specify a directory for output
outPath : "./dao/query"
# query code file name, default: gen.go
outFile : ""
# generate unit test for query code
withUnitTest : false
# generated model code's package name
modelPkgName : ""
# generate with pointer when field is nullable
fieldNullable : false
# generate field with gorm index tag
fieldWithIndexTag : false
# generate field with gorm column type tag
fieldWithTypeTag : false

白金贊助商

金牌贊助商

白金贊助商

金牌贊助商