~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/innodb_plugin/include/data0type.h

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
 
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 as published by the Free Software
 
7
Foundation; version 2 of the License.
 
8
 
 
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.
 
12
 
 
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
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/**************************************************//**
 
20
@file include/data0type.h
 
21
Data types
 
22
 
 
23
Created 1/16/1996 Heikki Tuuri
 
24
*******************************************************/
 
25
 
 
26
#ifndef data0type_h
 
27
#define data0type_h
 
28
 
 
29
#include "univ.i"
 
30
 
 
31
extern ulint    data_mysql_default_charset_coll;
 
32
#define DATA_MYSQL_LATIN1_SWEDISH_CHARSET_COLL 8
 
33
#define DATA_MYSQL_BINARY_CHARSET_COLL 63
 
34
 
 
35
/* SQL data type struct */
 
36
typedef struct dtype_struct             dtype_t;
 
37
 
 
38
/*-------------------------------------------*/
 
39
/* The 'MAIN TYPE' of a column */
 
40
#define DATA_VARCHAR    1       /* character varying of the
 
41
                                latin1_swedish_ci charset-collation; note
 
42
                                that the MySQL format for this, DATA_BINARY,
 
43
                                DATA_VARMYSQL, is also affected by whether the
 
44
                                'precise type' contains
 
45
                                DATA_MYSQL_TRUE_VARCHAR */
 
46
#define DATA_CHAR       2       /* fixed length character of the
 
47
                                latin1_swedish_ci charset-collation */
 
48
#define DATA_FIXBINARY  3       /* binary string of fixed length */
 
49
#define DATA_BINARY     4       /* binary string */
 
50
#define DATA_BLOB       5       /* binary large object, or a TEXT type;
 
51
                                if prtype & DATA_BINARY_TYPE == 0, then this is
 
52
                                actually a TEXT column (or a BLOB created
 
53
                                with < 4.0.14; since column prefix indexes
 
54
                                came only in 4.0.14, the missing flag in BLOBs
 
55
                                created before that does not cause any harm) */
 
56
#define DATA_INT        6       /* integer: can be any size 1 - 8 bytes */
 
57
#define DATA_SYS_CHILD  7       /* address of the child page in node pointer */
 
58
#define DATA_SYS        8       /* system column */
 
59
 
 
60
/* Data types >= DATA_FLOAT must be compared using the whole field, not as
 
61
binary strings */
 
62
 
 
63
#define DATA_FLOAT      9
 
64
#define DATA_DOUBLE     10
 
65
#define DATA_DECIMAL    11      /* decimal number stored as an ASCII string */
 
66
#define DATA_VARMYSQL   12      /* any charset varying length char */
 
67
#define DATA_MYSQL      13      /* any charset fixed length char */
 
68
                                /* NOTE that 4.1.1 used DATA_MYSQL and
 
69
                                DATA_VARMYSQL for all character sets, and the
 
70
                                charset-collation for tables created with it
 
71
                                can also be latin1_swedish_ci */
 
72
#define DATA_MTYPE_MAX  63      /* dtype_store_for_order_and_null_size()
 
73
                                requires the values are <= 63 */
 
74
/*-------------------------------------------*/
 
75
/* The 'PRECISE TYPE' of a column */
 
76
/*
 
77
Tables created by a MySQL user have the following convention:
 
78
 
 
79
- In the least significant byte in the precise type we store the MySQL type
 
80
code (not applicable for system columns).
 
81
 
 
82
- In the second least significant byte we OR flags DATA_NOT_NULL,
 
83
DATA_UNSIGNED, DATA_BINARY_TYPE.
 
84
 
 
85
- In the third least significant byte of the precise type of string types we
 
86
store the MySQL charset-collation code. In DATA_BLOB columns created with
 
87
< 4.0.14 we do not actually know if it is a BLOB or a TEXT column. Since there
 
88
are no indexes on prefixes of BLOB or TEXT columns in < 4.0.14, this is no
 
89
problem, though.
 
90
 
 
91
Note that versions < 4.1.2 or < 5.0.1 did not store the charset code to the
 
92
precise type, since the charset was always the default charset of the MySQL
 
93
installation. If the stored charset code is 0 in the system table SYS_COLUMNS
 
94
of InnoDB, that means that the default charset of this MySQL installation
 
95
should be used.
 
96
 
 
97
When loading a table definition from the system tables to the InnoDB data
 
98
dictionary cache in main memory, InnoDB versions >= 4.1.2 and >= 5.0.1 check
 
99
if the stored charset-collation is 0, and if that is the case and the type is
 
100
a non-binary string, replace that 0 by the default charset-collation code of
 
101
this MySQL installation. In short, in old tables, the charset-collation code
 
102
in the system tables on disk can be 0, but in in-memory data structures
 
103
(dtype_t), the charset-collation code is always != 0 for non-binary string
 
104
types.
 
105
 
 
106
In new tables, in binary string types, the charset-collation code is the
 
107
MySQL code for the 'binary charset', that is, != 0.
 
108
 
 
109
For binary string types and for DATA_CHAR, DATA_VARCHAR, and for those
 
110
DATA_BLOB which are binary or have the charset-collation latin1_swedish_ci,
 
111
InnoDB performs all comparisons internally, without resorting to the MySQL
 
112
comparison functions. This is to save CPU time.
 
113
 
 
114
InnoDB's own internal system tables have different precise types for their
 
115
columns, and for them the precise type is usually not used at all.
 
116
*/
 
117
 
 
118
#define DATA_ENGLISH    4       /* English language character string: this
 
119
                                is a relic from pre-MySQL time and only used
 
120
                                for InnoDB's own system tables */
 
121
#define DATA_ERROR      111     /* another relic from pre-MySQL time */
 
122
 
 
123
#define DATA_MYSQL_TYPE_MASK 255 /* AND with this mask to extract the MySQL
 
124
                                 type from the precise type */
 
125
#define DATA_MYSQL_TRUE_VARCHAR 15 /* MySQL type code for the >= 5.0.3
 
126
                                   format true VARCHAR */
 
127
 
 
128
/* Precise data types for system columns and the length of those columns;
 
129
NOTE: the values must run from 0 up in the order given! All codes must
 
130
be less than 256 */
 
131
#define DATA_ROW_ID     0       /* row id: a dulint */
 
132
#define DATA_ROW_ID_LEN 6       /* stored length for row id */
 
133
 
 
134
#define DATA_TRX_ID     1       /* transaction id: 6 bytes */
 
135
#define DATA_TRX_ID_LEN 6
 
136
 
 
137
#define DATA_ROLL_PTR   2       /* rollback data pointer: 7 bytes */
 
138
#define DATA_ROLL_PTR_LEN 7
 
139
 
 
140
#define DATA_N_SYS_COLS 3       /* number of system columns defined above */
 
141
 
 
142
#define DATA_SYS_PRTYPE_MASK 0xF /* mask to extract the above from prtype */
 
143
 
 
144
/* Flags ORed to the precise data type */
 
145
#define DATA_NOT_NULL   256     /* this is ORed to the precise type when
 
146
                                the column is declared as NOT NULL */
 
147
#define DATA_UNSIGNED   512     /* this id ORed to the precise type when
 
148
                                we have an unsigned integer type */
 
149
#define DATA_BINARY_TYPE 1024   /* if the data type is a binary character
 
150
                                string, this is ORed to the precise type:
 
151
                                this only holds for tables created with
 
152
                                >= MySQL-4.0.14 */
 
153
/* #define      DATA_NONLATIN1  2048 This is a relic from < 4.1.2 and < 5.0.1.
 
154
                                In earlier versions this was set for some
 
155
                                BLOB columns.
 
156
*/
 
157
#define DATA_LONG_TRUE_VARCHAR 4096     /* this is ORed to the precise data
 
158
                                type when the column is true VARCHAR where
 
159
                                MySQL uses 2 bytes to store the data len;
 
160
                                for shorter VARCHARs MySQL uses only 1 byte */
 
161
/*-------------------------------------------*/
 
162
 
 
163
/* This many bytes we need to store the type information affecting the
 
164
alphabetical order for a single field and decide the storage size of an
 
165
SQL null*/
 
166
#define DATA_ORDER_NULL_TYPE_BUF_SIZE           4
 
167
/* In the >= 4.1.x storage format we add 2 bytes more so that we can also
 
168
store the charset-collation number; one byte is left unused, though */
 
169
#define DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE       6
 
170
 
 
171
#ifndef UNIV_HOTBACKUP
 
172
/*********************************************************************//**
 
173
Gets the MySQL type code from a dtype.
 
174
@return MySQL type code; this is NOT an InnoDB type code! */
 
175
UNIV_INLINE
 
176
ulint
 
177
dtype_get_mysql_type(
 
178
/*=================*/
 
179
        const dtype_t*  type);  /*!< in: type struct */
 
180
/*********************************************************************//**
 
181
Determine how many bytes the first n characters of the given string occupy.
 
182
If the string is shorter than n characters, returns the number of bytes
 
183
the characters in the string occupy.
 
184
@return length of the prefix, in bytes */
 
185
UNIV_INTERN
 
186
ulint
 
187
dtype_get_at_most_n_mbchars(
 
188
/*========================*/
 
189
        ulint           prtype,         /*!< in: precise type */
 
190
        ulint           mbminlen,       /*!< in: minimum length of a
 
191
                                        multi-byte character */
 
192
        ulint           mbmaxlen,       /*!< in: maximum length of a
 
193
                                        multi-byte character */
 
194
        ulint           prefix_len,     /*!< in: length of the requested
 
195
                                        prefix, in characters, multiplied by
 
196
                                        dtype_get_mbmaxlen(dtype) */
 
197
        ulint           data_len,       /*!< in: length of str (in bytes) */
 
198
        const char*     str);           /*!< in: the string whose prefix
 
199
                                        length is being determined */
 
200
#endif /* !UNIV_HOTBACKUP */
 
201
/*********************************************************************//**
 
202
Checks if a data main type is a string type. Also a BLOB is considered a
 
203
string type.
 
204
@return TRUE if string type */
 
205
UNIV_INTERN
 
206
ibool
 
207
dtype_is_string_type(
 
208
/*=================*/
 
209
        ulint   mtype); /*!< in: InnoDB main data type code: DATA_CHAR, ... */
 
210
/*********************************************************************//**
 
211
Checks if a type is a binary string type. Note that for tables created with
 
212
< 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. For
 
213
those DATA_BLOB columns this function currently returns FALSE.
 
214
@return TRUE if binary string type */
 
215
UNIV_INTERN
 
216
ibool
 
217
dtype_is_binary_string_type(
 
218
/*========================*/
 
219
        ulint   mtype,  /*!< in: main data type */
 
220
        ulint   prtype);/*!< in: precise type */
 
221
/*********************************************************************//**
 
222
Checks if a type is a non-binary string type. That is, dtype_is_string_type is
 
223
TRUE and dtype_is_binary_string_type is FALSE. Note that for tables created
 
224
with < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column.
 
225
For those DATA_BLOB columns this function currently returns TRUE.
 
226
@return TRUE if non-binary string type */
 
227
UNIV_INTERN
 
228
ibool
 
229
dtype_is_non_binary_string_type(
 
230
/*============================*/
 
231
        ulint   mtype,  /*!< in: main data type */
 
232
        ulint   prtype);/*!< in: precise type */
 
233
/*********************************************************************//**
 
234
Sets a data type structure. */
 
235
UNIV_INLINE
 
236
void
 
237
dtype_set(
 
238
/*======*/
 
239
        dtype_t*        type,   /*!< in: type struct to init */
 
240
        ulint           mtype,  /*!< in: main data type */
 
241
        ulint           prtype, /*!< in: precise type */
 
242
        ulint           len);   /*!< in: precision of type */
 
243
/*********************************************************************//**
 
244
Copies a data type structure. */
 
245
UNIV_INLINE
 
246
void
 
247
dtype_copy(
 
248
/*=======*/
 
249
        dtype_t*        type1,  /*!< in: type struct to copy to */
 
250
        const dtype_t*  type2); /*!< in: type struct to copy from */
 
251
/*********************************************************************//**
 
252
Gets the SQL main data type.
 
253
@return SQL main data type */
 
254
UNIV_INLINE
 
255
ulint
 
256
dtype_get_mtype(
 
257
/*============*/
 
258
        const dtype_t*  type);  /*!< in: data type */
 
259
/*********************************************************************//**
 
260
Gets the precise data type.
 
261
@return precise data type */
 
262
UNIV_INLINE
 
263
ulint
 
264
dtype_get_prtype(
 
265
/*=============*/
 
266
        const dtype_t*  type);  /*!< in: data type */
 
267
#ifndef UNIV_HOTBACKUP
 
268
/*********************************************************************//**
 
269
Compute the mbminlen and mbmaxlen members of a data type structure. */
 
270
UNIV_INLINE
 
271
void
 
272
dtype_get_mblen(
 
273
/*============*/
 
274
        ulint   mtype,          /*!< in: main type */
 
275
        ulint   prtype,         /*!< in: precise type (and collation) */
 
276
        ulint*  mbminlen,       /*!< out: minimum length of a
 
277
                                multi-byte character */
 
278
        ulint*  mbmaxlen);      /*!< out: maximum length of a
 
279
                                multi-byte character */
 
280
/*********************************************************************//**
 
281
Gets the MySQL charset-collation code for MySQL string types.
 
282
@return MySQL charset-collation code */
 
283
UNIV_INLINE
 
284
ulint
 
285
dtype_get_charset_coll(
 
286
/*===================*/
 
287
        ulint   prtype);/*!< in: precise data type */
 
288
/*********************************************************************//**
 
289
Forms a precise type from the < 4.1.2 format precise type plus the
 
290
charset-collation code.
 
291
@return precise type, including the charset-collation code */
 
292
UNIV_INTERN
 
293
ulint
 
294
dtype_form_prtype(
 
295
/*==============*/
 
296
        ulint   old_prtype,     /*!< in: the MySQL type code and the flags
 
297
                                DATA_BINARY_TYPE etc. */
 
298
        ulint   charset_coll);  /*!< in: MySQL charset-collation code */
 
299
/*********************************************************************//**
 
300
Determines if a MySQL string type is a subset of UTF-8.  This function
 
301
may return false negatives, in case further character-set collation
 
302
codes are introduced in MySQL later.
 
303
@return TRUE if a subset of UTF-8 */
 
304
UNIV_INLINE
 
305
ibool
 
306
dtype_is_utf8(
 
307
/*==========*/
 
308
        ulint   prtype);/*!< in: precise data type */
 
309
#endif /* !UNIV_HOTBACKUP */
 
310
/*********************************************************************//**
 
311
Gets the type length.
 
312
@return fixed length of the type, in bytes, or 0 if variable-length */
 
313
UNIV_INLINE
 
314
ulint
 
315
dtype_get_len(
 
316
/*==========*/
 
317
        const dtype_t*  type);  /*!< in: data type */
 
318
#ifndef UNIV_HOTBACKUP
 
319
/*********************************************************************//**
 
320
Gets the minimum length of a character, in bytes.
 
321
@return minimum length of a char, in bytes, or 0 if this is not a
 
322
character type */
 
323
UNIV_INLINE
 
324
ulint
 
325
dtype_get_mbminlen(
 
326
/*===============*/
 
327
        const dtype_t*  type);  /*!< in: type */
 
328
/*********************************************************************//**
 
329
Gets the maximum length of a character, in bytes.
 
330
@return maximum length of a char, in bytes, or 0 if this is not a
 
331
character type */
 
332
UNIV_INLINE
 
333
ulint
 
334
dtype_get_mbmaxlen(
 
335
/*===============*/
 
336
        const dtype_t*  type);  /*!< in: type */
 
337
/*********************************************************************//**
 
338
Gets the padding character code for the type.
 
339
@return padding character code, or ULINT_UNDEFINED if no padding specified */
 
340
UNIV_INLINE
 
341
ulint
 
342
dtype_get_pad_char(
 
343
/*===============*/
 
344
        ulint   mtype,          /*!< in: main type */
 
345
        ulint   prtype);        /*!< in: precise type */
 
346
#endif /* !UNIV_HOTBACKUP */
 
347
/***********************************************************************//**
 
348
Returns the size of a fixed size data type, 0 if not a fixed size type.
 
349
@return fixed size, or 0 */
 
350
UNIV_INLINE
 
351
ulint
 
352
dtype_get_fixed_size_low(
 
353
/*=====================*/
 
354
        ulint   mtype,          /*!< in: main type */
 
355
        ulint   prtype,         /*!< in: precise type */
 
356
        ulint   len,            /*!< in: length */
 
357
        ulint   mbminlen,       /*!< in: minimum length of a multibyte char */
 
358
        ulint   mbmaxlen,       /*!< in: maximum length of a multibyte char */
 
359
        ulint   comp);          /*!< in: nonzero=ROW_FORMAT=COMPACT  */
 
360
#ifndef UNIV_HOTBACKUP
 
361
/***********************************************************************//**
 
362
Returns the minimum size of a data type.
 
363
@return minimum size */
 
364
UNIV_INLINE
 
365
ulint
 
366
dtype_get_min_size_low(
 
367
/*===================*/
 
368
        ulint   mtype,          /*!< in: main type */
 
369
        ulint   prtype,         /*!< in: precise type */
 
370
        ulint   len,            /*!< in: length */
 
371
        ulint   mbminlen,       /*!< in: minimum length of a multibyte char */
 
372
        ulint   mbmaxlen);      /*!< in: maximum length of a multibyte char */
 
373
/***********************************************************************//**
 
374
Returns the maximum size of a data type. Note: types in system tables may be
 
375
incomplete and return incorrect information.
 
376
@return maximum size */
 
377
UNIV_INLINE
 
378
ulint
 
379
dtype_get_max_size_low(
 
380
/*===================*/
 
381
        ulint   mtype,          /*!< in: main type */
 
382
        ulint   len);           /*!< in: length */
 
383
#endif /* !UNIV_HOTBACKUP */
 
384
/***********************************************************************//**
 
385
Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a type.
 
386
For fixed length types it is the fixed length of the type, otherwise 0.
 
387
@return SQL null storage size in ROW_FORMAT=REDUNDANT */
 
388
UNIV_INLINE
 
389
ulint
 
390
dtype_get_sql_null_size(
 
391
/*====================*/
 
392
        const dtype_t*  type,   /*!< in: type */
 
393
        ulint           comp);  /*!< in: nonzero=ROW_FORMAT=COMPACT  */
 
394
#ifndef UNIV_HOTBACKUP
 
395
/**********************************************************************//**
 
396
Reads to a type the stored information which determines its alphabetical
 
397
ordering and the storage size of an SQL NULL value. */
 
398
UNIV_INLINE
 
399
void
 
400
dtype_read_for_order_and_null_size(
 
401
/*===============================*/
 
402
        dtype_t*        type,   /*!< in: type struct */
 
403
        const byte*     buf);   /*!< in: buffer for the stored order info */
 
404
/**********************************************************************//**
 
405
Stores for a type the information which determines its alphabetical ordering
 
406
and the storage size of an SQL NULL value. This is the >= 4.1.x storage
 
407
format. */
 
408
UNIV_INLINE
 
409
void
 
410
dtype_new_store_for_order_and_null_size(
 
411
/*====================================*/
 
412
        byte*           buf,    /*!< in: buffer for
 
413
                                DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE
 
414
                                bytes where we store the info */
 
415
        const dtype_t*  type,   /*!< in: type struct */
 
416
        ulint           prefix_len);/*!< in: prefix length to
 
417
                                replace type->len, or 0 */
 
418
/**********************************************************************//**
 
419
Reads to a type the stored information which determines its alphabetical
 
420
ordering and the storage size of an SQL NULL value. This is the 4.1.x storage
 
421
format. */
 
422
UNIV_INLINE
 
423
void
 
424
dtype_new_read_for_order_and_null_size(
 
425
/*===================================*/
 
426
        dtype_t*        type,   /*!< in: type struct */
 
427
        const byte*     buf);   /*!< in: buffer for stored type order info */
 
428
#endif /* !UNIV_HOTBACKUP */
 
429
 
 
430
/*********************************************************************//**
 
431
Validates a data type structure.
 
432
@return TRUE if ok */
 
433
UNIV_INTERN
 
434
ibool
 
435
dtype_validate(
 
436
/*===========*/
 
437
        const dtype_t*  type);  /*!< in: type struct to validate */
 
438
/*********************************************************************//**
 
439
Prints a data type structure. */
 
440
UNIV_INTERN
 
441
void
 
442
dtype_print(
 
443
/*========*/
 
444
        const dtype_t*  type);  /*!< in: type */
 
445
 
 
446
/* Structure for an SQL data type.
 
447
If you add fields to this structure, be sure to initialize them everywhere.
 
448
This structure is initialized in the following functions:
 
449
dtype_set()
 
450
dtype_read_for_order_and_null_size()
 
451
dtype_new_read_for_order_and_null_size()
 
452
sym_tab_add_null_lit() */
 
453
 
 
454
struct dtype_struct{
 
455
        unsigned        mtype:8;        /*!< main data type */
 
456
        unsigned        prtype:24;      /*!< precise type; MySQL data
 
457
                                        type, charset code, flags to
 
458
                                        indicate nullability,
 
459
                                        signedness, whether this is a
 
460
                                        binary string, whether this is
 
461
                                        a true VARCHAR where MySQL
 
462
                                        uses 2 bytes to store the length */
 
463
 
 
464
        /* the remaining fields do not affect alphabetical ordering: */
 
465
 
 
466
        unsigned        len:16;         /*!< length; for MySQL data this
 
467
                                        is field->pack_length(),
 
468
                                        except that for a >= 5.0.3
 
469
                                        type true VARCHAR this is the
 
470
                                        maximum byte length of the
 
471
                                        string data (in addition to
 
472
                                        the string, MySQL uses 1 or 2
 
473
                                        bytes to store the string length) */
 
474
#ifndef UNIV_HOTBACKUP
 
475
        unsigned        mbminlen:2;     /*!< minimum length of a
 
476
                                        character, in bytes */
 
477
        unsigned        mbmaxlen:3;     /*!< maximum length of a
 
478
                                        character, in bytes */
 
479
#endif /* !UNIV_HOTBACKUP */
 
480
};
 
481
 
 
482
#ifndef UNIV_NONINL
 
483
#include "data0type.ic"
 
484
#endif
 
485
 
 
486
#endif