-
Notifications
You must be signed in to change notification settings - Fork 75
/
alias.go
99 lines (82 loc) · 3.04 KB
/
alias.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
package mysql
import (
"fmt"
"regexp"
"strings"
)
func unaliasUnparameterizedColumnType(requestedType string) string {
switch requestedType {
case "bool":
return "tinyint (1)"
case "boolean":
return "tinyint (1)"
}
// mysql gives us the length of these text types, but it won't
// accept them as create table arguments...
if requestedType == "text (65535)" {
requestedType = "text"
} else if requestedType == "tinytext (255)" {
requestedType = "tinytext"
} else if requestedType == "mediumtext (16777215)" {
requestedType = "mediumtext"
} else if requestedType == "longtext (4294967295)" {
requestedType = "longtext"
} else if requestedType == "blob (65535)" {
requestedType = "blob"
} else if requestedType == "tinyblob (255)" {
requestedType = "tinyblob"
} else if requestedType == "mediumblob (16777215)" {
requestedType = "mediumblob"
} else if requestedType == "longblob (4294967295)" {
requestedType = "longblob"
}
for _, unparameterizedColumnType := range unparameterizedColumnTypes {
if unparameterizedColumnType == requestedType {
return requestedType
}
}
return ""
}
func unaliasParameterizedColumnType(requestedType string) string {
if strings.HasPrefix(requestedType, "char ") || strings.HasPrefix(requestedType, "char(") ||
requestedType == "character" || requestedType == "char" {
r := regexp.MustCompile(`char\s*\((?P<len>\d*)\)`)
matchGroups := r.FindStringSubmatch(requestedType)
if len(matchGroups) == 0 {
return "character"
}
return fmt.Sprintf("character (%s)", matchGroups[1])
}
if strings.HasPrefix(requestedType, "integer") {
r := regexp.MustCompile(`integer\s*\(\s*(?P<max>\d*)\s*\)`)
matchGroups := r.FindStringSubmatch(requestedType)
if len(matchGroups) == 0 {
return "int (11)"
}
return fmt.Sprintf("int (%s)", matchGroups[1])
}
if strings.HasPrefix(requestedType, "dec") {
if strings.Contains(requestedType, "decimal") {
requestedType = strings.Replace(requestedType, "decimal", "dec", -1)
}
precisionAndScale := regexp.MustCompile(`dec\s*\(\s*(?P<precision>\d*),\s*(?P<scale>\d*)\s*\)`)
precisionOnly := regexp.MustCompile(`dec\s*\(\s*(?P<precision>\d*)\s*\)`)
precisionAndScaleMatchGroups := precisionAndScale.FindStringSubmatch(requestedType)
precisionOnlyMatchGroups := precisionOnly.FindStringSubmatch(requestedType)
if len(precisionAndScaleMatchGroups) == 3 {
return fmt.Sprintf("decimal (%s, %s)", precisionAndScaleMatchGroups[1], precisionAndScaleMatchGroups[2])
} else if len(precisionOnlyMatchGroups) == 2 {
return fmt.Sprintf("decimal (%s, 0)", precisionOnlyMatchGroups[1])
}
return "decimal (10, 0)"
}
if strings.HasPrefix(requestedType, "double precision") {
precisionAndScale := regexp.MustCompile(`double precision\s*\(\s*(?P<precision>\d*),\s*(?P<scale>\d*)\s*\)`)
precisionAndScaleMatchGroups := precisionAndScale.FindStringSubmatch(requestedType)
if len(precisionAndScaleMatchGroups) == 3 {
return fmt.Sprintf("double (%s, %s)", precisionAndScaleMatchGroups[1], precisionAndScaleMatchGroups[2])
}
return "double"
}
return ""
}