constants.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_DETAIL_PROTOCOL_CONSTANTS_HPP
  8. #define BOOST_MYSQL_DETAIL_PROTOCOL_CONSTANTS_HPP
  9. #include <boost/mysql/detail/auxiliar/make_string_view.hpp>
  10. #include <boost/mysql/detail/protocol/protocol_types.hpp>
  11. #include <cstddef>
  12. namespace boost {
  13. namespace mysql {
  14. namespace detail {
  15. enum class protocol_field_type : std::uint8_t
  16. {
  17. decimal = 0x00, // Apparently not sent
  18. tiny = 0x01, // TINYINT
  19. short_ = 0x02, // SMALLINT
  20. long_ = 0x03, // INT
  21. float_ = 0x04, // FLOAT
  22. double_ = 0x05, // DOUBLE
  23. null = 0x06, // Apparently not sent
  24. timestamp = 0x07, // TIMESTAMP
  25. longlong = 0x08, // BIGINT
  26. int24 = 0x09, // MEDIUMINT
  27. date = 0x0a, // DATE
  28. time = 0x0b, // TIME
  29. datetime = 0x0c, // DATETIME
  30. year = 0x0d, // YEAR
  31. varchar = 0x0f, // Apparently not sent
  32. bit = 0x10, // BIT
  33. json = 0xf5, // JSON
  34. newdecimal = 0xf6, // DECIMAL
  35. enum_ = 0xf7, // Apparently not sent
  36. set = 0xf8, // Apperently not sent
  37. tiny_blob = 0xf9, // Apparently not sent
  38. medium_blob = 0xfa, // Apparently not sent
  39. long_blob = 0xfb, // Apparently not sent
  40. blob = 0xfc, // Used for all TEXT and BLOB types
  41. var_string = 0xfd, // Used for VARCHAR and VARBINARY
  42. string = 0xfe, // Used for CHAR and BINARY, ENUM (enum flag set), SET (set flag set)
  43. geometry = 0xff // GEOMETRY
  44. };
  45. constexpr std::size_t MAX_PACKET_SIZE = 0xffffff;
  46. constexpr std::size_t HEADER_SIZE = 4;
  47. // Server status flags
  48. constexpr std::uint32_t SERVER_STATUS_IN_TRANS = 1;
  49. constexpr std::uint32_t SERVER_STATUS_AUTOCOMMIT = 2;
  50. constexpr std::uint32_t SERVER_MORE_RESULTS_EXISTS = 8;
  51. constexpr std::uint32_t SERVER_QUERY_NO_GOOD_INDEX_USED = 16;
  52. constexpr std::uint32_t SERVER_QUERY_NO_INDEX_USED = 32;
  53. constexpr std::uint32_t SERVER_STATUS_CURSOR_EXISTS = 64;
  54. constexpr std::uint32_t SERVER_STATUS_LAST_ROW_SENT = 128;
  55. constexpr std::uint32_t SERVER_STATUS_DB_DROPPED = 256;
  56. constexpr std::uint32_t SERVER_STATUS_NO_BACKSLASH_ESCAPES = 512;
  57. constexpr std::uint32_t SERVER_STATUS_METADATA_CHANGED = 1024;
  58. constexpr std::uint32_t SERVER_QUERY_WAS_SLOW = 2048;
  59. constexpr std::uint32_t SERVER_PS_OUT_PARAMS = 4096;
  60. constexpr std::uint32_t SERVER_STATUS_IN_TRANS_READONLY = 8192;
  61. constexpr std::uint32_t SERVER_SESSION_STATE_CHANGED = (1UL << 14);
  62. // Packet type constants
  63. constexpr std::uint8_t handshake_protocol_version_9 = 9;
  64. constexpr std::uint8_t handshake_protocol_version_10 = 10;
  65. constexpr std::uint8_t error_packet_header = 0xff;
  66. constexpr std::uint8_t ok_packet_header = 0x00;
  67. constexpr std::uint8_t eof_packet_header = 0xfe;
  68. constexpr std::uint8_t auth_switch_request_header = 0xfe;
  69. constexpr std::uint8_t auth_more_data_header = 0x01;
  70. constexpr string_view fast_auth_complete_challenge = make_string_view("\3");
  71. // The binary collation number, used to distinguish blobs from strings
  72. constexpr std::uint16_t binary_collation = 63;
  73. // Column flags
  74. namespace column_flags {
  75. constexpr std::uint16_t not_null = 1; // Field can't be NULL.
  76. constexpr std::uint16_t pri_key = 2; // Field is part of a primary key.
  77. constexpr std::uint16_t unique_key = 4; // Field is part of a unique key.
  78. constexpr std::uint16_t multiple_key = 8; // Field is part of a key.
  79. constexpr std::uint16_t blob = 16; // Field is a blob.
  80. constexpr std::uint16_t unsigned_ = 32; // Field is unsigned.
  81. constexpr std::uint16_t zerofill = 64; // Field is zerofill.
  82. constexpr std::uint16_t binary = 128; // Field is binary.
  83. constexpr std::uint16_t enum_ = 256; // field is an enum
  84. constexpr std::uint16_t auto_increment = 512; // field is a autoincrement field
  85. constexpr std::uint16_t timestamp = 1024; // Field is a timestamp.
  86. constexpr std::uint16_t set = 2048; // field is a set
  87. constexpr std::uint16_t no_default_value = 4096; // Field doesn't have default value.
  88. constexpr std::uint16_t on_update_now = 8192; // Field is set to NOW on UPDATE.
  89. constexpr std::uint16_t part_key = 16384; // Intern; Part of some key.
  90. constexpr std::uint16_t num = 32768; // Field is num (for clients)
  91. } // namespace column_flags
  92. // Prepared statements
  93. namespace cursor_types {
  94. constexpr std::uint8_t no_cursor = 0;
  95. constexpr std::uint8_t read_only = 1;
  96. constexpr std::uint8_t for_update = 2;
  97. constexpr std::uint8_t scrollable = 4;
  98. } // namespace cursor_types
  99. // Text protocol deserialization constants
  100. namespace textc {
  101. constexpr unsigned max_decimals = 6u;
  102. constexpr std::size_t year_sz = 4;
  103. constexpr std::size_t month_sz = 2;
  104. constexpr std::size_t day_sz = 2;
  105. constexpr std::size_t hours_min_sz = 2; // in TIME, it may be longer
  106. constexpr std::size_t mins_sz = 2;
  107. constexpr std::size_t secs_sz = 2;
  108. constexpr std::size_t date_sz = year_sz + month_sz + day_sz + 2; // delimiters
  109. constexpr std::size_t time_min_sz = hours_min_sz + mins_sz + secs_sz + 2; // delimiters
  110. constexpr std::size_t time_max_sz = time_min_sz + max_decimals + 3; // sign, period, hour extra character
  111. constexpr std::size_t datetime_min_sz = date_sz + time_min_sz + 1; // delimiter
  112. constexpr std::size_t datetime_max_sz = datetime_min_sz + max_decimals + 1; // period
  113. constexpr unsigned time_max_hour = 838;
  114. } // namespace textc
  115. // Binary protocol (de)serialization constants
  116. namespace binc {
  117. constexpr std::size_t length_sz = 1; // length byte, for date, datetime and time
  118. constexpr std::size_t year_sz = 2;
  119. constexpr std::size_t month_sz = 1;
  120. constexpr std::size_t date_day_sz = 1;
  121. constexpr std::size_t time_days_sz = 4;
  122. constexpr std::size_t hours_sz = 1;
  123. constexpr std::size_t mins_sz = 1;
  124. constexpr std::size_t secs_sz = 1;
  125. constexpr std::size_t micros_sz = 4;
  126. constexpr std::size_t time_sign_sz = 1;
  127. constexpr std::size_t date_sz = year_sz + month_sz + date_day_sz; // does not include length
  128. constexpr std::size_t datetime_d_sz = date_sz;
  129. constexpr std::size_t datetime_dhms_sz = datetime_d_sz + hours_sz + mins_sz + secs_sz;
  130. constexpr std::size_t datetime_dhmsu_sz = datetime_dhms_sz + micros_sz;
  131. constexpr std::size_t time_dhms_sz = time_sign_sz + time_days_sz + hours_sz + mins_sz + secs_sz;
  132. constexpr std::size_t time_dhmsu_sz = time_dhms_sz + micros_sz;
  133. constexpr std::size_t time_max_days = 34; // equivalent to the 839 hours, in the broken format
  134. } // namespace binc
  135. } // namespace detail
  136. } // namespace mysql
  137. } // namespace boost
  138. #endif