-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbutil.go
143 lines (127 loc) · 4.06 KB
/
dbutil.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
package main
import (
"bufio"
"context"
"database/sql"
"fmt"
"regexp"
"strings"
"sync"
_ "github.com/go-sql-driver/mysql"
"github.com/iancoleman/strcase"
)
// FkRelation is for foreign columns in given table
type FkRelation struct {
RefTableCamel string // foreign table camelCase (parent)
RefTablePascal string // foreign table PascalCase (parent)
RefPkPascal string // PK of foreign table (parent)
PascalName string // current table name in PascalCase
CamelCase string // current table name in camelCase
PkCamelCase string // PRIMARY KEY of current table
}
// TablePrimaryKey name and type
type TablePrimaryKey struct {
PkType string
PkName string
}
// map of TableName to its ForeignRelations
var fkRelationMap = make(map[string][]FkRelation)
// map of TableName to its PrimaryKey struct
var pkTableMap = make(map[string]TablePrimaryKey)
// common MySQL to JAVA typings for PK
var sqlJavaTypes = map[string]string{
"varchar": "String",
"bigint": "Long",
"int": "Integer",
"binary": "String",
"char": "String",
}
// will match the foreign columns
var foreignRegex = regexp.MustCompile("FOREIGN KEY \\(`\\w+`\\) REFERENCES `(\\w+)`")
// will match the primary column
var primaryNameRegex = regexp.MustCompile("PRIMARY KEY \\(`(\\w+)`\\),?")
// will match dataType of PK
var primaryPattern = "`%s` (\\w+)\\(?"
// GetTableNames from given database
func GetTableNames(dbConn *sql.DB) ([]string, error) {
rows, err := dbConn.QueryContext(context.Background(), "SHOW TABLES")
if err != nil {
return nil, err
}
defer rows.Close()
var tableList []string
for rows.Next() {
var name string
rows.Scan(&name)
tableList = append(tableList, name)
}
return tableList, nil
}
// GetForeignRelations within given table, ignore Views
func GetForeignRelations(dbConn *sql.DB, tableName string, wg *sync.WaitGroup, mu *sync.Mutex) {
defer wg.Done()
if strings.HasPrefix(tableName, "view_") {
return
}
var fkRelations []FkRelation
createStmt, err := showCreateStmt(dbConn, tableName)
check(err)
//TODO, read createStmt and if regexMatch `CREATE ...VIEW`, then return
camelPkName := getTablePrimaryKey(tableName, createStmt, mu)
br := bufio.NewScanner(strings.NewReader(createStmt))
for br.Scan() {
matchArr := foreignRegex.FindStringSubmatch(br.Text())
if len(matchArr) > 0 {
relation := &FkRelation{
RefTableCamel: strcase.ToLowerCamel(matchArr[1]),
RefTablePascal: strcase.ToCamel(matchArr[1]), //actually, it's Pascal Case
PascalName: strcase.ToCamel(tableName),
CamelCase: strcase.ToLowerCamel(tableName),
PkCamelCase: camelPkName,
RefPkPascal: "",
}
fkRelations = append(fkRelations, *relation)
}
}
pascalTableName := strcase.ToCamel(tableName)
mu.Lock()
fkRelationMap[pascalTableName] = fkRelations
mu.Unlock()
}
// Get table's primaryKey name and type, returns camelCase PK name
func getTablePrimaryKey(tableName, createStmt string, mu *sync.Mutex) string {
pkItem := new(TablePrimaryKey)
br := bufio.NewScanner(strings.NewReader(createStmt))
for br.Scan() {
matchArr := primaryNameRegex.FindStringSubmatch(br.Text())
if len(matchArr) > 0 {
regPKType := regexp.MustCompile(fmt.Sprintf(primaryPattern, matchArr[1]))
typeResults := regPKType.FindStringSubmatch(createStmt)
var sqlType = typeResults[1]
pkItem.PkType = sqlJavaTypes[sqlType]
pkItem.PkName = strcase.ToLowerCamel(matchArr[1])
break
}
}
pascalTableName := strcase.ToCamel(tableName) //actually, it's Pascal Case
mu.Lock()
pkTableMap[pascalTableName] = *pkItem
mu.Unlock()
return pkItem.PkName
}
// Get DDL of the given table
func showCreateStmt(dbConn *sql.DB, tableName string) (string, error) {
row := dbConn.QueryRowContext(context.Background(), fmt.Sprintf("SHOW CREATE TABLE `%s`", tableName))
var c1, c2 string
err := row.Scan(&c1, &c2)
return c2, err
}
// GetDbVersion helps decide which driver to use
func GetDbVersion(db *sql.DB) (string, error) {
var version string
err := db.QueryRowContext(context.Background(), "SELECT VERSION()").Scan(&version)
if err != nil {
return "", err
}
return version, nil
}