mysql_common/constants.rs
1// Copyright (c) 2017 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::convert::TryFrom;
10
11pub static MAX_PAYLOAD_LEN: usize = 16_777_215;
12pub static DEFAULT_MAX_ALLOWED_PACKET: usize = 4 * 1024 * 1024;
13pub static MIN_COMPRESS_LENGTH: usize = 50;
14
15pub static UTF8_GENERAL_CI: u16 = 33;
16pub static UTF8MB4_GENERAL_CI: u16 = 45;
17
18my_bitflags! {
19 StatusFlags,
20 #[error("Unknown flags in the raw value of StatusFlags (raw={:b})", _0)]
21 UnknownStatusFlags,
22 u16,
23
24 /// MySql server status flags
25 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
26 pub struct StatusFlags: u16 {
27 /// Is raised when a multi-statement transaction has been started, either explicitly,
28 /// by means of BEGIN or COMMIT AND CHAIN, or implicitly, by the first transactional
29 /// statement, when autocommit=off.
30 const SERVER_STATUS_IN_TRANS = 0x0001;
31
32 /// Server in auto_commit mode.
33 const SERVER_STATUS_AUTOCOMMIT = 0x0002;
34
35 /// Multi query - next query exists.
36 const SERVER_MORE_RESULTS_EXISTS = 0x0008;
37
38 const SERVER_STATUS_NO_GOOD_INDEX_USED = 0x0010;
39
40 const SERVER_STATUS_NO_INDEX_USED = 0x0020;
41
42 /// The server was able to fulfill the clients request and opened a read-only
43 /// non-scrollable cursor for a query. This flag comes in reply to COM_STMT_EXECUTE
44 /// and COM_STMT_FETCH commands. Used by Binary Protocol Resultset to signal that
45 /// COM_STMT_FETCH must be used to fetch the row-data.
46 const SERVER_STATUS_CURSOR_EXISTS = 0x0040;
47
48 /// This flag is sent when a read-only cursor is exhausted, in reply to
49 /// COM_STMT_FETCH command.
50 const SERVER_STATUS_LAST_ROW_SENT = 0x0080;
51
52 /// A database was dropped.
53 const SERVER_STATUS_DB_DROPPED = 0x0100;
54
55 const SERVER_STATUS_NO_BACKSLASH_ESCAPES = 0x0200;
56
57 /// Sent to the client if after a prepared statement reprepare we discovered
58 /// that the new statement returns a different number of result set columns.
59 const SERVER_STATUS_METADATA_CHANGED = 0x0400;
60
61 const SERVER_QUERY_WAS_SLOW = 0x0800;
62
63 /// To mark ResultSet containing output parameter values.
64 const SERVER_PS_OUT_PARAMS = 0x1000;
65
66 /// Set at the same time as SERVER_STATUS_IN_TRANS if the started multi-statement
67 /// transaction is a read-only transaction. Cleared when the transaction commits
68 /// or aborts. Since this flag is sent to clients in OK and EOF packets, the flag
69 /// indicates the transaction status at the end of command execution.
70 const SERVER_STATUS_IN_TRANS_READONLY = 0x2000;
71
72 /// This status flag, when on, implies that one of the state information has
73 /// changed on the server because of the execution of the last statement.
74 const SERVER_SESSION_STATE_CHANGED = 0x4000;
75
76 /// Introduced by mariadb. Contain the information about ANSI_QUOTES SQL_MODE.
77 const SERVER_STATUS_ANSI_QUOTES = 0x8000;
78 }
79}
80
81my_bitflags! {
82 CapabilityFlags,
83 #[error("Unknown flags in the raw value of CapabilityFlags (raw={:b})", _0)]
84 UnknownCapabilityFlags,
85 u32,
86
87 /// Client capability flags
88 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
89 pub struct CapabilityFlags: u32 {
90 /// Use the improved version of Old Password Authentication. Assumed to be set since 4.1.1.
91 const CLIENT_LONG_PASSWORD = 0x0000_0001;
92
93 /// Send found rows instead of affected rows in EOF_Packet.
94 const CLIENT_FOUND_ROWS = 0x0000_0002;
95
96 /// Get all column flags.
97 /// Longer flags in Protocol::ColumnDefinition320.
98 ///
99 /// ### Server
100 /// Supports longer flags.
101 ///
102 /// ### Client
103 /// Expects longer flags.
104 const CLIENT_LONG_FLAG = 0x0000_0004;
105
106 /// Database (schema) name can be specified on connect in Handshake Response Packet.
107 /// ### Server
108 /// Supports schema-name in Handshake Response Packet.
109 ///
110 /// ### Client
111 /// Handshake Response Packet contains a schema-name.
112 const CLIENT_CONNECT_WITH_DB = 0x0000_0008;
113
114 /// Don't allow database.table.column.
115 const CLIENT_NO_SCHEMA = 0x0000_0010;
116
117 /// Compression protocol supported.
118 ///
119 /// ### Server
120 /// Supports compression.
121 ///
122 /// ### Client
123 /// Switches to Compression compressed protocol after successful authentication.
124 const CLIENT_COMPRESS = 0x0000_0020;
125
126 /// Special handling of ODBC behavior.
127 const CLIENT_ODBC = 0x0000_0040;
128
129 /// Can use LOAD DATA LOCAL.
130 ///
131 /// ### Server
132 /// Enables the LOCAL INFILE request of LOAD DATA|XML.
133 ///
134 /// ### Client
135 /// Will handle LOCAL INFILE request.
136 const CLIENT_LOCAL_FILES = 0x0000_0080;
137
138 /// Ignore spaces before '('.
139 ///
140 /// ### Server
141 /// Parser can ignore spaces before '('.
142 ///
143 /// ### Client
144 /// Let the parser ignore spaces before '('.
145 const CLIENT_IGNORE_SPACE = 0x0000_0100;
146
147 const CLIENT_PROTOCOL_41 = 0x0000_0200;
148
149 /// This is an interactive client.
150 /// Use System_variables::net_wait_timeout versus System_variables::net_interactive_timeout.
151 ///
152 /// ### Server
153 /// Supports interactive and noninteractive clients.
154 ///
155 /// ### Client
156 /// Client is interactive.
157 const CLIENT_INTERACTIVE = 0x0000_0400;
158
159 /// Use SSL encryption for the session.
160 ///
161 /// ### Server
162 /// Supports SSL
163 ///
164 /// ### Client
165 /// Switch to SSL after sending the capability-flags.
166 const CLIENT_SSL = 0x0000_0800;
167
168 /// Client only flag. Not used.
169 ///
170 /// ### Client
171 /// Do not issue SIGPIPE if network failures occur (libmysqlclient only).
172 const CLIENT_IGNORE_SIGPIPE = 0x0000_1000;
173
174 /// Client knows about transactions.
175 ///
176 /// ### Server
177 /// Can send status flags in OK_Packet / EOF_Packet.
178 ///
179 /// ### Client
180 /// Expects status flags in OK_Packet / EOF_Packet.
181 ///
182 /// ### Note
183 /// This flag is optional in 3.23, but always set by the server since 4.0.
184 const CLIENT_TRANSACTIONS = 0x0000_2000;
185
186 const CLIENT_RESERVED = 0x0000_4000;
187
188 const CLIENT_SECURE_CONNECTION = 0x0000_8000;
189
190 /// Enable/disable multi-stmt support.
191 /// Also sets CLIENT_MULTI_RESULTS. Currently not checked anywhere.
192 ///
193 /// ### Server
194 /// Can handle multiple statements per COM_QUERY and COM_STMT_PREPARE.
195 ///
196 /// ### Client
197 /// May send multiple statements per COM_QUERY and COM_STMT_PREPARE.
198 const CLIENT_MULTI_STATEMENTS = 0x0001_0000;
199
200 /// Enable/disable multi-results.
201 ///
202 /// ### Server
203 /// Can send multiple resultsets for COM_QUERY. Error if the server needs to send
204 /// them and client does not support them.
205 ///
206 /// ### Client
207 /// Can handle multiple resultsets for COM_QUERY.
208 ///
209 /// ### Requires
210 /// `CLIENT_PROTOCOL_41`
211 const CLIENT_MULTI_RESULTS = 0x0002_0000;
212
213 /// Multi-results and OUT parameters in PS-protocol.
214 ///
215 /// ### Server
216 /// Can send multiple resultsets for COM_STMT_EXECUTE.
217 ///
218 /// ### Client
219 /// Can handle multiple resultsets for COM_STMT_EXECUTE.
220 ///
221 /// ### Requires
222 /// `CLIENT_PROTOCOL_41`
223 const CLIENT_PS_MULTI_RESULTS = 0x0004_0000;
224
225 /// Client supports plugin authentication.
226 ///
227 /// ### Server
228 /// Sends extra data in Initial Handshake Packet and supports the pluggable
229 /// authentication protocol.
230 ///
231 /// ### Client
232 /// Supports authentication plugins.
233 ///
234 /// ### Requires
235 /// `CLIENT_PROTOCOL_41`
236 const CLIENT_PLUGIN_AUTH = 0x0008_0000;
237
238 /// Client supports connection attributes.
239 ///
240 /// ### Server
241 /// Permits connection attributes in Protocol::HandshakeResponse41.
242 ///
243 /// ### Client
244 /// Sends connection attributes in Protocol::HandshakeResponse41.
245 const CLIENT_CONNECT_ATTRS = 0x0010_0000;
246
247 /// Enable authentication response packet to be larger than 255 bytes.
248 /// When the ability to change default plugin require that the initial password
249 /// field in the Protocol::HandshakeResponse41 paclet can be of arbitrary size.
250 /// However, the 4.1 client-server protocol limits the length of the auth-data-field
251 /// sent from client to server to 255 bytes. The solution is to change the type of
252 /// the field to a true length encoded string and indicate the protocol change with
253 /// this client capability flag.
254 ///
255 /// ### Server
256 /// Understands length-encoded integer for auth response data in
257 /// Protocol::HandshakeResponse41.
258 ///
259 /// ### Client
260 /// Length of auth response data in Protocol::HandshakeResponse41 is a
261 /// length-encoded integer.
262 ///
263 /// ### Note
264 /// The flag was introduced in 5.6.6, but had the wrong value.
265 const CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 0x0020_0000;
266
267 /// Don't close the connection for a user account with expired password.
268 ///
269 /// ### Server
270 /// Announces support for expired password extension.
271 ///
272 /// ### Client
273 /// Can handle expired passwords.
274 const CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS = 0x0040_0000;
275
276 /// Capable of handling server state change information.
277 /// Its a hint to the server to include the state change information in OK_Packet.
278 ///
279 /// ### Server
280 /// Can set SERVER_SESSION_STATE_CHANGED in the SERVER_STATUS_flags_enum and send
281 /// Session State Information in a OK_Packet.
282 ///
283 /// ### Client
284 /// Expects the server to send Session State Information in a OK_Packet.
285 const CLIENT_SESSION_TRACK = 0x0080_0000;
286
287 /// Client no longer needs EOF_Packet and will use OK_Packet instead.
288 ///
289 /// ### Server
290 /// Can send OK after a Text Resultset.
291 ///
292 /// ### Client
293 /// Expects an OK_Packet (instead of EOF_Packet) after the resultset
294 /// rows of a Text Resultset.
295 ///
296 /// ### Background
297 /// To support CLIENT_SESSION_TRACK, additional information must be sent after all
298 /// successful commands. Although the OK_Packet is extensible, the EOF_Packet is
299 /// not due to the overlap of its bytes with the content of the Text Resultset Row.
300 ///
301 /// Therefore, the EOF_Packet in the Text Resultset is replaced with an OK_Packet.
302 /// EOF_Packet is deprecated as of MySQL 5.7.5.
303 const CLIENT_DEPRECATE_EOF = 0x0100_0000;
304
305 /// The client can handle optional metadata information in the resultset.
306 const CLIENT_OPTIONAL_RESULTSET_METADATA = 0x0200_0000;
307
308 /// Compression protocol extended to support zstd compression method.
309 ///
310 /// This capability flag is used to send zstd compression level between client and server
311 /// provided both client and server are enabled with this flag.
312 ///
313 /// # Server
314 ///
315 /// Server sets this flag when global variable protocol-compression-algorithms has zstd
316 /// in its list of supported values.
317 ///
318 /// # Client
319 ///
320 /// Client sets this flag when it is configured to use zstd compression method.
321 const CLIENT_ZSTD_COMPRESSION_ALGORITHM = 0x0400_0000;
322
323 /// Support optional extension for query parameters into the COM_QUERY
324 /// and COM_STMT_EXECUTE packets.
325 ///
326 /// # Server
327 ///
328 /// Expects an optional part containing the query parameter set(s).
329 /// Executes the query for each set of parameters or returns an error if more than 1 set
330 /// of parameters is sent and the server can't execute it.
331 ///
332 /// # Client
333 ///
334 /// Can send the optional part containing the query parameter set(s).
335 const CLIENT_QUERY_ATTRIBUTES = 0x0800_0000;
336
337 /// Support Multi factor authentication.
338 ///
339 /// # Server
340 ///
341 /// Server sends AuthNextFactor packet after every nth factor
342 /// authentication method succeeds, except the last factor authentication.
343 ///
344 /// # Client
345 ///
346 /// Client reads AuthNextFactor packet sent by server
347 /// and initiates next factor authentication method.
348 const MULTI_FACTOR_AUTHENTICATION = 0x1000_0000;
349
350 /// Client or server supports progress reports within error packet.
351 const CLIENT_PROGRESS_OBSOLETE = 0x2000_0000;
352
353 /// Verify server certificate. Client only flag.
354 ///
355 /// Deprecated in favor of –ssl-mode.
356 const CLIENT_SSL_VERIFY_SERVER_CERT = 0x4000_0000;
357
358 /// Don't reset the options after an unsuccessful connect. Client only flag.
359 const CLIENT_REMEMBER_OPTIONS = 0x8000_0000;
360 }
361}
362
363my_bitflags! {
364 CursorType,
365 #[error("Unknown flags in the raw value of CursorType (raw={:b})", _0)]
366 UnknownCursorType,
367 u8,
368
369 /// Mysql cursor type.
370 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
371 pub struct CursorType: u8 {
372 const CURSOR_TYPE_NO_CURSOR = 0_u8;
373 const CURSOR_TYPE_READ_ONLY = 1_u8;
374 const CURSOR_TYPE_FOR_UPDATE = 2_u8;
375 const CURSOR_TYPE_SCROLLABLE = 4_u8;
376 }
377}
378
379my_bitflags! {
380 StmtExecuteParamsFlags,
381 #[error("Unknown flags in the raw value of StmtExecuteParamsFlags (raw={:b})", _0)]
382 UnknownStmtExecuteParamsFlags,
383 u8,
384
385 /// MySql stmt execute params flags.
386 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
387 pub struct StmtExecuteParamsFlags: u8 {
388 const NEW_PARAMS_BOUND = 1_u8;
389 }
390}
391
392my_bitflags! {
393 StmtExecuteParamFlags,
394 #[error("Unknown flags in the raw value of StmtExecuteParamFlags (raw={:b})", _0)]
395 UnknownStmtExecuteParamFlags,
396 u8,
397
398 /// MySql stmt execute params flags.
399 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
400 pub struct StmtExecuteParamFlags: u8 {
401 const UNSIGNED = 128_u8;
402 }
403}
404
405my_bitflags! {
406 ColumnFlags,
407 #[error("Unknown flags in the raw value of ColumnFlags (raw={:b})", _0)]
408 UnknownColumnFlags,
409 u16,
410
411 /// MySql column flags
412 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
413 pub struct ColumnFlags: u16 {
414 /// Field can't be NULL.
415 const NOT_NULL_FLAG = 1u16;
416
417 /// Field is part of a primary key.
418 const PRI_KEY_FLAG = 2u16;
419
420 /// Field is part of a unique key.
421 const UNIQUE_KEY_FLAG = 4u16;
422
423 /// Field is part of a key.
424 const MULTIPLE_KEY_FLAG = 8u16;
425
426 /// Field is a blob.
427 const BLOB_FLAG = 16u16;
428
429 /// Field is unsigned.
430 const UNSIGNED_FLAG = 32u16;
431
432 /// Field is zerofill.
433 const ZEROFILL_FLAG = 64u16;
434
435 /// Field is binary.
436 const BINARY_FLAG = 128u16;
437
438 /// Field is an enum.
439 const ENUM_FLAG = 256u16;
440
441 /// Field is a autoincrement field.
442 const AUTO_INCREMENT_FLAG = 512u16;
443
444 /// Field is a timestamp.
445 const TIMESTAMP_FLAG = 1024u16;
446
447 /// Field is a set.
448 const SET_FLAG = 2048u16;
449
450 /// Field doesn't have default value.
451 const NO_DEFAULT_VALUE_FLAG = 4096u16;
452
453 /// Field is set to NOW on UPDATE.
454 const ON_UPDATE_NOW_FLAG = 8192u16;
455
456 /// Intern; Part of some key.
457 const PART_KEY_FLAG = 16384u16;
458
459 /// Field is num (for clients).
460 const NUM_FLAG = 32768u16;
461 }
462}
463
464/// MySql server commands
465#[allow(non_camel_case_types)]
466#[derive(Clone, Copy, Eq, PartialEq, Debug)]
467#[repr(u8)]
468pub enum Command {
469 COM_SLEEP = 0,
470 COM_QUIT,
471 COM_INIT_DB,
472 COM_QUERY,
473 COM_FIELD_LIST,
474 COM_CREATE_DB,
475 COM_DROP_DB,
476 COM_REFRESH,
477 COM_DEPRECATED_1,
478 COM_STATISTICS,
479 COM_PROCESS_INFO,
480 COM_CONNECT,
481 COM_PROCESS_KILL,
482 COM_DEBUG,
483 COM_PING,
484 COM_TIME,
485 COM_DELAYED_INSERT,
486 COM_CHANGE_USER,
487 COM_BINLOG_DUMP,
488 COM_TABLE_DUMP,
489 COM_CONNECT_OUT,
490 COM_REGISTER_SLAVE,
491 COM_STMT_PREPARE,
492 COM_STMT_EXECUTE,
493 COM_STMT_SEND_LONG_DATA,
494 COM_STMT_CLOSE,
495 COM_STMT_RESET,
496 COM_SET_OPTION,
497 COM_STMT_FETCH,
498 COM_DAEMON,
499 COM_BINLOG_DUMP_GTID,
500 COM_RESET_CONNECTION,
501 COM_END,
502}
503
504/// Type of state change information (part of MySql's Ok packet).
505#[allow(non_camel_case_types)]
506#[derive(Clone, Copy, Eq, PartialEq, Debug)]
507#[repr(u8)]
508pub enum SessionStateType {
509 /// Session system variables.
510 SESSION_TRACK_SYSTEM_VARIABLES,
511 /// Current schema.
512 SESSION_TRACK_SCHEMA,
513 /// track session state changes
514 SESSION_TRACK_STATE_CHANGE,
515 /// See also: session_track_gtids.
516 SESSION_TRACK_GTIDS,
517 /// Transaction characteristics.
518 SESSION_TRACK_TRANSACTION_CHARACTERISTICS,
519 /// Transaction state.
520 SESSION_TRACK_TRANSACTION_STATE,
521}
522
523impl From<SessionStateType> for u8 {
524 fn from(x: SessionStateType) -> u8 {
525 x as u8
526 }
527}
528
529impl TryFrom<u8> for SessionStateType {
530 type Error = UnknownSessionStateType;
531
532 fn try_from(value: u8) -> Result<Self, Self::Error> {
533 match value {
534 0x00 => Ok(SessionStateType::SESSION_TRACK_SYSTEM_VARIABLES),
535 0x01 => Ok(SessionStateType::SESSION_TRACK_SCHEMA),
536 0x02 => Ok(SessionStateType::SESSION_TRACK_STATE_CHANGE),
537 0x03 => Ok(SessionStateType::SESSION_TRACK_GTIDS),
538 0x04 => Ok(SessionStateType::SESSION_TRACK_TRANSACTION_CHARACTERISTICS),
539 0x05 => Ok(SessionStateType::SESSION_TRACK_TRANSACTION_STATE),
540 x => Err(UnknownSessionStateType(x)),
541 }
542 }
543}
544
545#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
546#[error("Unknown session state type {}", _0)]
547pub struct UnknownSessionStateType(pub u8);
548
549/// Geometry type.
550#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
551#[allow(non_camel_case_types)]
552#[repr(u8)]
553pub enum GeometryType {
554 GEOM_GEOMETRY,
555 GEOM_POINT,
556 GEOM_LINESTRING,
557 GEOM_POLYGON,
558 GEOM_MULTIPOINT,
559 GEOM_MULTILINESTRING,
560 GEOM_MULTIPOLYGON,
561 GEOM_GEOMETRYCOLLECTION,
562}
563
564#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
565#[error("Unknown geometry type {}", _0)]
566#[repr(transparent)]
567pub struct UnknownGeometryType(pub u8);
568
569impl From<UnknownGeometryType> for u8 {
570 fn from(x: UnknownGeometryType) -> Self {
571 x.0
572 }
573}
574
575impl TryFrom<u8> for GeometryType {
576 type Error = UnknownGeometryType;
577 fn try_from(value: u8) -> Result<Self, Self::Error> {
578 match value {
579 0 => Ok(GeometryType::GEOM_GEOMETRY),
580 1 => Ok(GeometryType::GEOM_POINT),
581 2 => Ok(GeometryType::GEOM_LINESTRING),
582 3 => Ok(GeometryType::GEOM_POLYGON),
583 4 => Ok(GeometryType::GEOM_MULTIPOINT),
584 5 => Ok(GeometryType::GEOM_MULTILINESTRING),
585 6 => Ok(GeometryType::GEOM_MULTIPOLYGON),
586 7 => Ok(GeometryType::GEOM_GEOMETRYCOLLECTION),
587 x => Err(UnknownGeometryType(x)),
588 }
589 }
590}
591
592/// Type of MySql column field
593#[allow(non_camel_case_types)]
594#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
595#[repr(u8)]
596pub enum ColumnType {
597 MYSQL_TYPE_DECIMAL = 0,
598 MYSQL_TYPE_TINY,
599 MYSQL_TYPE_SHORT,
600 MYSQL_TYPE_LONG,
601 MYSQL_TYPE_FLOAT,
602 MYSQL_TYPE_DOUBLE,
603 MYSQL_TYPE_NULL,
604 MYSQL_TYPE_TIMESTAMP,
605 MYSQL_TYPE_LONGLONG,
606 MYSQL_TYPE_INT24,
607 MYSQL_TYPE_DATE,
608 MYSQL_TYPE_TIME,
609 MYSQL_TYPE_DATETIME,
610 MYSQL_TYPE_YEAR,
611 MYSQL_TYPE_NEWDATE, // Internal to MySql
612 MYSQL_TYPE_VARCHAR,
613 MYSQL_TYPE_BIT,
614 MYSQL_TYPE_TIMESTAMP2,
615 MYSQL_TYPE_DATETIME2,
616 MYSQL_TYPE_TIME2,
617 MYSQL_TYPE_TYPED_ARRAY, // Used for replication only
618 MYSQL_TYPE_VECTOR = 242,
619 MYSQL_TYPE_UNKNOWN = 243,
620 MYSQL_TYPE_JSON = 245,
621 MYSQL_TYPE_NEWDECIMAL = 246,
622 MYSQL_TYPE_ENUM = 247,
623 MYSQL_TYPE_SET = 248,
624 MYSQL_TYPE_TINY_BLOB = 249,
625 MYSQL_TYPE_MEDIUM_BLOB = 250,
626 MYSQL_TYPE_LONG_BLOB = 251,
627 MYSQL_TYPE_BLOB = 252,
628 MYSQL_TYPE_VAR_STRING = 253,
629 MYSQL_TYPE_STRING = 254,
630 MYSQL_TYPE_GEOMETRY = 255,
631}
632
633impl ColumnType {
634 pub fn is_numeric_type(&self) -> bool {
635 use ColumnType::*;
636 matches!(
637 self,
638 MYSQL_TYPE_TINY
639 | MYSQL_TYPE_SHORT
640 | MYSQL_TYPE_INT24
641 | MYSQL_TYPE_LONG
642 | MYSQL_TYPE_LONGLONG
643 | MYSQL_TYPE_DECIMAL
644 | MYSQL_TYPE_NEWDECIMAL
645 | MYSQL_TYPE_FLOAT
646 | MYSQL_TYPE_DOUBLE
647 | MYSQL_TYPE_YEAR // Yes, this is also included
648 )
649 }
650
651 pub fn is_character_type(&self) -> bool {
652 use ColumnType::*;
653 matches!(
654 self,
655 MYSQL_TYPE_STRING | MYSQL_TYPE_VAR_STRING | MYSQL_TYPE_VARCHAR | MYSQL_TYPE_BLOB
656 )
657 }
658
659 pub fn is_enum_or_set_type(&self) -> bool {
660 use ColumnType::*;
661 matches!(self, MYSQL_TYPE_ENUM | MYSQL_TYPE_SET)
662 }
663
664 pub fn is_enum_type(&self) -> bool {
665 matches!(self, ColumnType::MYSQL_TYPE_ENUM)
666 }
667
668 pub fn is_set_type(&self) -> bool {
669 matches!(self, ColumnType::MYSQL_TYPE_SET)
670 }
671
672 pub fn is_geometry_type(&self) -> bool {
673 matches!(self, ColumnType::MYSQL_TYPE_GEOMETRY)
674 }
675
676 pub fn is_vector_type(&self) -> bool {
677 matches!(self, ColumnType::MYSQL_TYPE_VECTOR)
678 }
679}
680
681impl TryFrom<u8> for ColumnType {
682 type Error = UnknownColumnType;
683
684 fn try_from(value: u8) -> Result<Self, Self::Error> {
685 match value {
686 0x00_u8 => Ok(ColumnType::MYSQL_TYPE_DECIMAL),
687 0x01_u8 => Ok(ColumnType::MYSQL_TYPE_TINY),
688 0x02_u8 => Ok(ColumnType::MYSQL_TYPE_SHORT),
689 0x03_u8 => Ok(ColumnType::MYSQL_TYPE_LONG),
690 0x04_u8 => Ok(ColumnType::MYSQL_TYPE_FLOAT),
691 0x05_u8 => Ok(ColumnType::MYSQL_TYPE_DOUBLE),
692 0x06_u8 => Ok(ColumnType::MYSQL_TYPE_NULL),
693 0x07_u8 => Ok(ColumnType::MYSQL_TYPE_TIMESTAMP),
694 0x08_u8 => Ok(ColumnType::MYSQL_TYPE_LONGLONG),
695 0x09_u8 => Ok(ColumnType::MYSQL_TYPE_INT24),
696 0x0a_u8 => Ok(ColumnType::MYSQL_TYPE_DATE),
697 0x0b_u8 => Ok(ColumnType::MYSQL_TYPE_TIME),
698 0x0c_u8 => Ok(ColumnType::MYSQL_TYPE_DATETIME),
699 0x0d_u8 => Ok(ColumnType::MYSQL_TYPE_YEAR),
700 0x0f_u8 => Ok(ColumnType::MYSQL_TYPE_VARCHAR),
701 0x10_u8 => Ok(ColumnType::MYSQL_TYPE_BIT),
702 0x11_u8 => Ok(ColumnType::MYSQL_TYPE_TIMESTAMP2),
703 0x12_u8 => Ok(ColumnType::MYSQL_TYPE_DATETIME2),
704 0x13_u8 => Ok(ColumnType::MYSQL_TYPE_TIME2),
705 0x14_u8 => Ok(ColumnType::MYSQL_TYPE_TYPED_ARRAY),
706 0xf2_u8 => Ok(ColumnType::MYSQL_TYPE_VECTOR),
707 0xf3_u8 => Ok(ColumnType::MYSQL_TYPE_UNKNOWN),
708 0xf5_u8 => Ok(ColumnType::MYSQL_TYPE_JSON),
709 0xf6_u8 => Ok(ColumnType::MYSQL_TYPE_NEWDECIMAL),
710 0xf7_u8 => Ok(ColumnType::MYSQL_TYPE_ENUM),
711 0xf8_u8 => Ok(ColumnType::MYSQL_TYPE_SET),
712 0xf9_u8 => Ok(ColumnType::MYSQL_TYPE_TINY_BLOB),
713 0xfa_u8 => Ok(ColumnType::MYSQL_TYPE_MEDIUM_BLOB),
714 0xfb_u8 => Ok(ColumnType::MYSQL_TYPE_LONG_BLOB),
715 0xfc_u8 => Ok(ColumnType::MYSQL_TYPE_BLOB),
716 0xfd_u8 => Ok(ColumnType::MYSQL_TYPE_VAR_STRING),
717 0xfe_u8 => Ok(ColumnType::MYSQL_TYPE_STRING),
718 0xff_u8 => Ok(ColumnType::MYSQL_TYPE_GEOMETRY),
719 x => Err(UnknownColumnType(x)),
720 }
721 }
722}
723
724impl From<ColumnType> for u8 {
725 fn from(val: ColumnType) -> u8 {
726 val as u8
727 }
728}
729
730my_bitflags! {
731 Flags2,
732 #[error("Unknown flags in the raw value of Flags2 (raw={:b})", _0)]
733 UnknownFlags2,
734 u32,
735
736 /// Bitmask of flags that are usually set with `SET`.
737 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
738 pub struct Flags2: u32 {
739 const OPTION_AUTO_IS_NULL = 0x00004000;
740 const OPTION_NOT_AUTOCOMMIT = 0x00080000;
741 const OPTION_NO_FOREIGN_KEY_CHECKS = 0x04000000;
742 const OPTION_RELAXED_UNIQUE_CHECKS = 0x08000000;
743 }
744}
745
746my_bitflags! {
747 SqlMode,
748 #[error("Unknown flags in the raw value of SqlMode (raw={:b})", _0)]
749 UnknownSqlMode,
750 u64,
751
752 /// Bitmask of flags that are usually set with `SET sql_mode`.
753 #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
754 pub struct SqlMode: u64 {
755 const MODE_REAL_AS_FLOAT = 0x00000001;
756 const MODE_PIPES_AS_CONCAT = 0x00000002;
757 const MODE_ANSI_QUOTES = 0x00000004;
758 const MODE_IGNORE_SPACE = 0x00000008;
759 const MODE_NOT_USED = 0x00000010;
760 const MODE_ONLY_FULL_GROUP_BY = 0x00000020;
761 const MODE_NO_UNSIGNED_SUBTRACTION = 0x00000040;
762 const MODE_NO_DIR_IN_CREATE = 0x00000080;
763 const MODE_POSTGRESQL = 0x00000100;
764 const MODE_ORACLE = 0x00000200;
765 const MODE_MSSQL = 0x00000400;
766 const MODE_DB2 = 0x00000800;
767 const MODE_MAXDB = 0x00001000;
768 const MODE_NO_KEY_OPTIONS = 0x00002000;
769 const MODE_NO_FIELD_OPTIONS = 0x00008000;
770 const MODE_NO_TABLE_OPTIONS = 0x00004000;
771 const MODE_MYSQL40 = 0x00020000;
772 const MODE_MYSQL323 = 0x00010000;
773 const MODE_ANSI = 0x00040000;
774 const MODE_NO_AUTO_VALUE_ON_ZERO = 0x00080000;
775 const MODE_NO_BACKSLASH_ESCAPES = 0x00100000;
776 const MODE_STRICT_TRANS_TABLES = 0x00200000;
777 const MODE_STRICT_ALL_TABLES = 0x00400000;
778 const MODE_NO_ZERO_IN_DATE = 0x00800000;
779 const MODE_NO_ZERO_DATE = 0x01000000;
780 const MODE_INVALID_DATES = 0x02000000;
781 const MODE_ERROR_FOR_DIVISION_BY_ZERO = 0x04000000;
782 const MODE_TRADITIONAL = 0x08000000;
783 const MODE_NO_AUTO_CREATE_USER = 0x10000000;
784 const MODE_HIGH_NOT_PRECEDENCE = 0x20000000;
785 const MODE_NO_ENGINE_SUBSTITUTION = 0x40000000;
786 const MODE_PAD_CHAR_TO_FULL_LENGTH = 0x80000000;
787 const MODE_TIME_TRUNCATE_FRACTIONAL = 0x100000000;
788 const MODE_LAST = 0x200000000;
789 }
790}
791
792/// Type of the user defined function return slot and arguments.
793#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
794#[allow(non_camel_case_types)]
795#[repr(i8)]
796pub enum ItemResult {
797 /// not valid for UDFs
798 INVALID_RESULT = -1,
799 /// char *
800 STRING_RESULT = 0,
801 REAL_RESULT,
802 /// double
803 /// long long
804 INT_RESULT,
805 /// not valid for UDFs
806 ROW_RESULT,
807 /// char *, to be converted to/from a decimal
808 DECIMAL_RESULT,
809}
810
811#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
812#[error("Unknown item result type {}", _0)]
813pub struct UnknownItemResultType(pub i8);
814
815impl From<UnknownItemResultType> for i8 {
816 fn from(x: UnknownItemResultType) -> Self {
817 x.0
818 }
819}
820
821impl TryFrom<i8> for ItemResult {
822 type Error = UnknownItemResultType;
823
824 fn try_from(value: i8) -> Result<Self, Self::Error> {
825 match value {
826 -1 => Ok(ItemResult::INVALID_RESULT),
827 0 => Ok(ItemResult::STRING_RESULT),
828 1 => Ok(ItemResult::REAL_RESULT),
829 2 => Ok(ItemResult::INT_RESULT),
830 3 => Ok(ItemResult::ROW_RESULT),
831 4 => Ok(ItemResult::DECIMAL_RESULT),
832 x => Err(UnknownItemResultType(x)),
833 }
834 }
835}
836
837#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
838#[error("Unknown column type {}", _0)]
839pub struct UnknownColumnType(pub u8);
840
841impl From<UnknownColumnType> for u8 {
842 fn from(x: UnknownColumnType) -> Self {
843 x.0
844 }
845}