MySQL 9.1.0
Source Code Documentation
classic_protocol_binary.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2023, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_ROUTER_CLASSIC_PROTOCOL_BINARY_H_
27#define MYSQL_ROUTER_CLASSIC_PROTOCOL_BINARY_H_
28
29#include <cstddef> // size_t
30#include <cstdint> // uint8_t
31#include <system_error> // error_code
32#include <utility> // move
33
38
40namespace borrowable {
41namespace binary {
42
43template <bool Borrowed>
44using string_type = std::conditional_t<Borrowed, std::string_view, std::string>;
45
46/**
47 * base type of all binary scalar value types.
48 */
49template <class T>
50class TypeBase {
51 public:
52 using value_type = T;
53
54 constexpr TypeBase(value_type val) : v_(val) {}
55
56 [[nodiscard]] constexpr value_type value() const { return v_; }
57
58 friend bool operator==(const TypeBase &lhs, const TypeBase &rhs) {
59 return lhs.value() == rhs.value();
60 }
61
62 friend bool operator!=(const TypeBase &lhs, const TypeBase &rhs) {
63 return !(lhs == rhs);
64 }
65
66 private:
68};
69
70/**
71 * POD base-type for Datetime, Timestamp, Date.
72 */
74 public:
75 DatetimeBase() = default;
76
77 constexpr DatetimeBase(uint16_t year, uint8_t month, uint8_t day,
78 uint8_t hour, uint8_t minute, uint8_t second,
79 uint32_t microsecond = 0)
80 : year_(year),
82 day_(day),
83 hour_(hour),
87
88 constexpr DatetimeBase(uint16_t year, uint8_t month, uint8_t day)
89 : DatetimeBase(year, month, day, 0, 0, 0, 0) {}
90
91 [[nodiscard]] constexpr uint16_t year() const { return year_; }
92 [[nodiscard]] constexpr uint8_t month() const { return month_; }
93 [[nodiscard]] constexpr uint8_t day() const { return day_; }
94 [[nodiscard]] constexpr uint8_t hour() const { return hour_; }
95 [[nodiscard]] constexpr uint8_t minute() const { return minute_; }
96 [[nodiscard]] constexpr uint8_t second() const { return second_; }
97 [[nodiscard]] constexpr uint32_t microsecond() const { return microsecond_; }
98
99 friend bool operator==(const DatetimeBase &lhs, const DatetimeBase &rhs) {
100 return lhs.year() == rhs.year() && //
101 lhs.month() == rhs.month() && //
102 lhs.day() == rhs.day() && //
103 lhs.hour() == rhs.hour() && //
104 lhs.minute() == rhs.minute() && //
105 lhs.second() == rhs.second() && //
106 lhs.microsecond() == rhs.microsecond();
107 }
108
109 friend bool operator!=(const DatetimeBase &lhs, const DatetimeBase &rhs) {
110 return !(lhs == rhs);
111 }
112
113 private:
114 uint16_t year_{};
115 uint8_t month_{};
116 uint8_t day_{};
117 uint8_t hour_{};
118 uint8_t minute_{};
119 uint8_t second_{};
120 uint32_t microsecond_{};
121};
122
123class DateTime : public DatetimeBase {
124 public:
126};
127
128class Timestamp : public DatetimeBase {
129 public:
131};
132
133class Date : public DatetimeBase {
134 public:
136};
137
138class Time {
139 public:
140 constexpr Time() = default;
141
142 constexpr Time(bool is_negative, uint32_t days, uint8_t hour, uint8_t minute,
143 uint8_t second, uint32_t microsecond = 0)
145 days_(days),
146 hour_(hour),
150
151 [[nodiscard]] constexpr bool is_negative() const { return is_negative_; }
152 [[nodiscard]] constexpr uint32_t days() const { return days_; }
153 [[nodiscard]] constexpr uint8_t hour() const { return hour_; }
154 [[nodiscard]] constexpr uint8_t minute() const { return minute_; }
155 [[nodiscard]] constexpr uint8_t second() const { return second_; }
156 [[nodiscard]] constexpr uint32_t microsecond() const { return microsecond_; }
157
158 friend bool operator==(const Time &lhs, const Time &rhs) {
159 return lhs.days() == rhs.days() && //
160 lhs.hour() == rhs.hour() && //
161 lhs.minute() == rhs.minute() && //
162 lhs.second() == rhs.second() && //
163 lhs.microsecond() == rhs.microsecond() && //
164 lhs.is_negative() == rhs.is_negative();
165 }
166
167 friend bool operator!=(const Time &lhs, const Time &rhs) {
168 return !(lhs == rhs);
169 }
170
171 private:
173 uint32_t days_{};
174 uint8_t hour_{};
175 uint8_t minute_{};
176 uint8_t second_{};
177 uint32_t microsecond_{};
178};
179
180class LongLong : public TypeBase<uint64_t> {
181 public:
182 using TypeBase::TypeBase;
183};
184
185class Long : public TypeBase<uint32_t> {
186 public:
187 using TypeBase::TypeBase;
188};
189
190class Int24 : public TypeBase<uint32_t> {
191 public:
192 using TypeBase::TypeBase;
193};
194
195class Short : public TypeBase<uint16_t> {
196 public:
197 using TypeBase::TypeBase;
198};
199
200class Year : public TypeBase<uint16_t> {
201 public:
202 using TypeBase::TypeBase;
203};
204
205class Tiny : public TypeBase<uint8_t> {
206 public:
207 using TypeBase::TypeBase;
208};
209
210class Double : public TypeBase<double> {
211 public:
212 using TypeBase::TypeBase;
213};
214
215class Float : public TypeBase<float> {
216 public:
217 using TypeBase::TypeBase;
218};
219
220template <bool Borrowed>
221class String : public TypeBase<string_type<Borrowed>> {
222 public:
224};
225
226template <bool Borrowed>
227class VarString : public TypeBase<string_type<Borrowed>> {
228 public:
230};
231
232template <bool Borrowed>
233class Varchar : public TypeBase<string_type<Borrowed>> {
234 public:
236};
237
238template <bool Borrowed>
239class Json : public TypeBase<string_type<Borrowed>> {
240 public:
242};
243
244template <bool Borrowed>
245class Blob : public TypeBase<string_type<Borrowed>> {
246 public:
248};
249
250template <bool Borrowed>
251class TinyBlob : public TypeBase<string_type<Borrowed>> {
252 public:
254};
255
256template <bool Borrowed>
257class MediumBlob : public TypeBase<string_type<Borrowed>> {
258 public:
260};
261
262template <bool Borrowed>
263class LongBlob : public TypeBase<string_type<Borrowed>> {
264 public:
266};
267
268template <bool Borrowed>
269class Enum : public TypeBase<string_type<Borrowed>> {
270 public:
272};
273
274template <bool Borrowed>
275class Set : public TypeBase<string_type<Borrowed>> {
276 public:
278};
279
280template <bool Borrowed>
281class Decimal : public TypeBase<string_type<Borrowed>> {
282 public:
284};
285
286template <bool Borrowed>
287class NewDecimal : public TypeBase<string_type<Borrowed>> {
288 public:
290};
291
292template <bool Borrowed>
293class Bit : public TypeBase<string_type<Borrowed>> {
294 public:
296};
297
298template <bool Borrowed>
299class Geometry : public TypeBase<string_type<Borrowed>> {
300 public:
302};
303
304class Null {};
305
306} // namespace binary
307} // namespace borrowable
308
309namespace borrowed {
310namespace binary {
338} // namespace binary
339} // namespace borrowed
340
341namespace binary {
369} // namespace binary
370} // namespace classic_protocol
371
372#endif
Definition: classic_protocol_binary.h:293
Definition: classic_protocol_binary.h:245
Definition: classic_protocol_binary.h:123
Definition: classic_protocol_binary.h:133
POD base-type for Datetime, Timestamp, Date.
Definition: classic_protocol_binary.h:73
constexpr DatetimeBase(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint32_t microsecond=0)
Definition: classic_protocol_binary.h:77
friend bool operator==(const DatetimeBase &lhs, const DatetimeBase &rhs)
Definition: classic_protocol_binary.h:99
constexpr uint8_t hour() const
Definition: classic_protocol_binary.h:94
constexpr uint8_t minute() const
Definition: classic_protocol_binary.h:95
uint8_t month_
Definition: classic_protocol_binary.h:115
constexpr uint32_t microsecond() const
Definition: classic_protocol_binary.h:97
constexpr uint16_t year() const
Definition: classic_protocol_binary.h:91
uint8_t second_
Definition: classic_protocol_binary.h:119
uint16_t year_
Definition: classic_protocol_binary.h:114
constexpr uint8_t second() const
Definition: classic_protocol_binary.h:96
friend bool operator!=(const DatetimeBase &lhs, const DatetimeBase &rhs)
Definition: classic_protocol_binary.h:109
constexpr DatetimeBase(uint16_t year, uint8_t month, uint8_t day)
Definition: classic_protocol_binary.h:88
uint32_t microsecond_
Definition: classic_protocol_binary.h:120
uint8_t hour_
Definition: classic_protocol_binary.h:117
constexpr uint8_t day() const
Definition: classic_protocol_binary.h:93
uint8_t minute_
Definition: classic_protocol_binary.h:118
constexpr uint8_t month() const
Definition: classic_protocol_binary.h:92
uint8_t day_
Definition: classic_protocol_binary.h:116
Definition: classic_protocol_binary.h:281
Definition: classic_protocol_binary.h:210
Definition: classic_protocol_binary.h:269
Definition: classic_protocol_binary.h:215
Definition: classic_protocol_binary.h:299
Definition: classic_protocol_binary.h:190
Definition: classic_protocol_binary.h:239
Definition: classic_protocol_binary.h:263
Definition: classic_protocol_binary.h:180
Definition: classic_protocol_binary.h:185
Definition: classic_protocol_binary.h:257
Definition: classic_protocol_binary.h:287
Definition: classic_protocol_binary.h:304
Definition: classic_protocol_binary.h:275
Definition: classic_protocol_binary.h:195
Definition: classic_protocol_binary.h:221
Definition: classic_protocol_binary.h:138
uint8_t hour_
Definition: classic_protocol_binary.h:174
constexpr uint8_t hour() const
Definition: classic_protocol_binary.h:153
constexpr uint32_t days() const
Definition: classic_protocol_binary.h:152
uint32_t microsecond_
Definition: classic_protocol_binary.h:177
constexpr uint32_t microsecond() const
Definition: classic_protocol_binary.h:156
uint8_t minute_
Definition: classic_protocol_binary.h:175
friend bool operator==(const Time &lhs, const Time &rhs)
Definition: classic_protocol_binary.h:158
uint8_t second_
Definition: classic_protocol_binary.h:176
constexpr uint8_t minute() const
Definition: classic_protocol_binary.h:154
friend bool operator!=(const Time &lhs, const Time &rhs)
Definition: classic_protocol_binary.h:167
constexpr Time(bool is_negative, uint32_t days, uint8_t hour, uint8_t minute, uint8_t second, uint32_t microsecond=0)
Definition: classic_protocol_binary.h:142
constexpr uint8_t second() const
Definition: classic_protocol_binary.h:155
uint32_t days_
Definition: classic_protocol_binary.h:173
constexpr bool is_negative() const
Definition: classic_protocol_binary.h:151
bool is_negative_
Definition: classic_protocol_binary.h:172
Definition: classic_protocol_binary.h:128
Definition: classic_protocol_binary.h:251
Definition: classic_protocol_binary.h:205
base type of all binary scalar value types.
Definition: classic_protocol_binary.h:50
friend bool operator==(const TypeBase &lhs, const TypeBase &rhs)
Definition: classic_protocol_binary.h:58
friend bool operator!=(const TypeBase &lhs, const TypeBase &rhs)
Definition: classic_protocol_binary.h:62
T value_type
Definition: classic_protocol_binary.h:52
constexpr TypeBase(value_type val)
Definition: classic_protocol_binary.h:54
constexpr value_type value() const
Definition: classic_protocol_binary.h:56
value_type v_
Definition: classic_protocol_binary.h:67
Definition: classic_protocol_binary.h:227
Definition: classic_protocol_binary.h:233
Definition: classic_protocol_binary.h:200
borrowable::binary::Float Float
Definition: classic_protocol_binary.h:347
borrowable::binary::LongLong LongLong
Definition: classic_protocol_binary.h:346
borrowable::binary::Date Date
Definition: classic_protocol_binary.h:351
borrowable::binary::Double Double
Definition: classic_protocol_binary.h:348
borrowable::binary::Short Short
Definition: classic_protocol_binary.h:343
borrowable::binary::DateTime DateTime
Definition: classic_protocol_binary.h:352
borrowable::binary::Null Null
Definition: classic_protocol_binary.h:368
borrowable::binary::Long Long
Definition: classic_protocol_binary.h:345
borrowable::binary::Int24 Int24
Definition: classic_protocol_binary.h:344
borrowable::binary::Timestamp Timestamp
Definition: classic_protocol_binary.h:353
borrowable::binary::Tiny Tiny
Definition: classic_protocol_binary.h:342
borrowable::binary::Year Year
Definition: classic_protocol_binary.h:349
std::conditional_t< Borrowed, std::string_view, std::string > string_type
Definition: classic_protocol_binary.h:44
constexpr value_type binary
Definition: classic_protocol_constants.h:275
Definition: classic_protocol_binary.h:39