forked from mpchadwick/dbanon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_table.go
62 lines (52 loc) · 1.27 KB
/
create_table.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
package dbanon
import (
"github.com/blastrain/vitess-sqlparser/sqlparser"
"strconv"
"strings"
)
type Column struct {
Name string
Type string
MaxLength int
}
func NewColumn(n string, t string, i int) *Column {
return &Column{Name: n, Type: t, MaxLength: i}
}
var nextTable = ""
var currentTable = make([]*Column, 0)
func findNextTable(s string) {
if len(nextTable) > 0 {
// TODO: Are we guaranteed this will delimit the end of the CREATE TABLE?
j := strings.Index(s, "/*!40101")
if j == 0 {
stmt, _ := sqlparser.Parse(nextTable)
currentTable = nil
createTable := stmt.(*sqlparser.CreateTable)
for _, col := range createTable.Columns {
column := NewColumn(col.Name, col.Type, extractMaxLength(col.Type))
currentTable = append(currentTable, column)
}
nextTable = ""
} else {
nextTable += s
}
}
k := strings.Index(s, "CREATE TABLE")
if k == 0 {
nextTable += s
}
}
func extractMaxLength(s string) int {
s = strings.ToLower(s)
// For now we'll only worry about VARCHAR...I've never heard of
// this being needed in practice anyway...
j := strings.Index(s, "varchar")
if j != 0 {
return -1
}
lenStart := strings.Index(s, "(")
lenEnd := strings.Index(s, ")")
len := s[lenStart+1 : lenEnd]
i, _ := strconv.Atoi(len)
return i
}