-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.go
executable file
·299 lines (274 loc) · 7.22 KB
/
shell.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2018 Yuanting Liang. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.1
package main
import (
"fmt"
"strings"
"xorm.io/core"
"github.com/liangyt123/gorm/xorm"
)
var CmdShell = &Command{
UsageLine: "shell driverName datasourceName",
Short: "a general shell to operate all kinds of database",
Long: `
general database's shell for sqlite3, mysql, postgres.
driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres
datasourceName Database connection uri, for detail infomation please visit driver's project page
`,
}
func init() {
CmdShell.Run = runShell
CmdShell.Flags = map[string]bool{}
}
var engine *xorm.Engine
func shellHelp() {
fmt.Println(`
the exmaple : gorm reverse mysql user:'password'@'(192.168.126.72:3306)/ztkposbackend?charset=utf8' 'templates\\goxorm'
show tables show all tables
columns <table_name> show table's column info
indexes <table_name> show table's index info
exit exit shell
source <sql_file> exec sql file to current database
dump [-nodata] <sql_file> dump structs or records to sql file
help show this document
<statement> SQL statement
`)
}
func runShell(cmd *Command, args []string) {
if len(args) != 2 {
fmt.Println("params error, please see gorm help shell")
return
}
var err error
engine, err = xorm.NewEngine(args[0], args[1])
if err != nil {
fmt.Println(err)
return
}
engine.ShowSQL(false)
engine.Logger().SetLevel(core.LOG_UNKNOWN)
err = engine.Ping()
if err != nil {
fmt.Println(err)
return
}
var scmd string
fmt.Print("gorm$ ")
for {
var input string
_, err := fmt.Scan(&input)
if err != nil {
fmt.Println(err)
continue
}
if strings.ToLower(input) == "help" {
shellHelp()
scmd = ""
fmt.Print("gorm$ ")
continue
}
if strings.ToLower(input) == "exit" {
fmt.Println("bye")
return
}
if !strings.HasSuffix(input, ";") {
scmd = scmd + " " + input
continue
}
scmd = scmd + " " + input
lcmd := strings.TrimSpace(strings.ToLower(scmd))
lcmd = strings.TrimRight(lcmd, ";")
if strings.HasPrefix(lcmd, "select") {
res, err := engine.Query(scmd + "\n")
if err != nil {
fmt.Println(err)
} else {
if len(res) <= 0 {
fmt.Println("no records")
} else {
columns := make(map[string]int)
for k, _ := range res[0] {
columns[k] = len(k)
}
for _, m := range res {
for k, s := range m {
l := len(string(s))
if l > columns[k] {
columns[k] = l
}
}
}
var maxlen = 0
for _, l := range columns {
maxlen = maxlen + l + 3
}
maxlen = maxlen + 1
fmt.Println(strings.Repeat("-", maxlen))
fmt.Print("|")
slice := make([]string, 0)
for k, l := range columns {
fmt.Print(" " + k + " ")
fmt.Print(strings.Repeat(" ", l-len(k)))
fmt.Print("|")
slice = append(slice, k)
}
fmt.Print("\n")
for _, r := range res {
fmt.Print("|")
for _, k := range slice {
fmt.Print(" " + string(r[k]) + " ")
fmt.Print(strings.Repeat(" ", columns[k]-len(string(r[k]))))
fmt.Print("|")
}
fmt.Print("\n")
}
fmt.Println(strings.Repeat("-", maxlen))
//fmt.Println(res)
}
}
} else if lcmd == "show tables" {
tables, err := engine.Dialect().GetTables()
if err != nil {
fmt.Println(err)
} else {
var maxlen int
for _, table := range tables {
if len(table.Name) > maxlen {
maxlen = len(table.Name)
}
}
head := "Table Name"
if maxlen < len(head) {
maxlen = len(head)
}
maxlen = maxlen + 2
fmt.Println(strings.Repeat("-", maxlen+3))
fmt.Print("| " + head + strings.Repeat(" ", maxlen-len(head)))
fmt.Println("|")
fmt.Println(strings.Repeat("-", maxlen+3))
for _, table := range tables {
fmt.Print("|")
fmt.Print(" " + table.Name)
fmt.Print(strings.Repeat(" ", maxlen-len(table.Name)))
fmt.Println("|")
}
fmt.Println(strings.Repeat("-", maxlen+3))
}
} else if strings.HasPrefix(lcmd, "dump") {
fields := strings.Fields(strings.TrimRight(scmd, ";"))
if len(fields) == 2 {
err = engine.DumpAllToFile(fields[1])
if err != nil {
fmt.Println(err)
} else {
fmt.Println("dump successfully!")
}
} else {
fmt.Println("param error")
}
} else if strings.HasPrefix(lcmd, "source") {
fields := strings.Fields(strings.TrimRight(scmd, ";"))
if len(fields) == 2 {
_, err = engine.ImportFile(fields[1])
if err.Error() == "not an error" {
err = nil
}
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println("param error")
}
} else if strings.HasPrefix(lcmd, "columns") {
fields := strings.Fields(strings.TrimRight(scmd, ";"))
if len(fields) == 2 {
_, columns, err := engine.Dialect().GetColumns(fields[1])
if err != nil {
fmt.Println(err)
} else {
if len(columns) == 0 {
fmt.Println("no column in", fields[1])
} else {
var maxlen int
for name, _ := range columns {
if len(name) > maxlen {
maxlen = len(name)
}
}
head := "Column Name"
if maxlen < len(head) {
maxlen = len(head)
}
maxlen = maxlen + 2
fmt.Println(strings.Repeat("-", maxlen+3))
fmt.Print("| " + head + strings.Repeat(" ", maxlen-len(head)))
fmt.Println("|")
fmt.Println(strings.Repeat("-", maxlen+3))
for name, _ := range columns {
fmt.Print("|")
fmt.Print(" " + name)
fmt.Print(strings.Repeat(" ", maxlen-len(name)))
fmt.Println("|")
}
fmt.Println(strings.Repeat("-", maxlen+3))
}
}
} else {
fmt.Println("param error")
}
} else if strings.HasPrefix(lcmd, "indexes") {
fields := strings.Fields(strings.TrimRight(scmd, ";"))
if len(fields) == 2 {
indexes, err := engine.Dialect().GetIndexes(fields[1])
if err != nil {
fmt.Println(err)
} else {
if len(indexes) == 0 {
fmt.Println("no index in", fields[1])
} else {
var maxlen int
for name, _ := range indexes {
if len(name) > maxlen {
maxlen = len(name)
}
}
head := "Index Name"
if maxlen < len(head) {
maxlen = len(head)
}
maxlen = maxlen + 2
fmt.Println(strings.Repeat("-", maxlen+3))
fmt.Print("| " + head + strings.Repeat(" ", maxlen-len(head)))
fmt.Println("|")
fmt.Println(strings.Repeat("-", maxlen+3))
for name, _ := range indexes {
fmt.Print("|")
fmt.Print(" " + name)
fmt.Print(strings.Repeat(" ", maxlen-len(name)))
fmt.Println("|")
}
fmt.Println(strings.Repeat("-", maxlen+3))
}
}
} else {
fmt.Println("param error")
}
} else {
res, err := engine.Exec(scmd)
if err != nil {
fmt.Println(err)
} else {
cnt, err := res.RowsAffected()
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%d records changed.\n", cnt)
}
}
}
scmd = ""
fmt.Print("gorm$ ")
}
}