├── .gitignore ├── migration ├── demo.go └── artisan.go ├── go.mod ├── version.go ├── go.sum ├── examples └── t2t.go ├── cmd ├── make.sh ├── readme.md └── cli.go ├── LICENSE ├── README.md └── table2struct.go /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | -------------------------------------------------------------------------------- /migration/demo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func Up() { 4 | 5 | } 6 | 7 | func Down() { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/gohouse/converter 2 | 3 | go 1.12 4 | 5 | require github.com/go-sql-driver/mysql v1.4.1 6 | -------------------------------------------------------------------------------- /version.go: -------------------------------------------------------------------------------- 1 | package converter 2 | 3 | const VERSION = "0.0.3" 4 | 5 | const VERSION_TEXT = "convert of mysql schema to golang struct" 6 | 7 | const LIB_IMG = `` 8 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= 2 | github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= 3 | -------------------------------------------------------------------------------- /examples/t2t.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/gohouse/converter" 6 | ) 7 | 8 | func main() { 9 | t2t := converter.NewTable2Struct() 10 | 11 | err := t2t. 12 | SavePath("/home/go/project/model/model.go"). 13 | Dsn("root:root@tcp(localhost:3306)/test?charset=utf8"). 14 | Run() 15 | fmt.Println(err) 16 | } 17 | -------------------------------------------------------------------------------- /cmd/make.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # 版本 4 | version="v0.0.3" 5 | 6 | # 名字 7 | darwin=table2struct-darwin."$version".bin 8 | linux=table2struct-linux."$version".bin 9 | win=table2struct-win."$version".exe 10 | 11 | # 打包 12 | go build -o "$darwin" cli.go 13 | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "$linux" cli.go 14 | CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o "$win" cli.go 15 | 16 | # 压缩 17 | upx $darwin 18 | upx $linux 19 | upx $win -------------------------------------------------------------------------------- /migration/artisan.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/gohouse/gorose" 6 | ) 7 | 8 | // ./artisan make (create_users_table) 生成迁移:创建用户表 9 | // ./artisan migrate 运行迁移 10 | // ./artisan rollback 回滚迁移 11 | // ./artisan reset 回滚所有的应用迁移 12 | // ./artisan refresh 在单个命令中回滚 & 迁移 13 | // ./artisan fresh 删除所有表 & 迁移 14 | 15 | var conn *gorose.Connection 16 | var err error 17 | 18 | func init() { 19 | } 20 | func main() { 21 | conn,err = gorose.Open("mysql", "gcore:gcore@tcp(192.168.200.248:3306)/test?charset=utf8") 22 | fmt.Println(conn,err) 23 | } -------------------------------------------------------------------------------- /cmd/readme.md: -------------------------------------------------------------------------------- 1 | ## 使用方法 2 | 3 | 1. 直接运行 4 | ```sh 5 | go run cli.go --file model.go --dsn root:root@tcp\(localhost:3306\)/test?charset=utf8 --table user 6 | ``` 7 | 8 | 2. 编译运行 9 | ```sh 10 | go build -o t2t.bin cli.go 11 | ./t2t.bin -file model.go -dsn xxx -table user 12 | ``` 13 | 14 | 3. 参数说明 15 | ```sh 16 | -dsn string 数据库dsn配置 17 | -enableJsonTag bool 是否添加json的tag 18 | -file string 保存路径 19 | -packageName string 包名 20 | -prefix string 表前缀 21 | -realNameMethod string 结构体对应的表名 22 | -table string 要迁移的表 23 | -tagKey string tag的key 24 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 飞哥(fizzday) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /cmd/cli.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "github.com/gohouse/converter" 7 | "log" 8 | ) 9 | 10 | func main() { 11 | parser() 12 | } 13 | 14 | func parser() { 15 | dsn := flag.String("dsn", "", "数据库dsn配置") 16 | file := flag.String("file", "", "保存路径") 17 | table := flag.String("table", "", "要迁移的表") 18 | realNameMethod := flag.String("realNameMethod", "", "结构体对应的表名") 19 | packageName := flag.String("packageName", "model", "生成的struct包名") 20 | tagKey := flag.String("tagKey", "orm", "字段tag的key") 21 | prefix := flag.String("prefix", "", "表前缀") 22 | version := flag.Bool("version", false, "版本号") 23 | v := flag.Bool("v", false, "版本号") 24 | enableJsonTag := flag.Bool("enableJsonTag", false, "是否添加json的tag,默认false") 25 | h := flag.Bool("h", false, "帮助") 26 | help := flag.Bool("help", false, "帮助") 27 | 28 | // 开始 29 | flag.Parse() 30 | 31 | if *h || *help { 32 | flag.Usage() 33 | return 34 | } 35 | 36 | // 版本号 37 | if *version || *v { 38 | fmt.Println(fmt.Sprintf("\n version: %s\n %s\n using -h param for more help \n", 39 | converter.VERSION, converter.VERSION_TEXT)) 40 | return 41 | } 42 | 43 | // 初始化 44 | t2t := converter.NewTable2Struct() 45 | // 个性化配置 46 | t2t.Config(&converter.T2tConfig{ 47 | // 如果字段首字母本来就是大写, 就不添加tag, 默认false添加, true不添加 48 | RmTagIfUcFirsted: false, 49 | // tag的字段名字是否转换为小写, 如果本身有大写字母的话, 默认false不转 50 | TagToLower: false, 51 | // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 52 | UcFirstOnly: false, 53 | //// 每个struct放入单独的文件,默认false,放入同一个文件(暂未提供) 54 | //SeperatFile: false, 55 | }) 56 | // 开始迁移转换 57 | err := t2t. 58 | // 指定某个表,如果不指定,则默认全部表都迁移 59 | Table(*table). 60 | // 表前缀 61 | Prefix(*prefix). 62 | // 是否添加json tag 63 | EnableJsonTag(*enableJsonTag). 64 | // 生成struct的包名(默认为空的话, 则取名为: package model) 65 | PackageName(*packageName). 66 | // tag字段的key值,默认是orm 67 | TagKey(*tagKey). 68 | // 是否添加结构体方法获取表名 69 | RealNameMethod(*realNameMethod). 70 | // 生成的结构体保存路径 71 | SavePath(*file). 72 | // 数据库dsn 73 | Dsn(*dsn). 74 | // 执行 75 | Run() 76 | 77 | if err != nil { 78 | log.Println(err.Error()) 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | a lib for golang , generate mysql table schema to golang struct 2 | ----- 3 | mysql表结构自动生成golang struct 4 | 5 | ## github地址 6 | [https://github.com/gohouse/converter](https://github.com/gohouse/converter) 7 | 8 | ## 安装 9 | 1. 直接下载可执行文件: [下载地址](https://github.com/gohouse/converter/releases) 10 | 2. golang源码包: `go get github.com/gohouse/converter` 11 | 12 | ## 示例表结构 13 | ```sql 14 | CREATE TABLE `prefix_user` ( 15 | `Id` int(11) NOT NULL AUTO_INCREMENT, 16 | `Email` varchar(32) NOT NULL DEFAULT '' COMMENT '邮箱', 17 | `Password` varchar(32) NOT NULL DEFAULT '' COMMENT '密码', 18 | `CreatedAt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 19 | PRIMARY KEY (`Id`) 20 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表' 21 | ``` 22 | 23 | ## 命令行用法 24 | 1. 下载对应平台的可执行文件, [下载地址](https://github.com/gohouse/converter/releases) 25 | 26 | 2. 命令行执行 27 | ```sh 28 | # 文件名: table2struct-[$platform].[$version].[$suffix] 29 | ./table2struct-linux.v0.0.3.bin -file model.go -dsn xxx -table user 30 | ``` 31 | 32 | 3. 参数说明 33 | ```sh 34 | -dsn string 数据库dsn配置 35 | -enableJsonTag bool 是否添加json的tag 36 | -file string 保存路径 37 | -packageName string 包名 38 | -prefix string 表前缀 39 | -realNameMethod string 结构体对应的表名 40 | -table string 要迁移的表 41 | -tagKey string tag的key 42 | ``` 43 | 44 | ## golang代码简单用法 45 | ```go 46 | package main 47 | import ( 48 | "fmt" 49 | "github.com/gohouse/converter" 50 | ) 51 | func main() { 52 | err := converter.NewTable2Struct(). 53 | SavePath("/home/go/project/model/model.go"). 54 | Dsn("root:root@tcp(localhost:3306)/test?charset=utf8"). 55 | Run() 56 | fmt.Println(err) 57 | } 58 | ``` 59 | 60 | ## golang代码详细用法示例 61 | ```go 62 | package main 63 | 64 | import ( 65 | "fmt" 66 | "github.com/gohouse/converter" 67 | ) 68 | 69 | func main() { 70 | // 初始化 71 | t2t := converter.NewTable2Struct() 72 | // 个性化配置 73 | t2t.Config(&converter.T2tConfig{ 74 | // 如果字段首字母本来就是大写, 就不添加tag, 默认false添加, true不添加 75 | RmTagIfUcFirsted: false, 76 | // tag的字段名字是否转换为小写, 如果本身有大写字母的话, 默认false不转 77 | TagToLower: false, 78 | // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 79 | UcFirstOnly: false, 80 | //// 每个struct放入单独的文件,默认false,放入同一个文件(暂未提供) 81 | //SeperatFile: false, 82 | }) 83 | // 开始迁移转换 84 | err := t2t. 85 | // 指定某个表,如果不指定,则默认全部表都迁移 86 | Table("user"). 87 | // 表前缀 88 | Prefix("prefix_"). 89 | // 是否添加json tag 90 | EnableJsonTag(true). 91 | // 生成struct的包名(默认为空的话, 则取名为: package model) 92 | PackageName("model"). 93 | // tag字段的key值,默认是orm 94 | TagKey("orm"). 95 | // 是否添加结构体方法获取表名 96 | RealNameMethod("TableName"). 97 | // 生成的结构体保存路径 98 | SavePath("/Users/fizz/go/src/github.com/gohouse/gupiao/model/model.go"). 99 | // 数据库dsn,这里可以使用 t2t.DB() 代替,参数为 *sql.DB 对象 100 | Dsn("root:root@tcp(localhost:3306)/test?charset=utf8"). 101 | // 执行 102 | Run() 103 | 104 | fmt.Println(err) 105 | } 106 | ``` 107 | 108 | result 109 | ```go 110 | package model 111 | 112 | import "time" 113 | 114 | type User struct { 115 | Id int `json:"Id" orm:"Id"` 116 | Email string `json:"Email" orm:"Email"` // 邮箱 117 | Password string `json:"Password" orm:"Password"` // 密码 118 | CreatedAt string `json:"CreatedAt" orm:"CreatedAt"` 119 | } 120 | 121 | func (*User) TableName() string { 122 | return "user" 123 | } 124 | ``` 125 | -------------------------------------------------------------------------------- /table2struct.go: -------------------------------------------------------------------------------- 1 | package converter 2 | 3 | import ( 4 | "database/sql" 5 | "errors" 6 | "fmt" 7 | "log" 8 | "os" 9 | "os/exec" 10 | "strings" 11 | 12 | _ "github.com/go-sql-driver/mysql" 13 | ) 14 | 15 | //map for converting mysql type to golang types 16 | var typeForMysqlToGo = map[string]string{ 17 | "int": "int64", 18 | "integer": "int64", 19 | "tinyint": "int64", 20 | "smallint": "int64", 21 | "mediumint": "int64", 22 | "bigint": "int64", 23 | "int unsigned": "int64", 24 | "integer unsigned": "int64", 25 | "tinyint unsigned": "int64", 26 | "smallint unsigned": "int64", 27 | "mediumint unsigned": "int64", 28 | "bigint unsigned": "int64", 29 | "bit": "int64", 30 | "bool": "bool", 31 | "enum": "string", 32 | "set": "string", 33 | "varchar": "string", 34 | "char": "string", 35 | "tinytext": "string", 36 | "mediumtext": "string", 37 | "text": "string", 38 | "longtext": "string", 39 | "blob": "string", 40 | "tinyblob": "string", 41 | "mediumblob": "string", 42 | "longblob": "string", 43 | "date": "time.Time", // time.Time or string 44 | "datetime": "time.Time", // time.Time or string 45 | "timestamp": "time.Time", // time.Time or string 46 | "time": "time.Time", // time.Time or string 47 | "float": "float64", 48 | "double": "float64", 49 | "decimal": "float64", 50 | "binary": "string", 51 | "varbinary": "string", 52 | "json": "json.RawMessage", 53 | } 54 | 55 | type Table2Struct struct { 56 | dsn string 57 | savePath string 58 | db *sql.DB 59 | table string 60 | prefix string 61 | config *T2tConfig 62 | err error 63 | realNameMethod string 64 | enableJsonTag bool // 是否添加json的tag, 默认不添加 65 | packageName string // 生成struct的包名(默认为空的话, 则取名为: package model) 66 | tagKey string // tag字段的key值,默认是orm 67 | dateToTime bool // 是否将 date相关字段转换为 time.Time,默认否 68 | } 69 | 70 | type T2tConfig struct { 71 | StructNameToHump bool // 结构体名称是否转为驼峰式,默认为false 72 | RmTagIfUcFirsted bool // 如果字段首字母本来就是大写, 就不添加tag, 默认false添加, true不添加 73 | TagToLower bool // tag的字段名字是否转换为小写, 如果本身有大写字母的话, 默认false不转 74 | JsonTagToHump bool // json tag是否转为驼峰,默认为false,不转换 75 | UcFirstOnly bool // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 76 | SeperatFile bool // 每个struct放入单独的文件,默认false,放入同一个文件 77 | } 78 | 79 | func NewTable2Struct() *Table2Struct { 80 | return &Table2Struct{} 81 | } 82 | 83 | func (t *Table2Struct) Dsn(d string) *Table2Struct { 84 | t.dsn = d 85 | return t 86 | } 87 | 88 | func (t *Table2Struct) TagKey(r string) *Table2Struct { 89 | t.tagKey = r 90 | return t 91 | } 92 | 93 | func (t *Table2Struct) PackageName(r string) *Table2Struct { 94 | t.packageName = r 95 | return t 96 | } 97 | 98 | func (t *Table2Struct) RealNameMethod(r string) *Table2Struct { 99 | t.realNameMethod = r 100 | return t 101 | } 102 | 103 | func (t *Table2Struct) SavePath(p string) *Table2Struct { 104 | t.savePath = p 105 | return t 106 | } 107 | 108 | func (t *Table2Struct) DB(d *sql.DB) *Table2Struct { 109 | t.db = d 110 | return t 111 | } 112 | 113 | func (t *Table2Struct) Table(tab string) *Table2Struct { 114 | t.table = tab 115 | return t 116 | } 117 | 118 | func (t *Table2Struct) Prefix(p string) *Table2Struct { 119 | t.prefix = p 120 | return t 121 | } 122 | 123 | func (t *Table2Struct) EnableJsonTag(p bool) *Table2Struct { 124 | t.enableJsonTag = p 125 | return t 126 | } 127 | 128 | func (t *Table2Struct) DateToTime(d bool) *Table2Struct { 129 | t.dateToTime = d 130 | return t 131 | } 132 | 133 | func (t *Table2Struct) Config(c *T2tConfig) *Table2Struct { 134 | t.config = c 135 | return t 136 | } 137 | 138 | func (t *Table2Struct) Run() error { 139 | if t.config == nil { 140 | t.config = new(T2tConfig) 141 | } 142 | // 链接mysql, 获取db对象 143 | t.dialMysql() 144 | if t.err != nil { 145 | return t.err 146 | } 147 | 148 | // 获取表和字段的shcema 149 | tableColumns, err := t.getColumns() 150 | if err != nil { 151 | return err 152 | } 153 | 154 | // 包名 155 | var packageName string 156 | if t.packageName == "" { 157 | packageName = "package model\n\n" 158 | } else { 159 | packageName = fmt.Sprintf("package %s\n\n", t.packageName) 160 | } 161 | 162 | // 组装struct 163 | var structContent string 164 | for tableRealName, item := range tableColumns { 165 | // 去除前缀 166 | if t.prefix != "" { 167 | tableRealName = tableRealName[len(t.prefix):] 168 | } 169 | tableName := tableRealName 170 | structName := tableName 171 | if t.config.StructNameToHump { 172 | structName = t.camelCase(structName) 173 | } 174 | 175 | /* 176 | switch len(tableName) { 177 | case 0: 178 | case 1: 179 | tableName = strings.ToUpper(tableName[0:1]) 180 | default: 181 | // 字符长度大于1时 182 | tableName = strings.ToUpper(tableName[0:1]) + tableName[1:] 183 | } 184 | */ 185 | tableName = t.camelCase(tableName) 186 | depth := 1 187 | structContent += "type " + structName + " struct {\n" 188 | for _, v := range item { 189 | //structContent += tab(depth) + v.ColumnName + " " + v.Type + " " + v.Json + "\n" 190 | // 字段注释 191 | var clumnComment string 192 | if v.ColumnComment != "" { 193 | clumnComment = fmt.Sprintf(" // %s", v.ColumnComment) 194 | } 195 | structContent += fmt.Sprintf("%s%s %s %s%s\n", 196 | tab(depth), v.ColumnName, v.Type, v.Tag, clumnComment) 197 | } 198 | structContent += tab(depth-1) + "}\n\n" 199 | 200 | // 添加 method 获取真实表名 201 | if t.realNameMethod != "" { 202 | structContent += fmt.Sprintf("func (%s) %s() string {\n", 203 | structName, t.realNameMethod) 204 | structContent += fmt.Sprintf("%sreturn \"%s\"\n", 205 | tab(depth), tableRealName) 206 | structContent += "}\n\n" 207 | } 208 | } 209 | 210 | // 如果有引入 time.Time, 则需要引入 time 包 211 | var importContent string 212 | if strings.Contains(structContent, "time.Time") { 213 | importContent = "import \"time\"\n\n" 214 | } 215 | 216 | // 添加json类型支持 217 | if strings.Contains(structContent, "json.RawMessage") { 218 | importContent += "import \"encoding/json\"\n\n" 219 | } 220 | 221 | // 写入文件struct 222 | var savePath = t.savePath 223 | // 是否指定保存路径 224 | if savePath == "" { 225 | savePath = "model.go" 226 | } 227 | filePath := fmt.Sprintf("%s", savePath) 228 | f, err := os.Create(filePath) 229 | if err != nil { 230 | log.Println("Can not write file") 231 | return err 232 | } 233 | defer f.Close() 234 | 235 | f.WriteString(packageName + importContent + structContent) 236 | 237 | cmd := exec.Command("gofmt", "-w", filePath) 238 | cmd.Run() 239 | 240 | log.Println("gen model finish!!!") 241 | 242 | return nil 243 | } 244 | 245 | func (t *Table2Struct) dialMysql() { 246 | if t.db == nil { 247 | if t.dsn == "" { 248 | t.err = errors.New("dsn数据库配置缺失") 249 | return 250 | } 251 | t.db, t.err = sql.Open("mysql", t.dsn) 252 | } 253 | return 254 | } 255 | 256 | type column struct { 257 | ColumnName string 258 | Type string 259 | Nullable string 260 | TableName string 261 | ColumnComment string 262 | Tag string 263 | } 264 | 265 | // Function for fetching schema definition of passed table 266 | func (t *Table2Struct) getColumns(table ...string) (tableColumns map[string][]column, err error) { 267 | // 根据设置,判断是否要把 date 相关字段替换为 string 268 | if t.dateToTime == false { 269 | typeForMysqlToGo["date"] = "string" 270 | typeForMysqlToGo["datetime"] = "string" 271 | typeForMysqlToGo["timestamp"] = "string" 272 | typeForMysqlToGo["time"] = "string" 273 | } 274 | tableColumns = make(map[string][]column) 275 | // sql 276 | var sqlStr = `SELECT COLUMN_NAME,DATA_TYPE,IS_NULLABLE,TABLE_NAME,COLUMN_COMMENT 277 | FROM information_schema.COLUMNS 278 | WHERE table_schema = DATABASE()` 279 | // 是否指定了具体的table 280 | if t.table != "" { 281 | sqlStr += fmt.Sprintf(" AND TABLE_NAME = '%s'", t.prefix+t.table) 282 | } 283 | // sql排序 284 | sqlStr += " order by TABLE_NAME asc, ORDINAL_POSITION asc" 285 | 286 | rows, err := t.db.Query(sqlStr) 287 | if err != nil { 288 | log.Println("Error reading table information: ", err.Error()) 289 | return 290 | } 291 | 292 | defer rows.Close() 293 | 294 | for rows.Next() { 295 | col := column{} 296 | err = rows.Scan(&col.ColumnName, &col.Type, &col.Nullable, &col.TableName, &col.ColumnComment) 297 | 298 | if err != nil { 299 | log.Println(err.Error()) 300 | return 301 | } 302 | 303 | //col.Json = strings.ToLower(col.ColumnName) 304 | col.Tag = col.ColumnName 305 | col.ColumnComment = col.ColumnComment 306 | col.ColumnName = t.camelCase(col.ColumnName) 307 | col.Type = typeForMysqlToGo[col.Type] 308 | jsonTag := col.Tag 309 | // 字段首字母本身大写, 是否需要删除tag 310 | if t.config.RmTagIfUcFirsted && 311 | col.ColumnName[0:1] == strings.ToUpper(col.ColumnName[0:1]) { 312 | col.Tag = "-" 313 | } else { 314 | // 是否需要将tag转换成小写 315 | if t.config.TagToLower { 316 | col.Tag = strings.ToLower(col.Tag) 317 | jsonTag = col.Tag 318 | } 319 | 320 | if t.config.JsonTagToHump { 321 | jsonTag = t.camelCase(jsonTag) 322 | } 323 | 324 | //if col.Nullable == "YES" { 325 | // col.Json = fmt.Sprintf("`json:\"%s,omitempty\"`", col.Json) 326 | //} else { 327 | //} 328 | } 329 | if t.tagKey == "" { 330 | t.tagKey = "orm" 331 | } 332 | if t.enableJsonTag { 333 | //col.Json = fmt.Sprintf("`json:\"%s\" %s:\"%s\"`", col.Json, t.config.TagKey, col.Json) 334 | col.Tag = fmt.Sprintf("`%s:\"%s\" json:\"%s\"`", t.tagKey, col.Tag, jsonTag) 335 | } else { 336 | col.Tag = fmt.Sprintf("`%s:\"%s\"`", t.tagKey, col.Tag) 337 | } 338 | //columns = append(columns, col) 339 | if _, ok := tableColumns[col.TableName]; !ok { 340 | tableColumns[col.TableName] = []column{} 341 | } 342 | tableColumns[col.TableName] = append(tableColumns[col.TableName], col) 343 | } 344 | return 345 | } 346 | 347 | func (t *Table2Struct) camelCase(str string) string { 348 | // 是否有表前缀, 设置了就先去除表前缀 349 | if t.prefix != "" { 350 | str = strings.Replace(str, t.prefix, "", 1) 351 | } 352 | var text string 353 | //for _, p := range strings.Split(name, "_") { 354 | for _, p := range strings.Split(str, "_") { 355 | // 字段首字母大写的同时, 是否要把其他字母转换为小写 356 | switch len(p) { 357 | case 0: 358 | case 1: 359 | text += strings.ToUpper(p[0:1]) 360 | default: 361 | // 字符长度大于1时 362 | if t.config.UcFirstOnly == true { 363 | text += strings.ToUpper(p[0:1]) + strings.ToLower(p[1:]) 364 | } else { 365 | text += strings.ToUpper(p[0:1]) + p[1:] 366 | } 367 | } 368 | } 369 | return text 370 | } 371 | func tab(depth int) string { 372 | return strings.Repeat("\t", depth) 373 | } 374 | --------------------------------------------------------------------------------