-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
390 lines (325 loc) · 8.49 KB
/
dump.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package dbunit
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"time"
"unicode/utf8"
"gopkg.in/yaml.v2"
"github.com/goapt/dbunit/fixtures"
)
func parseTableName(query string) string {
query = strings.ToLower(query)
// 先找到第一个from位置
fromIdex := strings.Index(query, "from")
if fromIdex == -1 {
return ""
}
query = query[fromIdex+4:]
// 查看有没有as
asIndex := strings.Index(query, " as")
if asIndex != -1 {
return strings.Trim(strings.TrimSpace(query[:asIndex]), "`")
}
// 查看有没有where
whereIndex := strings.Index(query, " where")
if whereIndex != -1 {
query = query[:whereIndex]
}
s := strings.Split(strings.TrimSpace(query), " ")
return strings.Trim(strings.TrimSpace(s[0]), "`")
}
func getPrimaryKey(db *sql.DB, query string) (string, error) {
tableName := parseTableName(query)
if tableName == "" {
return "", errors.New("sql parse table name is empty")
}
row := db.QueryRow("select database();")
var dbname string
err := row.Scan(&dbname)
if err != nil {
return "", fmt.Errorf("get database name error %w", err)
}
row = db.QueryRow("select column_name from information_schema.key_column_usage where constraint_name = 'PRIMARY' and table_schema = ? and table_name = ?", dbname, tableName)
var pk string
err = row.Scan(&pk)
if err != nil {
return "", err
}
return pk, nil
}
func Dump(db *sql.DB, filePath, query string, args ...interface{}) ([]map[string]interface{}, error) {
pk, err := getPrimaryKey(db, query)
if err != nil {
return nil, fmt.Errorf("get primary key error %w", err)
}
query, newArgs, err := inReplace(query, args...)
if err != nil {
return nil, err
}
stmt, err := db.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(newArgs...)
if err != nil {
return nil, err
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, err
}
var oldData = make([]map[string]interface{}, 0)
if isExists(filePath) {
d, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
tpl := fixtures.NewTemplate()
d, err = tpl.Parse(d)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(d, &oldData)
if err != nil {
return nil, err
}
}
fixturesSlice := make([]yaml.MapSlice, 0, 10)
fixtureMaps := make([]map[string]interface{}, 0)
for rows.Next() {
entries := make([]interface{}, len(columns))
entryPtrs := make([]interface{}, len(entries))
for i := range entries {
entryPtrs[i] = &entries[i]
}
if err := rows.Scan(entryPtrs...); err != nil {
return nil, err
}
entryMap := make([]yaml.MapItem, len(entries))
entryMap2 := make(map[string]interface{})
for i, column := range columns {
v := convertValue(entries[i])
entryMap[i] = yaml.MapItem{
Key: column,
Value: v,
}
entryMap2[column] = v
}
if !isDuplicate(oldData, entryMap2, pk) {
fixturesSlice = append(fixturesSlice, entryMap)
} else {
fmt.Println(fmt.Sprintf("[duplicate] %s ignore primary key:%v", filePath, entryMap2[pk]))
}
fixtureMaps = append(fixtureMaps, entryMap2)
}
if err = rows.Err(); err != nil {
return nil, err
}
if len(fixturesSlice) == 0 && len(oldData) != 0 {
return fixtureMaps, nil
}
err = writeYml(filePath, fixturesSlice, len(oldData))
return fixtureMaps, err
}
func writeYml(filePath string, fixtures []yaml.MapSlice, oldlen int) error {
var f *os.File
var err error
if isExists(filePath) && oldlen != 0 {
f, err = os.OpenFile(filePath, os.O_RDWR|os.O_APPEND, 0666)
} else {
f, err = os.Create(filePath)
}
if err != nil {
return err
}
defer f.Close()
data, err := yaml.Marshal(fixtures)
if err != nil {
return err
}
_, err = f.Write([]byte("\n#Created At:" + time.Now().Format("2006-01-02 13:04:05") + "\n"))
_, err = f.Write(data)
return err
}
func convertValue(value interface{}) interface{} {
switch v := value.(type) {
case []byte:
if utf8.Valid(v) {
return string(v)
}
}
return value
}
func isDuplicate(x []map[string]interface{}, y map[string]interface{}, pk string) bool {
for _, v := range x {
if pk != "" {
v1 := fmt.Sprintf("%v", v[pk])
v2 := fmt.Sprintf("%v", y[pk])
if v1 == v2 {
return true
}
} else {
v1 := fmt.Sprintf("%v", filterDate(v))
v2 := fmt.Sprintf("%v", filterDate(y))
if v1 == v2 {
return true
}
}
}
return false
}
func filterDate(m map[string]interface{}) map[string]interface{} {
newMap := make(map[string]interface{})
for k, v := range m {
if !tryParseDate(fmt.Sprintf("%s", v)) {
newMap[k] = v
}
}
return newMap
}
var timeFormats = [...]string{
"2006-01-02T15:04:05Z07:00",
"2006-01-02 15:04:05 Z0700 CST",
"2006-01-02 15:04:05Z0700",
"2006-01-02 15:04:05 MST",
}
func tryParseDate(s string) bool {
for _, f := range timeFormats {
_, err := time.Parse(f, s)
if err == nil {
return true
}
}
return false
}
func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
if i == nil {
return reflect.Value{}, false
}
v = reflect.ValueOf(i)
t := v.Type()
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
// Only expand slices
if t.Kind() != reflect.Slice {
return reflect.Value{}, false
}
// []byte is a driver.Value type so it should not be expanded
if t == reflect.TypeOf([]byte{}) {
return reflect.Value{}, false
}
return v, true
}
// inReplace expands slice values in args, returning the modified query string
// and a new arg list that can be executed by a database. The `query` should
// use the `?` bindVar. The return value uses the `?` bindVar.
func inReplace(query string, args ...interface{}) (string, []interface{}, error) {
// argMeta stores reflect.Value and length for slices and
// the value itself for non-slice arguments
type argMeta struct {
v reflect.Value
i interface{}
length int
}
var flatArgsCount int
var anySlices bool
var stackMeta [32]argMeta
var meta []argMeta
if len(args) <= len(stackMeta) {
meta = stackMeta[:len(args)]
} else {
meta = make([]argMeta, len(args))
}
for i, arg := range args {
if a, ok := arg.(driver.Valuer); ok {
var err error
arg, err = a.Value()
if err != nil {
return "", nil, err
}
}
if v, ok := asSliceForIn(arg); ok {
meta[i].length = v.Len()
meta[i].v = v
anySlices = true
flatArgsCount += meta[i].length
if meta[i].length == 0 {
return "", nil, errors.New("empty slice passed to 'in' query")
}
} else {
meta[i].i = arg
flatArgsCount++
}
}
// don't do any parsing if there aren't any slices; note that this means
// some errors that we might have caught below will not be returned.
if !anySlices {
return query, args, nil
}
newArgs := make([]interface{}, 0, flatArgsCount)
var buf strings.Builder
buf.Grow(len(query) + len(", ?")*flatArgsCount)
var arg, offset int
for i := strings.IndexByte(query[offset:], '?'); i != -1; i = strings.IndexByte(query[offset:], '?') {
if arg >= len(meta) {
// if an argument wasn't passed, lets return an error; this is
// not actually how database/sql Exec/Query works, but since we are
// creating an argument list programmatically, we want to be able
// to catch these programmer errors earlier.
return "", nil, errors.New("number of bindVars exceeds arguments")
}
argMeta := meta[arg]
arg++
// not a slice, continue.
// our questionmark will either be written before the next expansion
// of a slice or after the loop when writing the rest of the query
if argMeta.length == 0 {
offset = offset + i + 1
newArgs = append(newArgs, argMeta.i)
continue
}
// write everything up to and including our ? character
buf.WriteString(query[:offset+i+1])
for si := 1; si < argMeta.length; si++ {
buf.WriteString(", ?")
}
newArgs = appendReflectSlice(newArgs, argMeta.v, argMeta.length)
// slice the query and reset the offset. this avoids some bookkeeping for
// the write after the loop
query = query[offset+i+1:]
offset = 0
}
buf.WriteString(query)
if arg < len(meta) {
return "", nil, errors.New("number of bindVars less than number arguments")
}
return buf.String(), newArgs, nil
}
func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interface{} {
switch val := v.Interface().(type) {
case []interface{}:
args = append(args, val...)
case []int:
for i := range val {
args = append(args, val[i])
}
case []string:
for i := range val {
args = append(args, val[i])
}
default:
for si := 0; si < vlen; si++ {
args = append(args, v.Index(si).Interface())
}
}
return args
}