diff --git a/config.toml b/config.toml index 3994faa..f897fb5 100644 --- a/config.toml +++ b/config.toml @@ -1,11 +1,14 @@ -serial_number = "1.0" -service_name = "jewelryserver" -service_displayname = "jewelryserver" -sercice_desc = "jewelryserver" -is_dev = true +serial_number = "1.0" #版本号 +service_name = "jewelryserver" #服务名 +service_displayname = "jewelryserver" #服务显示名 +sercice_desc = "jewelryserver" #服务描述 +is_dev = true #是否开发者模式 +out_dir = "" #目录 +singular_table = true #单表模式:true:禁用表名复数,false:采用表明复数 参考:gorm.SingularTable [mysql_info] host = "127.0.0.1" port = 3306 username = "root" password = "qwer" - database = "oauth_db" \ No newline at end of file + database = "oauth_db" + diff --git a/data/cmd/cmd.go b/data/cmd/cmd.go index 5729bab..5aedc19 100644 --- a/data/cmd/cmd.go +++ b/data/cmd/cmd.go @@ -14,6 +14,8 @@ import ( ) var mysqlInfo config.MysqlDbInfo +var outDir string +var singular_table bool var rootCmd = &cobra.Command{ Use: "main", @@ -47,6 +49,12 @@ func init() { rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Database, "database", "d", "", "数据库名") rootCmd.MarkFlagRequired("database") + rootCmd.PersistentFlags().StringVarP(&outDir, "outdir", "o", "", "输出目录") + rootCmd.MarkFlagRequired("outdir") + + rootCmd.PersistentFlags().StringVarP(&singular_table, "singular", "s", "", "是否禁用表名复数") + rootCmd.MarkFlagRequired("singular") + rootCmd.Flags().IntVar(&mysqlInfo.Port, "port", 3306, "端口号") } @@ -85,4 +93,8 @@ func MergeMysqlDbInfo() { } config.SetMysqlDbInfo(&tmp) + + config.SetOutDir(outDir) + config.SetSingularTable(singular_table) + } diff --git a/data/config/MyIni.go b/data/config/MyIni.go index 82c435a..eb034c6 100644 --- a/data/config/MyIni.go +++ b/data/config/MyIni.go @@ -5,7 +5,9 @@ import "fmt" // type Config struct { CfgBase - MySQLInfo MysqlDbInfo `toml:"mysql_info"` + MySQLInfo MysqlDbInfo `toml:"mysql_info"` + OutDir string `toml:"out_dir"` + SingularTable bool `toml:"singular_table"` } //mysql 数据库信息 @@ -37,3 +39,23 @@ func GetMysqlConStr() string { _map.MySQLInfo.Database, ) } + +//设置输出目录 +func SetOutDir(outDir string) { + _map.OutDir = outDir +} + +//获取输出目录 +func GetOutDir() string { + return _map.OutDir +} + +//设置禁用表名复数 +func SetSingularTable(b bool) { + _map.SingularTable = b +} + +//获取禁用表名复数 +func GetSingularTable() bool { + return _map.SingularTable +} diff --git a/data/view/gtools/common.go b/data/view/gtools/common.go index 593d280..4f82445 100644 --- a/data/view/gtools/common.go +++ b/data/view/gtools/common.go @@ -1 +1,94 @@ package gtools + +import ( + "fmt" + + "github.com/xie1xiao1jun/gorm-tools/data/config" + "github.com/xie1xiao1jun/gorm-tools/data/view/gtools/generate" + "github.com/xie1xiao1jun/public/mybigcamel" + "github.com/xie1xiao1jun/public/mysqldb" +) + +//获取表列及注释 +func OnGetTables(orm *mysqldb.MySqlDB) map[string]string { + tbDesc := make(map[string]string) + + //获取列名 + var tables []string + rows, err := orm.Raw("show tables").Rows() + if err != nil { + fmt.Println(err) + return tbDesc + } + defer rows.Close() + + for rows.Next() { + var table string + rows.Scan(&table) + tables = append(tables, table) + tbDesc[table] = "" + } + + //获取表注释 + rows, err = orm.Raw("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema=?;", + config.GetMysqlDbInfo().Database).Rows() + if err != nil { + fmt.Println(err) + return tbDesc + } + + for rows.Next() { + var table, desc string + rows.Scan(&table, &desc) + tables = append(tables, table) + tbDesc[table] = desc + } + + return tbDesc +} + +//获取包信息 +func OnGetPackageInfo(orm *mysqldb.MySqlDB, tabls map[string]string) generate.GenPackage { + var pkg generate.GenPackage + for tab, desc := range tabls { + var sct generate.GenStruct + sct.SetStructName(OnGetCamelName(tab)) //大驼峰 + sct.SetNotes(desc) + //构造元素 + + //--------end + + pkg.AddStruct(sct) + } + + return pkg +} + +// //获取表列及注释 +func OnGetTables(orm *mysqldb.MySqlDB, tab string) []generate.GenElement { + var el []generate.GenElement + +} + +//大驼峰或者首字母大写 +func OnGetCamelName(name string) string { + if config.GetSingularTable() { //如果全局禁用表名复数 + return TitleCase(name) + } + + return mybigcamel.Marshal(name) +} + +/* + 首字母大写 +*/ +func TitleCase(name string) string { + vv := []rune(name) + if len(vv) > 0 { + if bool(vv[0] >= 'a' && vv[0] <= 'z') { //首字母大写 + vv[0] -= 32 + } + } + + return string(vv) +} diff --git a/data/view/gtools/def.go b/data/view/gtools/def.go deleted file mode 100644 index 593d280..0000000 --- a/data/view/gtools/def.go +++ /dev/null @@ -1 +0,0 @@ -package gtools diff --git a/data/view/gtools/generate/common.go b/data/view/gtools/generate/common.go new file mode 100644 index 0000000..acecae0 --- /dev/null +++ b/data/view/gtools/generate/common.go @@ -0,0 +1,159 @@ +package generate + +import ( + "fmt" + "strings" + + "github.com/xie1xiao1jun/public/tools" +) + +// 打印 +func (p *PrintAtom) Add(str ...interface{}) { + var tmp string + for _, v := range str { + tmp += tools.AsString(v) + _interval + } + p.lines = append(p.lines, tmp) +} + +// 打印 +func (p *PrintAtom) Generate() []string { + return p.lines +} + +//设置元素名字 +func (e *GenElement) SetName(name string) { + e.Name = name +} + +//设置元素类型 +func (e *GenElement) SetType(tp string) { + e.Type = tp +} + +//设置注释 +func (e *GenElement) SetNotes(notes string) { + e.Notes = notes +} + +//添加一个tag标记 +func (e *GenElement) AddTag(k string, v string) { + if e.Tags == nil { + e.Tags = make(map[string][]string) + } + e.Tags[k] = append(e.Tags[k], v) +} + +//获取结果数据 +func (e *GenElement) Generate() string { + tag := "" + if e.Tags != nil { + for k, v := range e.Tags { + tag += fmt.Sprintf(`%v:"%v"`, k, strings.Join(v, ",")) + } + tag = fmt.Sprintf("`%v`", tag) + } + + var p PrintAtom + p.Add(e.Name, e.Type, tag, "//", e.Notes) + return p.Generate()[0] +} + +////////////////////////////////////////////////////////////////////////////// +// struct +////////////////////////////////////////////////////////////////////////////// + +//获取结果数据 +func (s *GenStruct) SetStructName(name string) { + s.Name = name +} + +//设置注释 +func (e *GenStruct) SetNotes(notes string) { + e.Notes = notes +} + +//添加一个元素 +func (s *GenStruct) AddElement(e GenElement) { + s.Em = append(s.Em, e) +} + +//获取结果数据 +func (s *GenStruct) Generate() []string { + var p PrintAtom + p.Add("//", s.Notes) + p.Add("type", s.Name, "struct {") + for _, v := range s.Em { + p.Add(v.Generate()) + } + p.Add("}") + + return p.Generate() +} + +////////////////////////////////////////////////////////////////////////////// +// package +////////////////////////////////////////////////////////////////////////////// + +//定义包名 +func (p *GenPackage) SetPackage(pname string) { + p.Name = pname +} + +//通过类型添加import +func (p *GenPackage) AddImport(imp string) { + if p.Imports == nil { + p.Imports = make(map[string]string) + } + p.Imports[imp] = imp +} + +//添加一个结构体 +func (p *GenPackage) AddStruct(st GenStruct) { + p.Structs = append(p.Structs, st) +} + +//获取结果数据 +func (p *GenPackage) Generate() string { + p.genimport() //补充 import + + var pa PrintAtom + pa.Add("package", p.Name) + //add import + if p.Imports != nil { + pa.Add("import (") + for _, v := range p.Imports { + pa.Add(v) + } + pa.Add(")") + } + //-----------end + //add struct + for _, v := range p.Structs { + for _, v1 := range v.Generate() { + pa.Add(v1) + } + } + //-----------end + + //输出 + strOut := "" + for _, v := range pa.Generate() { + strOut += v + "\n" + } + + return strOut +} + +//获取结果数据 +func (p *GenPackage) genimport() { + for _, v := range p.Structs { + for _, v1 := range v.Em { + if v2, ok := EImportsHead[v1.Type]; ok { + if len(v2) > 0 { + p.AddImport(v2) + } + } + } + } +} diff --git a/data/view/gtools/generate/def.go b/data/view/gtools/generate/def.go new file mode 100644 index 0000000..cc51d95 --- /dev/null +++ b/data/view/gtools/generate/def.go @@ -0,0 +1,152 @@ +package generate + +//包类 +type IPackage interface { + //定义包名 + SetPackage(string) + //通过类型添加import + AddImport(string) + //添加一个结构体 + AddStruct(IStruct) + //获取结果数据 + Generate() string +} + +//结构体类 +type IStruct interface { + //设置结构体名字 + SetStructName(string) + + //设置注释 + SetNotes(string) + + //添加一个元素 + AddElement(IElement) + + //获取结果数据 + Generate() []string +} + +//元素类 +type IElement interface { + //设置元素名字 + SetName(string) + //设置元素类型 + SetType(string) + + //设置注释 + SetNotes(string) + + //添加一个tag标记 + AddTag(k string, v string) + + //获取结果数据 + Generate() string +} + +////////////////////////////////////////////////////////////////////////////// +// +////////////////////////////////////////////////////////////////////////////// + +//元素类 +type GenElement struct { + Name string //元素名 + Type string //类型标记 + Notes string //注释 + Tags map[string][]string //标记 +} + +//结构体 +type GenStruct struct { + Name string //名字 + Notes string //注释 + Em []GenElement //元素组合 +} + +//包体 +type GenPackage struct { + Name string //名字 + Imports map[string]string //元素组合 + Structs []GenStruct //结构体组合 +} + +//间隔 +var _interval = "\t" + +var EImportsHead = map[string]string{ + "stirng": "string", +} + +var isGoKeyword = map[string]bool{ + "break": true, + "case": true, + "chan": true, + "const": true, + "continue": true, + "default": true, + "else": true, + "defer": true, + "fallthrough": true, + "for": true, + "func": true, + "go": true, + "goto": true, + "if": true, + "import": true, + "interface": true, + "map": true, + "package": true, + "range": true, + "return": true, + "select": true, + "struct": true, + "switch": true, + "type": true, + "var": true, +} + +var isGoPredeclaredIdentifier = map[string]bool{ + "append": true, + "bool": true, + "byte": true, + "cap": true, + "close": true, + "complex": true, + "complex128": true, + "complex64": true, + "copy": true, + "delete": true, + "error": true, + "false": true, + "float32": true, + "float64": true, + "imag": true, + "int": true, + "int16": true, + "int32": true, + "int64": true, + "int8": true, + "iota": true, + "len": true, + "make": true, + "new": true, + "nil": true, + "panic": true, + "print": true, + "println": true, + "real": true, + "recover": true, + "rune": true, + "string": true, + "true": true, + "uint": true, + "uint16": true, + "uint32": true, + "uint64": true, + "uint8": true, + "uintptr": true, +} + +type PrintAtom struct { + lines []string +} diff --git a/data/view/gtools/generate/generate.go b/data/view/gtools/generate/generate.go new file mode 100644 index 0000000..eb83477 --- /dev/null +++ b/data/view/gtools/generate/generate.go @@ -0,0 +1 @@ +package generate diff --git a/data/view/gtools/gtools.go b/data/view/gtools/gtools.go index c3e8cc7..ee013cc 100644 --- a/data/view/gtools/gtools.go +++ b/data/view/gtools/gtools.go @@ -1,8 +1,6 @@ package gtools import ( - "fmt" - "github.com/xie1xiao1jun/gorm-tools/data/config" "github.com/xie1xiao1jun/public/mysqldb" ) @@ -12,20 +10,6 @@ func Execute() { orm := mysqldb.OnInitDBOrm(config.GetMysqlConStr()) defer orm.OnDestoryDB() - //获取列名 - var tables []string - rows, err := orm.Raw("show tables").Rows() - if err != nil { - fmt.Println(err) - return - } - defer rows.Close() - - for rows.Next() { - var table string - rows.Scan(&table) - tables = append(tables, table) - } + packageInfo := OnGetPackageInfo(orm, OnGetTables(orm)) - fmt.Println(tables) } diff --git a/main.go b/main.go index 62a18cf..f0fb043 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main -import "github.com/xie1xiao1jun/gorm-tools/data/cmd" +import ( + "github.com/xie1xiao1jun/gorm-tools/data/cmd" +) func main() { cmd.Execute()