-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.go
218 lines (197 loc) · 8.07 KB
/
variables.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mysql
import (
"regexp"
"strings"
"github.com/nging-plugins/dbmanager/application/library/dbmanager/driver/mysql/utils"
)
var (
//数据来自数据库
reCollate = regexp.MustCompile(` COLLATE ([^ ]+)`)
reCharacter = regexp.MustCompile(` CHARACTER SET ([^ ]+)`)
reInnoDBComment = regexp.MustCompile(`(?:(.+); )?InnoDB free: .*`)
reGrantOn = regexp.MustCompile(`GRANT (.*) ON (.*) TO `)
reGrantBrackets = regexp.MustCompile(` *([^(,]*[^ ,(])( *\([^)]+\))?`)
reGrantOption = regexp.MustCompile(` WITH GRANT OPTION`)
reGrantIdent = regexp.MustCompile(` IDENTIFIED BY PASSWORD '([^']+)`)
reFulltextKey = regexp.MustCompile("FULLTEXT KEY `([^`]+)`[ ]*\\([^)]+\\) /\\*[^ ]* WITH ([^*]+) \\*/")
reView = regexp.MustCompile("^.+?\\s+AS\\s+")
reField = regexp.MustCompile("^([^( ]+)(?:\\((.+)\\))?( unsigned)?( zerofill)?$")
reFieldOnUpdate = regexp.MustCompile("^(?i)(?:DEFAULT_GENERATED )?on update (.+)")
reFieldDefault = regexp.MustCompile("char|set")
reFieldPrivilegeDelim = regexp.MustCompile(", *")
reFriendlyName = regexp.MustCompile("(?i)[^a-z0-9_]")
//((?<!o)int(?!er)|numeric|real|float|double|decimal|money)
reFieldTypeNumber = regexp.MustCompile("(^|[^o])int(?:er)?|numeric|real|float|double|decimal|money")
reCSVText = regexp.MustCompile("[\"\n,;\t]")
reFieldTypeText = regexp.MustCompile("char|text|enum|set")
reFieldTypeBit = regexp.MustCompile("^([0-9]+|b'[0-1]+')$")
reFieldTypeBlob = regexp.MustCompile("blob|bytea|raw|file")
reFieldLengthInvalid = regexp.MustCompile("[^-0-9,+()[\\]]")
reFieldLengthNumber = regexp.MustCompile("^[0-9].*")
reFieldEnumValue = regexp.MustCompile(`'((?:[^']|'')*)'`)
reFieldTextValue = regexp.MustCompile(`text|lob`)
pgsqlFieldDefaultValue = regexp.MustCompile("^[a-z]+\\(('[^']*')+\\)$")
//以下数据来自客户端
reGrantColumn = regexp.MustCompile(`^([^() ]+)\s*(\([^)]*\))?$`)
reGrantOptionValue = regexp.MustCompile(`(GRANT OPTION)\([^)]*\)`)
reNotWord = regexp.MustCompile(`[^a-zA-Z0-9_]+`)
reOnlyWord = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
reOnlyNumber = regexp.MustCompile(`^[0-9]+(\.[0-9]+)?$`)
reOnlyFloatOrEmpty = regexp.MustCompile(`^[0-9]*\.[0-9]*$`)
reChineseAndPunctuation = regexp.MustCompile(`[\x80-\xFF]`)
reSQLCondOrder = regexp.MustCompile("^((COUNT\\(DISTINCT |[A-Z0-9_]+\\()(`(?:[^`]|``)+`|\"(?:[^\"]|\"\")+\")\\)|COUNT\\(\\*\\))$")
reSQLFunction = regexp.MustCompile("^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM|CHAR_LENGTH|DATE|FROM_UNIXTIME|LOWER|ROUND|SEC_TO_TIME|TIME_TO_SEC|UPPER)\\(`(?:[^`]|``)+`\\))$")
//Charsets MySQL 支持的字符集
Charsets = utils.Charsets
// UnsignedTags 无符号标签
UnsignedTags = []string{"unsigned", "zerofill", "unsigned zerofill"}
// EnumLength 枚举选项
EnumLength = "'(?:''|[^'\\\\]|\\\\.)*'"
reEnumLength = regexp.MustCompile(EnumLength)
reContainsEnumLength = regexp.MustCompile("^\\s*\\(?\\s*" + EnumLength + "(?:\\s*,\\s*" + EnumLength + ")*\\s*\\)?\\s*$")
// OnActions used in foreignKeys()
OnActions = "RESTRICT|NO ACTION|CASCADE|SET NULL|SET DEFAULT"
// PartitionTypes 分区类型
PartitionTypes = []string{`HASH`, `LINEAR HASH`, `KEY`, `LINEAR KEY`, `RANGE`, `LIST`}
typeGroups = []*FieldTypeGroup{
&FieldTypeGroup{
Types: []*FieldType{
{"tinyint", 3},
{"smallint", 5},
{"mediumint", 8},
{"int", 10},
{"bigint", 20},
{"decimal", 66},
{"float", 12},
{"double", 21},
},
Label: "数字",
Type: "Number",
},
&FieldTypeGroup{
Types: []*FieldType{
{"date", 10},
{"datetime", 19},
{"timestamp", 19},
{"time", 10},
{"year", 4},
},
Label: "日期和时间",
Type: "Datetime",
},
&FieldTypeGroup{
Types: []*FieldType{
{"char", 255},
{"varchar", 65535},
{"tinytext", 255},
{"text", 65535},
{"mediumtext", 16777215},
{"longtext", 4294967295},
},
Label: "字符串",
Type: "String",
},
&FieldTypeGroup{
Types: []*FieldType{
{"enum", 65535},
{"set", 64},
},
Label: "列表",
Type: "List",
},
&FieldTypeGroup{
Types: []*FieldType{
{"bit", 20},
{"binary", 255},
{"varbinary", 65535},
{"tinyblob", 255},
{"blob", 65535},
{"mediumblob", 16777215},
{"longblob", 4294967295},
},
Label: "二进制",
Type: "Binary",
},
&FieldTypeGroup{
Types: []*FieldType{
{"geometry", 0},
{"point", 0},
{"linestring", 0},
{"polygon", 0},
{"multipoint", 0},
{"multilinestring", 0},
{"multipolygon", 0},
{"geometrycollection", 0},
},
Label: "几何图形",
Type: "Geometry",
},
}
types = map[string]uint64{} ///< @var array ($type,$maximum_unsigned_length, ...)
operators = []string{"=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "REGEXP", "IN", "IS NULL", "NOT LIKE", "NOT REGEXP", "NOT IN", "IS NOT NULL", "SQL"} ///< @var array operators used in select
functions = []string{"char_length", "date", "from_unixtime", "lower", "round", "sec_to_time", "time_to_sec", "upper"} ///< @var array functions used in select
grouping = []string{"avg", "count", "count distinct", "group_concat", "max", "min", "sum"} ///< @var array grouping functions used in select
editFunctions = []map[string][]string{
///< @var array of array("$type|$type2" => "$function/$function2") functions used in editing, [0] - edit and insert, [1] - edit only
{
"char": []string{"md5", "sha1", "password", "encrypt", "uuid"}, //! JavaScript for disabling maxlength
"binary": []string{"md5", "sha1"},
"date|time": []string{"now"},
},
{
"(^|[^o])int|float|double|decimal": []string{"+", "-"}, // not point
"date": []string{"+ interval", "- interval"},
"time": []string{"addtime", "subtime"},
"char|text": []string{"concat"},
},
}
pattern = "`(?:[^`]|``)+`"
reQuotedCol = regexp.MustCompile(pattern)
reForeignKey = regexp.MustCompile(`CONSTRAINT (` + pattern + `) FOREIGN KEY ?\(((?:` + pattern + `,? ?)+)\) REFERENCES (` + pattern + `)(?:\.(` + pattern + `))? \(((?:` + pattern + `,? ?)+)\)(?: ON DELETE (` + OnActions + `))?(?: ON UPDATE (` + OnActions + `))?`)
trans = map[string]string{
":": ":1",
"]": ":2",
"[": ":3",
}
reFunctionAddOrSubOr = regexp.MustCompile(`^([+-]|\|\|)$`)
reFunctionInterval = regexp.MustCompile(`^[+-] interval$`)
reSQLValue = regexp.MustCompile(`^(\d+|'[0-9.: -]') [A-Z_]+$`)
reFieldName = regexp.MustCompile(`^([\w(]+)(` + strings.Replace(regexp.QuoteMeta(quoteCol(`_`)), "_", ".*", -1) + `)([ \w)]+)$`)
reNotSpaceOrDashOrAt = regexp.MustCompile(`[^ -@]`)
)
func init() {
for _, group := range typeGroups {
for _, typ := range group.Types {
types[typ.Name] = typ.MaxUnsignedLength
}
}
}
type FieldType struct {
Name string
MaxUnsignedLength uint64
}
type FieldTypeGroup struct {
Types []*FieldType
Label string
Type string
}
func (f *FieldTypeGroup) IsString(typeName string) bool {
return reFieldTypeText.MatchString(typeName)
}
func (f *FieldTypeGroup) IsNumeric(typeName string) bool {
return reFieldTypeNumber.MatchString(typeName)
}