前置更新

前置更新
master
xiexiaojun 6 years ago
parent 12b5a73d6e
commit fae35b9107

@ -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"
database = "oauth_db"

@ -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)
}

@ -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
}

@ -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)
}

@ -1 +0,0 @@
package gtools

@ -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)
}
}
}
}
}

@ -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
}

@ -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)
}

@ -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()

Loading…
Cancel
Save