Merge branch 'master' into master

This commit is contained in:
wclssdn
2020-04-06 17:09:38 +08:00
committed by GitHub
23 changed files with 224 additions and 60 deletions

View File

@@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"
"github.com/xxjwxc/public/tools"
@@ -21,6 +22,7 @@ var foreignKey bool
var funcKey bool
var ui bool
var urlTag string
var outFileName string
var rootCmd = &cobra.Command{
Use: "main",
@@ -72,6 +74,8 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&urlTag, "url", "l", "", "url标签(json,url)")
rootCmd.MarkFlagRequired("url tag")
rootCmd.Flags().StringVar(&outFileName, "outfilename", "", "输出文件名,默认以数据库名称命名")
rootCmd.Flags().IntVar(&mysqlInfo.Port, "port", 3306, "端口号")
}
@@ -86,7 +90,7 @@ func initConfig() {
os.Exit(1)
} else {
fmt.Println("using config info:")
fmt.Println(tools.GetJSONStr(config.GetMysqlDbInfo()))
fmt.Println(tools.GetJSONStr(config.GetMysqlDbInfo(), true))
}
}
@@ -111,6 +115,12 @@ func MergeMysqlDbInfo() {
if len(urlTag) > 0 {
config.SetURLTag(urlTag)
}
if len(outFileName) > 0 {
if !strings.HasSuffix(outFileName, ".go") {
outFileName += ".go"
}
config.SetOutFileName(outFileName)
}
config.SetMysqlDbInfo(&tmp)

View File

@@ -21,6 +21,8 @@ type Config struct {
IsOutSQL bool `yaml:"is_out_sql"`
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
OutFileName string `yaml:"-"`
}
// MysqlDbInfo mysql database information. mysql 数据库信息
@@ -132,6 +134,24 @@ func SetIsGUI(b bool) {
_map.IsGUI = b
}
// GetIsTableName if is table name .
func GetIsTableName() bool {
return _map.IsTableName
}
// SetIsTableName if is table name .
func SetIsTableName(b bool) {
_map.IsTableName = b
}
func SetOutFileName(f string) {
_map.OutFileName = f
}
func GetOutFileName() string {
return _map.OutFileName
}
// GetURLTag get url tag.
func GetURLTag() string {
if _map.URLTag != "json" && _map.URLTag != "url" {

View File

@@ -3,6 +3,8 @@ package config
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/xxjwxc/public/dev"
"github.com/xxjwxc/public/tools"
@@ -18,33 +20,65 @@ type CfgBase struct {
IsDev bool `json:"is_dev" yaml:"is_dev"` // Is it a development version?是否是开发版本
}
var _map = Config{}
var _map = Config{
CfgBase: CfgBase{
IsDev: false,
},
MySQLInfo: MysqlDbInfo{
Host: "127.0.0.1",
Port: 3306,
Username: "root",
Password: "root",
Database: "test",
},
OutDir: "./model",
URLTag: "json",
Language: "中 文",
DbTag: "gorm",
Simple: false,
IsWEBTag: false,
SingularTable: true,
IsForeignKey: true,
IsOutSQL: false,
IsOutFunc: true,
IsGUI: false,
}
var configPath string
func init() {
configPath = path.Join(tools.GetModelPath(), "config.yml")
onInit()
dev.OnSetDev(_map.IsDev)
}
func onInit() {
path := tools.GetModelPath()
err := InitFile(path + "/config.yml")
err := InitFile(configPath)
if err != nil {
fmt.Println("InitFile: ", err.Error())
fmt.Println("Load config file error: ", err.Error())
return
}
}
// InitFile default value from file .
func InitFile(filename string) error {
if _, e := os.Stat(filename); e != nil {
fmt.Println("init default config file: ", filename)
if err := SaveToFile(); err == nil {
fmt.Println("done,please restart.")
} else {
fmt.Println("shit,fail", err)
}
os.Exit(0)
}
bs, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if err := yaml.Unmarshal(bs, &_map); err != nil {
fmt.Println("read toml error: ", err.Error())
fmt.Println("read config file error: ", err.Error())
return err
}
return nil
}
@@ -72,9 +106,8 @@ func SaveToFile() error {
if err != nil {
return err
}
tools.WriteFile(tools.GetModelPath()+"/config.yml", []string{
tools.WriteFile(configPath, []string{
string(d),
}, true)
return nil
}

View File

@@ -230,6 +230,8 @@ func enterSet(g *gocui.Gui, v *gocui.View) error {
AddOptions(SLocalize("true"), SLocalize("false")).SetSelected(SLocalize(tools.AsString(config.GetIsForeignKey())))
form.AddSelect("is_gui", SLocalize("is_gui"), formPart[0], formPart[2]).
AddOptions(SLocalize("true"), SLocalize("false")).SetSelected(SLocalize(tools.AsString(config.GetIsGUI())))
form.AddSelect("is_table_name", SLocalize("is_table_name"), formPart[0], formPart[2]).
AddOptions(SLocalize("true"), SLocalize("false")).SetSelected(SLocalize(tools.AsString(config.GetIsTableName())))
form.AddSelect("url_tag", SLocalize("url_tag"), formPart[0], formPart[2]).
AddOptions("json", "url").SetSelected(tools.AsString(config.GetURLTag()))
form.AddSelect("db_tag", SLocalize("db_tag"), formPart[0], formPart[2]).
@@ -294,6 +296,7 @@ func buttonSave(g *gocui.Gui, v *gocui.View) error {
config.SetIsOutFunc(getBool(mp["is_out_func"]))
config.SetForeignKey(getBool(mp["is_foreign_key"]))
config.SetIsGUI(getBool(mp["is_gui"]))
config.SetIsTableName(getBool(mp["is_table_name"]))
config.SetURLTag(mp["url_tag"])
config.SetDBTag(mp["db_tag"])
config.SetLG(mp["language"])

View File

@@ -89,6 +89,9 @@ func addChinese() error {
}, &i18n.Message{
ID: "is_gui",
Other: " 界 面 模 式 :",
}, &i18n.Message{
ID: "is_table_name",
Other: " 生 成 表 名 :",
}, &i18n.Message{
ID: "url_tag",
Other: " web 标 签:",
@@ -180,6 +183,9 @@ func addEnglish() error {
}, &i18n.Message{
ID: "is_gui",
Other: "is show gui:",
}, &i18n.Message{
ID: "is_table_name",
Other: "is table name:",
}, &i18n.Message{
ID: "url_tag",
Other: "url tag:",

View File

@@ -10,13 +10,19 @@ var EImportsHead = map[string]string{
// TypeMysqlDicMp Accurate matching type.精确匹配类型
var TypeMysqlDicMp = map[string]string{
"smallint": "int16",
"smallint unsigned": "uint16",
"int": "int",
"int unsigned": "uint",
"bigint": "int64",
"bigint unsigned": "uint64",
"varchar": "string",
"char": "string",
"date": "time.Time",
"datetime": "time.Time",
"bit(1)": "int8",
"tinyint": "int8",
"tinyint unsigned": "uint8",
"tinyint(1)": "int8",
"tinyint(1) unsigned": "int8",
"json": "string",
@@ -26,22 +32,26 @@ var TypeMysqlDicMp = map[string]string{
"mediumtext": "string",
"longtext": "string",
"float": "float32",
"tinytext": "stirng",
"tinytext": "string",
"enum": "string",
"time": "time.Time",
}
// TypeMysqlMatchMp Fuzzy Matching Types.模糊匹配类型
var TypeMysqlMatchMp = map[string]string{
`^(tinyint)[(]\d+[)]`: "int8",
`^(smallint)[(]\d+[)]`: "int8",
`^(int)[(]\d+[)]`: "int",
`^(bigint)[(]\d+[)]`: "int64",
`^(char)[(]\d+[)]`: "string",
`^(enum)[(](.)+[)]`: "string",
`^(varchar)[(]\d+[)]`: "string",
`^(varbinary)[(]\d+[)]`: "[]byte",
`^(decimal)[(]\d+,\d+[)]`: "float64",
`^(mediumint)[(]\d+[)]`: "string",
`^(double)[(]\d+,\d+[)]`: "float64",
`^(float)[(]\d+,\d+[)]`: "float64",
`^(tinyint)[(]\d+[)]`: "int8",
`^(tinyint)[(]\d+[)] unsigned`: "uint8",
`^(smallint)[(]\d+[)]`: "int16",
`^(int)[(]\d+[)]`: "int",
`^(bigint)[(]\d+[)]`: "int64",
`^(char)[(]\d+[)]`: "string",
`^(enum)[(](.)+[)]`: "string",
`^(varchar)[(]\d+[)]`: "string",
`^(varbinary)[(]\d+[)]`: "[]byte",
`^(binary)[(]\d+[)]`: "[]byte",
`^(decimal)[(]\d+,\d+[)]`: "float64",
`^(mediumint)[(]\d+[)]`: "string",
`^(double)[(]\d+,\d+[)]`: "float64",
`^(float)[(]\d+,\d+[)]`: "float64",
`^(datetime)[(]\d+[)]`: "time.Time",
}

View File

@@ -1,6 +1,12 @@
package genfunc
const (
genTnf = `
// TableName get sql table name.获取数据库表名
func (m *{{.StructName}}) TableName() string {
return "{{.TableName}}"
}
`
genBase = `
package {{.PackageName}}
import (

View File

@@ -1,5 +1,10 @@
package genfunc
// GetGenTableNameTemp get gen base template str
func GetGenTableNameTemp() string {
return genTnf
}
// GetGenBaseTemp get gen base template str
func GetGenBaseTemp() string {
return genBase

View File

@@ -1,13 +1,16 @@
package genstruct
import (
"bytes"
"fmt"
"sort"
"strings"
"text/template"
"github.com/xxjwxc/gormt/data/config"
"github.com/xxjwxc/gormt/data/view/cnf"
"github.com/xxjwxc/gormt/data/view/generate"
"github.com/xxjwxc/gormt/data/view/genfunc"
)
// SetName Setting element name.设置元素名字
@@ -96,6 +99,11 @@ func (s *GenStruct) SetCreatTableStr(sql string) {
s.SQLBuildStr = sql
}
// SetTableName Setting the name of struct.设置struct名字
func (s *GenStruct) SetTableName(name string) {
s.TableName = name
}
// SetStructName Setting the name of struct.设置struct名字
func (s *GenStruct) SetStructName(name string) {
s.Name = name
@@ -125,6 +133,21 @@ func (s *GenStruct) AddElement(e ...GenElement) {
s.Em = append(s.Em, e...)
}
func (s *GenStruct) GenerateTableName() []string {
tmpl, err := template.New("gen_tnf").Parse(genfunc.GetGenTableNameTemp())
if err != nil {
panic(err)
}
var data struct {
TableName string
StructName string
}
data.TableName, data.StructName = s.TableName, s.Name
var buf bytes.Buffer
tmpl.Execute(&buf, data)
return []string{buf.String()}
}
// Generates Get the result data.获取结果数据
func (s *GenStruct) Generates() []string {
var p generate.PrintAtom
@@ -212,6 +235,12 @@ func (p *GenPackage) Generate() string {
for _, v1 := range v.Generates() {
pa.Add(v1)
}
if config.GetIsTableName() { // add table name func
for _, v1 := range v.GenerateTableName() {
pa.Add(v1)
}
}
}
// -----------end

View File

@@ -11,6 +11,7 @@ type GenElement struct {
// GenStruct struct of IStruct .结构体
type GenStruct struct {
SQLBuildStr string // Create SQL statements.创建sql语句
TableName string // table_name.表名
Name string // name.名字
Notes string // notes.注释
Em []GenElement // em.元素组合

View File

@@ -60,9 +60,20 @@ func (m *mysqlModel) GetPkgName() string {
}
func getPackageInfo(orm *mysqldb.MySqlDB, info *model.DBInfo) {
tables := getTables(orm) // get table and notes
for tabName, notes := range tables {
tabls := getTables(orm) // get table and notes
// if m := config.GetTableList(); len(m) > 0 {
// // 制定了表之后
// newTabls := make(map[string]string)
// for t := range m {
// if notes, ok := tabls[t]; ok {
// newTabls[t] = notes
// } else {
// fmt.Printf("table: %s not found in db\n", t)
// }
// }
// tabls = newTabls
// }
for tabName, notes := range tabls {
var tab model.TabInfo
tab.Name = tabName
tab.Notes = notes

View File

@@ -30,6 +30,7 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
var stt GenOutInfo
stt.FileCtx = m.generate()
stt.FileName = info.DbName + ".go"
out = append(out, stt)
// ------end
@@ -48,6 +49,7 @@ func (m *_Model) GetPackage() genstruct.GenPackage {
pkg.SetPackage(m.info.PackageName) //package name
for _, tab := range m.info.TabList {
var sct genstruct.GenStruct
sct.SetTableName(tab.Name)
sct.SetStructName(getCamelName(tab.Name)) // Big hump.大驼峰
sct.SetNotes(tab.Notes)
sct.AddElement(m.genTableElement(tab.Em)...) // build element.构造元素

File diff suppressed because one or more lines are too long