├── .gitignore ├── LICENSE ├── main.go └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | 17 | .idea/* 18 | vendor/ 19 | main 20 | go.sum -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 go-enthusiast 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 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "github.com/go-libraries/genModels" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | var ( 12 | modelPath string 13 | driver string 14 | dsn string 15 | ignoreTable string 16 | style string 17 | packageName string 18 | camel string 19 | help bool 20 | h bool 21 | ) 22 | 23 | func init() { 24 | currentPath, _ := os.Getwd() 25 | flag.StringVar(&modelPath, "dir", currentPath, "a dir name save model file path, default is current path") 26 | flag.StringVar(&driver, "driver", "mysql", "database driver,like `mysql` `mariadb`, default is mysql") 27 | flag.StringVar(&dsn, "dsn", "", "connection info names dsn") 28 | flag.StringVar(&ignoreTable, "ig_tables", "", "ignore table names, like [tableA,tableB]") 29 | flag.StringVar(&style, "style", "default", "use orm style like `bee` `gorm`, default `default`") 30 | flag.StringVar(&camel, "camel", "0", "json use camel 0 false 1 true") 31 | flag.StringVar(&packageName, "package", "", "help") 32 | flag.BoolVar(&help, "help", false, "this help") 33 | flag.BoolVar(&h, "h", false, "this help") 34 | } 35 | 36 | func main() { 37 | flag.Parse() 38 | if h || help { 39 | flag.Usage() 40 | } 41 | //dsn = "ceshiceshi:mgtj123456@tcp(rm-ly29w98y58jjy4s7x.mysql.rds.aliyuncs.com:3306)/mgtj_app_content?charset=utf8mb4" 42 | //driver = "mysql" 43 | //style = "grom" 44 | //packageName = "content" 45 | //modelPath = "d:\\work\\contents" 46 | if dsn == "" { 47 | flag.Usage() 48 | } 49 | 50 | if modelPath != "" { 51 | _,e := os.Stat(modelPath) 52 | if e != nil { 53 | _ = os.Mkdir(modelPath, os.ModePerm) 54 | } 55 | } 56 | flag.Usage = usage 57 | genModels.GormFormat.JsonUseCamel = false 58 | if camel == "0" { 59 | genModels.GormFormat.JsonUseCamel = true 60 | } 61 | fmt.Println(genModels.GormFormat,camel, style) 62 | driver := genModels.GetDriver(modelPath, driver, dsn, style, packageName) 63 | if ignoreTable != "" { 64 | ignoreTables := strings.Split(ignoreTable, ",") 65 | driver.SetIgnoreTables(ignoreTables...) 66 | } 67 | driver.Run() 68 | } 69 | 70 | func usage() { 71 | fmt.Println("Usage: model [-dir dirname] [-driver mysql] [-dsn username:password@tcp(host:port)/database]") 72 | flag.PrintDefaults() 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # model-gen 2 | 3 | 每次写go的orm的时候,总要去来回粘贴数据表,然后orm的东西又不对应,为此,懒人李只能造个轮子,为了提高效率(ps:就是想偷懒、摸鱼)。 4 | 5 | 一个Go专用的模型生成器就这样诞生啦 6 | 7 | # 原理 8 | 9 | 通过获取数据库表结构,进行model文件生成。 10 | 11 | # 迭代 12 | 13 | 目前支持mysql,未来预计支持mariadb和pgsql(sql server还未考量) 14 | 15 | 2019-12-19 16 | 17 | 1. 支持多种风格文件生成(目前支持 bee、gorm、默认格式) 18 | 2. 新增无符号整型和标签内容(size、type) 19 | 20 | 2019-12-23 21 | 22 | 1. 增加主键支持 23 | 24 | 2019-12-24 25 | 26 | 1. 增加gorm curd方法 27 | 2. 增加orm自动初始化模板并支持orm 28 | 29 | 2019-12-26 30 | 31 | 1. 增加gorm default 32 | 33 | # 快速入门 34 | 35 | 36 | go get -u github.com/go-libraries/gmodel 37 | 38 | gmodel -dsn="root:sa@tcp(localhost:3306)/blog" -dir=/tmp -style=bee -package=model 39 | 40 | cat /tmp/cate.go 41 | 42 | 文件内容如下: 43 | ```go 44 | package model 45 | 46 | import "github.com/astaxie/beego/orm" 47 | 48 | func init(){ 49 | orm.RegisterModel(new(Cate)) 50 | } 51 | 52 | type Cate struct { 53 | Id uint `orm:"column(id);size(10);type(int(11) unsigned);" json:"id"` 54 | Name string `orm:"column(name);size(50);type(varchar(50));" json:"name"` 55 | CreateTime string `orm:"column(create_time);type(timestamp);" json:"create_time"` 56 | UpdateTime string `orm:"column(update_time);type(timestamp);" json:"update_time"` 57 | } 58 | 59 | func (cate *Cate) GetTableName() string { 60 | return "cate" 61 | } 62 | ``` 63 | s 64 | help: 65 | 66 | Usage of gmodel: 67 | -dir string 68 | a dir name save model file path, default is current path (default "/Users/limars/Go/bin") 69 | -driver mysql 70 | database driver,like mysql `mariadb`, default is mysql (default "mysql") 71 | -dsn string 72 | connection info names dsn 73 | -h this help 74 | -help 75 | this help 76 | -ig_tables string 77 | ignore table names 78 | -package string 79 | help 80 | -style bee 81 | use orm style like bee `gorm`, default `default` (default "default") 82 | 83 | # 代码应用 84 | 85 | 代码: 86 | ``` 87 | mysqlHost := "127.0.0.1" 88 | mysqlPort := "3306" 89 | mysqlUser := "root" 90 | mysqlPassword := "sa" 91 | mysqlDbname := "blog" 92 | 93 | dsn := mysqlUser + ":" + mysqlPassword + "@tcp(" + mysqlHost + ":" + mysqlPort + ")/" + mysqlDbname + "?charset=utf8mb4" 94 | 95 | Mysql := GetMysqlToGo() 96 | Mysql.Driver.SetDsn(dsn) 97 | Mysql.SetModelPath("/tmp") 98 | Mysql.SetIgnoreTables("cate") 99 | Mysql.SetPackageName("models") 100 | Mysql.Run() 101 | ``` 102 | 103 | 执行结果: 104 | 105 | ll /tmp 106 | 107 | ``` 108 | total * 109 | -rw-r--r-- 1 limars wheel 297 12 18 17:59 cate.go 110 | -rw-r--r-- 1 limars wheel 597 12 18 17:59 comment.go 111 | -rw-r--r-- 1 limars wheel 826 12 18 17:59 content.go 112 | ...... 113 | ``` 114 | 115 | cat cate.go 116 | ``` 117 | package models 118 | 119 | type Cate struct { 120 | Id int `orm:"id" json:"id"` 121 | Name string `orm:"name" json:"name"` 122 | CreateTime string `orm:"create_time" json:"create_time"` 123 | UpdateTime string `orm:"update_time" json:"update_time"` 124 | } 125 | 126 | func (cate *Cate) GetTableName() string { 127 | return "cate" 128 | } 129 | ``` 130 | 131 | 132 | 附带上sql: 133 | 134 | ``` 135 | CREATE DATABASE `blog`; 136 | 137 | USE `blog`; 138 | 139 | CREATE TABLE `cate` ( 140 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 141 | `name` varchar(50) NOT NULL DEFAULT '', 142 | `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 143 | `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 144 | PRIMARY KEY (`id`) 145 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 146 | 147 | 148 | 149 | CREATE TABLE `comment` ( 150 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 151 | `blog_id` int(11) unsigned NOT NULL, 152 | `parent_id` int(11) unsigned NOT NULL DEFAULT '0', 153 | `ip` varchar(32) NOT NULL DEFAULT '', 154 | `email` varchar(255) NOT NULL DEFAULT '', 155 | `name` varchar(50) NOT NULL DEFAULT '', 156 | `content` tinytext NOT NULL, 157 | `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 158 | `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 159 | `status` tinyint(1) unsigned NOT NULL DEFAULT '1', 160 | PRIMARY KEY (`id`) 161 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 162 | 163 | 164 | CREATE TABLE `content` ( 165 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 166 | `cate_id` int(11) unsigned NOT NULL COMMENT '分类id', 167 | `title` varchar(255) NOT NULL DEFAULT '' COMMENT '标题', 168 | `description` tinytext NOT NULL COMMENT '简介', 169 | `content` text NOT NULL COMMENT '正文', 170 | `keyword` varchar(255) NOT NULL DEFAULT '' COMMENT 'seo关键字', 171 | `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 172 | `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 173 | `status` tinyint(1) unsigned NOT NULL DEFAULT '1', 174 | `is_original` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 原创 2 转载', 175 | `ext` text NOT NULL COMMENT '扩展字段', 176 | PRIMARY KEY (`id`) 177 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 178 | ``` 179 | 180 | 181 | --------------------------------------------------------------------------------