Debian Codegraph @

storage/innobase/data/data0type.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file data/data0type.cc
29 Data types
30
31 Created 1/16/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "data0type.h"
35 #include "rem0rec.h"
36
37 #include <sys/types.h>
38
39 #include "ha_prototypes.h"
40
41 /* At the database startup we store the default-charset collation number of
42 this MySQL installation to this global variable. If we have < 4.1.2 format
43 column definitions, or records in the insert buffer, we use this
44 charset-collation code for them. */
45
46 ulint data_mysql_default_charset_coll;
47
48 /** Determine how many bytes the first n characters of the given string occupy.
49 If the string is shorter than n characters, returns the number of bytes
50 the characters in the string occupy.
51 @return length of the prefix, in bytes */
52 ulint dtype_get_at_most_n_mbchars(
53 ulint prtype, /*!< in: precise type */
54 ulint mbminmaxlen, /*!< in: minimum and maximum length of
55 a multi-byte character */
56 ulint prefix_len, /*!< in: length of the requested
57 prefix, in characters, multiplied by
58 dtype_get_mbmaxlen(dtype) */
59 ulint data_len, /*!< in: length of str (in bytes) */
60 const char *str) /*!< in: the string whose prefix
61 length is being determined */
62 {
63 ulint mbminlen = DATA_MBMINLEN(mbminmaxlen);
64 ulint mbmaxlen = DATA_MBMAXLEN(mbminmaxlen);
65
66 ut_a(rec_field_not_null_not_add_col_def(data_len));
67 ut_ad(!mbmaxlen || !(prefix_len % mbmaxlen));
68
69 if (mbminlen != mbmaxlen) {
70 ut_a(!(prefix_len % mbmaxlen));
71 return (innobase_get_at_most_n_mbchars(dtype_get_charset_coll(prtype),
72 prefix_len, data_len, str));
73 }
74
75 if (prefix_len < data_len) {
76 return (prefix_len);
77 }
78
79 return (data_len);
80 }
81
82 /** Checks if a data main type is a string type. Also a BLOB is considered a
83 string type.
84 @return true if string type */
85 bool dtype_is_string_type(
86 ulint mtype) /*!< in: InnoDB main data type code: DATA_CHAR, ... */
87 {
88 if (mtype <= DATA_BLOB || mtype == DATA_MYSQL || mtype == DATA_VARMYSQL) {
89 return true;
90 }
91
92 return false;
93 }
94
95 /** Checks if a type is a binary string type. Note that for tables created with
96 < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. For
97 those DATA_BLOB columns this function currently returns false.
98 @return true if binary string type */
99 bool dtype_is_binary_string_type(ulint mtype, /*!< in: main data type */
100 ulint prtype) /*!< in: precise type */
101 {
102 if ((mtype == DATA_FIXBINARY) || (mtype == DATA_BINARY) ||
103 (mtype == DATA_BLOB && (prtype & DATA_BINARY_TYPE))) {
104 return true;
105 }
106
107 return false;
108 }
109
110 /** Checks if a type is a non-binary string type. That is, dtype_is_string_type
111 is true and dtype_is_binary_string_type is false. Note that for tables created
112 with < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column.
113 For those DATA_BLOB columns this function currently returns true.
114 @return true if non-binary string type */
115 bool dtype_is_non_binary_string_type(ulint mtype, /*!< in: main data type */
116 ulint prtype) /*!< in: precise type */
117 {
118 if (dtype_is_string_type(mtype) == true &&
119 dtype_is_binary_string_type(mtype, prtype) == false) {
120 return true;
121 }
122
123 return false;
124 }
125
126 /** Forms a precise type from the < 4.1.2 format precise type plus the
127 charset-collation code.
128 @return precise type, including the charset-collation code */
129 ulint dtype_form_prtype(
130 ulint old_prtype, /*!< in: the MySQL type code and the flags
131 DATA_BINARY_TYPE etc. */
132 ulint charset_coll) /*!< in: MySQL charset-collation code */
133 {
134 ut_a(old_prtype < 256 * 256);
135 ut_a(charset_coll <= MAX_CHAR_COLL_NUM);
136
137 return (old_prtype + (charset_coll << 16));
138 }
139
140 /** Validates a data type structure.
141 @return true if ok */
142 bool dtype_validate(const dtype_t *type) /*!< in: type struct to validate */
143 {
144 ut_a(type);
145 ut_a(type->mtype >= DATA_VARCHAR);
146 ut_a(type->mtype <= DATA_MTYPE_MAX);
147
148 if (type->mtype == DATA_SYS) {
149 ut_a((type->prtype & DATA_MYSQL_TYPE_MASK) < DATA_N_SYS_COLS);
150 }
151
152 ut_a(dtype_get_mbminlen(type) <= dtype_get_mbmaxlen(type));
153
154 return true;
155 }
156
157 #ifdef UNIV_DEBUG
158 /** Print a data type structure.
159 @param[in] type data type */
160 void dtype_print(const dtype_t *type) {
161 ulint mtype;
162 ulint prtype;
163 ulint len;
164
165 ut_a(type);
166
167 mtype = type->mtype;
168 prtype = type->prtype;
169
170 switch (mtype) {
171 case DATA_VARCHAR:
172 fputs("DATA_VARCHAR", stderr);
173 break;
174
175 case DATA_CHAR:
176 fputs("DATA_CHAR", stderr);
177 break;
178
179 case DATA_BINARY:
180 fputs("DATA_BINARY", stderr);
181 break;
182
183 case DATA_FIXBINARY:
184 fputs("DATA_FIXBINARY", stderr);
185 break;
186
187 case DATA_BLOB:
188 fputs("DATA_BLOB", stderr);
189 break;
190
191 case DATA_POINT:
192 fputs("DATA_POINT", stderr);
193 break;
194
195 case DATA_VAR_POINT:
196 fputs("DATA_VAR_POINT", stderr);
197 break;
198
199 case DATA_GEOMETRY:
200 fputs("DATA_GEOMETRY", stderr);
201 break;
202
203 case DATA_INT:
204 fputs("DATA_INT", stderr);
205 break;
206
207 case DATA_MYSQL:
208 fputs("DATA_MYSQL", stderr);
209 break;
210
211 case DATA_SYS:
212 fputs("DATA_SYS", stderr);
213 break;
214
215 case DATA_FLOAT:
216 fputs("DATA_FLOAT", stderr);
217 break;
218
219 case DATA_DOUBLE:
220 fputs("DATA_DOUBLE", stderr);
221 break;
222
223 case DATA_DECIMAL:
224 fputs("DATA_DECIMAL", stderr);
225 break;
226
227 case DATA_VARMYSQL:
228 fputs("DATA_VARMYSQL", stderr);
229 break;
230
231 default:
232 fprintf(stderr, "type %lu", (ulong)mtype);
233 break;
234 }
235
236 len = type->len;
237
238 if ((type->mtype == DATA_SYS) || (type->mtype == DATA_VARCHAR) ||
239 (type->mtype == DATA_CHAR)) {
240 putc(' ', stderr);
241 if (prtype == DATA_ROW_ID) {
242 fputs("DATA_ROW_ID", stderr);
243 len = DATA_ROW_ID_LEN;
244 } else if (prtype == DATA_ROLL_PTR) {
245 fputs("DATA_ROLL_PTR", stderr);
246 len = DATA_ROLL_PTR_LEN;
247 } else if (prtype == DATA_TRX_ID) {
248 fputs("DATA_TRX_ID", stderr);
249 len = DATA_TRX_ID_LEN;
250 } else if (prtype == DATA_ENGLISH) {
251 fputs("DATA_ENGLISH", stderr);
252 } else {
253 fprintf(stderr, "prtype %lu", (ulong)prtype);
254 }
255 } else {
256 if (prtype & DATA_UNSIGNED) {
257 fputs(" DATA_UNSIGNED", stderr);
258 }
259
260 if (prtype & DATA_BINARY_TYPE) {
261 fputs(" DATA_BINARY_TYPE", stderr);
262 }
263
264 if (prtype & DATA_NOT_NULL) {
265 fputs(" DATA_NOT_NULL", stderr);
266 }
267 }
268
269 fprintf(stderr, " len %lu", (ulong)len);
270 }
271 #endif /* UNIV_DEBUG */
272
273 std::ostream &dtype_t::print(std::ostream &out) const {
274 out << "[dtype_t: mtype=" << mtype << " (";
275
276 switch (mtype) {
277 case DATA_VARCHAR:
278 out << "DATA_VARCHAR";
279 break;
280
281 case DATA_CHAR:
282 out << "DATA_CHAR";
283 break;
284
285 case DATA_BINARY:
286 out << "DATA_BINARY";
287 break;
288
289 case DATA_FIXBINARY:
290 out << "DATA_FIXBINARY";
291 break;
292
293 case DATA_BLOB:
294 out << "DATA_BLOB";
295 break;
296
297 case DATA_POINT:
298 out << "DATA_POINT";
299 break;
300
301 case DATA_VAR_POINT:
302 out << "DATA_VAR_POINT";
303 break;
304
305 case DATA_GEOMETRY:
306 out << "DATA_GEOMETRY";
307 break;
308
309 case DATA_INT:
310 out << "DATA_INT";
311 break;
312
313 case DATA_MYSQL:
314 out << "DATA_MYSQL";
315 break;
316
317 case DATA_SYS:
318 out << "DATA_SYS";
319 break;
320
321 case DATA_FLOAT:
322 out << "DATA_FLOAT";
323 break;
324
325 case DATA_DOUBLE:
326 out << "DATA_DOUBLE";
327 break;
328
329 case DATA_DECIMAL:
330 out << "DATA_DECIMAL";
331 break;
332
333 case DATA_VARMYSQL:
334 out << "DATA_VARMYSQL";
335 break;
336
337 default:
338 out << "UKNOWN: " << mtype;
339 break;
340 }
341
342 out << "), prtype=" << prtype << " (";
343
344 if ((mtype == DATA_SYS) || (mtype == DATA_VARCHAR) || (mtype == DATA_CHAR)) {
345 out << ' ';
346 if (prtype == DATA_ROW_ID) {
347 out << "DATA_ROW_ID";
348 } else if (prtype == DATA_ROLL_PTR) {
349 out << "DATA_ROLL_PTR";
350 } else if (prtype == DATA_TRX_ID) {
351 out << "DATA_TRX_ID";
352 } else if (prtype == DATA_ENGLISH) {
353 out << "DATA_ENGLISH";
354 } else {
355 out << "UNKNOWN: " << prtype;
356 }
357 } else {
358 if (prtype & DATA_UNSIGNED) {
359 out << " DATA_UNSIGNED";
360 }
361
362 if (prtype & DATA_BINARY_TYPE) {
363 out << " DATA_BINARY_TYPE";
364 }
365
366 if (prtype & DATA_NOT_NULL) {
367 out << " DATA_NOT_NULL";
368 }
369
370 if (prtype & DATA_VIRTUAL) {
371 out << " DATA_VIRTUAL";
372 }
373
374 const auto other_flags = prtype & ~(DATA_UNSIGNED | DATA_BINARY_TYPE |
375 DATA_NOT_NULL | DATA_VIRTUAL);
376
377 if (other_flags != 0) {
378 out << " UNKNOWN:" << other_flags;
379 }
380 }
381
382 out << "), len=" << len << "]";
383
384 return (out);
385 }