-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
220 lines (198 loc) · 4.73 KB
/
cmd.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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"github.com/glebarez/sqlite"
"github.com/mkideal/cli"
"gorm.io/gorm"
)
type Params struct {
cli.Helper
Separator string `cli:"s,sep" usage:"column separator. default is \\t" dft:"\t"`
DB string `cli:"d,database" usage:"The path of database file. The file will be created when it does not exist. \n\t\t\t\tTo retain the db file, please provide a path, otherwise, the db in memory will be used." dft:":memory:"`
TableDefine string `cli:"t,table" usage:"Table define format '[tableName]:[colName1] coltype1,[colName2] coltype2 ...'. \n\t\t\t\tDefault table name is _t. Default col name is _num(num starts from 1). \n\t\t\t\tDefault col type is text."`
SQL string `cli:"*e,exec" usage:"SQL to be executed."`
}
type ColDefine struct {
ColName string
ColType string
}
type TableDefine struct {
TableName string
Cols []ColDefine
}
func cmd() {
os.Exit(cli.Run(new(Params), func(ctx *cli.Context) error {
params := ctx.Argv().(*Params)
// fmt.Println(*params)
//open db
db, err := gorm.Open(sqlite.Open(params.DB), &gorm.Config{})
if err != nil {
fmt.Fprintln(os.Stderr, err)
d, err2 := db.DB()
if err2 != nil {
fmt.Fprintln(os.Stderr, err2)
}
err3 := d.Close()
if err3 != nil {
fmt.Fprintln(os.Stderr, err3)
}
return err
}
openedDB = db
defer func() {
if d, err := openedDB.DB(); err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
if err2 := d.Close(); err2 != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}()
// parse table define
td := parseTableDefine(params.TableDefine)
if td.TableName == "" {
td.TableName = "_t"
}
if td.Cols != nil {
createTable(td)
}
//
r := csv.NewReader(os.Stdin)
sp := []rune(params.Separator)[0]
r.Comma = sp
var rowNum = 0
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
rowNum++
if rowNum == 1 && td.Cols == nil {
var cols []ColDefine
for i, _ := range record {
cols = append(cols, ColDefine{"_" + strconv.Itoa(i+1), "text"})
}
td.Cols = cols
createTable(td)
}
var insertSQL = "insert into `" + td.TableName + "` values ("
for i, colDefine := range td.Cols {
if i != 0 {
insertSQL += ", "
}
if colDefine.ColType == "text" {
insertSQL = insertSQL + "'" + record[i] + "'"
} else {
insertSQL = insertSQL + record[i]
}
}
insertSQL += ")"
_, err2 := runRetNum(insertSQL, "insert")
if err2 != nil {
log.Fatalln(err2)
}
}
// query
h, err2 := runSelect(params.SQL)
if err2 != nil {
log.Fatalln(err2)
}
cols := h["cols"].([]string)
rows := h["rows"].([][]any)
for i, n := range cols {
if i != 0 {
fmt.Print(string(sp))
}
fmt.Print(n)
}
fmt.Print("\n")
for _, row := range rows {
for i, col := range row {
if i != 0 {
fmt.Print(string(sp))
}
fmt.Print(*col.(*any))
}
fmt.Print("\n")
}
return nil
}))
}
func parseTableDefine(t string) TableDefine {
var ret TableDefine
if strings.Trim(t, " ") == "" {
return ret
}
if strings.IndexByte(t, ':') >= 0 {
items := strings.Split(t, ":")
if len(items) == 2 {
if strings.HasPrefix(t, ":") {
ret.Cols = parseCols(items[1])
} else if strings.HasSuffix(t, ":") {
ret.TableName = items[0]
} else {
ret.TableName = items[0]
ret.Cols = parseCols(items[1])
}
} else {
log.Fatalln("table define err A")
}
} else {
if strings.IndexByte(t, ' ') < 0 {
ret.TableName = t
} else {
ret.Cols = parseCols(t)
}
}
return ret
}
func parseCols(t string) []ColDefine {
var ret []ColDefine
if strings.Trim(t, " ") == "" {
return nil
} else {
items := strings.Split(t, ",")
for i, item := range items {
if strings.Trim(item, " ") == "" {
log.Fatalln("table define err B")
}
if strings.IndexByte(item, ' ') >= 0 {
xx := strings.Split(item, " ")
if len(xx) == 2 {
if strings.HasPrefix(item, " ") {
ret = append(ret, ColDefine{"_" + strconv.Itoa(i+1), xx[1]})
} else if strings.HasSuffix(item, " ") {
ret = append(ret, ColDefine{xx[0], "text"})
} else {
ret = append(ret, ColDefine{xx[0], xx[1]})
}
} else {
log.Fatalln("table define err C")
}
} else {
log.Fatalln("table define err D")
}
}
}
return ret
}
func createTable(td TableDefine) {
var colDefine []string
for _, nameType := range td.Cols {
colDefine = append(colDefine, " `"+nameType.ColName+"` "+nameType.ColType+" ")
}
var createTableSQL = "create table `" + td.TableName + "` ( " + strings.Join(colDefine, ", ") + " )"
_, err3 := runRetNum(createTableSQL, "createTable")
if err3 != nil {
log.Fatalln(err3)
}
}