-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
constants.go
795 lines (670 loc) · 25.7 KB
/
constants.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysql
import (
"strconv"
"strings"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/simplifiedchinese"
"vitess.io/vitess/go/mysql/binlog"
)
const (
// MaxPacketSize is the maximum payload length of a packet
// the server supports.
MaxPacketSize = (1 << 24) - 1
// protocolVersion is the current version of the protocol.
// Always 10.
protocolVersion = 10
// https://dev.mysql.com/doc/refman/en/identifier-length.html
MaxIdentifierLength = 64
)
// AuthMethodDescription is the type for different supported and
// implemented authentication methods.
type AuthMethodDescription string
// Supported auth forms.
const (
// MysqlNativePassword uses a salt and transmits a hash on the wire.
MysqlNativePassword = AuthMethodDescription("mysql_native_password")
// MysqlClearPassword transmits the password in the clear.
MysqlClearPassword = AuthMethodDescription("mysql_clear_password")
// CachingSha2Password uses a salt and transmits a SHA256 hash on the wire.
CachingSha2Password = AuthMethodDescription("caching_sha2_password")
// MysqlDialog uses the dialog plugin on the client side.
// It transmits data in the clear.
MysqlDialog = AuthMethodDescription("dialog")
)
// Capability flags.
// Originally found in include/mysql/mysql_com.h
const (
// CapabilityClientLongPassword is CLIENT_LONG_PASSWORD.
// New more secure passwords. Assumed to be set since 4.1.1.
// We do not check this anywhere.
CapabilityClientLongPassword = 1
// CapabilityClientFoundRows is CLIENT_FOUND_ROWS.
CapabilityClientFoundRows = 1 << 1
// CapabilityClientLongFlag is CLIENT_LONG_FLAG.
// Longer flags in Protocol::ColumnDefinition320.
// Set it everywhere, not used, as we use Protocol::ColumnDefinition41.
CapabilityClientLongFlag = 1 << 2
// CapabilityClientConnectWithDB is CLIENT_CONNECT_WITH_DB.
// One can specify db on connect.
CapabilityClientConnectWithDB = 1 << 3
// CLIENT_NO_SCHEMA 1 << 4
// Do not permit database.table.column. We do permit it.
// CLIENT_COMPRESS 1 << 5
// We do not support compression. CPU is usually our bottleneck.
// CLIENT_ODBC 1 << 6
// No special behavior since 3.22.
// CLIENT_LOCAL_FILES 1 << 7
// Client can use LOCAL INFILE request of LOAD DATA|XML.
// We do not set it.
// CLIENT_IGNORE_SPACE 1 << 8
// Parser can ignore spaces before '('.
// We ignore this.
// CapabilityClientProtocol41 is CLIENT_PROTOCOL_41.
// New 4.1 protocol. Enforced everywhere.
CapabilityClientProtocol41 = 1 << 9
// CLIENT_INTERACTIVE 1 << 10
// Not specified, ignored.
// CapabilityClientSSL is CLIENT_SSL.
// Switch to SSL after handshake.
CapabilityClientSSL = 1 << 11
// CLIENT_IGNORE_SIGPIPE 1 << 12
// Do not issue SIGPIPE if network failures occur (libmysqlclient only).
// CapabilityClientTransactions is CLIENT_TRANSACTIONS.
// Can send status flags in EOF_Packet.
// This flag is optional in 3.23, but always set by the server since 4.0.
// We just do it all the time.
CapabilityClientTransactions = 1 << 13
// CLIENT_RESERVED 1 << 14
// CapabilityClientSecureConnection is CLIENT_SECURE_CONNECTION.
// New 4.1 authentication. Always set, expected, never checked.
CapabilityClientSecureConnection = 1 << 15
// CapabilityClientMultiStatements is CLIENT_MULTI_STATEMENTS
// Can handle multiple statements per COM_QUERY and COM_STMT_PREPARE.
CapabilityClientMultiStatements = 1 << 16
// CapabilityClientMultiResults is CLIENT_MULTI_RESULTS
// Can send multiple resultsets for COM_QUERY.
CapabilityClientMultiResults = 1 << 17
// CapabilityClientPluginAuth is CLIENT_PLUGIN_AUTH.
// Client supports plugin authentication.
CapabilityClientPluginAuth = 1 << 19
// CapabilityClientConnAttr is CLIENT_CONNECT_ATTRS
// Permits connection attributes in Protocol::HandshakeResponse41.
CapabilityClientConnAttr = 1 << 20
// CapabilityClientPluginAuthLenencClientData is CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
CapabilityClientPluginAuthLenencClientData = 1 << 21
// CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS 1 << 22
// Announces support for expired password extension.
// Not yet supported.
// CLIENT_SESSION_TRACK 1 << 23
// Can set ServerSessionStateChanged in the Status Flags
// and send session-state change data after a OK packet.
// Not yet supported.
CapabilityClientSessionTrack = 1 << 23
// CapabilityClientDeprecateEOF is CLIENT_DEPRECATE_EOF
// Expects an OK (instead of EOF) after the resultset rows of a Text Resultset.
CapabilityClientDeprecateEOF = 1 << 24
)
// Status flags. They are returned by the server in a few cases.
// Originally found in include/mysql/mysql_com.h
// See http://dev.mysql.com/doc/internals/en/status-flags.html
const (
// a transaction is active
ServerStatusInTrans uint16 = 0x0001
NoServerStatusInTrans uint16 = 0xFFFE
// auto-commit is enabled
ServerStatusAutocommit uint16 = 0x0002
NoServerStatusAutocommit uint16 = 0xFFFD
ServerMoreResultsExists uint16 = 0x0008
ServerStatusNoGoodIndexUsed uint16 = 0x0010
ServerStatusNoIndexUsed uint16 = 0x0020
// Used by Binary Protocol Resultset to signal that COM_STMT_FETCH must be used to fetch the row-data.
ServerStatusCursorExists uint16 = 0x0040
ServerStatusLastRowSent uint16 = 0x0080
ServerStatusDbDropped uint16 = 0x0100
ServerStatusNoBackslashEscapes uint16 = 0x0200
ServerStatusMetadataChanged uint16 = 0x0400
ServerQueryWasSlow uint16 = 0x0800
ServerPsOutParams uint16 = 0x1000
// in a read-only transaction
ServerStatusInTransReadonly uint16 = 0x2000
// connection state information has changed
ServerSessionStateChanged uint16 = 0x4000
)
// State Change Information
const (
// one or more system variables changed.
SessionTrackSystemVariables uint8 = 0x00
// schema changed.
SessionTrackSchema uint8 = 0x01
// "track state change" changed.
SessionTrackStateChange uint8 = 0x02
// "track GTIDs" changed.
SessionTrackGtids uint8 = 0x03
)
// Packet types.
// Originally found in include/mysql/mysql_com.h
const (
// ComQuit is COM_QUIT.
ComQuit = 0x01
// ComInitDB is COM_INIT_DB.
ComInitDB = 0x02
// ComQuery is COM_QUERY.
ComQuery = 0x03
// ComFieldList is COM_Field_List.
ComFieldList = 0x04
// ComPing is COM_PING.
ComPing = 0x0e
// ComBinlogDump is COM_BINLOG_DUMP.
ComBinlogDump = 0x12
// ComSemiSyncAck is SEMI_SYNC_ACK.
ComSemiSyncAck = 0xef
// ComPrepare is COM_PREPARE.
ComPrepare = 0x16
// ComStmtExecute is COM_STMT_EXECUTE.
ComStmtExecute = 0x17
// ComStmtSendLongData is COM_STMT_SEND_LONG_DATA
ComStmtSendLongData = 0x18
// ComStmtClose is COM_STMT_CLOSE.
ComStmtClose = 0x19
// ComStmtReset is COM_STMT_RESET
ComStmtReset = 0x1a
//ComStmtFetch is COM_STMT_FETCH
ComStmtFetch = 0x1c
// ComSetOption is COM_SET_OPTION
ComSetOption = 0x1b
// ComResetConnection is COM_RESET_CONNECTION
ComResetConnection = 0x1f
// ComBinlogDumpGTID is COM_BINLOG_DUMP_GTID.
ComBinlogDumpGTID = 0x1e
// ComRegisterReplica is COM_REGISTER_SLAVE
// https://dev.mysql.com/doc/internals/en/com-register-slave.html
ComRegisterReplica = 0x15
// OKPacket is the header of the OK packet.
OKPacket = 0x00
// EOFPacket is the header of the EOF packet.
EOFPacket = 0xfe
// ErrPacket is the header of the error packet.
ErrPacket = 0xff
// NullValue is the encoded value of NULL.
NullValue = 0xfb
)
// Auth packet types
const (
// AuthMoreDataPacket is sent when server requires more data to authenticate
AuthMoreDataPacket = 0x01
// CachingSha2FastAuth is sent before OKPacket when server authenticates using cache
CachingSha2FastAuth = 0x03
// CachingSha2FullAuth is sent when server requests un-scrambled password to authenticate
CachingSha2FullAuth = 0x04
// AuthSwitchRequestPacket is used to switch auth method.
AuthSwitchRequestPacket = 0xfe
)
// Error codes for client-side errors.
// Originally found in include/mysql/errmsg.h and
// https://dev.mysql.com/doc/mysql-errors/en/client-error-reference.html
const (
// CRUnknownError is CR_UNKNOWN_ERROR
CRUnknownError = ErrorCode(2000)
// CRConnectionError is CR_CONNECTION_ERROR
// This is returned if a connection via a Unix socket fails.
CRConnectionError = ErrorCode(2002)
// CRConnHostError is CR_CONN_HOST_ERROR
// This is returned if a connection via a TCP socket fails.
CRConnHostError = ErrorCode(2003)
// CRUnknownHost is CR_UNKNOWN_HOST
// This is returned if the host name cannot be resolved.
CRUnknownHost = ErrorCode(2005)
// CRServerGone is CR_SERVER_GONE_ERROR.
// This is returned if the client tries to send a command but it fails.
CRServerGone = ErrorCode(2006)
// CRVersionError is CR_VERSION_ERROR
// This is returned if the server versions don't match what we support.
CRVersionError = ErrorCode(2007)
// CRServerHandshakeErr is CR_SERVER_HANDSHAKE_ERR
CRServerHandshakeErr = ErrorCode(2012)
// CRServerLost is CR_SERVER_LOST.
// Used when:
// - the client cannot write an initial auth packet.
// - the client cannot read an initial auth packet.
// - the client cannot read a response from the server.
// This happens when a running query is killed.
CRServerLost = ErrorCode(2013)
// CRCommandsOutOfSync is CR_COMMANDS_OUT_OF_SYNC
// Sent when the streaming calls are not done in the right order.
CRCommandsOutOfSync = ErrorCode(2014)
// CRNamedPipeStateError is CR_NAMEDPIPESETSTATE_ERROR.
// This is the highest possible number for a connection error.
CRNamedPipeStateError = ErrorCode(2018)
// CRCantReadCharset is CR_CANT_READ_CHARSET
CRCantReadCharset = ErrorCode(2019)
// CRSSLConnectionError is CR_SSL_CONNECTION_ERROR
CRSSLConnectionError = ErrorCode(2026)
// CRMalformedPacket is CR_MALFORMED_PACKET
CRMalformedPacket = ErrorCode(2027)
)
type ErrorCode uint16
func (e ErrorCode) ToString() string {
return strconv.FormatUint(uint64(e), 10)
}
// Error codes for server-side errors.
// Originally found in include/mysql/mysqld_error.h and
// https://dev.mysql.com/doc/mysql-errors/en/server-error-reference.html
// The below are in sorted order by value, grouped by vterror code they should be bucketed into.
// See above reference for more information on each code.
const (
// Vitess specific errors, (100-999)
ERNotReplica = ErrorCode(100)
// unknown
ERUnknownError = ErrorCode(1105)
// internal
ERInternalError = ErrorCode(1815)
// unimplemented
ERNotSupportedYet = ErrorCode(1235)
ERUnsupportedPS = ErrorCode(1295)
// resource exhausted
ERDiskFull = ErrorCode(1021)
EROutOfMemory = ErrorCode(1037)
EROutOfSortMemory = ErrorCode(1038)
ERConCount = ErrorCode(1040)
EROutOfResources = ErrorCode(1041)
ERRecordFileFull = ErrorCode(1114)
ERHostIsBlocked = ErrorCode(1129)
ERCantCreateThread = ErrorCode(1135)
ERTooManyDelayedThreads = ErrorCode(1151)
ERNetPacketTooLarge = ErrorCode(1153)
ERTooManyUserConnections = ErrorCode(1203)
ERLockTableFull = ErrorCode(1206)
ERUserLimitReached = ErrorCode(1226)
// deadline exceeded
ERLockWaitTimeout = ErrorCode(1205)
// unavailable
ERServerShutdown = ErrorCode(1053)
// not found
ERDbDropExists = ErrorCode(1008)
ERCantFindFile = ErrorCode(1017)
ERFormNotFound = ErrorCode(1029)
ERKeyNotFound = ErrorCode(1032)
ERBadFieldError = ErrorCode(1054)
ERNoSuchThread = ErrorCode(1094)
ERUnknownTable = ErrorCode(1109)
ERCantFindUDF = ErrorCode(1122)
ERNonExistingGrant = ErrorCode(1141)
ERNoSuchTable = ErrorCode(1146)
ERNonExistingTableGrant = ErrorCode(1147)
ERKeyDoesNotExist = ErrorCode(1176)
// permissions
ERDBAccessDenied = ErrorCode(1044)
ERAccessDeniedError = ErrorCode(1045)
ERKillDenied = ErrorCode(1095)
ERNoPermissionToCreateUsers = ErrorCode(1211)
ERSpecifiedAccessDenied = ErrorCode(1227)
// failed precondition
ERNoDb = ErrorCode(1046)
ERNoSuchIndex = ErrorCode(1082)
ERCantDropFieldOrKey = ErrorCode(1091)
ERTableNotLockedForWrite = ErrorCode(1099)
ERTableNotLocked = ErrorCode(1100)
ERTooBigSelect = ErrorCode(1104)
ERNotAllowedCommand = ErrorCode(1148)
ERTooLongString = ErrorCode(1162)
ERDelayedInsertTableLocked = ErrorCode(1165)
ERDupUnique = ErrorCode(1169)
ERRequiresPrimaryKey = ErrorCode(1173)
ERCantDoThisDuringAnTransaction = ErrorCode(1179)
ERReadOnlyTransaction = ErrorCode(1207)
ERCannotAddForeign = ErrorCode(1215)
ERNoReferencedRow = ErrorCode(1216)
ERRowIsReferenced = ErrorCode(1217)
ERCantUpdateWithReadLock = ErrorCode(1223)
ERNoDefault = ErrorCode(1230)
ERMasterFatalReadingBinlog = ErrorCode(1236)
EROperandColumns = ErrorCode(1241)
ERSubqueryNo1Row = ErrorCode(1242)
ERUnknownStmtHandler = ErrorCode(1243)
ERWarnDataOutOfRange = ErrorCode(1264)
ERNonUpdateableTable = ErrorCode(1288)
ERFeatureDisabled = ErrorCode(1289)
EROptionPreventsStatement = ErrorCode(1290)
ERDuplicatedValueInType = ErrorCode(1291)
ERSPDoesNotExist = ErrorCode(1305)
ERNoDefaultForField = ErrorCode(1364)
ErSPNotVarArg = ErrorCode(1414)
ERRowIsReferenced2 = ErrorCode(1451)
ErNoReferencedRow2 = ErrorCode(1452)
ERDupIndex = ErrorCode(1831)
ERInnodbReadOnly = ErrorCode(1874)
// already exists
ERDbCreateExists = ErrorCode(1007)
ERTableExists = ErrorCode(1050)
ERDupEntry = ErrorCode(1062)
ERFileExists = ErrorCode(1086)
ERUDFExists = ErrorCode(1125)
// aborted
ERGotSignal = ErrorCode(1078)
ERForcingClose = ErrorCode(1080)
ERAbortingConnection = ErrorCode(1152)
ERLockDeadlock = ErrorCode(1213)
// invalid arg
ERUnknownComError = ErrorCode(1047)
ERBadNullError = ErrorCode(1048)
ERBadDb = ErrorCode(1049)
ERBadTable = ErrorCode(1051)
ERNonUniq = ErrorCode(1052)
ERWrongFieldWithGroup = ErrorCode(1055)
ERWrongGroupField = ErrorCode(1056)
ERWrongSumSelect = ErrorCode(1057)
ERWrongValueCount = ErrorCode(1058)
ERTooLongIdent = ErrorCode(1059)
ERDupFieldName = ErrorCode(1060)
ERDupKeyName = ErrorCode(1061)
ERWrongFieldSpec = ErrorCode(1063)
ERParseError = ErrorCode(1064)
EREmptyQuery = ErrorCode(1065)
ERNonUniqTable = ErrorCode(1066)
ERInvalidDefault = ErrorCode(1067)
ERMultiplePriKey = ErrorCode(1068)
ERTooManyKeys = ErrorCode(1069)
ERTooManyKeyParts = ErrorCode(1070)
ERTooLongKey = ErrorCode(1071)
ERKeyColumnDoesNotExist = ErrorCode(1072)
ERBlobUsedAsKey = ErrorCode(1073)
ERTooBigFieldLength = ErrorCode(1074)
ERWrongAutoKey = ErrorCode(1075)
ERWrongFieldTerminators = ErrorCode(1083)
ERBlobsAndNoTerminated = ErrorCode(1084)
ERTextFileNotReadable = ErrorCode(1085)
ERWrongSubKey = ErrorCode(1089)
ERCantRemoveAllFields = ErrorCode(1090)
ERUpdateTableUsed = ErrorCode(1093)
ERNoTablesUsed = ErrorCode(1096)
ERTooBigSet = ErrorCode(1097)
ERBlobCantHaveDefault = ErrorCode(1101)
ERWrongDbName = ErrorCode(1102)
ERWrongTableName = ErrorCode(1103)
ERUnknownProcedure = ErrorCode(1106)
ERWrongParamCountToProcedure = ErrorCode(1107)
ERWrongParametersToProcedure = ErrorCode(1108)
ERFieldSpecifiedTwice = ErrorCode(1110)
ERInvalidGroupFuncUse = ErrorCode(1111)
ERTableMustHaveColumns = ErrorCode(1113)
ERUnknownCharacterSet = ErrorCode(1115)
ERTooManyTables = ErrorCode(1116)
ERTooManyFields = ErrorCode(1117)
ERTooBigRowSize = ErrorCode(1118)
ERWrongOuterJoin = ErrorCode(1120)
ERNullColumnInIndex = ErrorCode(1121)
ERFunctionNotDefined = ErrorCode(1128)
ERWrongValueCountOnRow = ErrorCode(1136)
ERInvalidUseOfNull = ErrorCode(1138)
ERRegexpError = ErrorCode(1139)
ERMixOfGroupFuncAndFields = ErrorCode(1140)
ERIllegalGrantForTable = ErrorCode(1144)
ERSyntaxError = ErrorCode(1149)
ERWrongColumnName = ErrorCode(1166)
ERWrongKeyColumn = ErrorCode(1167)
ERBlobKeyWithoutLength = ErrorCode(1170)
ERPrimaryCantHaveNull = ErrorCode(1171)
ERTooManyRows = ErrorCode(1172)
ERLockOrActiveTransaction = ErrorCode(1192)
ERUnknownSystemVariable = ErrorCode(1193)
ERSetConstantsOnly = ErrorCode(1204)
ERWrongArguments = ErrorCode(1210)
ERWrongUsage = ErrorCode(1221)
ERWrongNumberOfColumnsInSelect = ErrorCode(1222)
ERDupArgument = ErrorCode(1225)
ERLocalVariable = ErrorCode(1228)
ERGlobalVariable = ErrorCode(1229)
ERWrongValueForVar = ErrorCode(1231)
ERWrongTypeForVar = ErrorCode(1232)
ERVarCantBeRead = ErrorCode(1233)
ERCantUseOptionHere = ErrorCode(1234)
ERIncorrectGlobalLocalVar = ErrorCode(1238)
ERWrongFKDef = ErrorCode(1239)
ERKeyRefDoNotMatchTableRef = ErrorCode(1240)
ERCyclicReference = ErrorCode(1245)
ERIllegalReference = ErrorCode(1247)
ERDerivedMustHaveAlias = ErrorCode(1248)
ERTableNameNotAllowedHere = ErrorCode(1250)
ERCollationCharsetMismatch = ErrorCode(1253)
ERWarnDataTruncated = ErrorCode(1265)
ERCantAggregate2Collations = ErrorCode(1267)
ERCantAggregate3Collations = ErrorCode(1270)
ERCantAggregateNCollations = ErrorCode(1271)
ERVariableIsNotStruct = ErrorCode(1272)
ERUnknownCollation = ErrorCode(1273)
ERWrongNameForIndex = ErrorCode(1280)
ERWrongNameForCatalog = ErrorCode(1281)
ERBadFTColumn = ErrorCode(1283)
ERTruncatedWrongValue = ErrorCode(1292)
ERTooMuchAutoTimestampCols = ErrorCode(1293)
ERInvalidOnUpdate = ErrorCode(1294)
ERUnknownTimeZone = ErrorCode(1298)
ERInvalidCharacterString = ErrorCode(1300)
ERQueryInterrupted = ErrorCode(1317)
ERTruncatedWrongValueForField = ErrorCode(1366)
ERIllegalValueForType = ErrorCode(1367)
ERDataTooLong = ErrorCode(1406)
ErrWrongValueForType = ErrorCode(1411)
ERForbidSchemaChange = ErrorCode(1450)
ERWrongValue = ErrorCode(1525)
ERDataOutOfRange = ErrorCode(1690)
ERInvalidJSONText = ErrorCode(3140)
ERInvalidJSONTextInParams = ErrorCode(3141)
ERInvalidJSONBinaryData = ErrorCode(3142)
ERInvalidJSONCharset = ErrorCode(3144)
ERInvalidCastToJSON = ErrorCode(3147)
ERJSONValueTooBig = ErrorCode(3150)
ERJSONDocumentTooDeep = ErrorCode(3157)
// max execution time exceeded
ERQueryTimeout = ErrorCode(3024)
ErrCantCreateGeometryObject = ErrorCode(1416)
ErrGISDataWrongEndianess = ErrorCode(3055)
ErrNotImplementedForCartesianSRS = ErrorCode(3704)
ErrNotImplementedForProjectedSRS = ErrorCode(3705)
ErrNonPositiveRadius = ErrorCode(3706)
// server not available
ERServerIsntAvailable = ErrorCode(3168)
)
// Sql states for errors.
// Originally found in include/mysql/sql_state.h
const (
// SSUnknownSqlstate is ER_SIGNAL_EXCEPTION in
// include/mysql/sql_state.h, but:
// const char *unknown_sqlstate= "HY000"
// in client.c. So using that one.
SSUnknownSQLState = "HY000"
// SSNetError is network related error
SSNetError = "08S01"
// SSWrongNumberOfColumns is related to columns error
SSWrongNumberOfColumns = "21000"
// SSWrongValueCountOnRow is related to columns count mismatch error
SSWrongValueCountOnRow = "21S01"
// SSDataTooLong is ER_DATA_TOO_LONG
SSDataTooLong = "22001"
// SSDataOutOfRange is ER_DATA_OUT_OF_RANGE
SSDataOutOfRange = "22003"
// SSConstraintViolation is constraint violation
SSConstraintViolation = "23000"
// SSCantDoThisDuringAnTransaction is
// ER_CANT_DO_THIS_DURING_AN_TRANSACTION
SSCantDoThisDuringAnTransaction = "25000"
// SSAccessDeniedError is ER_ACCESS_DENIED_ERROR
SSAccessDeniedError = "28000"
// SSNoDB is ER_NO_DB_ERROR
SSNoDB = "3D000"
// SSLockDeadlock is ER_LOCK_DEADLOCK
SSLockDeadlock = "40001"
// SSClientError is the state on client errors
SSClientError = "42000"
// SSDupFieldName is ER_DUP_FIELD_NAME
SSDupFieldName = "42S21"
// SSBadFieldError is ER_BAD_FIELD_ERROR
SSBadFieldError = "42S22"
// SSUnknownTable is ER_UNKNOWN_TABLE
SSUnknownTable = "42S02"
// SSQueryInterrupted is ER_QUERY_INTERRUPTED;
SSQueryInterrupted = "70100"
)
// CharacterSetEncoding maps a charset name to a golang encoder.
// golang does not support encoders for all MySQL charsets.
// A charset not in this map is unsupported.
// A trivial encoding (e.g. utf8) has a `nil` encoder
var CharacterSetEncoding = map[string]encoding.Encoding{
"cp850": charmap.CodePage850,
"koi8r": charmap.KOI8R,
"latin1": charmap.Windows1252,
"latin2": charmap.ISO8859_2,
"ascii": nil,
"hebrew": charmap.ISO8859_8,
"greek": charmap.ISO8859_7,
"cp1250": charmap.Windows1250,
"gbk": simplifiedchinese.GBK,
"latin5": charmap.ISO8859_9,
"utf8": nil,
"utf8mb3": nil,
"cp866": charmap.CodePage866,
"cp852": charmap.CodePage852,
"latin7": charmap.ISO8859_13,
"utf8mb4": nil,
"cp1251": charmap.Windows1251,
"cp1256": charmap.Windows1256,
"cp1257": charmap.Windows1257,
"binary": nil,
}
// IsNum returns true if a MySQL type is a numeric value.
// It is the same as IS_NUM defined in mysql.h.
func IsNum(typ uint8) bool {
return (typ <= binlog.TypeInt24 && typ != binlog.TypeTimestamp) ||
typ == binlog.TypeYear ||
typ == binlog.TypeNewDecimal
}
// IsConnErr returns true if the error is a connection error.
func IsConnErr(err error) bool {
if IsTooManyConnectionsErr(err) {
return false
}
if sqlErr, ok := err.(*SQLError); ok {
num := sqlErr.Number()
return (num >= CRUnknownError && num <= CRNamedPipeStateError) || num == ERQueryInterrupted
}
return false
}
// IsConnLostDuringQuery returns true if the error is a CRServerLost error.
// Happens most commonly when a query is killed MySQL server-side.
func IsConnLostDuringQuery(err error) bool {
if sqlErr, ok := err.(*SQLError); ok {
num := sqlErr.Number()
return (num == CRServerLost)
}
return false
}
// IsEphemeralError returns true if the error is ephemeral and the caller should
// retry if possible. Note: non-SQL errors are always treated as ephemeral.
func IsEphemeralError(err error) bool {
if sqlErr, ok := err.(*SQLError); ok {
en := sqlErr.Number()
switch en {
case
CRConnectionError,
CRConnHostError,
CRMalformedPacket,
CRNamedPipeStateError,
CRServerHandshakeErr,
CRServerGone,
CRServerLost,
CRSSLConnectionError,
CRUnknownError,
CRUnknownHost,
ERCantCreateThread,
ERDiskFull,
ERForcingClose,
ERGotSignal,
ERHostIsBlocked,
ERLockTableFull,
ERInnodbReadOnly,
ERInternalError,
ERLockDeadlock,
ERLockWaitTimeout,
ERQueryTimeout,
EROutOfMemory,
EROutOfResources,
EROutOfSortMemory,
ERQueryInterrupted,
ERServerIsntAvailable,
ERServerShutdown,
ERTooManyUserConnections,
ERUnknownError,
ERUserLimitReached:
return true
default:
return false
}
}
// If it's not an sqlError then we assume it's ephemeral
return true
}
// IsTooManyConnectionsErr returns true if the error is due to too many connections.
func IsTooManyConnectionsErr(err error) bool {
if sqlErr, ok := err.(*SQLError); ok {
if sqlErr.Number() == CRServerHandshakeErr && strings.Contains(sqlErr.Message, "Too many connections") {
return true
}
}
return false
}
// IsSchemaApplyError returns true when given error is a MySQL error applying schema change
func IsSchemaApplyError(err error) bool {
merr, isSQLErr := err.(*SQLError)
if !isSQLErr {
return false
}
switch merr.Num {
case
ERDupKeyName,
ERCantDropFieldOrKey,
ERTableExists,
ERDupFieldName:
return true
}
return false
}
type ReplicationState int32
const (
ReplicationStateUnknown ReplicationState = iota
ReplicationStateStopped
ReplicationStateConnecting
ReplicationStateRunning
)
// ReplicationStatusToState converts a value you have for the IO thread(s) or SQL
// thread(s) or Group Replication applier thread(s) from MySQL or intermediate
// layers to a mysql.ReplicationState.
// on,yes,true == ReplicationStateRunning
// off,no,false == ReplicationStateStopped
// connecting == ReplicationStateConnecting
// anything else == ReplicationStateUnknown
func ReplicationStatusToState(s string) ReplicationState {
// Group Replication uses ON instead of Yes
switch strings.ToLower(s) {
case "yes", "on", "true":
return ReplicationStateRunning
case "no", "off", "false":
return ReplicationStateStopped
case "connecting":
return ReplicationStateConnecting
default:
return ReplicationStateUnknown
}
}