-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_load.go
50 lines (46 loc) · 1.24 KB
/
schema_load.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
package sqlsmith
import (
"strings"
"regexp"
"github.com/you06/sqlsmith-go/types"
)
const typeRegex = `\(\d+\)`
// LoadSchema init schemas, tables and columns
// record[0] dbname
// record[1] table name
// record[2] table type
// record[3] column name
// record[4] column type
func (s *SQLSmith) LoadSchema (records [][5]string) {
// init databases
for _, record := range records {
dbname := record[0]
tableName := record[1]
tableType := record[2]
columnName := record[3]
columnType := record[4]
if _, ok := s.Databases[dbname]; !ok {
s.Databases[dbname] = &types.Database{
Name: dbname,
Tables: make(map[string]*types.Table),
}
}
if _, ok := s.Databases[dbname].Tables[tableName]; !ok {
s.Databases[dbname].Tables[tableName] = &types.Table{
DB: dbname,
Table: tableName,
Type: tableType,
Columns: make(map[string]*types.Column),
}
}
if _, ok := s.Databases[dbname].Tables[tableName].Columns[columnName]; !ok {
s.Databases[dbname].Tables[tableName].Columns[columnName] = &types.Column{
DB: dbname,
Table: tableName,
Column: columnName,
// remove the data size in type definition
DataType: regexp.MustCompile(typeRegex).ReplaceAllString(strings.ToLower(columnType), ""),
}
}
}
}