1
/*****************************************************************************
3
Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file data/data0type.c
23
Created 1/16/1996 Heikki Tuuri
24
*******************************************************/
26
#include "data0type.h"
29
#include "data0type.ic"
32
#ifndef UNIV_HOTBACKUP
33
# include "ha_prototypes.h"
35
/* At the database startup we store the default-charset collation number of
36
this MySQL installation to this global variable. If we have < 4.1.2 format
37
column definitions, or records in the insert buffer, we use this
38
charset-collation code for them. */
40
UNIV_INTERN ulint data_mysql_default_charset_coll;
42
/*********************************************************************//**
43
Determine how many bytes the first n characters of the given string occupy.
44
If the string is shorter than n characters, returns the number of bytes
45
the characters in the string occupy.
46
@return length of the prefix, in bytes */
49
dtype_get_at_most_n_mbchars(
50
/*========================*/
51
ulint prtype, /*!< in: precise type */
52
ulint mbminlen, /*!< in: minimum length of a
53
multi-byte character */
54
ulint mbmaxlen, /*!< in: maximum length of a
55
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 */
63
ut_a(data_len != UNIV_SQL_NULL);
64
ut_ad(!mbmaxlen || !(prefix_len % mbmaxlen));
66
if (mbminlen != mbmaxlen) {
67
ut_a(!(prefix_len % mbmaxlen));
68
return(innobase_get_at_most_n_mbchars(
69
dtype_get_charset_coll(prtype),
70
prefix_len, data_len, str));
73
if (prefix_len < data_len) {
81
#endif /* UNIV_HOTBACKUP */
83
/*********************************************************************//**
84
Checks if a data main type is a string type. Also a BLOB is considered a
86
@return TRUE if string type */
91
ulint mtype) /*!< in: InnoDB main data type code: DATA_CHAR, ... */
93
if (mtype <= DATA_BLOB
94
|| mtype == DATA_MYSQL
95
|| mtype == DATA_VARMYSQL) {
103
/*********************************************************************//**
104
Checks if a type is a binary string type. Note that for tables created with
105
< 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. For
106
those DATA_BLOB columns this function currently returns FALSE.
107
@return TRUE if binary string type */
110
dtype_is_binary_string_type(
111
/*========================*/
112
ulint mtype, /*!< in: main data type */
113
ulint prtype) /*!< in: precise type */
115
if ((mtype == DATA_FIXBINARY)
116
|| (mtype == DATA_BINARY)
117
|| (mtype == DATA_BLOB && (prtype & DATA_BINARY_TYPE))) {
125
/*********************************************************************//**
126
Checks if a type is a non-binary string type. That is, dtype_is_string_type is
127
TRUE and dtype_is_binary_string_type is FALSE. Note that for tables created
128
with < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column.
129
For those DATA_BLOB columns this function currently returns TRUE.
130
@return TRUE if non-binary string type */
133
dtype_is_non_binary_string_type(
134
/*============================*/
135
ulint mtype, /*!< in: main data type */
136
ulint prtype) /*!< in: precise type */
138
if (dtype_is_string_type(mtype) == TRUE
139
&& dtype_is_binary_string_type(mtype, prtype) == FALSE) {
147
/*********************************************************************//**
148
Forms a precise type from the < 4.1.2 format precise type plus the
149
charset-collation code.
150
@return precise type, including the charset-collation code */
155
ulint old_prtype, /*!< in: the MySQL type code and the flags
156
DATA_BINARY_TYPE etc. */
157
ulint charset_coll) /*!< in: MySQL charset-collation code */
159
ut_a(old_prtype < 256 * 256);
160
ut_a(charset_coll < 256);
162
return(old_prtype + (charset_coll << 16));
165
/*********************************************************************//**
166
Validates a data type structure.
167
@return TRUE if ok */
172
const dtype_t* type) /*!< in: type struct to validate */
175
ut_a(type->mtype >= DATA_VARCHAR);
176
ut_a(type->mtype <= DATA_MYSQL);
178
if (type->mtype == DATA_SYS) {
179
ut_a((type->prtype & DATA_MYSQL_TYPE_MASK) < DATA_N_SYS_COLS);
182
#ifndef UNIV_HOTBACKUP
183
ut_a(type->mbminlen <= type->mbmaxlen);
184
#endif /* !UNIV_HOTBACKUP */
189
#ifndef UNIV_HOTBACKUP
190
/*********************************************************************//**
191
Prints a data type structure. */
196
const dtype_t* type) /*!< in: type */
205
prtype = type->prtype;
209
fputs("DATA_VARCHAR", stderr);
213
fputs("DATA_CHAR", stderr);
217
fputs("DATA_BINARY", stderr);
221
fputs("DATA_FIXBINARY", stderr);
225
fputs("DATA_BLOB", stderr);
229
fputs("DATA_INT", stderr);
233
fputs("DATA_MYSQL", stderr);
237
fputs("DATA_SYS", stderr);
241
fputs("DATA_FLOAT", stderr);
245
fputs("DATA_DOUBLE", stderr);
249
fputs("DATA_DECIMAL", stderr);
253
fputs("DATA_VARMYSQL", stderr);
257
fprintf(stderr, "type %lu", (ulong) mtype);
263
if ((type->mtype == DATA_SYS)
264
|| (type->mtype == DATA_VARCHAR)
265
|| (type->mtype == DATA_CHAR)) {
267
if (prtype == DATA_ROW_ID) {
268
fputs("DATA_ROW_ID", stderr);
269
len = DATA_ROW_ID_LEN;
270
} else if (prtype == DATA_ROLL_PTR) {
271
fputs("DATA_ROLL_PTR", stderr);
272
len = DATA_ROLL_PTR_LEN;
273
} else if (prtype == DATA_TRX_ID) {
274
fputs("DATA_TRX_ID", stderr);
275
len = DATA_TRX_ID_LEN;
276
} else if (prtype == DATA_ENGLISH) {
277
fputs("DATA_ENGLISH", stderr);
279
fprintf(stderr, "prtype %lu", (ulong) prtype);
282
if (prtype & DATA_UNSIGNED) {
283
fputs(" DATA_UNSIGNED", stderr);
286
if (prtype & DATA_BINARY_TYPE) {
287
fputs(" DATA_BINARY_TYPE", stderr);
290
if (prtype & DATA_NOT_NULL) {
291
fputs(" DATA_NOT_NULL", stderr);
295
fprintf(stderr, " len %lu", (ulong) len);
297
#endif /* !UNIV_HOTBACKUP */