gorm tools
This commit is contained in:
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/xie1xiao1jun/public/tools"
|
||||
|
||||
"github.com/xie1xiao1jun/gorm-tools/data/config"
|
||||
"github.com/xie1xiao1jun/gorm-tools/data/view/gtools"
|
||||
"github.com/xie1xiao1jun/gormt/data/config"
|
||||
"github.com/xie1xiao1jun/gormt/data/view/gtools"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
@@ -94,7 +94,11 @@ func MergeMysqlDbInfo() {
|
||||
|
||||
config.SetMysqlDbInfo(&tmp)
|
||||
|
||||
config.SetOutDir(outDir)
|
||||
config.SetSingularTable(singular_table)
|
||||
if len(outDir) > 0 {
|
||||
config.SetOutDir(outDir)
|
||||
}
|
||||
|
||||
if singular_table {
|
||||
config.SetSingularTable(singular_table)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ type Config struct {
|
||||
CfgBase
|
||||
MySQLInfo MysqlDbInfo `toml:"mysql_info"`
|
||||
OutDir string `toml:"out_dir"`
|
||||
Simple bool `toml:"simple"`
|
||||
IsJsonTag bool `toml:"isJsonTag"`
|
||||
SingularTable bool `toml:"singular_table"`
|
||||
}
|
||||
|
||||
@@ -59,3 +61,13 @@ func SetSingularTable(b bool) {
|
||||
func GetSingularTable() bool {
|
||||
return _map.SingularTable
|
||||
}
|
||||
|
||||
//简单输出
|
||||
func GetSimple() bool {
|
||||
return _map.Simple
|
||||
}
|
||||
|
||||
//json标记
|
||||
func GetIsJsonTag() bool {
|
||||
return _map.IsJsonTag
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@ package gtools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/xie1xiao1jun/gorm-tools/data/config"
|
||||
"github.com/xie1xiao1jun/gorm-tools/data/view/gtools/generate"
|
||||
"github.com/xie1xiao1jun/gormt/data/config"
|
||||
"github.com/xie1xiao1jun/gormt/data/view/gtools/generate"
|
||||
"github.com/xie1xiao1jun/public/mybigcamel"
|
||||
"github.com/xie1xiao1jun/public/mysqldb"
|
||||
)
|
||||
@@ -40,7 +42,6 @@ func OnGetTables(orm *mysqldb.MySqlDB) map[string]string {
|
||||
for rows.Next() {
|
||||
var table, desc string
|
||||
rows.Scan(&table, &desc)
|
||||
tables = append(tables, table)
|
||||
tbDesc[table] = desc
|
||||
}
|
||||
|
||||
@@ -55,8 +56,20 @@ func OnGetPackageInfo(orm *mysqldb.MySqlDB, tabls map[string]string) generate.Ge
|
||||
sct.SetStructName(OnGetCamelName(tab)) //大驼峰
|
||||
sct.SetNotes(desc)
|
||||
//构造元素
|
||||
OnGetTableElement(orm, tab)
|
||||
ems := OnGetTableElement(orm, tab)
|
||||
//--------end
|
||||
sct.AddElement(ems...)
|
||||
//获取表注释
|
||||
rows, err := orm.Raw("show create table " + tab).Rows()
|
||||
defer rows.Close()
|
||||
if err == nil {
|
||||
if rows.Next() {
|
||||
var table, CreateTable string
|
||||
rows.Scan(&table, &CreateTable)
|
||||
sct.SetCreatTableStr(CreateTable)
|
||||
}
|
||||
}
|
||||
//----------end
|
||||
|
||||
pkg.AddStruct(sct)
|
||||
}
|
||||
@@ -67,25 +80,144 @@ func OnGetPackageInfo(orm *mysqldb.MySqlDB, tabls map[string]string) generate.Ge
|
||||
// 获取表列及注释
|
||||
func OnGetTableElement(orm *mysqldb.MySqlDB, tab string) []generate.GenElement {
|
||||
var el []generate.GenElement
|
||||
|
||||
keyNums := make(map[string]int)
|
||||
//获取keys
|
||||
var Keys []struct {
|
||||
NonUnique int `gorm:"column:Non_unique"`
|
||||
KeyName string `gorm:"column:Key_name"`
|
||||
ColumnName string `gorm:"column:Column_name"`
|
||||
}
|
||||
orm.Raw("show keys from " + tab).Find(&Keys)
|
||||
for _, v := range Keys {
|
||||
keyNums[v.KeyName]++
|
||||
}
|
||||
//----------end
|
||||
|
||||
var list []struct {
|
||||
Field string `gorm:"column:Field"`
|
||||
Type string `gorm:"column:Type"`
|
||||
Key string `gorm:"column:key"`
|
||||
Key string `gorm:"column:Key"`
|
||||
Desc string `gorm:"column:Comment"`
|
||||
Null string `gorm:"column:Null"`
|
||||
}
|
||||
|
||||
//获取表注释
|
||||
orm.Raw("show FULL COLUMNS from " + tab).Find(&list)
|
||||
//过滤 gorm.Model
|
||||
if OnHaveModel(&list) {
|
||||
var tmp generate.GenElement
|
||||
tmp.SetType("gorm.Model")
|
||||
el = append(el, tmp)
|
||||
}
|
||||
//-----------------end
|
||||
|
||||
for _, v := range list {
|
||||
var tmp generate.GenElement
|
||||
tmp.SetName(OnGetCamelName(v.Field))
|
||||
tmp.SetNotes(v.Desc)
|
||||
tmp.SetType(v.Type)
|
||||
tmp.SetType(OnGetTypeName(v.Type))
|
||||
|
||||
if strings.EqualFold(v.Key, "PRI") { //设置主键
|
||||
tmp.AddTag(_tag_gorm, "primary_key")
|
||||
} else if strings.EqualFold(v.Key, "UNI") { //unique
|
||||
tmp.AddTag(_tag_gorm, "unique")
|
||||
} else {
|
||||
//index
|
||||
for _, v1 := range Keys {
|
||||
if strings.EqualFold(v1.ColumnName, v.Field) {
|
||||
_val := ""
|
||||
if v1.NonUnique == 1 { //index
|
||||
_val += "index"
|
||||
} else {
|
||||
_val += "unique_index"
|
||||
}
|
||||
if keyNums[v1.KeyName] > 1 { //复合索引?
|
||||
_val += ":" + v1.KeyName
|
||||
}
|
||||
|
||||
tmp.AddTag(_tag_gorm, _val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//simple output
|
||||
if !config.GetSimple() {
|
||||
tmp.AddTag(_tag_gorm, "column:"+v.Field)
|
||||
tmp.AddTag(_tag_gorm, "type:"+v.Type)
|
||||
if strings.EqualFold(v.Null, "NO") {
|
||||
tmp.AddTag(_tag_gorm, "not null")
|
||||
}
|
||||
}
|
||||
|
||||
//json tag
|
||||
if config.GetIsJsonTag() {
|
||||
if strings.EqualFold(v.Field, "id") {
|
||||
tmp.AddTag(_tag_json, "-")
|
||||
} else {
|
||||
tmp.AddTag(_tag_json, v.Field)
|
||||
}
|
||||
}
|
||||
|
||||
el = append(el, tmp)
|
||||
}
|
||||
|
||||
return el
|
||||
}
|
||||
|
||||
//过滤 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"`
|
||||
}
|
||||
|
||||
num := 0
|
||||
for _, v := range *list {
|
||||
if strings.EqualFold(v.Field, "id") ||
|
||||
strings.EqualFold(v.Field, "created_at") ||
|
||||
strings.EqualFold(v.Field, "updated_at") ||
|
||||
strings.EqualFold(v.Field, "deleted_at") {
|
||||
num++
|
||||
} else {
|
||||
_temp = append(_temp, v)
|
||||
}
|
||||
}
|
||||
|
||||
if num >= 4 {
|
||||
*list = _temp
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
//类型获取过滤
|
||||
func OnGetTypeName(name string) string {
|
||||
//先精确匹配
|
||||
if v, ok := TypeDicMp[name]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
//模糊正则匹配
|
||||
for k, v := range TypeMatchMp {
|
||||
if ok, _ := regexp.MatchString(k, name); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("type (%v) not match in any way.", name))
|
||||
}
|
||||
|
||||
//大驼峰或者首字母大写
|
||||
func OnGetCamelName(name string) string {
|
||||
if config.GetSingularTable() { //如果全局禁用表名复数
|
||||
|
||||
@@ -1,6 +1,24 @@
|
||||
package gtools
|
||||
|
||||
const (
|
||||
_tag_gorm = "gorm"
|
||||
_tag_json = "json"
|
||||
)
|
||||
|
||||
//精确匹配类型
|
||||
var TypeDicMp = map[string]string{
|
||||
"1000": "M",
|
||||
"900": "CM",
|
||||
"int": "int",
|
||||
"bigint": "int64",
|
||||
"varchar": "string",
|
||||
"datetime": "time.Time",
|
||||
"tinyint(1)": "bool",
|
||||
"tinyint(1) unsigned": "bool",
|
||||
"json": "string",
|
||||
}
|
||||
|
||||
//模糊匹配类型
|
||||
var TypeMatchMp = map[string]string{
|
||||
`int[(]\d+[)]`: "int",
|
||||
`bigint[(]\d+[)]`: "int64",
|
||||
`varchar[(]\d+[)]`: "string",
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/xie1xiao1jun/gormt/data/config"
|
||||
|
||||
"github.com/xie1xiao1jun/public/tools"
|
||||
)
|
||||
|
||||
@@ -47,11 +49,12 @@ func (e *GenElement) AddTag(k string, v string) {
|
||||
//获取结果数据
|
||||
func (e *GenElement) Generate() string {
|
||||
tag := ""
|
||||
var tags []string
|
||||
if e.Tags != nil {
|
||||
for k, v := range e.Tags {
|
||||
tag += fmt.Sprintf(`%v:"%v"`, k, strings.Join(v, ","))
|
||||
tags = append(tags, fmt.Sprintf(`%v:"%v"`, k, strings.Join(v, ";")))
|
||||
}
|
||||
tag = fmt.Sprintf("`%v`", tag)
|
||||
tag = fmt.Sprintf("`%v`", strings.Join(tags, " "))
|
||||
}
|
||||
|
||||
var p PrintAtom
|
||||
@@ -63,6 +66,11 @@ func (e *GenElement) Generate() string {
|
||||
// struct
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//设置创建语句,备份使用
|
||||
func (s *GenStruct) SetCreatTableStr(sql string) {
|
||||
s.SqlBuildStr = sql
|
||||
}
|
||||
|
||||
//获取结果数据
|
||||
func (s *GenStruct) SetStructName(name string) {
|
||||
s.Name = name
|
||||
@@ -73,14 +81,19 @@ func (e *GenStruct) SetNotes(notes string) {
|
||||
e.Notes = notes
|
||||
}
|
||||
|
||||
//添加一个元素
|
||||
func (s *GenStruct) AddElement(e GenElement) {
|
||||
s.Em = append(s.Em, e)
|
||||
//添加一个/或多个元素
|
||||
func (s *GenStruct) AddElement(e ...GenElement) {
|
||||
s.Em = append(s.Em, e...)
|
||||
}
|
||||
|
||||
//获取结果数据
|
||||
func (s *GenStruct) Generate() []string {
|
||||
var p PrintAtom
|
||||
if !config.GetSimple() {
|
||||
p.Add("/******sql******")
|
||||
p.Add(s.SqlBuildStr)
|
||||
p.Add("******sql******/")
|
||||
}
|
||||
p.Add("//", s.Notes)
|
||||
p.Add("type", s.Name, "struct {")
|
||||
for _, v := range s.Em {
|
||||
|
||||
@@ -14,6 +14,9 @@ type IPackage interface {
|
||||
|
||||
//结构体类
|
||||
type IStruct interface {
|
||||
//设置创建语句,备份使用
|
||||
SetCreatTableStr(string)
|
||||
|
||||
//设置结构体名字
|
||||
SetStructName(string)
|
||||
|
||||
@@ -21,7 +24,7 @@ type IStruct interface {
|
||||
SetNotes(string)
|
||||
|
||||
//添加一个元素
|
||||
AddElement(IElement)
|
||||
AddElement(...IElement)
|
||||
|
||||
//获取结果数据
|
||||
Generate() []string
|
||||
@@ -58,9 +61,10 @@ type GenElement struct {
|
||||
|
||||
//结构体
|
||||
type GenStruct struct {
|
||||
Name string //名字
|
||||
Notes string //注释
|
||||
Em []GenElement //元素组合
|
||||
SqlBuildStr string //创建sql语句
|
||||
Name string //名字
|
||||
Notes string //注释
|
||||
Em []GenElement //元素组合
|
||||
}
|
||||
|
||||
//包体
|
||||
@@ -74,77 +78,9 @@ type GenPackage struct {
|
||||
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,
|
||||
"stirng": `"string"`,
|
||||
"time.Time": `"time"`,
|
||||
"gorm.Model": `"github.com/jinzhu/gorm"`,
|
||||
}
|
||||
|
||||
type PrintAtom struct {
|
||||
|
||||
@@ -1,15 +1,33 @@
|
||||
package gtools
|
||||
|
||||
import (
|
||||
"github.com/xie1xiao1jun/gorm-tools/data/config"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
|
||||
"github.com/xie1xiao1jun/gormt/data/config"
|
||||
"github.com/xie1xiao1jun/public/mysqldb"
|
||||
"github.com/xie1xiao1jun/public/tools"
|
||||
)
|
||||
|
||||
//开始执行
|
||||
func Execute() {
|
||||
|
||||
orm := mysqldb.OnInitDBOrm(config.GetMysqlConStr())
|
||||
defer orm.OnDestoryDB()
|
||||
|
||||
OnGetPackageInfo(orm, OnGetTables(orm))
|
||||
// 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.SetPackage(config.GetMysqlDbInfo().Database)
|
||||
str := pkg.Generate()
|
||||
|
||||
path := config.GetOutDir() + "/" + config.GetMysqlDbInfo().Database + "/" + config.GetMysqlDbInfo().Database + ".go"
|
||||
tools.WriteFile(path,
|
||||
[]string{str}, true)
|
||||
|
||||
cmd, _ := exec.Command("gofmt", "-l", "-w", path).Output()
|
||||
fmt.Println(string(cmd))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user