-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_enumset.go
More file actions
74 lines (67 loc) · 1.35 KB
/
mysql_enumset.go
File metadata and controls
74 lines (67 loc) · 1.35 KB
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
package main
import (
"fmt"
"strings"
)
func parseMySQLEnumSetValues(columnType string) ([]string, error) {
open := strings.IndexByte(columnType, '(')
close := strings.LastIndexByte(columnType, ')')
if open < 0 || close <= open {
return nil, fmt.Errorf("invalid enum/set column_type %q", columnType)
}
inside := columnType[open+1 : close]
var values []string
i := 0
for i < len(inside) {
for i < len(inside) && (inside[i] == ' ' || inside[i] == ',') {
i++
}
if i >= len(inside) {
break
}
if inside[i] != '\'' {
return nil, fmt.Errorf("invalid enum/set value list in %q", columnType)
}
i++
var b strings.Builder
for i < len(inside) {
c := inside[i]
if c == '\\' {
if i+1 >= len(inside) {
return nil, fmt.Errorf("invalid escape in %q", columnType)
}
b.WriteByte(inside[i+1])
i += 2
continue
}
if c == '\'' {
if i+1 < len(inside) && inside[i+1] == '\'' {
b.WriteByte('\'')
i += 2
continue
}
i++
break
}
b.WriteByte(c)
i++
}
values = append(values, b.String())
}
return values, nil
}
func parseMySQLSetDefault(v string) []string {
if v == "" {
return nil
}
parts := strings.Split(v, ",")
out := make([]string, 0, len(parts))
for _, p := range parts {
p = strings.TrimSpace(p)
if p == "" {
continue
}
out = append(out, p)
}
return out
}