-
Notifications
You must be signed in to change notification settings - Fork 0
/
csql.go
executable file
·171 lines (162 loc) · 3.9 KB
/
csql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/urfave/cli"
"jvole.com/createProject/lexer"
"jvole.com/createProject/util"
)
var (
sqlFileName = "auto_gen.sql"
)
func CsqlFile(c *cli.Context) error {
if c.String("cover") == "true" {
COVRE = true
} else {
COVRE = false
}
db := c.String("database")
file := c.String("file")
//处理file如果是目录的情况。确保目录后有『/』
hasf, _ := regexp.MatchString(`\/$`, file)
if !hasf {
file = file + "/"
}
if db != "mysql" && db != "cockroach" && db != "mariadb" && db != "postgresql" {
fmt.Println("目前只支持mysql,mariadb,postgresql,cockroach")
return ERRNOSQL
}
if file == "" {
fmt.Println("您未指定struct文件")
return ERRNOFILE
}
if db == "mysql" || db == "mariadb" {
return genMysqlByStruct(file)
}
if db == "cockroach" || db == "postgresql" {
return genCockroachSqlByStruct(file)
}
return nil
}
//生成mysql的脚本
func genMysqlByStruct(file string) error {
exist, err := ut.FileOrPathExists(sqlFileName)
str := ""
if !COVRE && exist {
fmt.Println(util.Red(sqlFileName + "文件已存在"))
return err
}
f, _ := os.Stat(file)
if f.IsDir() { //如果是目录
str = getAllMysqlString(file)
} else { //如果是单文件
mysqlLexer := new(lexer.MysqlLexer)
sl := new(lexer.StructLexer)
fileStr := sl.GetStructFile(file)
slist := sl.StructStr(fileStr)
for _, v := range slist {
str = str + "\n" + mysqlLexer.CreateSqlByStructStr(v)
}
}
sf, err := os.Create(sqlFileName)
if err != nil {
fmt.Println(err)
return err
}
_, err = sf.Write([]byte(str))
if err != nil {
fmt.Println(err)
return err
}
if !(!COVRE && exist) && err == nil {
fmt.Println(sqlFileName + "生成成功")
}
return nil
}
//生成cockroach脚本
func genCockroachSqlByStruct(file string) error {
exist, err := ut.FileOrPathExists(sqlFileName)
str := ""
if !COVRE && exist {
fmt.Println(util.Red(sqlFileName + "文件已存在"))
return err
}
f, _ := os.Stat(file)
if f.IsDir() { //如果是目录
str = getAllCockDBString(file)
} else { //如果是单文件
cockDBLexer := new(lexer.CockDBLexer)
sl := new(lexer.StructLexer)
fileStr := sl.GetStructFile(file)
slist := sl.StructStr(fileStr)
for _, v := range slist {
str = str + "\n" + cockDBLexer.CreateSqlByStructStr(v)
}
}
sf, err := os.Create(sqlFileName)
if err != nil {
fmt.Println(err)
return err
}
_, err = sf.Write([]byte(str))
if err != nil {
fmt.Println(err)
return err
}
if !(!COVRE && exist) && err == nil {
fmt.Println(sqlFileName + "生成成功")
}
return nil
}
//cockroach用遍历目录拼接所有文件并去除生成sql文件string
func getAllCockDBString(fp string) (allStr string) {
err := filepath.Walk(fp, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if !f.IsDir() {
cockDBLexer := new(lexer.CockDBLexer)
sl := new(lexer.StructLexer)
fileStr := sl.GetStructFile(fp + f.Name())
slist := sl.StructStr(fileStr)
str := ""
for _, v := range slist {
str = str + "\n" + cockDBLexer.CreateSqlByStructStr(v)
}
allStr = allStr + "\n" + str
// fmt.Println(str)
}
return nil
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
return allStr
}
//mysqlsql用遍历目录拼接所有文件并去除生成sql文件string
func getAllMysqlString(fp string) (allStr string) {
err := filepath.Walk(fp, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if !f.IsDir() {
mysqlLexer := new(lexer.MysqlLexer)
sl := new(lexer.StructLexer)
fileStr := sl.GetStructFile(fp + f.Name())
slist := sl.StructStr(fileStr)
str := ""
for _, v := range slist {
str = str + "\n" + mysqlLexer.CreateSqlByStructStr(v)
}
allStr = allStr + "\n" + str
// fmt.Println(str)
}
return nil
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
return allStr
}