~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to storage/innobase/data/data0type.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************
 
2
Data types
 
3
 
 
4
(c) 1996 Innobase Oy
 
5
 
 
6
Created 1/16/1996 Heikki Tuuri
 
7
*******************************************************/
 
8
 
 
9
#include "data0type.h"
 
10
 
 
11
#ifdef UNIV_NONINL
 
12
#include "data0type.ic"
 
13
#endif
 
14
 
 
15
/**********************************************************************
 
16
This function is used to find the storage length in bytes of the first n
 
17
characters for prefix indexes using a multibyte character set. The function
 
18
finds charset information and returns length of prefix_len characters in the
 
19
index field in bytes.
 
20
 
 
21
NOTE: the prototype of this function is copied from ha_innodb.cc! If you change
 
22
this function, you MUST change also the prototype here! */
 
23
 
 
24
ulint
 
25
innobase_get_at_most_n_mbchars(
 
26
/*===========================*/
 
27
                                /* out: number of bytes occupied by the first
 
28
                                n characters */
 
29
        ulint charset_id,       /* in: character set id */
 
30
        ulint prefix_len,       /* in: prefix length in bytes of the index
 
31
                                (this has to be divided by mbmaxlen to get the
 
32
                                number of CHARACTERS n in the prefix) */
 
33
        ulint data_len,         /* in: length of the string in bytes */
 
34
        const char* str);       /* in: character string */
 
35
 
 
36
/* At the database startup we store the default-charset collation number of
 
37
this MySQL installation to this global variable. If we have < 4.1.2 format
 
38
column definitions, or records in the insert buffer, we use this
 
39
charset-collation code for them. */
 
40
 
 
41
ulint   data_mysql_default_charset_coll         = 99999999;
 
42
 
 
43
/*************************************************************************
 
44
Determine how many bytes the first n characters of the given string occupy.
 
45
If the string is shorter than n characters, returns the number of bytes
 
46
the characters in the string occupy. */
 
47
 
 
48
ulint
 
49
dtype_get_at_most_n_mbchars(
 
50
/*========================*/
 
51
                                        /* out: length of the prefix,
 
52
                                        in bytes */
 
53
        ulint           prtype,         /* in: precise type */
 
54
        ulint           mbminlen,       /* in: minimum length of a
 
55
                                        multi-byte character */
 
56
        ulint           mbmaxlen,       /* in: maximum length of a
 
57
                                        multi-byte character */
 
58
        ulint           prefix_len,     /* in: length of the requested
 
59
                                        prefix, in characters, multiplied by
 
60
                                        dtype_get_mbmaxlen(dtype) */
 
61
        ulint           data_len,       /* in: length of str (in bytes) */
 
62
        const char*     str)            /* in: the string whose prefix
 
63
                                        length is being determined */
 
64
{
 
65
#ifndef UNIV_HOTBACKUP
 
66
        ut_a(data_len != UNIV_SQL_NULL);
 
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(
 
72
                        dtype_get_charset_coll(prtype),
 
73
                        prefix_len, data_len, str));
 
74
        }
 
75
 
 
76
        if (prefix_len < data_len) {
 
77
 
 
78
                return(prefix_len);
 
79
 
 
80
        }
 
81
 
 
82
        return(data_len);
 
83
#else /* UNIV_HOTBACKUP */
 
84
        /* This function depends on MySQL code that is not included in
 
85
        InnoDB Hot Backup builds.  Besides, this function should never
 
86
        be called in InnoDB Hot Backup. */
 
87
        ut_error;
 
88
#endif /* UNIV_HOTBACKUP */
 
89
}
 
90
 
 
91
/*************************************************************************
 
92
Checks if a data main type is a string type. Also a BLOB is considered a
 
93
string type. */
 
94
 
 
95
ibool
 
96
dtype_is_string_type(
 
97
/*=================*/
 
98
                        /* out: TRUE if string type */
 
99
        ulint   mtype)  /* in: InnoDB main data type code: DATA_CHAR, ... */
 
100
{
 
101
        if (mtype <= DATA_BLOB
 
102
            || mtype == DATA_MYSQL
 
103
            || mtype == DATA_VARMYSQL) {
 
104
 
 
105
                return(TRUE);
 
106
        }
 
107
 
 
108
        return(FALSE);
 
109
}
 
110
 
 
111
/*************************************************************************
 
112
Checks if a type is a binary string type. Note that for tables created with
 
113
< 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. For
 
114
those DATA_BLOB columns this function currently returns FALSE. */
 
115
 
 
116
ibool
 
117
dtype_is_binary_string_type(
 
118
/*========================*/
 
119
                        /* out: TRUE if binary string type */
 
120
        ulint   mtype,  /* in: main data type */
 
121
        ulint   prtype) /* in: precise type */
 
122
{
 
123
        if ((mtype == DATA_FIXBINARY)
 
124
            || (mtype == DATA_BINARY)
 
125
            || (mtype == DATA_BLOB && (prtype & DATA_BINARY_TYPE))) {
 
126
 
 
127
                return(TRUE);
 
128
        }
 
129
 
 
130
        return(FALSE);
 
131
}
 
132
 
 
133
/*************************************************************************
 
134
Checks if a type is a non-binary string type. That is, dtype_is_string_type is
 
135
TRUE and dtype_is_binary_string_type is FALSE. Note that for tables created
 
136
with < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column.
 
137
For those DATA_BLOB columns this function currently returns TRUE. */
 
138
 
 
139
ibool
 
140
dtype_is_non_binary_string_type(
 
141
/*============================*/
 
142
                        /* out: TRUE if non-binary string type */
 
143
        ulint   mtype,  /* in: main data type */
 
144
        ulint   prtype) /* in: precise type */
 
145
{
 
146
        if (dtype_is_string_type(mtype) == TRUE
 
147
            && dtype_is_binary_string_type(mtype, prtype) == FALSE) {
 
148
 
 
149
                return(TRUE);
 
150
        }
 
151
 
 
152
        return(FALSE);
 
153
}
 
154
 
 
155
/*************************************************************************
 
156
Gets the MySQL charset-collation code for MySQL string types. */
 
157
 
 
158
ulint
 
159
dtype_get_charset_coll_noninline(
 
160
/*=============================*/
 
161
        ulint   prtype) /* in: precise data type */
 
162
{
 
163
        return(dtype_get_charset_coll(prtype));
 
164
}
 
165
 
 
166
/*************************************************************************
 
167
Forms a precise type from the < 4.1.2 format precise type plus the
 
168
charset-collation code. */
 
169
 
 
170
ulint
 
171
dtype_form_prtype(
 
172
/*==============*/
 
173
        ulint   old_prtype,     /* in: the MySQL type code and the flags
 
174
                                DATA_BINARY_TYPE etc. */
 
175
        ulint   charset_coll)   /* in: MySQL charset-collation code */
 
176
{
 
177
        ut_a(old_prtype < 256 * 256);
 
178
        ut_a(charset_coll < 256);
 
179
 
 
180
        return(old_prtype + (charset_coll << 16));
 
181
}
 
182
 
 
183
/*************************************************************************
 
184
Validates a data type structure. */
 
185
 
 
186
ibool
 
187
dtype_validate(
 
188
/*===========*/
 
189
                                /* out: TRUE if ok */
 
190
        dtype_t*        type)   /* in: type struct to validate */
 
191
{
 
192
        ut_a(type);
 
193
        ut_a(type->mtype >= DATA_VARCHAR);
 
194
        ut_a(type->mtype <= DATA_MYSQL);
 
195
 
 
196
        if (type->mtype == DATA_SYS) {
 
197
                ut_a((type->prtype & DATA_MYSQL_TYPE_MASK) < DATA_N_SYS_COLS);
 
198
        }
 
199
 
 
200
        ut_a(type->mbminlen <= type->mbmaxlen);
 
201
 
 
202
        return(TRUE);
 
203
}
 
204
 
 
205
/*************************************************************************
 
206
Prints a data type structure. */
 
207
 
 
208
void
 
209
dtype_print(
 
210
/*========*/
 
211
        dtype_t*        type)   /* in: type */
 
212
{
 
213
        ulint   mtype;
 
214
        ulint   prtype;
 
215
        ulint   len;
 
216
 
 
217
        ut_a(type);
 
218
 
 
219
        mtype = type->mtype;
 
220
        prtype = type->prtype;
 
221
 
 
222
        switch (mtype) {
 
223
        case DATA_VARCHAR:
 
224
                fputs("DATA_VARCHAR", stderr);
 
225
                break;
 
226
 
 
227
        case DATA_CHAR:
 
228
                fputs("DATA_CHAR", stderr);
 
229
                break;
 
230
 
 
231
        case DATA_BINARY:
 
232
                fputs("DATA_BINARY", stderr);
 
233
                break;
 
234
 
 
235
        case DATA_FIXBINARY:
 
236
                fputs("DATA_FIXBINARY", stderr);
 
237
                break;
 
238
 
 
239
        case DATA_BLOB:
 
240
                fputs("DATA_BLOB", stderr);
 
241
                break;
 
242
 
 
243
        case DATA_INT:
 
244
                fputs("DATA_INT", stderr);
 
245
                break;
 
246
 
 
247
        case DATA_MYSQL:
 
248
                fputs("DATA_MYSQL", stderr);
 
249
                break;
 
250
 
 
251
        case DATA_SYS:
 
252
                fputs("DATA_SYS", stderr);
 
253
                break;
 
254
 
 
255
        default:
 
256
                fprintf(stderr, "type %lu", (ulong) mtype);
 
257
                break;
 
258
        }
 
259
 
 
260
        len = type->len;
 
261
 
 
262
        if ((type->mtype == DATA_SYS)
 
263
            || (type->mtype == DATA_VARCHAR)
 
264
            || (type->mtype == DATA_CHAR)) {
 
265
                putc(' ', stderr);
 
266
                if (prtype == DATA_ROW_ID) {
 
267
                        fputs("DATA_ROW_ID", stderr);
 
268
                        len = DATA_ROW_ID_LEN;
 
269
                } else if (prtype == DATA_ROLL_PTR) {
 
270
                        fputs("DATA_ROLL_PTR", stderr);
 
271
                        len = DATA_ROLL_PTR_LEN;
 
272
                } else if (prtype == DATA_TRX_ID) {
 
273
                        fputs("DATA_TRX_ID", stderr);
 
274
                        len = DATA_TRX_ID_LEN;
 
275
                } else if (prtype == DATA_ENGLISH) {
 
276
                        fputs("DATA_ENGLISH", stderr);
 
277
                } else {
 
278
                        fprintf(stderr, "prtype %lu", (ulong) prtype);
 
279
                }
 
280
        } else {
 
281
                if (prtype & DATA_UNSIGNED) {
 
282
                        fputs(" DATA_UNSIGNED", stderr);
 
283
                }
 
284
 
 
285
                if (prtype & DATA_BINARY_TYPE) {
 
286
                        fputs(" DATA_BINARY_TYPE", stderr);
 
287
                }
 
288
 
 
289
                if (prtype & DATA_NOT_NULL) {
 
290
                        fputs(" DATA_NOT_NULL", stderr);
 
291
                }
 
292
        }
 
293
 
 
294
        fprintf(stderr, " len %lu", (ulong) len);
 
295
}