You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
db2go is a command to export database table structure to go or proto file
Usage
$ db2go -h
NAME:
db2go - db2go [options] --url <DSN>
USAGE:
db2go [global options] command [command options] [arguments...]
VERSION:
v2.13.0 2024-07-31 commit <N/A>
COMMANDS:
help, h Shows a list of commands or helpfor one command
GLOBAL OPTIONS:
--url value data source name of database
--out value output path (default: ".")
--db value database name to export
--table value database tables to export
--tag value export tags for golang
--prefix value filename prefix
--suffix value filename suffix
--package value package name
--without value exclude columns split by colon
--readonly value readonly columns split by colon
--proto export protobuf file (default: false)
--spec-type value specify column as customized types, e.g 'user.detail=UserDetail, user.data=UserData'
--enable-decimal decimal as sqlca.Decimal type (default: false)
--gogo-options value gogo proto options
--merge export to one file (default: false)
--dao value generate data access object file
--import-models value project name
--omitempty json omitempty (default: false)
--json-properties value customized properties for json tag
--tinyint-as-bool value convert tinyint columns redeclare as bool type
--ssh value ssh tunnel e.g ssh://root:123456@192.168.1.23:22
--v1 v1 package imports (default: false)
--export value export database DDL to file
--debug open debug mode (default: false)
--json-style value json style: smallcamel or bigcamel (default: "default")
--common-tags value set common tag values, e.g gorm
--proto-options value set protobuf options, multiple options seperated by ';'
--help, -h show help (default: false)
--version, -v print the version (default: false)
1. 编译安装
Ubuntu 20.04 or later
$ sudo apt update && sudo apt install -y make gcc
$ go env -w CGO_ENABLED=1
$ make
2. 数据库表导出到go文件
Windows batch 脚本
@echooffrem 数据源连接串setDSN_URL="mysql://root:123456@127.0.0.1:3306/test?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=true&loc=Local"rem 数据模型(models)和数据库操作对象(dao)文件输出基础目录setOUT_DIR=.
rem 数据模型包名(数据模型文件目录名)setPACK_NAME="models"rem 文件名后缀(一般不需要加)setSUFFIX_NAME=""rem 只读字段(例如create_at/update_at这种通过设置数据库自动生成/更新的时间字段),使用sqlca包Insert/Update/Upsert方法时不会对这些字段进行插入和更新操作)setREAD_ONLY=""rem 指定数据库表名(留空表示导出全部表结构)setTABLE_NAME=""rem 忽略哪些数据库表字段setWITH_OUT=""rem 增加自定义标签名(例如: bson)setTAGS=""rem 指定所有表结构中属于tinyint类型的某些字段为布尔类型setTINYINT_TO_BOOL=""rem 附加的json标签属性(例如: omitempty)setJSON_PROPERTIES=""rem 指定某表的某字段为指定类型,多个表字段以英文逗号分隔(例如:user.is_deleted=bool表示指定user表is_deleted字段为bool类型; 如果不指定表名则所有表的is_deleted字段均为bool类型;支持第三方包类型,例如:user.weight=github.com/shopspring/decimal.Decimal)setSPEC_TYPES="users.extra_data=struct{}"rem 指定其他orm的标签和值(以空格分隔)setCOMMON_TAGS="id=gorm:primarykey created_at=gorm:\"autoCreateTime;type:timestamp\" updated_at=gorm:\"autoUpdateTime;type:timestamp\""rem 数据库操作对象生成目录名setDAO_OUT=dao
rem 数据库操作对象导入数据库表模型数据路径(指定--dao选项时必填)setIMPORT_MODELS="test/sqler/models"rem 导出全部建表SQL到指定文件setDEPLOY_SQL="deploy/test.sql"rem 判断本地系统是否已安装db2go工具,没有则进行安装where db2go.exe
IF"%errorlevel%"=="0" (
echo db2go already installed.
) ELSE (
echo db2go not found in system %%PATH%%, installing...
go install github.com/civet148/db2go@latest
If"%errorlevel%"=="0" (
echo db2go install succeeded
) ELSE (
rem 安装失败,Linux/Mac请安装gcc工具链,Windows系统可以通过链接直接下载二进制(https://github.com/civet148/release/tree/master/db2go/v2)echo error: db2go install failed, Linux/Mac please install gcc tool-chain, Windows download from https://github.com/civet148/release/tree/master/db2go/v2
)
)
rem 判断db2go是否安装成功If"%errorlevel%"=="0" (
db2go --url %DSN_URL% --out %OUT_DIR% --table %TABLE_NAME% --json-properties %JSON_PROPERTIES% --enable-decimal --spec-type %SPEC_TYPES%^
--suffix %SUFFIX_NAME% --package %PACK_NAME% --readonly %READ_ONLY% --without %WITH_OUT% --tinyint-as-bool %TINYINT_TO_BOOL%^
--tag %TAGS% --common-tags %COMMON_TAGS% --dao %DAO_OUT% --import-models %IMPORT_MODELS%^rem --export %DEPLOY_SQL%echo generate go file ok, formatting...
gofmt -w %OUT_DIR%/%PACK_NAME%
)
pause