Skip to content

Commit 6d0d32b

Browse files
author
jaysun
committed
optimize db2struct
1 parent b78a04b commit 6d0d32b

File tree

10 files changed

+277
-510
lines changed

10 files changed

+277
-510
lines changed

assets/stub/stub.go

Lines changed: 92 additions & 341 deletions
Large diffs are not rendered by default.

config/config.go

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package config
22

3+
import (
4+
"database/sql"
5+
"flag"
6+
"log"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
// App global config
12+
var GlobalConfig *Config
13+
314
type Config struct {
4-
Database *DbSet
5-
// The directory go-sword store new file
6-
RootPath string
7-
// Project go mod module name
8-
ModuleName string
9-
// Go-sword server port
10-
ServerPort string
15+
DatabaseSet DbSet // MySQL config
16+
RootPath string // The directory go-sword store new file
17+
ModuleName string // Project go mod module name
18+
ServerPort string // Go-sword server port
19+
DbConn *sql.DB
1120
}
1221

1322
type DbSet struct {
@@ -17,3 +26,43 @@ type DbSet struct {
1726
Port int
1827
Database string
1928
}
29+
30+
func (c Config) InitConfig() {
31+
32+
var dbHost = flag.String("host", "localhost", "MySQL Host")
33+
var dbUser = flag.String("user", "", "MySQL user")
34+
var dbPassword = flag.String("password", "", "MySQL password")
35+
var dbDatabase = flag.String("db", "", "MySQL database ")
36+
var dbPort = flag.Int("port", 3306, "MySQL port")
37+
var serverPort = flag.String("p", "8080", "Go-sword Server port")
38+
var rootPath = flag.String("module", "go-sword-app/", "New project module, the same as 'module' in go.mod file. ")
39+
40+
flag.Parse()
41+
42+
GlobalConfig = &Config{
43+
ServerPort: *serverPort,
44+
DatabaseSet: DbSet{
45+
Host: *dbHost,
46+
User: *dbUser,
47+
Password: *dbPassword,
48+
Port: *dbPort,
49+
Database: *dbDatabase,
50+
},
51+
ModuleName: "github.com/sunshinev/go-sword",
52+
RootPath: strings.TrimRight(*rootPath, "/"),
53+
}
54+
55+
// Init MySQL connection
56+
c.initDbConnect()
57+
}
58+
59+
func (c Config) initDbConnect() {
60+
dbc := GlobalConfig.DatabaseSet
61+
// user:password@(localhost)/dbname?charset=utf8&parseTime=True&loc=Local
62+
db, err := sql.Open("mysql", dbc.User+":"+dbc.Password+"@tcp("+dbc.Host+":"+strconv.Itoa(dbc.Port)+")/"+dbc.Database+"?&parseTime=True")
63+
if err != nil {
64+
log.Fatalf("%v", err)
65+
}
66+
67+
GlobalConfig.DbConn = db
68+
}

core/generator.go

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ import (
88
"strconv"
99
"strings"
1010

11-
"github.com/sunshinev/go-sword/core/utils"
12-
11+
_ "github.com/go-sql-driver/mysql"
1312
"github.com/sunshinev/go-sword/assets/resource"
14-
1513
"github.com/sunshinev/go-sword/assets/stub"
16-
1714
"github.com/sunshinev/go-sword/config"
18-
19-
_ "github.com/go-sql-driver/mysql"
15+
"github.com/sunshinev/go-sword/core/utils"
2016
)
2117

2218
type Generator struct {
@@ -42,61 +38,51 @@ type FileInstance struct {
4238
IsNew bool `json:"is_new"`
4339
}
4440

45-
func (g *Generator) Init(c *config.Config) *Generator {
46-
g.config = c
47-
g.ColumnDataTypes = make(map[string]string)
48-
return g
41+
func (g Generator) Init() *Generator {
42+
return &Generator{
43+
config: config.GlobalConfig,
44+
ColumnDataTypes: map[string]string{},
45+
}
4946
}
5047

5148
// Entry
52-
func (g *Generator) parseTable(s *Sword, table string) {
49+
func (g *Generator) parseTable(table string) {
5350

54-
columnDataTypes, err := utils.Db2struct{}.Convert(s, table)
51+
columnDataTypes, err := utils.Db2struct{}.Convert(table)
5552
if err != nil {
5653
panic(err.Error())
5754
}
5855

59-
structName := strings.Replace(strings.Title(strings.Replace("hi_ni_hao", "_", " ", -1)), " ", "", -1)
60-
6156
// Set columns
6257
for _, r := range *columnDataTypes {
6358
g.Columns = append(g.Columns, r.ColumnName)
6459
g.ColumnDataTypes[r.ColumnName] = r.DataType
6560
}
66-
6761
g.Columns = utils.ResortMySQLFields(&g.Columns)
68-
69-
struc := utils.Db2struct{}.FetchWholeStructFile(table, structName, table, columnDataTypes)
70-
7162
// Set TableName
7263
g.TableName = table
7364
// Set PackageName
7465
g.PackageName = table
7566
// Set StructName
67+
structName := strings.Replace(strings.Title(strings.Replace(table, "_", " ", -1)), " ", "", -1)
7668
g.StructName = structName
7769
// Set Content
78-
g.Struc = struc
79-
}
80-
81-
func (g *Generator) Preview(s *Sword, table string) {
82-
g.parseTable(s, table)
83-
// Main.go
84-
g.gMainFile()
85-
// Core.go
86-
g.gCoreFile()
87-
// Route.go
88-
g.gRouteFile()
89-
// Model
90-
g.gModelFile()
91-
// Controller
92-
g.gControllerFile()
93-
// Response
94-
g.gResponseFile()
95-
// Html
96-
// default.html
97-
g.gHtmlDefaultFile()
98-
// list.html
99-
g.gHtmlListFile()
70+
g.Struc = utils.Db2struct{}.FetchWholeStructFile("model", structName, table, columnDataTypes)
71+
}
72+
73+
func (g *Generator) Preview(table string) {
74+
g.parseTable(table)
75+
76+
g.gGoModFile() // go.mod
77+
g.gMainFile() // Main.go
78+
g.gCoreFile() // Core.go
79+
g.gRouteFile() // Route.go
80+
g.gModelFile() // Model
81+
g.gControllerFile() // Controller
82+
g.gResponseFile() // Response
83+
84+
g.gHtmlDefaultFile() // default.html
85+
g.gHtmlListFile() // list.htm
10086
g.gHtmlCreateFile()
10187
g.gHtmlDetailFile()
10288
g.gHtmlEditFile()
@@ -108,7 +94,7 @@ func (g *Generator) Preview(s *Sword, table string) {
10894
}
10995

11096
func (g *Generator) Generate(s *Sword, table string, files []string) {
111-
g.Preview(s, table)
97+
g.Preview(table)
11298

11399
for _, file := range g.FileList {
114100
var path = file.FilePath
@@ -432,8 +418,17 @@ func (g *Generator) createRouteContent(path string) string {
432418
return content
433419
}
434420

435-
func (g *Generator) gMainFile() {
421+
func (g *Generator) gGoModFile() {
422+
var file = &FileInstance{
423+
FilePath: filepath.Join(g.config.RootPath, "go.mod"),
424+
FileName: "go.mod",
425+
FileContent: g.createGoModContent(),
426+
}
436427

428+
g.FileList = append(g.FileList, file)
429+
}
430+
431+
func (g *Generator) gMainFile() {
437432
var file = &FileInstance{
438433
FilePath: filepath.Join(g.config.RootPath, "main.go"),
439434
FileName: "main.go",
@@ -443,6 +438,17 @@ func (g *Generator) gMainFile() {
443438
g.FileList = append(g.FileList, file)
444439
}
445440

441+
func (g *Generator) createGoModContent() string {
442+
// Read stub
443+
data, err := stub.Asset("stub/go.mod.stub")
444+
if err != nil {
445+
panic(err.Error())
446+
}
447+
content := string(data)
448+
content = strings.ReplaceAll(content, "<<module_name>>", g.config.RootPath)
449+
return content
450+
}
451+
446452
func (g *Generator) createMainContent() string {
447453
// Read stub
448454
data, err := stub.Asset("stub/main.stub")
@@ -454,11 +460,11 @@ func (g *Generator) createMainContent() string {
454460

455461
content := string(data)
456462

457-
content = strings.ReplaceAll(content, "<<db_host>>", g.config.Database.Host)
458-
content = strings.ReplaceAll(content, "<<db_user>>", g.config.Database.User)
459-
content = strings.ReplaceAll(content, "<<db_password>>", g.config.Database.Password)
460-
content = strings.ReplaceAll(content, "<<db_port>>", strconv.Itoa(g.config.Database.Port))
461-
content = strings.ReplaceAll(content, "<<db_database>>", g.config.Database.Database)
463+
content = strings.ReplaceAll(content, "<<db_host>>", g.config.DatabaseSet.Host)
464+
content = strings.ReplaceAll(content, "<<db_user>>", g.config.DatabaseSet.User)
465+
content = strings.ReplaceAll(content, "<<db_password>>", g.config.DatabaseSet.Password)
466+
content = strings.ReplaceAll(content, "<<db_port>>", strconv.Itoa(g.config.DatabaseSet.Port))
467+
content = strings.ReplaceAll(content, "<<db_database>>", g.config.DatabaseSet.Database)
462468
content = strings.ReplaceAll(content, "<<import_core>>", str)
463469
return content
464470
}

core/protocal.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package core
2+
3+
type Ret struct {
4+
Code int `json:"code"`
5+
Msg string `json:"msg"`
6+
Data interface{} `json:"data"`
7+
}
8+
9+
type List struct {
10+
List interface{} `json:"list"`
11+
}

0 commit comments

Comments
 (0)