Restructure
重构
This commit is contained in:
@@ -24,7 +24,7 @@ var rootCmd = &cobra.Command{
|
||||
Long: `base on gorm tools for mysql database to golang struct`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
gtools.Execute()
|
||||
//开始做事情
|
||||
// Start doing things.开始做事情
|
||||
},
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ func initConfig() {
|
||||
}
|
||||
}
|
||||
|
||||
//MergeMysqlDbInfo 合并
|
||||
// MergeMysqlDbInfo merge parm
|
||||
func MergeMysqlDbInfo() {
|
||||
var tmp = config.GetMysqlDbInfo()
|
||||
if len(mysqlInfo.Database) > 0 {
|
||||
|
||||
@@ -2,7 +2,7 @@ package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
//Config .
|
||||
// Config custom config struct
|
||||
type Config struct {
|
||||
CfgBase
|
||||
MySQLInfo MysqlDbInfo `toml:"mysql_info"`
|
||||
@@ -12,26 +12,26 @@ type Config struct {
|
||||
SingularTable bool `toml:"singular_table"`
|
||||
}
|
||||
|
||||
//MysqlDbInfo mysql 数据库信息
|
||||
// MysqlDbInfo mysql database information. mysql 数据库信息
|
||||
type MysqlDbInfo struct {
|
||||
Host string `validate:"required"` //地址
|
||||
Port int `validate:"required"` //端口号
|
||||
Username string `validate:"required"` //用户名
|
||||
Password string `validate:"required"` //密码
|
||||
Database string `validate:"required"` //数据库名
|
||||
Host string `validate:"required"` // Host. 地址
|
||||
Port int `validate:"required"` // Port 端口号
|
||||
Username string `validate:"required"` // Username 用户名
|
||||
Password string `validate:"required"` // Password 密码
|
||||
Database string `validate:"required"` // Database 数据库名
|
||||
}
|
||||
|
||||
//SetMysqlDbInfo 更新mysql配置信息
|
||||
// SetMysqlDbInfo Update MySQL configuration information
|
||||
func SetMysqlDbInfo(info *MysqlDbInfo) {
|
||||
_map.MySQLInfo = *info
|
||||
}
|
||||
|
||||
//GetMysqlDbInfo 获取mysql配置信息
|
||||
// GetMysqlDbInfo Get MySQL configuration information .获取mysql配置信息
|
||||
func GetMysqlDbInfo() MysqlDbInfo {
|
||||
return _map.MySQLInfo
|
||||
}
|
||||
|
||||
//GetMysqlConStr 获取mysql 连接字符串
|
||||
// GetMysqlConStr Get MySQL connection string.获取mysql 连接字符串
|
||||
func GetMysqlConStr() string {
|
||||
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
|
||||
_map.MySQLInfo.Username,
|
||||
@@ -42,32 +42,32 @@ func GetMysqlConStr() string {
|
||||
)
|
||||
}
|
||||
|
||||
//SetOutDir 设置输出目录
|
||||
// SetOutDir Setting Output Directory.设置输出目录
|
||||
func SetOutDir(outDir string) {
|
||||
_map.OutDir = outDir
|
||||
}
|
||||
|
||||
//GetOutDir 获取输出目录
|
||||
// GetOutDir Get Output Directory.获取输出目录
|
||||
func GetOutDir() string {
|
||||
return _map.OutDir
|
||||
}
|
||||
|
||||
//SetSingularTable 设置禁用表名复数
|
||||
// SetSingularTable Set Disabled Table Name Plurals.设置禁用表名复数
|
||||
func SetSingularTable(b bool) {
|
||||
_map.SingularTable = b
|
||||
}
|
||||
|
||||
//GetSingularTable 获取禁用表名复数
|
||||
// GetSingularTable Get Disabled Table Name Plurals.获取禁用表名复数
|
||||
func GetSingularTable() bool {
|
||||
return _map.SingularTable
|
||||
}
|
||||
|
||||
//GetSimple 简单输出
|
||||
// GetSimple simple output.简单输出
|
||||
func GetSimple() bool {
|
||||
return _map.Simple
|
||||
}
|
||||
|
||||
//GetIsJSONTag json标记
|
||||
// GetIsJSONTag json tag.json标记
|
||||
func GetIsJSONTag() bool {
|
||||
return _map.IsJSONTag
|
||||
}
|
||||
|
||||
@@ -9,13 +9,13 @@ import (
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
//CfgBase .
|
||||
// CfgBase base config struct
|
||||
type CfgBase struct {
|
||||
SerialNumber string `json:"serial_number" toml:"serial_number"` //版本号
|
||||
ServiceName string `json:"service_name" toml:"service_name"` //service名字
|
||||
ServiceDisplayname string `json:"service_displayname" toml:"service_displayname"` //显示名
|
||||
SerciceDesc string `json:"sercice_desc" toml:"sercice_desc"` //service描述
|
||||
IsDev bool `json:"is_dev" toml:"is_dev"` //是否是开发版本
|
||||
SerialNumber string `json:"serial_number" toml:"serial_number"` // version.版本号
|
||||
ServiceName string `json:"service_name" toml:"service_name"` // service name .service名字
|
||||
ServiceDisplayname string `json:"service_displayname" toml:"service_displayname"` // display name .显示名
|
||||
SerciceDesc string `json:"sercice_desc" toml:"sercice_desc"` // sercice desc .service描述
|
||||
IsDev bool `json:"is_dev" toml:"is_dev"` // Is it a development version?是否是开发版本
|
||||
}
|
||||
|
||||
var _map = Config{}
|
||||
@@ -34,7 +34,7 @@ func onInit() {
|
||||
}
|
||||
}
|
||||
|
||||
//InitFile ...
|
||||
// InitFile default value from file .
|
||||
func InitFile(filename string) error {
|
||||
if _, err := toml.DecodeFile(filename, &_map); err != nil {
|
||||
fmt.Println("read toml error: ", err.Error())
|
||||
@@ -44,7 +44,7 @@ func InitFile(filename string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//GetServiceConfig 获取service配置信息
|
||||
// GetServiceConfig Get service configuration information
|
||||
func GetServiceConfig() (name, displayName, desc string) {
|
||||
name = _map.ServiceName
|
||||
displayName = _map.ServiceDisplayname
|
||||
|
||||
@@ -10,7 +10,7 @@ const (
|
||||
`
|
||||
)
|
||||
|
||||
//IsRunTesting 判断是否在测试环境下使用
|
||||
// IsRunTesting Determine whether to use it in a test environment.判断是否在测试环境下使用
|
||||
func IsRunTesting() bool {
|
||||
if len(os.Args) > 1 {
|
||||
return strings.HasPrefix(os.Args[1], "-test")
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package gtools
|
||||
package cnf
|
||||
|
||||
const (
|
||||
_tagGorm = "gorm"
|
||||
_tagJSON = "json"
|
||||
)
|
||||
// EImportsHead imports head options. import包含选项
|
||||
var EImportsHead = map[string]string{
|
||||
"stirng": `"string"`,
|
||||
"time.Time": `"time"`,
|
||||
"gorm.Model": `"github.com/jinzhu/gorm"`,
|
||||
}
|
||||
|
||||
//TypeDicMp 精确匹配类型
|
||||
var TypeDicMp = map[string]string{
|
||||
// TypeMysqlDicMp Accurate matching type.精确匹配类型
|
||||
var TypeMysqlDicMp = map[string]string{
|
||||
"int": "int",
|
||||
"bigint": "int64",
|
||||
"varchar": "string",
|
||||
@@ -22,8 +24,8 @@ var TypeDicMp = map[string]string{
|
||||
"timestamp": "time.Time",
|
||||
}
|
||||
|
||||
//TypeMatchMp 模糊匹配类型
|
||||
var TypeMatchMp = map[string]string{
|
||||
// TypeMysqlMatchMp Fuzzy Matching Types.模糊匹配类型
|
||||
var TypeMysqlMatchMp = map[string]string{
|
||||
`^(int)[(]\d+[)]`: "int",
|
||||
`^(bigint)[(]\d+[)]`: "int64",
|
||||
`^(char)[(]\d+[)]`: "string",
|
||||
17
data/view/generate/common.go
Normal file
17
data/view/generate/common.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package generate
|
||||
|
||||
import "github.com/xxjwxc/public/tools"
|
||||
|
||||
// Add add one to print.打印
|
||||
func (p *PrintAtom) Add(str ...interface{}) {
|
||||
var tmp string
|
||||
for _, v := range str {
|
||||
tmp += tools.AsString(v) + _interval
|
||||
}
|
||||
p.lines = append(p.lines, tmp)
|
||||
}
|
||||
|
||||
// Generates Get the generated list.获取生成列表
|
||||
func (p *PrintAtom) Generates() []string {
|
||||
return p.lines
|
||||
}
|
||||
9
data/view/generate/def.go
Normal file
9
data/view/generate/def.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package generate
|
||||
|
||||
// interval.间隔
|
||||
var _interval = "\t"
|
||||
|
||||
// PrintAtom . atom print .原始打印
|
||||
type PrintAtom struct {
|
||||
lines []string
|
||||
}
|
||||
7
data/view/generate/def_ifs.go
Normal file
7
data/view/generate/def_ifs.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package generate
|
||||
|
||||
// IGenerate Generate Printing Interface.生成打印接口
|
||||
type IGenerate interface {
|
||||
// Get the generate data .获取结果数据
|
||||
Generate() string
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package generate
|
||||
package genstruct
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -6,40 +6,26 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
|
||||
"github.com/xxjwxc/public/tools"
|
||||
"github.com/xxjwxc/gormt/data/view/cnf"
|
||||
"github.com/xxjwxc/gormt/data/view/generate"
|
||||
)
|
||||
|
||||
//Add 打印
|
||||
func (p *PrintAtom) Add(str ...interface{}) {
|
||||
var tmp string
|
||||
for _, v := range str {
|
||||
tmp += tools.AsString(v) + _interval
|
||||
}
|
||||
p.lines = append(p.lines, tmp)
|
||||
}
|
||||
|
||||
//Generate 打印
|
||||
func (p *PrintAtom) Generate() []string {
|
||||
return p.lines
|
||||
}
|
||||
|
||||
//SetName 设置元素名字
|
||||
// SetName Setting element name.设置元素名字
|
||||
func (e *GenElement) SetName(name string) {
|
||||
e.Name = name
|
||||
}
|
||||
|
||||
//SetType 设置元素类型
|
||||
// SetType Setting element type.设置元素类型
|
||||
func (e *GenElement) SetType(tp string) {
|
||||
e.Type = tp
|
||||
}
|
||||
|
||||
//SetNotes 设置注释
|
||||
// SetNotes Setting element notes.设置注释
|
||||
func (e *GenElement) SetNotes(notes string) {
|
||||
e.Notes = strings.Replace(notes, "\n", ",", -1)
|
||||
}
|
||||
|
||||
//AddTag 添加一个tag标记
|
||||
// AddTag Add a tag .添加一个tag标记
|
||||
func (e *GenElement) AddTag(k string, v string) {
|
||||
if e.Tags == nil {
|
||||
e.Tags = make(map[string][]string)
|
||||
@@ -47,7 +33,7 @@ func (e *GenElement) AddTag(k string, v string) {
|
||||
e.Tags[k] = append(e.Tags[k], v)
|
||||
}
|
||||
|
||||
//Generate 获取结果数据
|
||||
// Generate Get the result data.获取结果数据
|
||||
func (e *GenElement) Generate() string {
|
||||
tag := ""
|
||||
|
||||
@@ -65,31 +51,31 @@ func (e *GenElement) Generate() string {
|
||||
tag = fmt.Sprintf("`%v`", strings.Join(tags, " "))
|
||||
}
|
||||
|
||||
var p PrintAtom
|
||||
var p generate.PrintAtom
|
||||
if len(e.Notes) > 0 {
|
||||
p.Add(e.Name, e.Type, tag, "// "+e.Notes)
|
||||
} else {
|
||||
p.Add(e.Name, e.Type, tag)
|
||||
}
|
||||
|
||||
return p.Generate()[0]
|
||||
return p.Generates()[0]
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// struct
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//SetCreatTableStr 设置创建语句,备份使用
|
||||
// SetCreatTableStr Set up SQL create statement, backup use setup create statement, backup use.设置创建语句,备份使用
|
||||
func (s *GenStruct) SetCreatTableStr(sql string) {
|
||||
s.SQLBuildStr = sql
|
||||
}
|
||||
|
||||
//SetStructName 获取结果数据
|
||||
// SetStructName Setting the name of struct.设置struct名字
|
||||
func (s *GenStruct) SetStructName(name string) {
|
||||
s.Name = name
|
||||
}
|
||||
|
||||
//SetNotes 设置注释
|
||||
// SetNotes set the notes.设置注释
|
||||
func (s *GenStruct) SetNotes(notes string) {
|
||||
a := strings.Split(notes, "\n")
|
||||
var text []string
|
||||
@@ -102,14 +88,14 @@ func (s *GenStruct) SetNotes(notes string) {
|
||||
s.Notes = strings.Join(text, "\r\n")
|
||||
}
|
||||
|
||||
//AddElement 添加一个/或多个元素
|
||||
// AddElement Add one or more elements.添加一个/或多个元素
|
||||
func (s *GenStruct) AddElement(e ...GenElement) {
|
||||
s.Em = append(s.Em, e...)
|
||||
}
|
||||
|
||||
//Generate 获取结果数据
|
||||
func (s *GenStruct) Generate() []string {
|
||||
var p PrintAtom
|
||||
// Generates Get the result data.获取结果数据
|
||||
func (s *GenStruct) Generates() []string {
|
||||
var p generate.PrintAtom
|
||||
if !config.GetSimple() {
|
||||
p.Add("/******sql******")
|
||||
p.Add(s.SQLBuildStr)
|
||||
@@ -122,19 +108,19 @@ func (s *GenStruct) Generate() []string {
|
||||
}
|
||||
p.Add("}")
|
||||
|
||||
return p.Generate()
|
||||
return p.Generates()
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// package
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//SetPackage 定义包名
|
||||
// SetPackage Defining package names.定义包名
|
||||
func (p *GenPackage) SetPackage(pname string) {
|
||||
p.Name = pname
|
||||
}
|
||||
|
||||
//AddImport 通过类型添加import
|
||||
// AddImport Add import by type.通过类型添加import
|
||||
func (p *GenPackage) AddImport(imp string) {
|
||||
if p.Imports == nil {
|
||||
p.Imports = make(map[string]string)
|
||||
@@ -142,16 +128,16 @@ func (p *GenPackage) AddImport(imp string) {
|
||||
p.Imports[imp] = imp
|
||||
}
|
||||
|
||||
//AddStruct 添加一个结构体
|
||||
// AddStruct Add a structure.添加一个结构体
|
||||
func (p *GenPackage) AddStruct(st GenStruct) {
|
||||
p.Structs = append(p.Structs, st)
|
||||
}
|
||||
|
||||
//Generate 获取结果数据
|
||||
// Generate Get the result data.获取结果数据
|
||||
func (p *GenPackage) Generate() string {
|
||||
p.genimport() //补充 import
|
||||
p.genimport() // auto add import .补充 import
|
||||
|
||||
var pa PrintAtom
|
||||
var pa generate.PrintAtom
|
||||
pa.Add("package", p.Name)
|
||||
// add import
|
||||
if p.Imports != nil {
|
||||
@@ -164,26 +150,26 @@ func (p *GenPackage) Generate() string {
|
||||
// -----------end
|
||||
// add struct
|
||||
for _, v := range p.Structs {
|
||||
for _, v1 := range v.Generate() {
|
||||
for _, v1 := range v.Generates() {
|
||||
pa.Add(v1)
|
||||
}
|
||||
}
|
||||
// -----------end
|
||||
|
||||
//输出
|
||||
// output.输出
|
||||
strOut := ""
|
||||
for _, v := range pa.Generate() {
|
||||
for _, v := range pa.Generates() {
|
||||
strOut += v + "\n"
|
||||
}
|
||||
|
||||
return strOut
|
||||
}
|
||||
|
||||
//获取结果数据
|
||||
// compensate and import .获取结果数据
|
||||
func (p *GenPackage) genimport() {
|
||||
for _, v := range p.Structs {
|
||||
for _, v1 := range v.Em {
|
||||
if v2, ok := EImportsHead[v1.Type]; ok {
|
||||
if v2, ok := cnf.EImportsHead[v1.Type]; ok {
|
||||
if len(v2) > 0 {
|
||||
p.AddImport(v2)
|
||||
}
|
||||
24
data/view/genstruct/def.go
Normal file
24
data/view/genstruct/def.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package genstruct
|
||||
|
||||
// GenElement element of sturct.元素类
|
||||
type GenElement struct {
|
||||
Name string // Name.元素名
|
||||
Type string // Type.类型标记
|
||||
Notes string // Notes.注释
|
||||
Tags map[string][]string // tages.标记
|
||||
}
|
||||
|
||||
// GenStruct struct of IStruct .结构体
|
||||
type GenStruct struct {
|
||||
SQLBuildStr string // Create SQL statements.创建sql语句
|
||||
Name string // name.名字
|
||||
Notes string // notes.注释
|
||||
Em []GenElement // em.元素组合
|
||||
}
|
||||
|
||||
// GenPackage package of IPackage.包体
|
||||
type GenPackage struct {
|
||||
Name string // name.名字
|
||||
Imports map[string]string // Inclusion term.元素组合
|
||||
Structs []GenStruct // struct list .结构体组合
|
||||
}
|
||||
47
data/view/genstruct/def_ifs.go
Normal file
47
data/view/genstruct/def_ifs.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package genstruct
|
||||
|
||||
import "github.com/xxjwxc/gormt/data/view/generate"
|
||||
|
||||
// IPackage package of interface
|
||||
type IPackage interface {
|
||||
generate.IGenerate
|
||||
// Defining package names.定义包名
|
||||
SetPackage(string)
|
||||
// Add import by type.通过类型添加import
|
||||
AddImport(string)
|
||||
// Add a structure.添加一个结构体
|
||||
AddStruct(IStruct)
|
||||
}
|
||||
|
||||
// IStruct struct of interface
|
||||
type IStruct interface {
|
||||
generate.IGenerate
|
||||
|
||||
// Set up SQL create statement, backup use.设置创建语句,备份使用
|
||||
SetCreatTableStr(string)
|
||||
|
||||
// Setting Structural Name设置结构体名字
|
||||
SetStructName(string)
|
||||
|
||||
// setting the notes.设置注释
|
||||
SetNotes(string)
|
||||
|
||||
// add one element.添加一个元素
|
||||
AddElement(...IElement)
|
||||
}
|
||||
|
||||
// IElement element in stuct of interface.元素类
|
||||
type IElement interface {
|
||||
generate.IGenerate
|
||||
|
||||
// setting name of element.设置元素名字
|
||||
SetName(string)
|
||||
// Setting element type.设置元素类型
|
||||
SetType(string)
|
||||
|
||||
// setting notes of element .设置注释
|
||||
SetNotes(string)
|
||||
|
||||
// add one tag.添加一个tag标记
|
||||
AddTag(k string, v string)
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package generate
|
||||
|
||||
//IPackage 包类
|
||||
type IPackage interface {
|
||||
//定义包名
|
||||
SetPackage(string)
|
||||
//通过类型添加import
|
||||
AddImport(string)
|
||||
//添加一个结构体
|
||||
AddStruct(IStruct)
|
||||
//获取结果数据
|
||||
Generate() string
|
||||
}
|
||||
|
||||
//IStruct 结构体类
|
||||
type IStruct interface {
|
||||
//设置创建语句,备份使用
|
||||
SetCreatTableStr(string)
|
||||
|
||||
//设置结构体名字
|
||||
SetStructName(string)
|
||||
|
||||
//设置注释
|
||||
SetNotes(string)
|
||||
|
||||
//添加一个元素
|
||||
AddElement(...IElement)
|
||||
|
||||
//获取结果数据
|
||||
Generate() []string
|
||||
}
|
||||
|
||||
//IElement 元素类
|
||||
type IElement interface {
|
||||
//设置元素名字
|
||||
SetName(string)
|
||||
//设置元素类型
|
||||
SetType(string)
|
||||
|
||||
//设置注释
|
||||
SetNotes(string)
|
||||
|
||||
//添加一个tag标记
|
||||
AddTag(k string, v string)
|
||||
|
||||
//获取结果数据
|
||||
Generate() string
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//GenElement 元素类
|
||||
type GenElement struct {
|
||||
Name string //元素名
|
||||
Type string //类型标记
|
||||
Notes string //注释
|
||||
Tags map[string][]string //标记
|
||||
}
|
||||
|
||||
//GenStruct 结构体
|
||||
type GenStruct struct {
|
||||
SQLBuildStr string //创建sql语句
|
||||
Name string //名字
|
||||
Notes string //注释
|
||||
Em []GenElement //元素组合
|
||||
}
|
||||
|
||||
//GenPackage 包体
|
||||
type GenPackage struct {
|
||||
Name string //名字
|
||||
Imports map[string]string //元素组合
|
||||
Structs []GenStruct //结构体组合
|
||||
}
|
||||
|
||||
//间隔
|
||||
var _interval = "\t"
|
||||
|
||||
//EImportsHead .
|
||||
var EImportsHead = map[string]string{
|
||||
"stirng": `"string"`,
|
||||
"time.Time": `"time"`,
|
||||
"gorm.Model": `"github.com/jinzhu/gorm"`,
|
||||
}
|
||||
|
||||
//PrintAtom .
|
||||
type PrintAtom struct {
|
||||
lines []string
|
||||
}
|
||||
@@ -6,23 +6,20 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"github.com/xxjwxc/gormt/data/view/model/genmysql"
|
||||
|
||||
"github.com/xxjwxc/public/mysqldb"
|
||||
"github.com/xxjwxc/public/tools"
|
||||
)
|
||||
|
||||
//Execute 开始执行
|
||||
// Execute
|
||||
func Execute() {
|
||||
|
||||
orm := mysqldb.OnInitDBOrm(config.GetMysqlConStr())
|
||||
defer orm.OnDestoryDB()
|
||||
|
||||
// var tt oauth_db.UserInfoTbl
|
||||
// tt.Nickname = "ticket_001"
|
||||
// orm.Where("nickname = ?", "ticket_001").Find(&tt)
|
||||
// fmt.Println(tt)
|
||||
|
||||
pkg := OnGetPackageInfo(orm, OnGetTables(orm))
|
||||
pkg := genmysql.GenMysql()
|
||||
pkg.SetPackage(getPkgName())
|
||||
str := pkg.Generate()
|
||||
|
||||
@@ -39,7 +36,7 @@ func Execute() {
|
||||
fmt.Println(string(cmd))
|
||||
}
|
||||
|
||||
// 通过config outdir 配置获取报名
|
||||
// Getting package names through config outdir configuration.通过config outdir 配置获取包名
|
||||
func getPkgName() string {
|
||||
dir := config.GetOutDir()
|
||||
dir = strings.Replace(dir, "\\", "/", -1)
|
||||
|
||||
1
data/view/model/genmysql/common.go
Normal file
1
data/view/model/genmysql/common.go
Normal file
@@ -0,0 +1 @@
|
||||
package genmysql
|
||||
15
data/view/model/genmysql/def.go
Normal file
15
data/view/model/genmysql/def.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package genmysql
|
||||
|
||||
const (
|
||||
_tagGorm = "gorm"
|
||||
_tagJSON = "json"
|
||||
)
|
||||
|
||||
// GenColumns show full columns
|
||||
type GenColumns struct {
|
||||
Field string `gorm:"column:Field"`
|
||||
Type string `gorm:"column:Type"`
|
||||
Key string `gorm:"column:Key"`
|
||||
Desc string `gorm:"column:Comment"`
|
||||
Null string `gorm:"column:Null"`
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package gtools
|
||||
package genmysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -6,17 +6,25 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"github.com/xxjwxc/gormt/data/view/gtools/generate"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/view/cnf"
|
||||
"github.com/xxjwxc/gormt/data/view/genstruct"
|
||||
"github.com/xxjwxc/public/mybigcamel"
|
||||
"github.com/xxjwxc/public/mysqldb"
|
||||
)
|
||||
|
||||
//OnGetTables 获取表列及注释
|
||||
// GenMysql 开始mysql解析
|
||||
func GenMysql() genstruct.GenPackage {
|
||||
orm := mysqldb.OnInitDBOrm(config.GetMysqlConStr())
|
||||
defer orm.OnDestoryDB()
|
||||
|
||||
return OnGetPackageInfo(orm, OnGetTables(orm))
|
||||
}
|
||||
|
||||
// OnGetTables Get columns and comments.获取表列及注释
|
||||
func OnGetTables(orm *mysqldb.MySqlDB) map[string]string {
|
||||
tbDesc := make(map[string]string)
|
||||
|
||||
//获取列名
|
||||
// Get column names.获取列名
|
||||
var tables []string
|
||||
rows, err := orm.Raw("show tables").Rows()
|
||||
if err != nil {
|
||||
@@ -32,7 +40,7 @@ func OnGetTables(orm *mysqldb.MySqlDB) map[string]string {
|
||||
tbDesc[table] = ""
|
||||
}
|
||||
|
||||
//获取表注释
|
||||
// Get table annotations.获取表注释
|
||||
rows, err = orm.Raw("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema=?;",
|
||||
config.GetMysqlDbInfo().Database).Rows()
|
||||
if err != nil {
|
||||
@@ -49,18 +57,18 @@ func OnGetTables(orm *mysqldb.MySqlDB) map[string]string {
|
||||
return tbDesc
|
||||
}
|
||||
|
||||
//OnGetPackageInfo 获取包信息
|
||||
func OnGetPackageInfo(orm *mysqldb.MySqlDB, tabls map[string]string) generate.GenPackage {
|
||||
var pkg generate.GenPackage
|
||||
// OnGetPackageInfo getpackage info.获取包信息
|
||||
func OnGetPackageInfo(orm *mysqldb.MySqlDB, tabls map[string]string) genstruct.GenPackage {
|
||||
var pkg genstruct.GenPackage
|
||||
for tab, desc := range tabls {
|
||||
var sct generate.GenStruct
|
||||
sct.SetStructName(OnGetCamelName(tab)) //大驼峰
|
||||
var sct genstruct.GenStruct
|
||||
sct.SetStructName(OnGetCamelName(tab)) // Big hump.大驼峰
|
||||
sct.SetNotes(desc)
|
||||
//构造元素
|
||||
// build element.构造元素
|
||||
ems := OnGetTableElement(orm, tab)
|
||||
// --------end
|
||||
sct.AddElement(ems...)
|
||||
//获取表注释
|
||||
// Get table annotations.获取表注释
|
||||
rows, err := orm.Raw("show create table " + tab).Rows()
|
||||
defer rows.Close()
|
||||
if err == nil {
|
||||
@@ -78,12 +86,12 @@ func OnGetPackageInfo(orm *mysqldb.MySqlDB, tabls map[string]string) generate.Ge
|
||||
return pkg
|
||||
}
|
||||
|
||||
//OnGetTableElement 获取表列及注释
|
||||
func OnGetTableElement(orm *mysqldb.MySqlDB, tab string) []generate.GenElement {
|
||||
var el []generate.GenElement
|
||||
// OnGetTableElement Get table columns and comments.获取表列及注释
|
||||
func OnGetTableElement(orm *mysqldb.MySqlDB, tab string) []genstruct.GenElement {
|
||||
var el []genstruct.GenElement
|
||||
|
||||
keyNums := make(map[string]int)
|
||||
//获取keys
|
||||
// get keys
|
||||
var Keys []struct {
|
||||
NonUnique int `gorm:"column:Non_unique"`
|
||||
KeyName string `gorm:"column:Key_name"`
|
||||
@@ -95,31 +103,24 @@ func OnGetTableElement(orm *mysqldb.MySqlDB, tab string) []generate.GenElement {
|
||||
}
|
||||
// ----------end
|
||||
|
||||
var list []struct {
|
||||
Field string `gorm:"column:Field"`
|
||||
Type string `gorm:"column:Type"`
|
||||
Key string `gorm:"column:Key"`
|
||||
Desc string `gorm:"column:Comment"`
|
||||
Null string `gorm:"column:Null"`
|
||||
}
|
||||
|
||||
//获取表注释
|
||||
var list []GenColumns
|
||||
// Get table annotations.获取表注释
|
||||
orm.Raw("show FULL COLUMNS from " + tab).Find(&list)
|
||||
//过滤 gorm.Model
|
||||
// filter gorm.Model.过滤 gorm.Model
|
||||
if OnHaveModel(&list) {
|
||||
var tmp generate.GenElement
|
||||
var tmp genstruct.GenElement
|
||||
tmp.SetType("gorm.Model")
|
||||
el = append(el, tmp)
|
||||
}
|
||||
// -----------------end
|
||||
|
||||
for _, v := range list {
|
||||
var tmp generate.GenElement
|
||||
var tmp genstruct.GenElement
|
||||
tmp.SetName(OnGetCamelName(v.Field))
|
||||
tmp.SetNotes(v.Desc)
|
||||
tmp.SetType(OnGetTypeName(v.Type))
|
||||
|
||||
if strings.EqualFold(v.Key, "PRI") { //设置主键
|
||||
if strings.EqualFold(v.Key, "PRI") { // Set primary key.设置主键
|
||||
tmp.AddTag(_tagGorm, "primary_key")
|
||||
} else if strings.EqualFold(v.Key, "UNI") { // unique
|
||||
tmp.AddTag(_tagGorm, "unique")
|
||||
@@ -133,7 +134,7 @@ func OnGetTableElement(orm *mysqldb.MySqlDB, tab string) []generate.GenElement {
|
||||
} else {
|
||||
_val += "unique_index"
|
||||
}
|
||||
if keyNums[v1.KeyName] > 1 { //复合索引?
|
||||
if keyNums[v1.KeyName] > 1 { // Composite index.复合索引
|
||||
_val += ":" + v1.KeyName
|
||||
}
|
||||
|
||||
@@ -159,28 +160,14 @@ func OnGetTableElement(orm *mysqldb.MySqlDB, tab string) []generate.GenElement {
|
||||
tmp.AddTag(_tagJSON, v.Field)
|
||||
}
|
||||
}
|
||||
|
||||
el = append(el, tmp)
|
||||
}
|
||||
|
||||
return el
|
||||
}
|
||||
|
||||
//OnHaveModel 过滤 gorm.Model
|
||||
func OnHaveModel(list *[]struct {
|
||||
Field string `gorm:"column:Field"`
|
||||
Type string `gorm:"column:Type"`
|
||||
Key string `gorm:"column:Key"`
|
||||
Desc string `gorm:"column:Comment"`
|
||||
Null string `gorm:"column:Null"`
|
||||
}) bool {
|
||||
var _temp []struct {
|
||||
Field string `gorm:"column:Field"`
|
||||
Type string `gorm:"column:Type"`
|
||||
Key string `gorm:"column:Key"`
|
||||
Desc string `gorm:"column:Comment"`
|
||||
Null string `gorm:"column:Null"`
|
||||
}
|
||||
// OnHaveModel filter.过滤 gorm.Model
|
||||
func OnHaveModel(list *[]GenColumns) bool {
|
||||
var _temp []GenColumns
|
||||
|
||||
num := 0
|
||||
for _, v := range *list {
|
||||
@@ -202,37 +189,37 @@ func OnHaveModel(list *[]struct {
|
||||
return false
|
||||
}
|
||||
|
||||
//OnGetTypeName 类型获取过滤
|
||||
// OnGetTypeName Type acquisition filtering.类型获取过滤
|
||||
func OnGetTypeName(name string) string {
|
||||
//先精确匹配
|
||||
if v, ok := TypeDicMp[name]; ok {
|
||||
// Precise matching first.先精确匹配
|
||||
if v, ok := cnf.TypeMysqlDicMp[name]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
//模糊正则匹配
|
||||
for k, v := range TypeMatchMp {
|
||||
// Fuzzy Regular Matching.模糊正则匹配
|
||||
for k, v := range cnf.TypeMysqlMatchMp {
|
||||
if ok, _ := regexp.MatchString(k, name); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("type (%v) not match in any way.", name))
|
||||
panic(fmt.Sprintf("type (%v) not match in any way.maybe need to add on ()", name))
|
||||
}
|
||||
|
||||
//OnGetCamelName 大驼峰或者首字母大写
|
||||
// OnGetCamelName Big Hump or Capital Letter.大驼峰或者首字母大写
|
||||
func OnGetCamelName(name string) string {
|
||||
if config.GetSingularTable() { //如果全局禁用表名复数
|
||||
if config.GetSingularTable() { // If the table name plural is globally disabled.如果全局禁用表名复数
|
||||
return TitleCase(name)
|
||||
}
|
||||
|
||||
return mybigcamel.Marshal(name)
|
||||
}
|
||||
|
||||
//TitleCase 首字母大写
|
||||
// TitleCase title case.首字母大写
|
||||
func TitleCase(name string) string {
|
||||
vv := []rune(name)
|
||||
if len(vv) > 0 {
|
||||
if bool(vv[0] >= 'a' && vv[0] <= 'z') { //首字母大写
|
||||
if bool(vv[0] >= 'a' && vv[0] <= 'z') { // title case.首字母大写
|
||||
vv[0] -= 32
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user