-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdata_gen.go
119 lines (106 loc) · 3.04 KB
/
data_gen.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
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlsmith
import (
"errors"
"fmt"
"strings"
"github.com/chaos-mesh/go-sqlsmith/types"
"github.com/chaos-mesh/go-sqlsmith/util"
)
// BatchData generate testing data by schema in given batch
// return SQLs with insert statement
func (s *SQLSmith) BatchData(total, batchSize int) ([]string, error) {
if s.currDB == "" {
return []string{}, errors.New("no selected database")
}
database, ok := s.Databases[s.currDB]
if !ok {
return []string{}, errors.New("selected database's schema not loaded")
}
var sqls []string
for _, table := range database.Tables {
var columns []*types.Column
for _, column := range table.Columns {
if column.Column == "id" {
continue
}
columns = append(columns, column)
}
var lines [][]string
count := 0
for i := 0; i < total; i++ {
var line []string
for _, column := range columns {
line = append(line, util.GenerateDataItemString(column.DataType))
}
lines = append(lines, line)
count++
if count >= batchSize {
count = 0
sqls = append(sqls, makeSQL(table, columns, lines))
lines = [][]string{}
}
}
if len(lines) != 0 {
sqls = append(sqls, makeSQL(table, columns, lines))
}
}
return sqls, nil
}
func makeSQL(table *types.Table, columns []*types.Column, lines [][]string) string {
var columnNames []string
for _, column := range columns {
columnNames = append(columnNames, fmt.Sprintf("`%s`", column.Column))
}
return fmt.Sprintf("INSERT INTO `%s` (%s) VALUES \n %s",
table.Table,
strings.Join(columnNames, ", "),
strings.Join(mapFn(lines, func(line []string) string {
return fmt.Sprintf("(%s)", strings.Join(line, ", "))
}), ",\n"))
}
func mapFn(arr [][]string, fn func([]string) string) []string {
var res []string
for _, item := range arr {
res = append(res, fn(item))
}
return res
}
func (s *SQLSmith) generateDataItem(columnType string) string {
switch columnType {
case "varchar":
return s.generateStringItem()
case "text":
return s.generateStringItem()
case "int":
return s.generateIntItem()
case "timestamp":
return s.generateDateItem()
case "float":
return s.generateFloatItem()
}
return ""
}
func (s *SQLSmith) generateStringItem() string {
return fmt.Sprintf("\"%s\"", s.rdString(s.rd(100)))
}
func (s *SQLSmith) generateIntItem() string {
return fmt.Sprintf("%d", s.rd(2147483647))
}
func (s *SQLSmith) generateFloatItem() string {
return fmt.Sprintf("%f", float64(s.rd(100000))*s.rdFloat64())
}
func (s *SQLSmith) generateDateItem() string {
return fmt.Sprintf("\"%s\"", s.rdDate().Format("2006-01-02 15:04:05"))
}