const.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // Copyright 2017 PingCAP, Inc.
  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. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package mysql
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/pingcap/errors"
  18. "github.com/pingcap/parser/format"
  19. )
  20. func newInvalidModeErr(s string) error {
  21. return NewErr(ErrWrongValueForVar, "sql_mode", s)
  22. }
  23. // Version information.
  24. var (
  25. // TiDBReleaseVersion is initialized by (git describe --tags) in Makefile.
  26. TiDBReleaseVersion = "None"
  27. // ServerVersion is the version information of this tidb-server in MySQL's format.
  28. ServerVersion = fmt.Sprintf("5.7.25-TiDB-%s", TiDBReleaseVersion)
  29. )
  30. // Header information.
  31. const (
  32. OKHeader byte = 0x00
  33. ErrHeader byte = 0xff
  34. EOFHeader byte = 0xfe
  35. LocalInFileHeader byte = 0xfb
  36. )
  37. // Protocol Features
  38. const AuthSwitchRequest byte = 0xfe
  39. // Server information.
  40. const (
  41. ServerStatusInTrans uint16 = 0x0001
  42. ServerStatusAutocommit uint16 = 0x0002
  43. ServerMoreResultsExists uint16 = 0x0008
  44. ServerStatusNoGoodIndexUsed uint16 = 0x0010
  45. ServerStatusNoIndexUsed uint16 = 0x0020
  46. ServerStatusCursorExists uint16 = 0x0040
  47. ServerStatusLastRowSend uint16 = 0x0080
  48. ServerStatusDBDropped uint16 = 0x0100
  49. ServerStatusNoBackslashEscaped uint16 = 0x0200
  50. ServerStatusMetadataChanged uint16 = 0x0400
  51. ServerStatusWasSlow uint16 = 0x0800
  52. ServerPSOutParams uint16 = 0x1000
  53. )
  54. // HasCursorExistsFlag return true if cursor exists indicated by server status.
  55. func HasCursorExistsFlag(serverStatus uint16) bool {
  56. return serverStatus&ServerStatusCursorExists > 0
  57. }
  58. // Identifier length limitations.
  59. // See https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
  60. const (
  61. // MaxPayloadLen is the max packet payload length.
  62. MaxPayloadLen = 1<<24 - 1
  63. // MaxTableNameLength is max length of table name identifier.
  64. MaxTableNameLength = 64
  65. // MaxDatabaseNameLength is max length of database name identifier.
  66. MaxDatabaseNameLength = 64
  67. // MaxColumnNameLength is max length of column name identifier.
  68. MaxColumnNameLength = 64
  69. // MaxKeyParts is max length of key parts.
  70. MaxKeyParts = 16
  71. // MaxIndexIdentifierLen is max length of index identifier.
  72. MaxIndexIdentifierLen = 64
  73. // MaxConstraintIdentifierLen is max length of constrain identifier.
  74. MaxConstraintIdentifierLen = 64
  75. // MaxViewIdentifierLen is max length of view identifier.
  76. MaxViewIdentifierLen = 64
  77. // MaxAliasIdentifierLen is max length of alias identifier.
  78. MaxAliasIdentifierLen = 256
  79. // MaxUserDefinedVariableLen is max length of user-defined variable.
  80. MaxUserDefinedVariableLen = 64
  81. )
  82. // ErrTextLength error text length limit.
  83. const ErrTextLength = 80
  84. // Command information.
  85. const (
  86. ComSleep byte = iota
  87. ComQuit
  88. ComInitDB
  89. ComQuery
  90. ComFieldList
  91. ComCreateDB
  92. ComDropDB
  93. ComRefresh
  94. ComShutdown
  95. ComStatistics
  96. ComProcessInfo
  97. ComConnect
  98. ComProcessKill
  99. ComDebug
  100. ComPing
  101. ComTime
  102. ComDelayedInsert
  103. ComChangeUser
  104. ComBinlogDump
  105. ComTableDump
  106. ComConnectOut
  107. ComRegisterSlave
  108. ComStmtPrepare
  109. ComStmtExecute
  110. ComStmtSendLongData
  111. ComStmtClose
  112. ComStmtReset
  113. ComSetOption
  114. ComStmtFetch
  115. ComDaemon
  116. ComBinlogDumpGtid
  117. ComResetConnection
  118. ComEnd
  119. )
  120. // Client information.
  121. const (
  122. ClientLongPassword uint32 = 1 << iota
  123. ClientFoundRows
  124. ClientLongFlag
  125. ClientConnectWithDB
  126. ClientNoSchema
  127. ClientCompress
  128. ClientODBC
  129. ClientLocalFiles
  130. ClientIgnoreSpace
  131. ClientProtocol41
  132. ClientInteractive
  133. ClientSSL
  134. ClientIgnoreSigpipe
  135. ClientTransactions
  136. ClientReserved
  137. ClientSecureConnection
  138. ClientMultiStatements
  139. ClientMultiResults
  140. ClientPSMultiResults
  141. ClientPluginAuth
  142. ClientConnectAtts
  143. ClientPluginAuthLenencClientData
  144. )
  145. // Cache type information.
  146. const (
  147. TypeNoCache byte = 0xff
  148. )
  149. // Auth name information.
  150. const (
  151. AuthNativePassword = "mysql_native_password"
  152. AuthCachingSha2Password = "caching_sha2_password"
  153. )
  154. // MySQL database and tables.
  155. const (
  156. // SystemDB is the name of system database.
  157. SystemDB = "mysql"
  158. // GlobalPrivTable is the table in system db contains global scope privilege info.
  159. GlobalPrivTable = "global_priv"
  160. // UserTable is the table in system db contains user info.
  161. UserTable = "User"
  162. // DBTable is the table in system db contains db scope privilege info.
  163. DBTable = "DB"
  164. // TablePrivTable is the table in system db contains table scope privilege info.
  165. TablePrivTable = "Tables_priv"
  166. // ColumnPrivTable is the table in system db contains column scope privilege info.
  167. ColumnPrivTable = "Columns_priv"
  168. // GlobalVariablesTable is the table contains global system variables.
  169. GlobalVariablesTable = "GLOBAL_VARIABLES"
  170. // GlobalStatusTable is the table contains global status variables.
  171. GlobalStatusTable = "GLOBAL_STATUS"
  172. // TiDBTable is the table contains tidb info.
  173. TiDBTable = "tidb"
  174. // RoleEdgesTable is the table contains role relation info
  175. RoleEdgeTable = "role_edges"
  176. // DefaultRoleTable is the table contain default active role info
  177. DefaultRoleTable = "default_roles"
  178. )
  179. // MySQL type maximum length.
  180. const (
  181. // For arguments that have no fixed number of decimals, the decimals value is set to 31,
  182. // which is 1 more than the maximum number of decimals permitted for the DECIMAL, FLOAT, and DOUBLE data types.
  183. NotFixedDec = 31
  184. MaxIntWidth = 20
  185. MaxRealWidth = 23
  186. MaxFloatingTypeScale = 30
  187. MaxFloatingTypeWidth = 255
  188. MaxDecimalScale = 30
  189. MaxDecimalWidth = 65
  190. MaxDateWidth = 10 // YYYY-MM-DD.
  191. MaxDatetimeWidthNoFsp = 19 // YYYY-MM-DD HH:MM:SS
  192. MaxDatetimeWidthWithFsp = 26 // YYYY-MM-DD HH:MM:SS[.fraction]
  193. MaxDatetimeFullWidth = 29 // YYYY-MM-DD HH:MM:SS.###### AM
  194. MaxDurationWidthNoFsp = 10 // HH:MM:SS
  195. MaxDurationWidthWithFsp = 15 // HH:MM:SS[.fraction]
  196. MaxBlobWidth = 16777216
  197. MaxBitDisplayWidth = 64
  198. MaxFloatPrecisionLength = 24
  199. MaxDoublePrecisionLength = 53
  200. )
  201. // MySQL max type field length.
  202. const (
  203. MaxFieldCharLength = 255
  204. MaxFieldVarCharLength = 65535
  205. )
  206. // MaxTypeSetMembers is the number of set members.
  207. const MaxTypeSetMembers = 64
  208. // PWDHashLen is the length of password's hash.
  209. const PWDHashLen = 40
  210. // Command2Str is the command information to command name.
  211. var Command2Str = map[byte]string{
  212. ComSleep: "Sleep",
  213. ComQuit: "Quit",
  214. ComInitDB: "Init DB",
  215. ComQuery: "Query",
  216. ComFieldList: "Field List",
  217. ComCreateDB: "Create DB",
  218. ComDropDB: "Drop DB",
  219. ComRefresh: "Refresh",
  220. ComShutdown: "Shutdown",
  221. ComStatistics: "Statistics",
  222. ComProcessInfo: "Processlist",
  223. ComConnect: "Connect",
  224. ComProcessKill: "Kill",
  225. ComDebug: "Debug",
  226. ComPing: "Ping",
  227. ComTime: "Time",
  228. ComDelayedInsert: "Delayed Insert",
  229. ComChangeUser: "Change User",
  230. ComBinlogDump: "Binlog Dump",
  231. ComTableDump: "Table Dump",
  232. ComConnectOut: "Connect out",
  233. ComRegisterSlave: "Register Slave",
  234. ComStmtPrepare: "Prepare",
  235. ComStmtExecute: "Execute",
  236. ComStmtSendLongData: "Long Data",
  237. ComStmtClose: "Close stmt",
  238. ComStmtReset: "Reset stmt",
  239. ComSetOption: "Set option",
  240. ComStmtFetch: "Fetch",
  241. ComDaemon: "Daemon",
  242. ComBinlogDumpGtid: "Binlog Dump",
  243. ComResetConnection: "Reset connect",
  244. }
  245. // DefaultSQLMode for GLOBAL_VARIABLES
  246. const DefaultSQLMode = "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
  247. // DefaultLengthOfMysqlTypes is the map for default physical length of MySQL data types.
  248. // See http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html
  249. var DefaultLengthOfMysqlTypes = map[byte]int{
  250. TypeYear: 1,
  251. TypeDate: 3,
  252. TypeDuration: 3,
  253. TypeDatetime: 8,
  254. TypeTimestamp: 4,
  255. TypeTiny: 1,
  256. TypeShort: 2,
  257. TypeInt24: 3,
  258. TypeLong: 4,
  259. TypeLonglong: 8,
  260. TypeFloat: 4,
  261. TypeDouble: 8,
  262. TypeEnum: 2,
  263. TypeString: 1,
  264. TypeSet: 8,
  265. }
  266. // DefaultLengthOfTimeFraction is the map for default physical length of time fractions.
  267. var DefaultLengthOfTimeFraction = map[int]int{
  268. 0: 0,
  269. 1: 1,
  270. 2: 1,
  271. 3: 2,
  272. 4: 2,
  273. 5: 3,
  274. 6: 3,
  275. }
  276. // SQLMode is the type for MySQL sql_mode.
  277. // See https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
  278. type SQLMode int64
  279. // HasNoZeroDateMode detects if 'NO_ZERO_DATE' mode is set in SQLMode
  280. func (m SQLMode) HasNoZeroDateMode() bool {
  281. return m&ModeNoZeroDate == ModeNoZeroDate
  282. }
  283. // HasNoZeroInDateMode detects if 'NO_ZERO_IN_DATE' mode is set in SQLMode
  284. func (m SQLMode) HasNoZeroInDateMode() bool {
  285. return m&ModeNoZeroInDate == ModeNoZeroInDate
  286. }
  287. // HasErrorForDivisionByZeroMode detects if 'ERROR_FOR_DIVISION_BY_ZERO' mode is set in SQLMode
  288. func (m SQLMode) HasErrorForDivisionByZeroMode() bool {
  289. return m&ModeErrorForDivisionByZero == ModeErrorForDivisionByZero
  290. }
  291. // HasOnlyFullGroupBy detects if 'ONLY_FULL_GROUP_BY' mode is set in SQLMode
  292. func (m SQLMode) HasOnlyFullGroupBy() bool {
  293. return m&ModeOnlyFullGroupBy == ModeOnlyFullGroupBy
  294. }
  295. // HasStrictMode detects if 'STRICT_TRANS_TABLES' or 'STRICT_ALL_TABLES' mode is set in SQLMode
  296. func (m SQLMode) HasStrictMode() bool {
  297. return m&ModeStrictTransTables == ModeStrictTransTables || m&ModeStrictAllTables == ModeStrictAllTables
  298. }
  299. // HasPipesAsConcatMode detects if 'PIPES_AS_CONCAT' mode is set in SQLMode
  300. func (m SQLMode) HasPipesAsConcatMode() bool {
  301. return m&ModePipesAsConcat == ModePipesAsConcat
  302. }
  303. // HasNoUnsignedSubtractionMode detects if 'NO_UNSIGNED_SUBTRACTION' mode is set in SQLMode
  304. func (m SQLMode) HasNoUnsignedSubtractionMode() bool {
  305. return m&ModeNoUnsignedSubtraction == ModeNoUnsignedSubtraction
  306. }
  307. // HasHighNotPrecedenceMode detects if 'HIGH_NOT_PRECEDENCE' mode is set in SQLMode
  308. func (m SQLMode) HasHighNotPrecedenceMode() bool {
  309. return m&ModeHighNotPrecedence == ModeHighNotPrecedence
  310. }
  311. // HasANSIQuotesMode detects if 'ANSI_QUOTES' mode is set in SQLMode
  312. func (m SQLMode) HasANSIQuotesMode() bool {
  313. return m&ModeANSIQuotes == ModeANSIQuotes
  314. }
  315. // HasRealAsFloatMode detects if 'REAL_AS_FLOAT' mode is set in SQLMode
  316. func (m SQLMode) HasRealAsFloatMode() bool {
  317. return m&ModeRealAsFloat == ModeRealAsFloat
  318. }
  319. // HasPadCharToFullLengthMode detects if 'PAD_CHAR_TO_FULL_LENGTH' mode is set in SQLMode
  320. func (m SQLMode) HasPadCharToFullLengthMode() bool {
  321. return m&ModePadCharToFullLength == ModePadCharToFullLength
  322. }
  323. // HasNoBackslashEscapesMode detects if 'NO_BACKSLASH_ESCAPES' mode is set in SQLMode
  324. func (m SQLMode) HasNoBackslashEscapesMode() bool {
  325. return m&ModeNoBackslashEscapes == ModeNoBackslashEscapes
  326. }
  327. // HasIgnoreSpaceMode detects if 'IGNORE_SPACE' mode is set in SQLMode
  328. func (m SQLMode) HasIgnoreSpaceMode() bool {
  329. return m&ModeIgnoreSpace == ModeIgnoreSpace
  330. }
  331. // HasNoAutoCreateUserMode detects if 'NO_AUTO_CREATE_USER' mode is set in SQLMode
  332. func (m SQLMode) HasNoAutoCreateUserMode() bool {
  333. return m&ModeNoAutoCreateUser == ModeNoAutoCreateUser
  334. }
  335. // HasAllowInvalidDatesMode detects if 'ALLOW_INVALID_DATES' mode is set in SQLMode
  336. func (m SQLMode) HasAllowInvalidDatesMode() bool {
  337. return m&ModeAllowInvalidDates == ModeAllowInvalidDates
  338. }
  339. // consts for sql modes.
  340. // see https://dev.mysql.com/doc/internals/en/query-event.html#q-sql-mode-code
  341. const (
  342. ModeRealAsFloat SQLMode = 1 << iota
  343. ModePipesAsConcat
  344. ModeANSIQuotes
  345. ModeIgnoreSpace
  346. ModeNotUsed
  347. ModeOnlyFullGroupBy
  348. ModeNoUnsignedSubtraction
  349. ModeNoDirInCreate
  350. ModePostgreSQL
  351. ModeOracle
  352. ModeMsSQL
  353. ModeDb2
  354. ModeMaxdb
  355. ModeNoKeyOptions
  356. ModeNoTableOptions
  357. ModeNoFieldOptions
  358. ModeMySQL323
  359. ModeMySQL40
  360. ModeANSI
  361. ModeNoAutoValueOnZero
  362. ModeNoBackslashEscapes
  363. ModeStrictTransTables
  364. ModeStrictAllTables
  365. ModeNoZeroInDate
  366. ModeNoZeroDate
  367. ModeInvalidDates
  368. ModeErrorForDivisionByZero
  369. ModeTraditional
  370. ModeNoAutoCreateUser
  371. ModeHighNotPrecedence
  372. ModeNoEngineSubstitution
  373. ModePadCharToFullLength
  374. ModeAllowInvalidDates
  375. ModeNone = 0
  376. )
  377. // FormatSQLModeStr re-format 'SQL_MODE' variable.
  378. func FormatSQLModeStr(s string) string {
  379. s = strings.ToUpper(strings.TrimRight(s, " "))
  380. parts := strings.Split(s, ",")
  381. var nonEmptyParts []string
  382. existParts := make(map[string]string)
  383. for _, part := range parts {
  384. if len(part) == 0 {
  385. continue
  386. }
  387. if modeParts, ok := CombinationSQLMode[part]; ok {
  388. for _, modePart := range modeParts {
  389. if _, exist := existParts[modePart]; !exist {
  390. nonEmptyParts = append(nonEmptyParts, modePart)
  391. existParts[modePart] = modePart
  392. }
  393. }
  394. }
  395. if _, exist := existParts[part]; !exist {
  396. nonEmptyParts = append(nonEmptyParts, part)
  397. existParts[part] = part
  398. }
  399. }
  400. return strings.Join(nonEmptyParts, ",")
  401. }
  402. // GetSQLMode gets the sql mode for string literal. SQL_mode is a list of different modes separated by commas.
  403. // The input string must be formatted by 'FormatSQLModeStr'
  404. func GetSQLMode(s string) (SQLMode, error) {
  405. strs := strings.Split(s, ",")
  406. var sqlMode SQLMode
  407. for i, length := 0, len(strs); i < length; i++ {
  408. mode, ok := Str2SQLMode[strs[i]]
  409. if !ok && strs[i] != "" {
  410. return sqlMode, newInvalidModeErr(strs[i])
  411. }
  412. sqlMode = sqlMode | mode
  413. }
  414. return sqlMode, nil
  415. }
  416. // Str2SQLMode is the string represent of sql_mode to sql_mode map.
  417. var Str2SQLMode = map[string]SQLMode{
  418. "REAL_AS_FLOAT": ModeRealAsFloat,
  419. "PIPES_AS_CONCAT": ModePipesAsConcat,
  420. "ANSI_QUOTES": ModeANSIQuotes,
  421. "IGNORE_SPACE": ModeIgnoreSpace,
  422. "NOT_USED": ModeNotUsed,
  423. "ONLY_FULL_GROUP_BY": ModeOnlyFullGroupBy,
  424. "NO_UNSIGNED_SUBTRACTION": ModeNoUnsignedSubtraction,
  425. "NO_DIR_IN_CREATE": ModeNoDirInCreate,
  426. "POSTGRESQL": ModePostgreSQL,
  427. "ORACLE": ModeOracle,
  428. "MSSQL": ModeMsSQL,
  429. "DB2": ModeDb2,
  430. "MAXDB": ModeMaxdb,
  431. "NO_KEY_OPTIONS": ModeNoKeyOptions,
  432. "NO_TABLE_OPTIONS": ModeNoTableOptions,
  433. "NO_FIELD_OPTIONS": ModeNoFieldOptions,
  434. "MYSQL323": ModeMySQL323,
  435. "MYSQL40": ModeMySQL40,
  436. "ANSI": ModeANSI,
  437. "NO_AUTO_VALUE_ON_ZERO": ModeNoAutoValueOnZero,
  438. "NO_BACKSLASH_ESCAPES": ModeNoBackslashEscapes,
  439. "STRICT_TRANS_TABLES": ModeStrictTransTables,
  440. "STRICT_ALL_TABLES": ModeStrictAllTables,
  441. "NO_ZERO_IN_DATE": ModeNoZeroInDate,
  442. "NO_ZERO_DATE": ModeNoZeroDate,
  443. "INVALID_DATES": ModeInvalidDates,
  444. "ERROR_FOR_DIVISION_BY_ZERO": ModeErrorForDivisionByZero,
  445. "TRADITIONAL": ModeTraditional,
  446. "NO_AUTO_CREATE_USER": ModeNoAutoCreateUser,
  447. "HIGH_NOT_PRECEDENCE": ModeHighNotPrecedence,
  448. "NO_ENGINE_SUBSTITUTION": ModeNoEngineSubstitution,
  449. "PAD_CHAR_TO_FULL_LENGTH": ModePadCharToFullLength,
  450. "ALLOW_INVALID_DATES": ModeAllowInvalidDates,
  451. }
  452. // CombinationSQLMode is the special modes that provided as shorthand for combinations of mode values.
  453. // See https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-combo.
  454. var CombinationSQLMode = map[string][]string{
  455. "ANSI": {"REAL_AS_FLOAT", "PIPES_AS_CONCAT", "ANSI_QUOTES", "IGNORE_SPACE", "ONLY_FULL_GROUP_BY"},
  456. "DB2": {"PIPES_AS_CONCAT", "ANSI_QUOTES", "IGNORE_SPACE", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS"},
  457. "MAXDB": {"PIPES_AS_CONCAT", "ANSI_QUOTES", "IGNORE_SPACE", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS", "NO_AUTO_CREATE_USER"},
  458. "MSSQL": {"PIPES_AS_CONCAT", "ANSI_QUOTES", "IGNORE_SPACE", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS"},
  459. "MYSQL323": {"MYSQL323", "HIGH_NOT_PRECEDENCE"},
  460. "MYSQL40": {"MYSQL40", "HIGH_NOT_PRECEDENCE"},
  461. "ORACLE": {"PIPES_AS_CONCAT", "ANSI_QUOTES", "IGNORE_SPACE", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS", "NO_AUTO_CREATE_USER"},
  462. "POSTGRESQL": {"PIPES_AS_CONCAT", "ANSI_QUOTES", "IGNORE_SPACE", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS"},
  463. "TRADITIONAL": {"STRICT_TRANS_TABLES", "STRICT_ALL_TABLES", "NO_ZERO_IN_DATE", "NO_ZERO_DATE", "ERROR_FOR_DIVISION_BY_ZERO", "NO_AUTO_CREATE_USER", "NO_ENGINE_SUBSTITUTION"},
  464. }
  465. // FormatFunc is the locale format function signature.
  466. type FormatFunc func(string, string) (string, error)
  467. // GetLocaleFormatFunction get the format function for sepcific locale.
  468. func GetLocaleFormatFunction(loc string) FormatFunc {
  469. locale, exist := locale2FormatFunction[loc]
  470. if !exist {
  471. return formatNotSupport
  472. }
  473. return locale
  474. }
  475. // locale2FormatFunction is the string represent of locale format function.
  476. var locale2FormatFunction = map[string]FormatFunc{
  477. "en_US": formatENUS,
  478. "zh_CN": formatZHCN,
  479. }
  480. // PriorityEnum is defined for Priority const values.
  481. type PriorityEnum int
  482. // Priority const values.
  483. // See https://dev.mysql.com/doc/refman/5.7/en/insert.html
  484. const (
  485. NoPriority PriorityEnum = iota
  486. LowPriority
  487. HighPriority
  488. DelayedPriority
  489. )
  490. // Priority2Str is used to convert the statement priority to string.
  491. var Priority2Str = map[PriorityEnum]string{
  492. NoPriority: "NO_PRIORITY",
  493. LowPriority: "LOW_PRIORITY",
  494. HighPriority: "HIGH_PRIORITY",
  495. DelayedPriority: "DELAYED",
  496. }
  497. // Str2Priority is used to convert a string to a priority.
  498. func Str2Priority(val string) PriorityEnum {
  499. val = strings.ToUpper(val)
  500. switch val {
  501. case "NO_PRIORITY":
  502. return NoPriority
  503. case "HIGH_PRIORITY":
  504. return HighPriority
  505. case "LOW_PRIORITY":
  506. return LowPriority
  507. case "DELAYED":
  508. return DelayedPriority
  509. default:
  510. return NoPriority
  511. }
  512. }
  513. // Restore implements Node interface.
  514. func (n *PriorityEnum) Restore(ctx *format.RestoreCtx) error {
  515. switch *n {
  516. case NoPriority:
  517. return nil
  518. case LowPriority:
  519. ctx.WriteKeyWord("LOW_PRIORITY")
  520. case HighPriority:
  521. ctx.WriteKeyWord("HIGH_PRIORITY")
  522. case DelayedPriority:
  523. ctx.WriteKeyWord("DELAYED")
  524. default:
  525. return errors.Errorf("undefined PriorityEnum Type[%d]", *n)
  526. }
  527. return nil
  528. }
  529. // PrimaryKeyName defines primary key name.
  530. const (
  531. PrimaryKeyName = "PRIMARY"
  532. )