Comprehensive optimization
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1 +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
|
||||
}
|
||||
|
||||
@@ -76,6 +76,10 @@ func (s *GenStruct) SetStructName(name string) {
|
||||
|
||||
// SetNotes set the notes.设置注释
|
||||
func (s *GenStruct) SetNotes(notes string) {
|
||||
// if len(notes) > 0 {
|
||||
// notes = s.Name + " " + notes
|
||||
// }
|
||||
|
||||
a := strings.Split(notes, "\n")
|
||||
var text []string
|
||||
|
||||
|
||||
12
data/view/gtools/commont.go
Normal file
12
data/view/gtools/commont.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package gtools
|
||||
|
||||
import (
|
||||
"github.com/xxjwxc/gormt/data/view/model"
|
||||
"github.com/xxjwxc/gormt/data/view/model/genmysql"
|
||||
)
|
||||
|
||||
// GetModel get model interface. 获取model接口
|
||||
func GetModel() model.IModel {
|
||||
//now just support mysql
|
||||
return &genmysql.MySQLModel
|
||||
}
|
||||
@@ -3,10 +3,10 @@ package gtools
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/view/model"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"github.com/xxjwxc/gormt/data/view/model/genmysql"
|
||||
|
||||
"github.com/xxjwxc/public/tools"
|
||||
)
|
||||
@@ -19,11 +19,12 @@ func Execute() {
|
||||
// orm.Where("nickname = ?", "ticket_001").Find(&tt)
|
||||
// fmt.Println(tt)
|
||||
|
||||
pkg := genmysql.GenMysql()
|
||||
pkg.SetPackage(getPkgName())
|
||||
str := pkg.Generate()
|
||||
modeldb := GetModel()
|
||||
pkg := modeldb.GenModel()
|
||||
pkg.PackageName = modeldb.GetPkgName()
|
||||
str := model.Generate(pkg)
|
||||
|
||||
path := config.GetOutDir() + "/" + config.GetMysqlDbInfo().Database + ".go"
|
||||
path := config.GetOutDir() + "/" + modeldb.GetDbName() + ".go"
|
||||
tools.WriteFile(path,
|
||||
[]string{str}, true)
|
||||
|
||||
@@ -35,28 +36,3 @@ func Execute() {
|
||||
cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output()
|
||||
fmt.Println(string(cmd))
|
||||
}
|
||||
|
||||
// Getting package names through config outdir configuration.通过config outdir 配置获取包名
|
||||
func getPkgName() string {
|
||||
dir := config.GetOutDir()
|
||||
dir = strings.Replace(dir, "\\", "/", -1)
|
||||
if len(dir) > 0 {
|
||||
if dir[len(dir)-1] == '/' {
|
||||
dir = dir[:(len(dir) - 1)]
|
||||
}
|
||||
}
|
||||
var pkgName string
|
||||
list := strings.Split(dir, "/")
|
||||
if len(list) > 0 {
|
||||
pkgName = list[len(list)-1]
|
||||
}
|
||||
|
||||
if len(pkgName) == 0 || pkgName == "." {
|
||||
list = strings.Split(tools.GetModelPath(), "/")
|
||||
if len(list) > 0 {
|
||||
pkgName = list[len(list)-1]
|
||||
}
|
||||
}
|
||||
|
||||
return pkgName
|
||||
}
|
||||
|
||||
48
data/view/model/common.go
Normal file
48
data/view/model/common.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"github.com/xxjwxc/gormt/data/view/cnf"
|
||||
"github.com/xxjwxc/public/mybigcamel"
|
||||
)
|
||||
|
||||
// getCamelName Big Hump or Capital Letter.大驼峰或者首字母大写
|
||||
func getCamelName(name string) string {
|
||||
if config.GetSingularTable() { // If the table name plural is globally disabled.如果全局禁用表名复数
|
||||
return titleCase(name)
|
||||
}
|
||||
|
||||
return mybigcamel.Marshal(name)
|
||||
}
|
||||
|
||||
// titleCase title case.首字母大写
|
||||
func titleCase(name string) string {
|
||||
vv := []rune(name)
|
||||
if len(vv) > 0 {
|
||||
if bool(vv[0] >= 'a' && vv[0] <= 'z') { // title case.首字母大写
|
||||
vv[0] -= 32
|
||||
}
|
||||
}
|
||||
|
||||
return string(vv)
|
||||
}
|
||||
|
||||
// getTypeName Type acquisition filtering.类型获取过滤
|
||||
func getTypeName(name string) string {
|
||||
// Precise matching first.先精确匹配
|
||||
if v, ok := cnf.TypeMysqlDicMp[name]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
// 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.maybe need to add on ()", name))
|
||||
}
|
||||
59
data/view/model/def.go
Normal file
59
data/view/model/def.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package model
|
||||
|
||||
const (
|
||||
_tagGorm = "gorm"
|
||||
_tagJSON = "json"
|
||||
)
|
||||
|
||||
// ColumusKey Columus type elem. 类型枚举
|
||||
type ColumusKey int
|
||||
|
||||
const (
|
||||
// ColumusKeyDefault default
|
||||
ColumusKeyDefault = iota
|
||||
// ColumusKeyPrimary primary key.主键
|
||||
ColumusKeyPrimary
|
||||
// ColumusKeyUnique unique key.唯一索引
|
||||
ColumusKeyUnique
|
||||
// ColumusKeyIndex index key.复合索引
|
||||
ColumusKeyIndex
|
||||
// ColumusKeyUniqueIndex unique index key.唯一复合索引
|
||||
ColumusKeyUniqueIndex
|
||||
)
|
||||
|
||||
// DBInfo database default info
|
||||
type DBInfo struct {
|
||||
PackageName string
|
||||
TabList []TabInfo // table list .表列表
|
||||
}
|
||||
|
||||
// TabInfo database table default attribute
|
||||
type TabInfo struct {
|
||||
BaseInfo
|
||||
SQLBuildStr string // Create SQL statements.创建sql语句
|
||||
Em []ColumusInfo // Columus list .表列表组合
|
||||
}
|
||||
|
||||
// ColumusInfo Columus list .表列信息
|
||||
type ColumusInfo struct {
|
||||
BaseInfo
|
||||
Type string // Type.类型标记
|
||||
//Index string // index or unique_index or ''
|
||||
Index []KList // index list.index列表
|
||||
IsNull bool // null if db is set null
|
||||
|
||||
//
|
||||
// Tags map[string][]string // tages.标记
|
||||
}
|
||||
|
||||
// KList database index /unique_index list.数据库index /unique_index 列表
|
||||
type KList struct {
|
||||
Key ColumusKey // non_unique of (show keys from [table])
|
||||
KeyName string // key_name of (show keys from [table])
|
||||
}
|
||||
|
||||
// BaseInfo base common attribute. 基础属性
|
||||
type BaseInfo struct {
|
||||
Name string // table name.表名
|
||||
Notes string // table comment . 表注释
|
||||
}
|
||||
8
data/view/model/def_ifs.go
Normal file
8
data/view/model/def_ifs.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package model
|
||||
|
||||
// IModel Implement the interface to acquire database information and initialize it.实现接口获取数据库信息获取并初始化
|
||||
type IModel interface {
|
||||
GenModel() DBInfo
|
||||
GetDbName() string
|
||||
GetPkgName() string // Getting package names through config outdir configuration.通过config outdir 配置获取包名
|
||||
}
|
||||
@@ -1 +1,27 @@
|
||||
package genmysql
|
||||
|
||||
import "strings"
|
||||
|
||||
// filterModel filter.过滤 gorm.Model
|
||||
func filterModel(list *[]genColumns) bool {
|
||||
var _temp []genColumns
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package genmysql
|
||||
|
||||
const (
|
||||
_tagGorm = "gorm"
|
||||
_tagJSON = "json"
|
||||
)
|
||||
type keys struct {
|
||||
NonUnique int `gorm:"column:Non_unique"`
|
||||
KeyName string `gorm:"column:Key_name"`
|
||||
ColumnName string `gorm:"column:Column_name"`
|
||||
}
|
||||
|
||||
// GenColumns show full columns
|
||||
type GenColumns struct {
|
||||
// genColumns show full columns
|
||||
type genColumns struct {
|
||||
Field string `gorm:"column:Field"`
|
||||
Type string `gorm:"column:Type"`
|
||||
Key string `gorm:"column:Key"`
|
||||
|
||||
@@ -2,26 +2,149 @@ package genmysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"github.com/xxjwxc/gormt/data/view/cnf"
|
||||
"github.com/xxjwxc/gormt/data/view/genstruct"
|
||||
"github.com/xxjwxc/public/mybigcamel"
|
||||
"github.com/xxjwxc/gormt/data/view/model"
|
||||
"github.com/xxjwxc/public/mysqldb"
|
||||
"github.com/xxjwxc/public/tools"
|
||||
)
|
||||
|
||||
// GenMysql 开始mysql解析
|
||||
func GenMysql() genstruct.GenPackage {
|
||||
// MySQLModel mysql model from IModel
|
||||
var MySQLModel mysqlModel
|
||||
|
||||
type mysqlModel struct {
|
||||
}
|
||||
|
||||
// GenModel get model.DBInfo info.获取数据库相关属性
|
||||
func (m *mysqlModel) GenModel() model.DBInfo {
|
||||
orm := mysqldb.OnInitDBOrm(config.GetMysqlConStr())
|
||||
defer orm.OnDestoryDB()
|
||||
|
||||
return OnGetPackageInfo(orm, OnGetTables(orm))
|
||||
var dbInfo model.DBInfo
|
||||
getPackageInfo(orm, &dbInfo)
|
||||
return dbInfo
|
||||
}
|
||||
|
||||
// OnGetTables Get columns and comments.获取表列及注释
|
||||
func OnGetTables(orm *mysqldb.MySqlDB) map[string]string {
|
||||
// GetDbName get database name.获取数据库名字
|
||||
func (m *mysqlModel) GetDbName() string {
|
||||
return config.GetMysqlDbInfo().Database
|
||||
}
|
||||
|
||||
// GetPkgName package names through config outdir configuration.通过config outdir 配置获取包名
|
||||
func (m *mysqlModel) GetPkgName() string {
|
||||
dir := config.GetOutDir()
|
||||
dir = strings.Replace(dir, "\\", "/", -1)
|
||||
if len(dir) > 0 {
|
||||
if dir[len(dir)-1] == '/' {
|
||||
dir = dir[:(len(dir) - 1)]
|
||||
}
|
||||
}
|
||||
var pkgName string
|
||||
list := strings.Split(dir, "/")
|
||||
if len(list) > 0 {
|
||||
pkgName = list[len(list)-1]
|
||||
}
|
||||
|
||||
if len(pkgName) == 0 || pkgName == "." {
|
||||
list = strings.Split(tools.GetModelPath(), "/")
|
||||
if len(list) > 0 {
|
||||
pkgName = list[len(list)-1]
|
||||
}
|
||||
}
|
||||
|
||||
return pkgName
|
||||
}
|
||||
|
||||
func getPackageInfo(orm *mysqldb.MySqlDB, info *model.DBInfo) {
|
||||
tabls := getTables(orm) // get table and notes
|
||||
for tabName, notes := range tabls {
|
||||
var tab model.TabInfo
|
||||
tab.Name = tabName
|
||||
tab.Notes = notes
|
||||
|
||||
// Get create SQL statements.获取创建sql语句
|
||||
rows, err := orm.Raw("show create table " + tabName).Rows()
|
||||
defer rows.Close()
|
||||
if err == nil {
|
||||
if rows.Next() {
|
||||
var table, CreateTable string
|
||||
rows.Scan(&table, &CreateTable)
|
||||
tab.SQLBuildStr = CreateTable
|
||||
}
|
||||
}
|
||||
// ----------end
|
||||
|
||||
// build element.构造元素
|
||||
tab.Em = getTableElement(orm, tabName)
|
||||
// --------end
|
||||
|
||||
info.TabList = append(info.TabList, tab)
|
||||
}
|
||||
}
|
||||
|
||||
// getTableElement Get table columns and comments.获取表列及注释
|
||||
func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo) {
|
||||
keyNums := make(map[string]int)
|
||||
// get keys
|
||||
var Keys []keys
|
||||
orm.Raw("show keys from " + tab).Find(&Keys)
|
||||
for _, v := range Keys {
|
||||
keyNums[v.KeyName]++
|
||||
}
|
||||
// ----------end
|
||||
|
||||
var list []genColumns
|
||||
// Get table annotations.获取表注释
|
||||
orm.Raw("show FULL COLUMNS from " + tab).Find(&list)
|
||||
// filter gorm.Model.过滤 gorm.Model
|
||||
if filterModel(&list) {
|
||||
el = append(el, model.ColumusInfo{
|
||||
Type: "gorm.Model",
|
||||
})
|
||||
}
|
||||
// -----------------end
|
||||
|
||||
for _, v := range list {
|
||||
var tmp model.ColumusInfo
|
||||
tmp.Name = v.Field
|
||||
tmp.Notes = v.Desc
|
||||
tmp.Type = v.Type
|
||||
|
||||
// keys
|
||||
if strings.EqualFold(v.Key, "PRI") { // Set primary key.设置主键
|
||||
tmp.Index = append(tmp.Index, model.KList{
|
||||
Key: model.ColumusKeyPrimary,
|
||||
})
|
||||
} else if strings.EqualFold(v.Key, "UNI") { // unique
|
||||
tmp.Index = append(tmp.Index, model.KList{
|
||||
Key: model.ColumusKeyUnique,
|
||||
})
|
||||
} else {
|
||||
for _, v1 := range Keys {
|
||||
if strings.EqualFold(v1.ColumnName, v.Field) {
|
||||
var k model.KList
|
||||
if v1.NonUnique == 1 { // index
|
||||
k.Key = model.ColumusKeyIndex
|
||||
} else {
|
||||
k.Key = model.ColumusKeyUniqueIndex
|
||||
}
|
||||
if keyNums[v1.KeyName] > 1 { // Composite index.复合索引
|
||||
k.KeyName = v1.KeyName
|
||||
}
|
||||
tmp.Index = append(tmp.Index, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tmp.IsNull = strings.EqualFold(v.Null, "YES")
|
||||
el = append(el, tmp)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// getTables Get columns and comments.获取表列及注释
|
||||
func getTables(orm *mysqldb.MySqlDB) map[string]string {
|
||||
tbDesc := make(map[string]string)
|
||||
|
||||
// Get column names.获取列名
|
||||
@@ -56,173 +179,3 @@ func OnGetTables(orm *mysqldb.MySqlDB) map[string]string {
|
||||
|
||||
return tbDesc
|
||||
}
|
||||
|
||||
// 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 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 {
|
||||
if rows.Next() {
|
||||
var table, CreateTable string
|
||||
rows.Scan(&table, &CreateTable)
|
||||
sct.SetCreatTableStr(CreateTable)
|
||||
}
|
||||
}
|
||||
// ----------end
|
||||
|
||||
pkg.AddStruct(sct)
|
||||
}
|
||||
|
||||
return pkg
|
||||
}
|
||||
|
||||
// OnGetTableElement Get table columns and comments.获取表列及注释
|
||||
func OnGetTableElement(orm *mysqldb.MySqlDB, tab string) []genstruct.GenElement {
|
||||
var el []genstruct.GenElement
|
||||
|
||||
keyNums := make(map[string]int)
|
||||
// get 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 []GenColumns
|
||||
// Get table annotations.获取表注释
|
||||
orm.Raw("show FULL COLUMNS from " + tab).Find(&list)
|
||||
// filter gorm.Model.过滤 gorm.Model
|
||||
if OnHaveModel(&list) {
|
||||
var tmp genstruct.GenElement
|
||||
tmp.SetType("gorm.Model")
|
||||
el = append(el, tmp)
|
||||
}
|
||||
// -----------------end
|
||||
|
||||
for _, v := range list {
|
||||
var tmp genstruct.GenElement
|
||||
tmp.SetName(OnGetCamelName(v.Field))
|
||||
tmp.SetNotes(v.Desc)
|
||||
tmp.SetType(OnGetTypeName(v.Type))
|
||||
|
||||
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")
|
||||
} 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 { // Composite index.复合索引
|
||||
_val += ":" + v1.KeyName
|
||||
}
|
||||
|
||||
tmp.AddTag(_tagGorm, _val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// simple output
|
||||
if !config.GetSimple() {
|
||||
tmp.AddTag(_tagGorm, "column:"+v.Field)
|
||||
tmp.AddTag(_tagGorm, "type:"+v.Type)
|
||||
if strings.EqualFold(v.Null, "NO") {
|
||||
tmp.AddTag(_tagGorm, "not null")
|
||||
}
|
||||
}
|
||||
|
||||
// json tag
|
||||
if config.GetIsJSONTag() {
|
||||
if strings.EqualFold(v.Field, "id") {
|
||||
tmp.AddTag(_tagJSON, "-")
|
||||
} else {
|
||||
tmp.AddTag(_tagJSON, v.Field)
|
||||
}
|
||||
}
|
||||
el = append(el, tmp)
|
||||
}
|
||||
return el
|
||||
}
|
||||
|
||||
// OnHaveModel filter.过滤 gorm.Model
|
||||
func OnHaveModel(list *[]GenColumns) bool {
|
||||
var _temp []GenColumns
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// OnGetTypeName Type acquisition filtering.类型获取过滤
|
||||
func OnGetTypeName(name string) string {
|
||||
// Precise matching first.先精确匹配
|
||||
if v, ok := cnf.TypeMysqlDicMp[name]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
// 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.maybe need to add on ()", name))
|
||||
}
|
||||
|
||||
// OnGetCamelName Big Hump or Capital Letter.大驼峰或者首字母大写
|
||||
func OnGetCamelName(name string) string {
|
||||
if config.GetSingularTable() { // If the table name plural is globally disabled.如果全局禁用表名复数
|
||||
return TitleCase(name)
|
||||
}
|
||||
|
||||
return mybigcamel.Marshal(name)
|
||||
}
|
||||
|
||||
// TitleCase title case.首字母大写
|
||||
func TitleCase(name string) string {
|
||||
vv := []rune(name)
|
||||
if len(vv) > 0 {
|
||||
if bool(vv[0] >= 'a' && vv[0] <= 'z') { // title case.首字母大写
|
||||
vv[0] -= 32
|
||||
}
|
||||
}
|
||||
|
||||
return string(vv)
|
||||
}
|
||||
|
||||
80
data/view/model/model.go
Normal file
80
data/view/model/model.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"github.com/xxjwxc/gormt/data/view/genstruct"
|
||||
)
|
||||
|
||||
// Generate build code string.生成代码
|
||||
func Generate(info DBInfo) string {
|
||||
var pkg genstruct.GenPackage
|
||||
pkg.SetPackage(info.PackageName) //package name
|
||||
for _, tab := range info.TabList {
|
||||
var sct genstruct.GenStruct
|
||||
sct.SetStructName(getCamelName(tab.Name)) // Big hump.大驼峰
|
||||
sct.SetNotes(tab.Notes)
|
||||
sct.AddElement(getTableElement(tab.Em)...) // build element.构造元素
|
||||
sct.SetCreatTableStr(tab.SQLBuildStr)
|
||||
pkg.AddStruct(sct)
|
||||
}
|
||||
|
||||
return pkg.Generate()
|
||||
}
|
||||
|
||||
// getTableElement Get table columns and comments.获取表列及注释
|
||||
func getTableElement(tabs []ColumusInfo) (el []genstruct.GenElement) {
|
||||
for _, v := range tabs {
|
||||
var tmp genstruct.GenElement
|
||||
if strings.EqualFold(v.Type, "gorm.Model") { // gorm model
|
||||
tmp.SetType(v.Type) //
|
||||
} else {
|
||||
tmp.SetName(getCamelName(v.Name))
|
||||
tmp.SetNotes(v.Notes)
|
||||
tmp.SetType(getTypeName(v.Type))
|
||||
for _, v1 := range v.Index {
|
||||
switch v1.Key {
|
||||
// case ColumusKeyDefault:
|
||||
case ColumusKeyPrimary: // primary key.主键
|
||||
tmp.AddTag(_tagGorm, "primary_key")
|
||||
case ColumusKeyUnique: // unique key.唯一索引
|
||||
tmp.AddTag(_tagGorm, "unique")
|
||||
case ColumusKeyIndex: // index key.复合索引
|
||||
if len(v1.KeyName) > 0 {
|
||||
tmp.AddTag(_tagGorm, "index:"+v1.KeyName)
|
||||
} else {
|
||||
tmp.AddTag(_tagGorm, "index")
|
||||
}
|
||||
case ColumusKeyUniqueIndex: // unique index key.唯一复合索引
|
||||
if len(v1.KeyName) > 0 {
|
||||
tmp.AddTag(_tagGorm, "unique_index:"+v1.KeyName)
|
||||
} else {
|
||||
tmp.AddTag(_tagGorm, "unique_index")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// not simple output
|
||||
if !config.GetSimple() && len(v.Name) > 0 {
|
||||
tmp.AddTag(_tagGorm, "column:"+v.Name)
|
||||
tmp.AddTag(_tagGorm, "type:"+v.Type)
|
||||
if !v.IsNull {
|
||||
tmp.AddTag(_tagGorm, "not null")
|
||||
}
|
||||
}
|
||||
|
||||
// json tag
|
||||
if config.GetIsJSONTag() {
|
||||
if strings.EqualFold(v.Name, "id") {
|
||||
tmp.AddTag(_tagJSON, "-")
|
||||
} else if len(v.Name) > 0 {
|
||||
tmp.AddTag(_tagJSON, v.Name)
|
||||
}
|
||||
}
|
||||
el = append(el, tmp)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
11
go.sum
11
go.sum
@@ -43,6 +43,7 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
@@ -55,6 +56,7 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
|
||||
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
@@ -80,7 +82,9 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
|
||||
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||
@@ -109,6 +113,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
|
||||
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
||||
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||
@@ -138,6 +143,7 @@ golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73r
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
@@ -152,8 +158,10 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
|
||||
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
@@ -172,13 +180,16 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/eapache/queue.v1 v1.1.0/go.mod h1:wNtmx1/O7kZSR9zNT1TTOJ7GLpm3Vn7srzlfylFbQwU=
|
||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
||||
Reference in New Issue
Block a user