github.com/XiaoMi/Gaea@v1.2.5/mysql/constants.go (about) 1 // Copyright 2019 The Gaea Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mysql 16 17 import ( 18 "strings" 19 20 "github.com/pingcap/errors" 21 22 "github.com/XiaoMi/Gaea/parser/format" 23 ) 24 25 const ( 26 // MinProtocolVersion min protocol version 27 MinProtocolVersion byte = 10 28 // MaxPayloadLen max payload length 29 MaxPayloadLen int = 1<<24 - 1 30 // TimeFormat time format 31 TimeFormat string = "2006-01-02 15:04:05" 32 // ServerVersion server version 33 ServerVersion string = "5.6.20-gaea" 34 // MysqlNativePassword uses a salt and transmits a hash on the wire. 35 MysqlNativePassword = "mysql_native_password" 36 CachingSHA2Password = "caching_sha2_password" 37 // ProtocolVersion is the current version of the protocol. 38 // Always 10. 39 ProtocolVersion = 10 40 ) 41 42 const ( 43 // CursorTypeReadOnly readonly cursor 44 CursorTypeReadOnly = 0x01 45 ) 46 47 // Header information. 48 const ( 49 OKHeader byte = 0x00 50 ErrHeader byte = 0xff 51 EOFHeader byte = 0xfe 52 LocalInFileHeader byte = 0xfb 53 AuthSwitchHeader byte = 0xfe 54 ) 55 56 // Server information. 57 const ( 58 ServerStatusInTrans uint16 = 0x0001 59 ServerStatusAutocommit uint16 = 0x0002 60 ServerMoreResultsExists uint16 = 0x0008 61 ServerStatusNoGoodIndexUsed uint16 = 0x0010 62 ServerStatusNoIndexUsed uint16 = 0x0020 63 ServerStatusCursorExists uint16 = 0x0040 64 ServerStatusLastRowSend uint16 = 0x0080 65 ServerStatusDBDropped uint16 = 0x0100 66 ServerStatusNoBackslashEscaped uint16 = 0x0200 67 ServerStatusMetadataChanged uint16 = 0x0400 68 ServerStatusWasSlow uint16 = 0x0800 69 ServerPSOutParams uint16 = 0x1000 70 ) 71 72 // ErrTextLength error text length limit. 73 const ErrTextLength = 80 74 75 // Command information. 76 const ( 77 ComSleep byte = iota 78 ComQuit 79 ComInitDB 80 ComQuery 81 ComFieldList 82 ComCreateDB 83 ComDropDB 84 ComRefresh 85 ComShutdown 86 ComStatistics 87 ComProcessInfo 88 ComConnect 89 ComProcessKill 90 ComDebug 91 ComPing 92 ComTime 93 ComDelayedInsert 94 ComChangeUser 95 ComBinlogDump 96 ComTableDump 97 ComConnectOut 98 ComRegisterSlave 99 ComStmtPrepare 100 ComStmtExecute 101 ComStmtSendLongData 102 ComStmtClose 103 ComStmtReset 104 ComSetOption 105 ComStmtFetch 106 ComDaemon 107 ComBinlogDumpGtid 108 ComResetConnection 109 ComEnd 110 ) 111 112 // Client information. 113 const ( 114 ClientLongPassword uint32 = 1 << iota 115 ClientFoundRows 116 ClientLongFlag 117 ClientConnectWithDB 118 ClientNoSchema 119 ClientCompress 120 ClientODBC 121 ClientLocalFiles 122 ClientIgnoreSpace 123 ClientProtocol41 124 ClientInteractive 125 ClientSSL 126 ClientIgnoreSigpipe 127 ClientTransactions 128 ClientReserved 129 ClientSecureConnection 130 ClientMultiStatements 131 ClientMultiResults 132 ClientPSMultiResults 133 ClientPluginAuth 134 ClientConnectAtts 135 ClientPluginAuthLenencClientData 136 ) 137 138 // PrivilegeType privilege 139 type PrivilegeType uint32 140 141 const ( 142 _ PrivilegeType = 1 << iota 143 // CreatePriv is the privilege to create schema/table. 144 CreatePriv 145 // SelectPriv is the privilege to read from table. 146 SelectPriv 147 // InsertPriv is the privilege to insert data into table. 148 InsertPriv 149 // UpdatePriv is the privilege to update data in table. 150 UpdatePriv 151 // DeletePriv is the privilege to delete data from table. 152 DeletePriv 153 // ShowDBPriv is the privilege to run show databases statement. 154 ShowDBPriv 155 // SuperPriv enables many operations and server behaviors. 156 SuperPriv 157 // CreateUserPriv is the privilege to create user. 158 CreateUserPriv 159 // TriggerPriv is not checked yet. 160 TriggerPriv 161 // DropPriv is the privilege to drop schema/table. 162 DropPriv 163 // ProcessPriv pertains to display of information about the threads executing within the server. 164 ProcessPriv 165 // GrantPriv is the privilege to grant privilege to user. 166 GrantPriv 167 // ReferencesPriv is not checked yet. 168 ReferencesPriv 169 // AlterPriv is the privilege to run alter statement. 170 AlterPriv 171 // ExecutePriv is the privilege to run execute statement. 172 ExecutePriv 173 // IndexPriv is the privilege to create/drop index. 174 IndexPriv 175 // CreateViewPriv is the privilege to create view. 176 CreateViewPriv 177 // ShowViewPriv is the privilege to show create view. 178 ShowViewPriv 179 // CreateRolePriv the privilege to create a role. 180 CreateRolePriv 181 // DropRolePriv is the privilege to drop a role. 182 DropRolePriv 183 // AllPriv is the privilege for all actions. 184 AllPriv 185 ) 186 187 // AllPrivMask is the mask for PrivilegeType with all bits set to 1. 188 // If it's passed to RequestVerification, it means any privilege would be OK. 189 const AllPrivMask = AllPriv - 1 190 191 // MySQL type maximum length. 192 const ( 193 // For arguments that have no fixed number of decimals, the decimals value is set to 31, 194 // which is 1 more than the maximum number of decimals permitted for the DECIMAL, FLOAT, and DOUBLE data types. 195 NotFixedDec = 31 196 197 MaxIntWidth = 20 198 MaxRealWidth = 23 199 MaxFloatingTypeScale = 30 200 MaxFloatingTypeWidth = 255 201 MaxDecimalScale = 30 202 MaxDecimalWidth = 65 203 MaxDateWidth = 10 // YYYY-MM-DD. 204 MaxDatetimeWidthNoFsp = 19 // YYYY-MM-DD HH:MM:SS 205 MaxDatetimeWidthWithFsp = 26 // YYYY-MM-DD HH:MM:SS[.fraction] 206 MaxDatetimeFullWidth = 29 // YYYY-MM-DD HH:MM:SS.###### AM 207 MaxDurationWidthNoFsp = 10 // HH:MM:SS 208 MaxDurationWidthWithFsp = 15 // HH:MM:SS[.fraction] 209 MaxBlobWidth = 16777216 210 ) 211 212 // SQLMode is the type for MySQL sql_mode. 213 // See https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html 214 type SQLMode int 215 216 // HasNoZeroDateMode detects if 'NO_ZERO_DATE' mode is set in SQLMode 217 func (m SQLMode) HasNoZeroDateMode() bool { 218 return m&ModeNoZeroDate == ModeNoZeroDate 219 } 220 221 // HasNoZeroInDateMode detects if 'NO_ZERO_IN_DATE' mode is set in SQLMode 222 func (m SQLMode) HasNoZeroInDateMode() bool { 223 return m&ModeNoZeroInDate == ModeNoZeroInDate 224 } 225 226 // HasErrorForDivisionByZeroMode detects if 'ERROR_FOR_DIVISION_BY_ZERO' mode is set in SQLMode 227 func (m SQLMode) HasErrorForDivisionByZeroMode() bool { 228 return m&ModeErrorForDivisionByZero == ModeErrorForDivisionByZero 229 } 230 231 // HasOnlyFullGroupBy detects if 'ONLY_FULL_GROUP_BY' mode is set in SQLMode 232 func (m SQLMode) HasOnlyFullGroupBy() bool { 233 return m&ModeOnlyFullGroupBy == ModeOnlyFullGroupBy 234 } 235 236 // HasStrictMode detects if 'STRICT_TRANS_TABLES' or 'STRICT_ALL_TABLES' mode is set in SQLMode 237 func (m SQLMode) HasStrictMode() bool { 238 return m&ModeStrictTransTables == ModeStrictTransTables || m&ModeStrictAllTables == ModeStrictAllTables 239 } 240 241 // HasPipesAsConcatMode detects if 'PIPES_AS_CONCAT' mode is set in SQLMode 242 func (m SQLMode) HasPipesAsConcatMode() bool { 243 return m&ModePipesAsConcat == ModePipesAsConcat 244 } 245 246 // HasNoUnsignedSubtractionMode detects if 'NO_UNSIGNED_SUBTRACTION' mode is set in SQLMode 247 func (m SQLMode) HasNoUnsignedSubtractionMode() bool { 248 return m&ModeNoUnsignedSubtraction == ModeNoUnsignedSubtraction 249 } 250 251 // HasHighNotPrecedenceMode detects if 'HIGH_NOT_PRECEDENCE' mode is set in SQLMode 252 func (m SQLMode) HasHighNotPrecedenceMode() bool { 253 return m&ModeHighNotPrecedence == ModeHighNotPrecedence 254 } 255 256 // HasANSIQuotesMode detects if 'ANSI_QUOTES' mode is set in SQLMode 257 func (m SQLMode) HasANSIQuotesMode() bool { 258 return m&ModeANSIQuotes == ModeANSIQuotes 259 } 260 261 // HasRealAsFloatMode detects if 'REAL_AS_FLOAT' mode is set in SQLMode 262 func (m SQLMode) HasRealAsFloatMode() bool { 263 return m&ModeRealAsFloat == ModeRealAsFloat 264 } 265 266 // HasPadCharToFullLengthMode detects if 'PAD_CHAR_TO_FULL_LENGTH' mode is set in SQLMode 267 func (m SQLMode) HasPadCharToFullLengthMode() bool { 268 return m&ModePadCharToFullLength == ModePadCharToFullLength 269 } 270 271 // HasNoBackslashEscapesMode detects if 'NO_BACKSLASH_ESCAPES' mode is set in SQLMode 272 func (m SQLMode) HasNoBackslashEscapesMode() bool { 273 return m&ModeNoBackslashEscapes == ModeNoBackslashEscapes 274 } 275 276 // HasIgnoreSpaceMode detects if 'IGNORE_SPACE' mode is set in SQLMode 277 func (m SQLMode) HasIgnoreSpaceMode() bool { 278 return m&ModeIgnoreSpace == ModeIgnoreSpace 279 } 280 281 // HasNoAutoCreateUserMode detects if 'NO_AUTO_CREATE_USER' mode is set in SQLMode 282 func (m SQLMode) HasNoAutoCreateUserMode() bool { 283 return m&ModeNoAutoCreateUser == ModeNoAutoCreateUser 284 } 285 286 // HasAllowInvalidDatesMode detects if 'ALLOW_INVALID_DATES' mode is set in SQLMode 287 func (m SQLMode) HasAllowInvalidDatesMode() bool { 288 return m&ModeAllowInvalidDates == ModeAllowInvalidDates 289 } 290 291 // consts for sql modes. 292 const ( 293 ModeNone SQLMode = 0 294 ModeRealAsFloat SQLMode = 1 << iota 295 ModePipesAsConcat 296 ModeANSIQuotes 297 ModeIgnoreSpace 298 ModeNotUsed 299 ModeOnlyFullGroupBy 300 ModeNoUnsignedSubtraction 301 ModeNoDirInCreate 302 ModePostgreSQL 303 ModeOracle 304 ModeMsSQL 305 ModeDb2 306 ModeMaxdb 307 ModeNoKeyOptions 308 ModeNoTableOptions 309 ModeNoFieldOptions 310 ModeMySQL323 311 ModeMySQL40 312 ModeANSI 313 ModeNoAutoValueOnZero 314 ModeNoBackslashEscapes 315 ModeStrictTransTables 316 ModeStrictAllTables 317 ModeNoZeroInDate 318 ModeNoZeroDate 319 ModeInvalidDates 320 ModeErrorForDivisionByZero 321 ModeTraditional 322 ModeNoAutoCreateUser 323 ModeHighNotPrecedence 324 ModeNoEngineSubstitution 325 ModePadCharToFullLength 326 ModeAllowInvalidDates 327 ) 328 329 // GetSQLMode gets the sql mode for string literal. SQL_mode is a list of different modes separated by commas. 330 // The input string must be formatted by 'FormatSQLModeStr' 331 func GetSQLMode(s string) (SQLMode, error) { 332 strs := strings.Split(s, ",") 333 var sqlMode SQLMode 334 for i, length := 0, len(strs); i < length; i++ { 335 mode, ok := Str2SQLMode[strs[i]] 336 if !ok && strs[i] != "" { 337 return sqlMode, newInvalidModeErr(strs[i]) 338 } 339 sqlMode = sqlMode | mode 340 } 341 return sqlMode, nil 342 } 343 344 // Str2SQLMode is the string represent of sql_mode to sql_mode map. 345 var Str2SQLMode = map[string]SQLMode{ 346 "REAL_AS_FLOAT": ModeRealAsFloat, 347 "PIPES_AS_CONCAT": ModePipesAsConcat, 348 "ANSI_QUOTES": ModeANSIQuotes, 349 "IGNORE_SPACE": ModeIgnoreSpace, 350 "NOT_USED": ModeNotUsed, 351 "ONLY_FULL_GROUP_BY": ModeOnlyFullGroupBy, 352 "NO_UNSIGNED_SUBTRACTION": ModeNoUnsignedSubtraction, 353 "NO_DIR_IN_CREATE": ModeNoDirInCreate, 354 "POSTGRESQL": ModePostgreSQL, 355 "ORACLE": ModeOracle, 356 "MSSQL": ModeMsSQL, 357 "DB2": ModeDb2, 358 "MAXDB": ModeMaxdb, 359 "NO_KEY_OPTIONS": ModeNoKeyOptions, 360 "NO_TABLE_OPTIONS": ModeNoTableOptions, 361 "NO_FIELD_OPTIONS": ModeNoFieldOptions, 362 "MYSQL323": ModeMySQL323, 363 "MYSQL40": ModeMySQL40, 364 "ANSI": ModeANSI, 365 "NO_AUTO_VALUE_ON_ZERO": ModeNoAutoValueOnZero, 366 "NO_BACKSLASH_ESCAPES": ModeNoBackslashEscapes, 367 "STRICT_TRANS_TABLES": ModeStrictTransTables, 368 "STRICT_ALL_TABLES": ModeStrictAllTables, 369 "NO_ZERO_IN_DATE": ModeNoZeroInDate, 370 "NO_ZERO_DATE": ModeNoZeroDate, 371 "INVALID_DATES": ModeInvalidDates, 372 "ERROR_FOR_DIVISION_BY_ZERO": ModeErrorForDivisionByZero, 373 "TRADITIONAL": ModeTraditional, 374 "NO_AUTO_CREATE_USER": ModeNoAutoCreateUser, 375 "HIGH_NOT_PRECEDENCE": ModeHighNotPrecedence, 376 "NO_ENGINE_SUBSTITUTION": ModeNoEngineSubstitution, 377 "PAD_CHAR_TO_FULL_LENGTH": ModePadCharToFullLength, 378 "ALLOW_INVALID_DATES": ModeAllowInvalidDates, 379 } 380 381 func newInvalidModeErr(s string) error { 382 return NewDefaultError(ErrWrongValueForVar, "sql_mode", s) 383 } 384 385 // PriorityEnum is defined for Priority const values. 386 type PriorityEnum int 387 388 // Priority const values. 389 // See https://dev.mysql.com/doc/refman/5.7/en/insert.html 390 const ( 391 NoPriority PriorityEnum = iota 392 LowPriority 393 HighPriority 394 DelayedPriority 395 ) 396 397 // Priority2Str is used to convert the statement priority to string. 398 var Priority2Str = map[PriorityEnum]string{ 399 NoPriority: "NO_PRIORITY", 400 LowPriority: "LOW_PRIORITY", 401 HighPriority: "HIGH_PRIORITY", 402 DelayedPriority: "DELAYED", 403 } 404 405 // Str2Priority is used to convert a string to a priority. 406 func Str2Priority(val string) PriorityEnum { 407 val = strings.ToUpper(val) 408 switch val { 409 case "NO_PRIORITY": 410 return NoPriority 411 case "HIGH_PRIORITY": 412 return HighPriority 413 case "LOW_PRIORITY": 414 return LowPriority 415 case "DELAYED": 416 return DelayedPriority 417 default: 418 return NoPriority 419 } 420 } 421 422 // Restore implements Node interface. 423 func (n *PriorityEnum) Restore(ctx *format.RestoreCtx) error { 424 switch *n { 425 case NoPriority: 426 return nil 427 case LowPriority: 428 ctx.WriteKeyWord("LOW_PRIORITY") 429 case HighPriority: 430 ctx.WriteKeyWord("HIGH_PRIORITY") 431 case DelayedPriority: 432 ctx.WriteKeyWord("DELAYED") 433 default: 434 return errors.Errorf("undefined PriorityEnum Type[%d]", *n) 435 } 436 return nil 437 }