~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/field.cc

Removed/replaced DBUG symbols and standardized TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  @brief
21
21
  This file implements classes defined in field.h
22
22
*/
23
 
#include <drizzled/server_includes.h>
 
23
 
 
24
#ifdef USE_PRAGMA_IMPLEMENTATION
 
25
#pragma implementation                          // gcc: Class implementation
 
26
#endif
 
27
 
 
28
#include "mysql_priv.h"
24
29
#include "sql_select.h"
 
30
#include <m_ctype.h>
25
31
#include <errno.h>
26
 
#include <drizzled/drizzled_error_messages.h>
27
32
 
28
33
// Maximum allowed exponent value for converting string to decimal
29
34
#define MAX_EXPONENT 1024
37
42
template class List_iterator<Create_field>;
38
43
#endif
39
44
 
 
45
uchar Field_null::null[1]={1};
 
46
const char field_separator=',';
 
47
 
 
48
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
 
49
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
 
50
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
 
51
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
 
52
((ulong) ((1LL << min(arg, 4) * 8) - 1LL))
 
53
 
 
54
#define ASSERT_COLUMN_MARKED_FOR_READ DBUG_ASSERT(!table || (!table->read_set || bitmap_is_set(table->read_set, field_index)))
 
55
#define ASSERT_COLUMN_MARKED_FOR_WRITE DBUG_ASSERT(!table || (!table->write_set || bitmap_is_set(table->write_set, field_index)))
40
56
 
41
57
/*
42
58
  Rules for merging different types of fields in UNION
45
61
  following #defines describe that gap and how to canculate number of fields
46
62
  and index of field in thia array.
47
63
*/
48
 
#define FIELDTYPE_TEAR_FROM (DRIZZLE_TYPE_VARCHAR + 1)
49
 
#define FIELDTYPE_TEAR_TO   (DRIZZLE_TYPE_NEWDECIMAL - 1)
 
64
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_VARCHAR + 1)
 
65
#define FIELDTYPE_TEAR_TO   (MYSQL_TYPE_NEWDECIMAL - 1)
50
66
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
51
67
inline int field_type2index (enum_field_types field_type)
52
68
{
58
74
 
59
75
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
60
76
{
61
 
  /* DRIZZLE_TYPE_DECIMAL -> */
62
 
  {
63
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
64
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
65
 
  //DRIZZLE_TYPE_LONG
66
 
    DRIZZLE_TYPE_NEWDECIMAL,
67
 
  //DRIZZLE_TYPE_DOUBLE
68
 
    DRIZZLE_TYPE_DOUBLE,
69
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
70
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
71
 
  //DRIZZLE_TYPE_LONGLONG
72
 
    DRIZZLE_TYPE_NEWDECIMAL,
73
 
  //DRIZZLE_TYPE_TIME
74
 
    DRIZZLE_TYPE_VARCHAR,
75
 
  //DRIZZLE_TYPE_DATETIME
76
 
    DRIZZLE_TYPE_VARCHAR,
77
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
78
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
79
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
80
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
81
 
  //DRIZZLE_TYPE_BLOB
82
 
    DRIZZLE_TYPE_BLOB,
83
 
  },
84
 
  /* DRIZZLE_TYPE_TINY -> */
85
 
  {
86
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
87
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
88
 
  //DRIZZLE_TYPE_LONG
89
 
    DRIZZLE_TYPE_LONG,
90
 
  //DRIZZLE_TYPE_DOUBLE
91
 
    DRIZZLE_TYPE_DOUBLE,
92
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
93
 
    DRIZZLE_TYPE_TINY,        DRIZZLE_TYPE_VARCHAR,
94
 
  //DRIZZLE_TYPE_LONGLONG
95
 
    DRIZZLE_TYPE_LONGLONG,
96
 
  //DRIZZLE_TYPE_TIME
97
 
    DRIZZLE_TYPE_VARCHAR,
98
 
  //DRIZZLE_TYPE_DATETIME
99
 
    DRIZZLE_TYPE_VARCHAR,
100
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
101
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
102
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
103
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
104
 
  //DRIZZLE_TYPE_BLOB
105
 
    DRIZZLE_TYPE_BLOB,
106
 
  },
107
 
  /* DRIZZLE_TYPE_LONG -> */
108
 
  {
109
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
110
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONG,
111
 
  //DRIZZLE_TYPE_LONG
112
 
    DRIZZLE_TYPE_LONG,
113
 
  //DRIZZLE_TYPE_DOUBLE
114
 
    DRIZZLE_TYPE_DOUBLE,
115
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
116
 
    DRIZZLE_TYPE_LONG,         DRIZZLE_TYPE_VARCHAR,
117
 
  //DRIZZLE_TYPE_LONGLONG
118
 
    DRIZZLE_TYPE_LONGLONG,
119
 
  //DRIZZLE_TYPE_TIME
120
 
    DRIZZLE_TYPE_VARCHAR,
121
 
  //DRIZZLE_TYPE_DATETIME
122
 
    DRIZZLE_TYPE_VARCHAR,
123
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
124
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
125
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
126
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
127
 
  //DRIZZLE_TYPE_BLOB
128
 
    DRIZZLE_TYPE_BLOB,
129
 
  },
130
 
  /* DRIZZLE_TYPE_DOUBLE -> */
131
 
  {
132
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
133
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_DOUBLE,
134
 
  //DRIZZLE_TYPE_LONG
135
 
    DRIZZLE_TYPE_DOUBLE,
136
 
  //DRIZZLE_TYPE_DOUBLE
137
 
    DRIZZLE_TYPE_DOUBLE,
138
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
139
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
140
 
  //DRIZZLE_TYPE_LONGLONG
141
 
    DRIZZLE_TYPE_DOUBLE,
142
 
  //DRIZZLE_TYPE_TIME
143
 
    DRIZZLE_TYPE_VARCHAR,
144
 
  //DRIZZLE_TYPE_DATETIME
145
 
    DRIZZLE_TYPE_VARCHAR,
146
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
147
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
148
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
149
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
150
 
  //DRIZZLE_TYPE_BLOB
151
 
    DRIZZLE_TYPE_BLOB,
152
 
  },
153
 
  /* DRIZZLE_TYPE_NULL -> */
154
 
  {
155
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
156
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
157
 
  //DRIZZLE_TYPE_LONG
158
 
    DRIZZLE_TYPE_LONG,
159
 
  //DRIZZLE_TYPE_DOUBLE
160
 
    DRIZZLE_TYPE_DOUBLE,
161
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
162
 
    DRIZZLE_TYPE_NULL,        DRIZZLE_TYPE_TIMESTAMP,
163
 
  //DRIZZLE_TYPE_LONGLONG
164
 
    DRIZZLE_TYPE_LONGLONG,
165
 
  //DRIZZLE_TYPE_TIME
166
 
    DRIZZLE_TYPE_TIME,
167
 
  //DRIZZLE_TYPE_DATETIME
168
 
    DRIZZLE_TYPE_DATETIME,
169
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
170
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
171
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
172
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_ENUM,
173
 
  //DRIZZLE_TYPE_BLOB
174
 
    DRIZZLE_TYPE_BLOB,
175
 
  },
176
 
  /* DRIZZLE_TYPE_TIMESTAMP -> */
177
 
  {
178
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
179
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
180
 
  //DRIZZLE_TYPE_LONG
181
 
    DRIZZLE_TYPE_VARCHAR,
182
 
  //DRIZZLE_TYPE_DOUBLE
183
 
    DRIZZLE_TYPE_VARCHAR,
184
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
185
 
    DRIZZLE_TYPE_TIMESTAMP,   DRIZZLE_TYPE_TIMESTAMP,
186
 
  //DRIZZLE_TYPE_LONGLONG
187
 
    DRIZZLE_TYPE_VARCHAR,
188
 
  //DRIZZLE_TYPE_TIME
189
 
    DRIZZLE_TYPE_DATETIME,
190
 
  //DRIZZLE_TYPE_DATETIME
191
 
    DRIZZLE_TYPE_DATETIME,
192
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
193
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
194
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
195
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
196
 
  //DRIZZLE_TYPE_BLOB
197
 
    DRIZZLE_TYPE_BLOB,
198
 
  },
199
 
  /* DRIZZLE_TYPE_LONGLONG -> */
200
 
  {
201
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
202
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONGLONG,
203
 
  //DRIZZLE_TYPE_LONG
204
 
    DRIZZLE_TYPE_LONGLONG,
205
 
  //DRIZZLE_TYPE_DOUBLE
206
 
    DRIZZLE_TYPE_DOUBLE,
207
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
208
 
    DRIZZLE_TYPE_LONGLONG,    DRIZZLE_TYPE_VARCHAR,
209
 
  //DRIZZLE_TYPE_LONGLONG
210
 
    DRIZZLE_TYPE_LONGLONG,
211
 
  //DRIZZLE_TYPE_TIME
212
 
    DRIZZLE_TYPE_VARCHAR,
213
 
  //DRIZZLE_TYPE_DATETIME
214
 
    DRIZZLE_TYPE_VARCHAR,
215
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
216
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
217
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
218
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
219
 
  //DRIZZLE_TYPE_BLOB
220
 
    DRIZZLE_TYPE_BLOB,
221
 
  },
222
 
  /* DRIZZLE_TYPE_TIME -> */
223
 
  {
224
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
225
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
226
 
  //DRIZZLE_TYPE_LONG
227
 
    DRIZZLE_TYPE_VARCHAR,
228
 
  //DRIZZLE_TYPE_DOUBLE
229
 
    DRIZZLE_TYPE_VARCHAR,
230
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
231
 
    DRIZZLE_TYPE_TIME,        DRIZZLE_TYPE_DATETIME,
232
 
  //DRIZZLE_TYPE_LONGLONG
233
 
    DRIZZLE_TYPE_VARCHAR,
234
 
  //DRIZZLE_TYPE_TIME
235
 
    DRIZZLE_TYPE_TIME,
236
 
  //DRIZZLE_TYPE_DATETIME
237
 
    DRIZZLE_TYPE_DATETIME,
238
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
239
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
240
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
241
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
242
 
  //DRIZZLE_TYPE_BLOB
243
 
    DRIZZLE_TYPE_BLOB,
244
 
  },
245
 
  /* DRIZZLE_TYPE_DATETIME -> */
246
 
  {
247
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
248
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
249
 
  //DRIZZLE_TYPE_LONG
250
 
    DRIZZLE_TYPE_VARCHAR,
251
 
  //DRIZZLE_TYPE_DOUBLE
252
 
    DRIZZLE_TYPE_VARCHAR,
253
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
254
 
    DRIZZLE_TYPE_DATETIME,    DRIZZLE_TYPE_DATETIME,
255
 
  //DRIZZLE_TYPE_LONGLONG
256
 
    DRIZZLE_TYPE_VARCHAR,
257
 
  //DRIZZLE_TYPE_TIME
258
 
    DRIZZLE_TYPE_DATETIME,
259
 
  //DRIZZLE_TYPE_DATETIME
260
 
    DRIZZLE_TYPE_DATETIME,
261
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
262
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
263
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
264
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
265
 
  //DRIZZLE_TYPE_BLOB
266
 
    DRIZZLE_TYPE_BLOB,
267
 
  },
268
 
  /* DRIZZLE_TYPE_NEWDATE -> */
269
 
  {
270
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
271
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
272
 
  //DRIZZLE_TYPE_LONG
273
 
    DRIZZLE_TYPE_VARCHAR,
274
 
  //DRIZZLE_TYPE_DOUBLE
275
 
    DRIZZLE_TYPE_VARCHAR,
276
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
277
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_DATETIME,
278
 
  //DRIZZLE_TYPE_LONGLONG
279
 
    DRIZZLE_TYPE_VARCHAR,
280
 
  //DRIZZLE_TYPE_TIME
281
 
    DRIZZLE_TYPE_DATETIME,
282
 
  //DRIZZLE_TYPE_DATETIME
283
 
    DRIZZLE_TYPE_DATETIME,
284
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
285
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
286
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
287
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
288
 
  //DRIZZLE_TYPE_BLOB
289
 
    DRIZZLE_TYPE_BLOB,
290
 
  },
291
 
  /* DRIZZLE_TYPE_VARCHAR -> */
292
 
  {
293
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
294
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
295
 
  //DRIZZLE_TYPE_LONG
296
 
    DRIZZLE_TYPE_VARCHAR,
297
 
  //DRIZZLE_TYPE_DOUBLE
298
 
    DRIZZLE_TYPE_VARCHAR,
299
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
300
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
301
 
  //DRIZZLE_TYPE_LONGLONG
302
 
    DRIZZLE_TYPE_VARCHAR,
303
 
  //DRIZZLE_TYPE_TIME
304
 
    DRIZZLE_TYPE_VARCHAR,
305
 
  //DRIZZLE_TYPE_DATETIME
306
 
    DRIZZLE_TYPE_VARCHAR,
307
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
308
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
309
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
310
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
311
 
  //DRIZZLE_TYPE_BLOB
312
 
    DRIZZLE_TYPE_BLOB,
313
 
  },
314
 
  /* DRIZZLE_TYPE_NEWDECIMAL -> */
315
 
  {
316
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
317
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
318
 
  //DRIZZLE_TYPE_LONG
319
 
    DRIZZLE_TYPE_NEWDECIMAL,
320
 
  //DRIZZLE_TYPE_DOUBLE
321
 
    DRIZZLE_TYPE_DOUBLE,
322
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
323
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
324
 
  //DRIZZLE_TYPE_LONGLONG
325
 
    DRIZZLE_TYPE_NEWDECIMAL,
326
 
  //DRIZZLE_TYPE_TIME
327
 
    DRIZZLE_TYPE_VARCHAR,
328
 
  //DRIZZLE_TYPE_DATETIME
329
 
    DRIZZLE_TYPE_VARCHAR,
330
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
331
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
332
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
333
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
334
 
  //DRIZZLE_TYPE_BLOB
335
 
    DRIZZLE_TYPE_BLOB,
336
 
  },
337
 
  /* DRIZZLE_TYPE_ENUM -> */
338
 
  {
339
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
340
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
341
 
  //DRIZZLE_TYPE_LONG
342
 
    DRIZZLE_TYPE_VARCHAR,
343
 
  //DRIZZLE_TYPE_DOUBLE
344
 
    DRIZZLE_TYPE_VARCHAR,
345
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
346
 
    DRIZZLE_TYPE_ENUM,        DRIZZLE_TYPE_VARCHAR,
347
 
  //DRIZZLE_TYPE_LONGLONG
348
 
    DRIZZLE_TYPE_VARCHAR,
349
 
  //DRIZZLE_TYPE_TIME
350
 
    DRIZZLE_TYPE_VARCHAR,
351
 
  //DRIZZLE_TYPE_DATETIME
352
 
    DRIZZLE_TYPE_VARCHAR,
353
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
354
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
355
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
356
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
357
 
  //DRIZZLE_TYPE_BLOB
358
 
    DRIZZLE_TYPE_BLOB,
359
 
  },
360
 
  /* DRIZZLE_TYPE_BLOB -> */
361
 
  {
362
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
363
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
364
 
  //DRIZZLE_TYPE_LONG
365
 
    DRIZZLE_TYPE_BLOB,
366
 
  //DRIZZLE_TYPE_DOUBLE
367
 
    DRIZZLE_TYPE_BLOB,
368
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
369
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
370
 
  //DRIZZLE_TYPE_LONGLONG
371
 
    DRIZZLE_TYPE_BLOB,
372
 
  //DRIZZLE_TYPE_TIME
373
 
    DRIZZLE_TYPE_BLOB,
374
 
  //DRIZZLE_TYPE_DATETIME
375
 
    DRIZZLE_TYPE_BLOB,
376
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
377
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
378
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
379
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
380
 
  //DRIZZLE_TYPE_BLOB
381
 
    DRIZZLE_TYPE_BLOB,
382
 
  },
 
77
  /* MYSQL_TYPE_DECIMAL -> */
 
78
  {
 
79
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
80
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
81
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
82
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
83
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
84
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
85
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
86
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
87
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
88
  //MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
 
89
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
90
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
91
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
92
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
93
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
94
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
95
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
96
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
97
  //MYSQL_TYPE_SET
 
98
    MYSQL_TYPE_VARCHAR,
 
99
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
100
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
101
  //MYSQL_TYPE_STRING       
 
102
    MYSQL_TYPE_STRING
 
103
  },
 
104
  /* MYSQL_TYPE_TINY -> */
 
105
  {
 
106
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
107
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
108
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
109
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
110
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
111
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
112
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
113
    MYSQL_TYPE_TINY,        MYSQL_TYPE_VARCHAR,
 
114
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
115
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
116
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
117
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
118
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
119
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY,
 
120
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
121
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
122
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
123
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
124
  //MYSQL_TYPE_SET
 
125
    MYSQL_TYPE_VARCHAR,
 
126
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
127
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
128
  //MYSQL_TYPE_STRING       
 
129
    MYSQL_TYPE_STRING     
 
130
  },
 
131
  /* MYSQL_TYPE_SHORT -> */
 
132
  {
 
133
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
134
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_SHORT,
 
135
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
136
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
137
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
138
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
139
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
140
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_VARCHAR,
 
141
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
142
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
143
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
144
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
145
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
146
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_SHORT,
 
147
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
148
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
149
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
150
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
151
  //MYSQL_TYPE_SET
 
152
    MYSQL_TYPE_VARCHAR,
 
153
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
154
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
155
  //MYSQL_TYPE_STRING
 
156
    MYSQL_TYPE_STRING
 
157
  },
 
158
  /* MYSQL_TYPE_LONG -> */
 
159
  {
 
160
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
161
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONG,
 
162
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
163
    MYSQL_TYPE_LONG,        MYSQL_TYPE_LONG,
 
164
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
165
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
166
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
167
    MYSQL_TYPE_LONG,         MYSQL_TYPE_VARCHAR,
 
168
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
169
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
170
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
171
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
172
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
173
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONG,
 
174
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
175
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
176
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
177
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
178
  //MYSQL_TYPE_SET
 
179
    MYSQL_TYPE_VARCHAR,
 
180
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
181
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
182
  //MYSQL_TYPE_STRING
 
183
    MYSQL_TYPE_STRING
 
184
  },
 
185
  /* MYSQL_TYPE_FLOAT -> */
 
186
  {
 
187
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
188
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_FLOAT,
 
189
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
190
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
191
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
192
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
193
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
194
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_VARCHAR,
 
195
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
196
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_INT24,
 
197
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
198
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
199
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
200
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_FLOAT,
 
201
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
202
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
203
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
204
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
205
  //MYSQL_TYPE_SET
 
206
    MYSQL_TYPE_VARCHAR,
 
207
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
208
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
209
  //MYSQL_TYPE_STRING
 
210
    MYSQL_TYPE_STRING
 
211
  },
 
212
  /* MYSQL_TYPE_DOUBLE -> */
 
213
  {
 
214
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
215
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
216
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
217
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
218
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
219
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
220
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
221
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
222
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
223
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_INT24,
 
224
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
225
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
226
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
227
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_DOUBLE,
 
228
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
229
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
230
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
231
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
232
  //MYSQL_TYPE_SET
 
233
    MYSQL_TYPE_VARCHAR,
 
234
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
235
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
236
  //MYSQL_TYPE_STRING
 
237
    MYSQL_TYPE_STRING
 
238
  },
 
239
  /* MYSQL_TYPE_NULL -> */
 
240
  {
 
241
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
242
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
243
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
244
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
245
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
246
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
247
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
248
    MYSQL_TYPE_NULL,        MYSQL_TYPE_TIMESTAMP,
 
249
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
250
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
251
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
252
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_TIME,
 
253
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
254
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_YEAR,
 
255
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
256
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
257
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
258
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_ENUM,
 
259
  //MYSQL_TYPE_SET
 
260
    MYSQL_TYPE_SET,
 
261
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
262
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
263
  //MYSQL_TYPE_STRING
 
264
    MYSQL_TYPE_STRING
 
265
  },
 
266
  /* MYSQL_TYPE_TIMESTAMP -> */
 
267
  {
 
268
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
269
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
270
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
271
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
272
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
273
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
274
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
275
    MYSQL_TYPE_TIMESTAMP,   MYSQL_TYPE_TIMESTAMP,
 
276
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
277
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
278
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
279
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
280
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
281
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
282
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
283
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
284
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
285
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
286
  //MYSQL_TYPE_SET
 
287
    MYSQL_TYPE_VARCHAR,
 
288
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
289
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
290
  //MYSQL_TYPE_STRING
 
291
    MYSQL_TYPE_STRING
 
292
  },
 
293
  /* MYSQL_TYPE_LONGLONG -> */
 
294
  {
 
295
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
296
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONGLONG,
 
297
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
298
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONGLONG,
 
299
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
300
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
301
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
302
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_VARCHAR,
 
303
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
304
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONG,
 
305
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
306
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
307
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
308
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONGLONG,
 
309
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
310
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
311
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
312
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
313
  //MYSQL_TYPE_SET
 
314
    MYSQL_TYPE_VARCHAR,
 
315
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
316
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
317
  //MYSQL_TYPE_STRING
 
318
    MYSQL_TYPE_STRING
 
319
  },
 
320
  /* MYSQL_TYPE_INT24 -> */
 
321
  {
 
322
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
323
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_INT24,
 
324
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
325
    MYSQL_TYPE_INT24,       MYSQL_TYPE_LONG,
 
326
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
327
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
328
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
329
    MYSQL_TYPE_INT24,       MYSQL_TYPE_VARCHAR,
 
330
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
331
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
332
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
333
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
334
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
335
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_INT24,
 
336
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
337
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
338
  //MYSQL_TYPE_NEWDECIMAL    MYSQL_TYPE_ENUM
 
339
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
340
  //MYSQL_TYPE_SET
 
341
    MYSQL_TYPE_VARCHAR,
 
342
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
343
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
344
  //MYSQL_TYPE_STRING
 
345
    MYSQL_TYPE_STRING
 
346
  },
 
347
  /* MYSQL_TYPE_DATE -> */
 
348
  {
 
349
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
350
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
351
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
352
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
353
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
354
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
355
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
356
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
357
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
358
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
359
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
360
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
361
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
362
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
363
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
364
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
365
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
366
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
367
  //MYSQL_TYPE_SET
 
368
    MYSQL_TYPE_VARCHAR,
 
369
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
370
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
371
  //MYSQL_TYPE_STRING
 
372
    MYSQL_TYPE_STRING
 
373
  },
 
374
  /* MYSQL_TYPE_TIME -> */
 
375
  {
 
376
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
377
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
378
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
379
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
380
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
381
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
382
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
383
    MYSQL_TYPE_TIME,        MYSQL_TYPE_DATETIME,
 
384
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
385
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
386
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
387
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_TIME,
 
388
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
389
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
390
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
391
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
392
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
393
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
394
  //MYSQL_TYPE_SET
 
395
    MYSQL_TYPE_VARCHAR,
 
396
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
397
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
398
  //MYSQL_TYPE_STRING
 
399
    MYSQL_TYPE_STRING
 
400
  },
 
401
  /* MYSQL_TYPE_DATETIME -> */
 
402
  {
 
403
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
404
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
405
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
406
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
407
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
408
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
409
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
410
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
411
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
412
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
413
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
414
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
415
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
416
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
417
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
418
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
419
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
420
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
421
  //MYSQL_TYPE_SET
 
422
    MYSQL_TYPE_VARCHAR,
 
423
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
424
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
425
  //MYSQL_TYPE_STRING
 
426
    MYSQL_TYPE_STRING
 
427
  },
 
428
  /* MYSQL_TYPE_YEAR -> */
 
429
  {
 
430
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
431
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
432
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
433
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
434
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
435
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
436
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
437
    MYSQL_TYPE_YEAR,        MYSQL_TYPE_VARCHAR,
 
438
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
439
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
440
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
441
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
442
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
443
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_YEAR,
 
444
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
445
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
446
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
447
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
448
  //MYSQL_TYPE_SET
 
449
    MYSQL_TYPE_VARCHAR,
 
450
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
451
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
452
  //MYSQL_TYPE_STRING
 
453
    MYSQL_TYPE_STRING
 
454
  },
 
455
  /* MYSQL_TYPE_NEWDATE -> */
 
456
  {
 
457
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
458
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
459
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
460
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
461
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
462
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
463
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
464
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
465
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
466
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
467
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
468
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
469
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
470
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
471
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
472
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
473
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
474
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
475
  //MYSQL_TYPE_SET
 
476
    MYSQL_TYPE_VARCHAR,
 
477
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
478
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
479
  //MYSQL_TYPE_STRING
 
480
    MYSQL_TYPE_STRING
 
481
  },
 
482
  /* MYSQL_TYPE_VARCHAR -> */
 
483
  {
 
484
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
485
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
486
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
487
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
488
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
489
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
490
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
491
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
492
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
493
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
494
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
495
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
496
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
497
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
498
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
499
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
500
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
501
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
502
  //MYSQL_TYPE_SET
 
503
    MYSQL_TYPE_VARCHAR,
 
504
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
505
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
506
  //MYSQL_TYPE_STRING
 
507
    MYSQL_TYPE_VARCHAR
 
508
  },
 
509
  /* MYSQL_TYPE_NEWDECIMAL -> */
 
510
  {
 
511
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
512
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
513
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
514
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
515
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
516
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
517
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
518
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
519
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
520
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
521
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
522
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
523
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
524
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_NEWDECIMAL,
 
525
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
526
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
527
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
528
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
529
  //MYSQL_TYPE_SET
 
530
    MYSQL_TYPE_VARCHAR,
 
531
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
532
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
533
  //MYSQL_TYPE_STRING
 
534
    MYSQL_TYPE_STRING
 
535
  },
 
536
  /* MYSQL_TYPE_ENUM -> */
 
537
  {
 
538
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
539
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
540
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
541
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
542
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
543
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
544
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
545
    MYSQL_TYPE_ENUM,        MYSQL_TYPE_VARCHAR,
 
546
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
547
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
548
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
549
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
550
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
551
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
552
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
553
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
554
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
555
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
556
  //MYSQL_TYPE_SET
 
557
    MYSQL_TYPE_VARCHAR,
 
558
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
559
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
560
  //MYSQL_TYPE_STRING
 
561
    MYSQL_TYPE_STRING
 
562
  },
 
563
  /* MYSQL_TYPE_SET -> */
 
564
  {
 
565
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
566
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
567
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
568
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
569
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
570
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
571
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
572
    MYSQL_TYPE_SET,         MYSQL_TYPE_VARCHAR,
 
573
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
574
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
575
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
576
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
577
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
578
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
579
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
580
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
581
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
582
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
583
  //MYSQL_TYPE_SET
 
584
    MYSQL_TYPE_VARCHAR,
 
585
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
586
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
587
  //MYSQL_TYPE_STRING
 
588
    MYSQL_TYPE_STRING
 
589
  },
 
590
  /* MYSQL_TYPE_BLOB -> */
 
591
  {
 
592
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
593
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
594
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
595
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
596
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
597
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
598
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
599
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
600
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
601
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
602
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
603
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
604
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
605
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
606
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
607
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
608
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
609
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
610
  //MYSQL_TYPE_SET
 
611
    MYSQL_TYPE_BLOB,
 
612
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
613
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
614
  //MYSQL_TYPE_STRING
 
615
    MYSQL_TYPE_BLOB
 
616
  },
 
617
  /* MYSQL_TYPE_VAR_STRING -> */
 
618
  {
 
619
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
620
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
621
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
622
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
623
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
624
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
625
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
626
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
627
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
628
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
629
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
630
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
631
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
632
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
633
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
634
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
635
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
636
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
637
  //MYSQL_TYPE_SET
 
638
    MYSQL_TYPE_VARCHAR,
 
639
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
640
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
641
  //MYSQL_TYPE_STRING
 
642
    MYSQL_TYPE_VARCHAR
 
643
  },
 
644
  /* MYSQL_TYPE_STRING -> */
 
645
  {
 
646
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
647
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
648
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
649
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
650
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
651
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
652
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
653
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
654
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
655
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
656
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
657
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
658
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
659
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
660
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
661
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR,
 
662
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
663
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
664
  //MYSQL_TYPE_SET
 
665
    MYSQL_TYPE_STRING,
 
666
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
667
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
668
  //MYSQL_TYPE_STRING
 
669
    MYSQL_TYPE_STRING
 
670
  }
383
671
};
384
672
 
385
673
/**
395
683
enum_field_types Field::field_type_merge(enum_field_types a,
396
684
                                         enum_field_types b)
397
685
{
398
 
  assert(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
399
 
  assert(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
 
686
  DBUG_ASSERT(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
 
687
  DBUG_ASSERT(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
400
688
  return field_types_merge_rules[field_type2index(a)]
401
689
                                [field_type2index(b)];
402
690
}
404
692
 
405
693
static Item_result field_types_result_type [FIELDTYPE_NUM]=
406
694
{
407
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
 
695
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
408
696
  DECIMAL_RESULT,           INT_RESULT,
409
 
  //DRIZZLE_TYPE_LONG
410
 
  INT_RESULT,
411
 
  //DRIZZLE_TYPE_DOUBLE
412
 
  REAL_RESULT,
413
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
414
 
  STRING_RESULT,            STRING_RESULT,
415
 
  //DRIZZLE_TYPE_LONGLONG
416
 
  INT_RESULT,
417
 
  //DRIZZLE_TYPE_TIME
418
 
  STRING_RESULT,
419
 
  //DRIZZLE_TYPE_DATETIME
420
 
  STRING_RESULT,
421
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
422
 
  STRING_RESULT,            STRING_RESULT,
423
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
 
697
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
698
  INT_RESULT,               INT_RESULT,
 
699
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
700
  REAL_RESULT,              REAL_RESULT,
 
701
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
702
  STRING_RESULT,            STRING_RESULT,
 
703
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
704
  INT_RESULT,               INT_RESULT,
 
705
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
706
  STRING_RESULT,            STRING_RESULT,
 
707
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
708
  STRING_RESULT,            INT_RESULT,
 
709
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
710
  STRING_RESULT,            STRING_RESULT,
 
711
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
424
712
  DECIMAL_RESULT,           STRING_RESULT,
425
 
  //DRIZZLE_TYPE_BLOB
 
713
  //MYSQL_TYPE_SET
426
714
  STRING_RESULT,
 
715
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
716
  STRING_RESULT,            STRING_RESULT,
 
717
  //MYSQL_TYPE_STRING
 
718
  STRING_RESULT
427
719
};
428
720
 
429
721
 
439
731
    strend      String end
440
732
 
441
733
  RETURN
442
 
    false - If string does not have important data
443
 
    true  - If string has some important data
 
734
    FALSE - If string does not have important data
 
735
    TRUE  - If string has some important data
444
736
*/
445
737
 
446
738
static bool
447
 
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
448
 
                       const char *strend)
 
739
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
449
740
{
450
741
  if (cs != &my_charset_bin)
451
742
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
464
755
 
465
756
Item_result Field::result_merge_type(enum_field_types field_type)
466
757
{
467
 
  assert(field_type < FIELDTYPE_TEAR_FROM || field_type
 
758
  DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM || field_type
468
759
              > FIELDTYPE_TEAR_TO);
469
760
  return field_types_result_type[field_type2index(field_type)];
470
761
}
483
774
  @param type  field type
484
775
 
485
776
  @retval
486
 
    true  Type can have a prefixed key
 
777
    TRUE  Type can have a prefixed key
487
778
  @retval
488
 
    false Type can not have a prefixed key
 
779
    FALSE Type can not have a prefixed key
489
780
*/
490
781
 
491
782
bool Field::type_can_have_key_part(enum enum_field_types type)
492
783
{
493
784
  switch (type) {
494
 
  case DRIZZLE_TYPE_VARCHAR:
495
 
  case DRIZZLE_TYPE_BLOB:
496
 
    return true;
 
785
  case MYSQL_TYPE_VARCHAR:
 
786
  case MYSQL_TYPE_BLOB:
 
787
  case MYSQL_TYPE_VAR_STRING:
 
788
  case MYSQL_TYPE_STRING:
 
789
    return TRUE;
497
790
  default:
498
 
    return false;
 
791
    return FALSE;
499
792
  }
500
793
}
501
794
 
503
796
/**
504
797
  Numeric fields base class constructor.
505
798
*/
506
 
Field_num::Field_num(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
507
 
                     unsigned char null_bit_arg, utype unireg_check_arg,
 
799
Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
800
                     uchar null_bit_arg, utype unireg_check_arg,
508
801
                     const char *field_name_arg,
509
 
                     uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
 
802
                     uint8 dec_arg, bool zero_arg, bool unsigned_arg)
510
803
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
511
804
         unireg_check_arg, field_name_arg),
512
 
  dec(dec_arg),decimal_precision(zero_arg),unsigned_flag(unsigned_arg)
 
805
  dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
513
806
{
 
807
  if (zerofill)
 
808
    flags|=ZEROFILL_FLAG;
514
809
  if (unsigned_flag)
515
810
    flags|=UNSIGNED_FLAG;
516
811
}
517
812
 
518
813
 
 
814
void Field_num::prepend_zeros(String *value)
 
815
{
 
816
  int diff;
 
817
  if ((diff= (int) (field_length - value->length())) > 0)
 
818
  {
 
819
    bmove_upp((uchar*) value->ptr()+field_length,
 
820
              (uchar*) value->ptr()+value->length(),
 
821
              value->length());
 
822
    bfill((uchar*) value->ptr(),diff,'0');
 
823
    value->length(field_length);
 
824
    (void) value->c_ptr_quick();                // Avoid warnings in purify
 
825
  }
 
826
}
 
827
 
519
828
/**
520
829
  Test if given number is a int.
521
830
 
538
847
    2   error: garbage at the end of string.
539
848
*/
540
849
 
541
 
int Field_num::check_int(const CHARSET_INFO * const cs, const char *str, int length, 
 
850
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
542
851
                         const char *int_end, int error)
543
852
{
544
853
  /* Test if we get an empty string or wrong integer */
545
854
  if (str == int_end || error == MY_ERRNO_EDOM)
546
855
  {
547
856
    char buff[128];
548
 
    String tmp(buff, (uint32_t) sizeof(buff), system_charset_info);
 
857
    String tmp(buff, (uint32) sizeof(buff), system_charset_info);
549
858
    tmp.copy(str, length, system_charset_info);
550
 
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
859
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
551
860
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
552
861
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
553
862
                        "integer", tmp.c_ptr(), field_name,
554
 
                        (uint32_t) table->in_use->row_count);
 
863
                        (ulong) table->in_use->row_count);
555
864
    return 1;
556
865
  }
557
866
  /* Test if we have garbage at the end of the given string. */
558
867
  if (test_if_important_data(cs, int_end, str + length))
559
868
  {
560
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
869
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
561
870
    return 2;
562
871
  }
563
872
  return 0;
572
881
    cs            Character set
573
882
    from          String to convert
574
883
    len           Length of the string
575
 
    rnd           OUT int64_t value
 
884
    rnd           OUT longlong value
576
885
    unsigned_max  max unsigned value
577
886
    signed_min    min signed value
578
887
    signed_max    max signed value
587
896
    1   error
588
897
*/
589
898
 
590
 
bool Field_num::get_int(const CHARSET_INFO * const cs, const char *from, uint32_t len,
591
 
                        int64_t *rnd, uint64_t unsigned_max, 
592
 
                        int64_t signed_min, int64_t signed_max)
 
899
bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len,
 
900
                        longlong *rnd, uint64_t unsigned_max, 
 
901
                        longlong signed_min, longlong signed_max)
593
902
{
594
903
  char *end;
595
904
  int error;
596
905
  
597
 
  *rnd= (int64_t) cs->cset->strntoull10rnd(cs, from, len,
 
906
  *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
598
907
                                            unsigned_flag, &end,
599
908
                                            &error);
600
909
  if (unsigned_flag)
601
910
  {
602
911
 
603
 
    if ((((uint64_t) *rnd > unsigned_max) && (*rnd= (int64_t) unsigned_max)) ||
 
912
    if ((((uint64_t) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max)) ||
604
913
        error == MY_ERRNO_ERANGE)
605
914
    {
606
915
      goto out_of_range;
625
934
  return 0;
626
935
 
627
936
out_of_range:
628
 
  set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
937
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
629
938
  return 1;
630
939
}
631
940
 
645
954
{
646
955
  if (op_result == E_DEC_OVERFLOW)
647
956
  {
648
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
957
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
649
958
    return 1;
650
959
  }
651
960
  if (op_result == E_DEC_TRUNCATED)
652
961
  {
653
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
962
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
654
963
    /* We return 0 here as this is not a critical issue */
655
964
  }
656
965
  return 0;
658
967
 
659
968
 
660
969
#ifdef NOT_USED
661
 
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
 
970
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
662
971
{
663
972
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
664
973
 
718
1027
  This is used for printing bit_fields as numbers while debugging.
719
1028
*/
720
1029
 
721
 
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
 
1030
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
722
1031
{
723
 
  const CHARSET_INFO * const cs= &my_charset_bin;
724
 
  uint32_t length;
725
 
  int64_t value= val_int();
 
1032
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1033
  CHARSET_INFO *cs= &my_charset_bin;
 
1034
  uint length;
 
1035
  longlong value= val_int();
726
1036
 
727
1037
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
728
1038
    return 0;
729
 
  length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
 
1039
  length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
730
1040
                                                MY_INT64_NUM_DECIMAL_DIGITS,
731
1041
                                                unsigned_val ? 10 : -10,
732
1042
                                                value);
736
1046
 
737
1047
 
738
1048
/// This is used as a table name when the table structure is not set up
739
 
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
740
 
             unsigned char null_bit_arg,
 
1049
Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
 
1050
             uchar null_bit_arg,
741
1051
             utype unireg_check_arg, const char *field_name_arg)
742
1052
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
743
1053
   table(0), orig_table(0), table_name(0),
745
1055
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
746
1056
   part_of_sortkey(0), unireg_check(unireg_check_arg),
747
1057
   field_length(length_arg), null_bit(null_bit_arg), 
748
 
   is_created_from_null_item(false)
 
1058
   is_created_from_null_item(FALSE)
749
1059
{
750
1060
  flags=null_ptr ? 0: NOT_NULL_FLAG;
751
1061
  comment.str= (char*) "";
754
1064
}
755
1065
 
756
1066
 
757
 
void Field::hash(uint32_t *nr, uint32_t *nr2)
 
1067
void Field::hash(ulong *nr, ulong *nr2)
758
1068
{
759
1069
  if (is_null())
760
1070
  {
762
1072
  }
763
1073
  else
764
1074
  {
765
 
    uint32_t len= pack_length();
766
 
    const CHARSET_INFO * const cs= charset();
 
1075
    uint len= pack_length();
 
1076
    CHARSET_INFO *cs= charset();
767
1077
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
768
1078
  }
769
1079
}
771
1081
size_t
772
1082
Field::do_last_null_byte() const
773
1083
{
774
 
  assert(null_ptr == NULL || null_ptr >= table->record[0]);
 
1084
  DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
775
1085
  if (null_ptr)
776
1086
    return (size_t) (null_ptr - table->record[0]) + 1;
777
1087
  return LAST_NULL_BYTE_UNDEF;
783
1093
  memcpy(ptr,ptr+row_offset,pack_length());
784
1094
  if (null_ptr)
785
1095
  {
786
 
    *null_ptr= (unsigned char) ((null_ptr[0] & (unsigned char) ~(uint32_t) null_bit) | (null_ptr[row_offset] & (unsigned char) null_bit));
 
1096
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | (null_ptr[row_offset] & (uchar) null_bit));
787
1097
  }
788
1098
}
789
1099
 
810
1120
   @retval 0 if this field's size is < the source field's size
811
1121
   @retval 1 if this field's size is >= the source field's size
812
1122
*/
813
 
int Field::compatible_field_size(uint32_t field_metadata)
 
1123
int Field::compatible_field_size(uint field_metadata)
814
1124
{
815
 
  uint32_t const source_size= pack_length_from_metadata(field_metadata);
816
 
  uint32_t const destination_size= row_pack_length();
 
1125
  uint const source_size= pack_length_from_metadata(field_metadata);
 
1126
  uint const destination_size= row_pack_length();
817
1127
  return (source_size <= destination_size);
818
1128
}
819
1129
 
820
1130
 
821
 
int Field::store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
 
1131
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
822
1132
                 enum_check_fields check_level)
823
1133
{
824
1134
  int res;
862
1172
   the data.
863
1173
 
864
1174
   @param low_byte_first
865
 
   @c true if integers should be stored little-endian, @c false if
 
1175
   @c TRUE if integers should be stored little-endian, @c FALSE if
866
1176
   native format should be used. Note that for little-endian machines,
867
1177
   the value of this flag is a moot point since the native format is
868
1178
   little-endian.
869
1179
*/
870
 
unsigned char *
871
 
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
 
1180
uchar *
 
1181
Field::pack(uchar *to, const uchar *from, uint max_length,
872
1182
            bool low_byte_first __attribute__((unused)))
873
1183
{
874
 
  uint32_t length= pack_length();
 
1184
  uint32 length= pack_length();
875
1185
  set_if_smaller(length, max_length);
876
1186
  memcpy(to, from, length);
877
1187
  return to+length;
907
1217
 
908
1218
   @return  New pointer into memory based on from + length of the data
909
1219
*/
910
 
const unsigned char *
911
 
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
 
1220
const uchar *
 
1221
Field::unpack(uchar* to, const uchar *from, uint param_data,
912
1222
              bool low_byte_first __attribute__((unused)))
913
1223
{
914
 
  uint32_t length=pack_length();
 
1224
  uint length=pack_length();
915
1225
  int from_type= 0;
916
1226
  /*
917
1227
    If from length is > 255, it has encoded data in the upper bits. Need
931
1241
    return from+length;
932
1242
  }
933
1243
 
934
 
  uint32_t len= (param_data && (param_data < length)) ?
 
1244
  uint len= (param_data && (param_data < length)) ?
935
1245
            param_data : length;
936
1246
 
937
1247
  memcpy(to, from, param_data > length ? length : len);
939
1249
}
940
1250
 
941
1251
 
942
 
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((unused)))
 
1252
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((__unused__)))
943
1253
{
944
1254
  /* This never have to be called */
945
 
  assert(0);
 
1255
  DBUG_ASSERT(0);
946
1256
  return 0;
947
1257
}
948
1258
 
949
1259
 
950
 
void Field_num::add_unsigned(String &res) const
 
1260
void Field_num::add_zerofill_and_unsigned(String &res) const
951
1261
{
952
1262
  if (unsigned_flag)
953
1263
    res.append(STRING_WITH_LEN(" unsigned"));
 
1264
  if (zerofill)
 
1265
    res.append(STRING_WITH_LEN(" zerofill"));
954
1266
}
955
1267
 
956
1268
 
983
1295
 
984
1296
 
985
1297
/**
986
 
  Conversion from decimal to int64_t with checking overflow and
 
1298
  Conversion from decimal to longlong with checking overflow and
987
1299
  setting correct value (min/max) in case of overflow.
988
1300
 
989
1301
  @param val             value which have to be converted
993
1305
  @return
994
1306
    value converted from val
995
1307
*/
996
 
int64_t Field::convert_decimal2int64_t(const my_decimal *val,
 
1308
longlong Field::convert_decimal2longlong(const my_decimal *val,
997
1309
                                         bool unsigned_flag, int *err)
998
1310
{
999
 
  int64_t i;
 
1311
  longlong i;
1000
1312
  if (unsigned_flag)
1001
1313
  {
1002
1314
    if (val->sign())
1003
1315
    {
1004
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1316
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1005
1317
      i= 0;
1006
1318
      *err= 1;
1007
1319
    }
1008
1320
    else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1009
1321
                                           ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1010
 
                                           val, true, &i)))
 
1322
                                           val, TRUE, &i)))
1011
1323
    {
1012
 
      i= ~(int64_t) 0;
 
1324
      i= ~(longlong) 0;
1013
1325
      *err= 1;
1014
1326
    }
1015
1327
  }
1016
1328
  else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1017
1329
                                         ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1018
 
                                         val, false, &i)))
 
1330
                                         val, FALSE, &i)))
1019
1331
  {
1020
 
    i= (val->sign() ? INT64_MIN : INT64_MAX);
 
1332
    i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
1021
1333
    *err= 1;
1022
1334
  }
1023
1335
  return i;
1040
1352
 
1041
1353
int Field_num::store_decimal(const my_decimal *val)
1042
1354
{
 
1355
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1043
1356
  int err= 0;
1044
 
  int64_t i= convert_decimal2int64_t(val, unsigned_flag, &err);
 
1357
  longlong i= convert_decimal2longlong(val, unsigned_flag, &err);
1045
1358
  return test(err | store(i, unsigned_flag));
1046
1359
}
1047
1360
 
1053
1366
 
1054
1367
  @note
1055
1368
    This method is used by all integer fields, real/decimal redefine it.
1056
 
    All int64_t values fit in our decimal buffer which cal store 8*9=72
 
1369
    All longlong values fit in our decimal buffer which cal store 8*9=72
1057
1370
    digits of integer number
1058
1371
 
1059
1372
  @return
1062
1375
 
1063
1376
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
1064
1377
{
1065
 
  assert(result_type() == INT_RESULT);
1066
 
  int64_t nr= val_int();
 
1378
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1379
  DBUG_ASSERT(result_type() == INT_RESULT);
 
1380
  longlong nr= val_int();
1067
1381
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1068
1382
  return decimal_value;
1069
1383
}
1070
1384
 
1071
1385
 
1072
 
Field_str::Field_str(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
1073
 
                     unsigned char null_bit_arg, utype unireg_check_arg,
1074
 
                     const char *field_name_arg, const CHARSET_INFO * const charset_arg)
 
1386
Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
1387
                     uchar null_bit_arg, utype unireg_check_arg,
 
1388
                     const char *field_name_arg, CHARSET_INFO *charset_arg)
1075
1389
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1076
1390
         unireg_check_arg, field_name_arg)
1077
1391
{
1110
1424
 
1111
1425
int Field_str::store_decimal(const my_decimal *d)
1112
1426
{
 
1427
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1113
1428
  double val;
1114
1429
  /* TODO: use decimal2string? */
1115
1430
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
1120
1435
 
1121
1436
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
1122
1437
{
1123
 
  int64_t nr= val_int();
 
1438
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1439
  longlong nr= val_int();
1124
1440
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
1125
1441
  return decimal_value;
1126
1442
}
1127
1443
 
1128
1444
 
1129
 
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
 
1445
uint Field::fill_cache_field(CACHE_FIELD *copy)
1130
1446
{
1131
 
  uint32_t store_length;
 
1447
  uint store_length;
1132
1448
  copy->str=ptr;
1133
1449
  copy->length=pack_length();
1134
1450
  copy->blob_field=0;
1139
1455
    copy->length-= table->s->blob_ptr_size;
1140
1456
    return copy->length;
1141
1457
  }
 
1458
  else if (!zero_pack() &&
 
1459
           (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
 
1460
            copy->length < 256))
 
1461
  {
 
1462
    copy->strip=1;                              /* Remove end space */
 
1463
    store_length= 2;
 
1464
  }
1142
1465
  else
1143
1466
  {
1144
1467
    copy->strip=0;
1148
1471
}
1149
1472
 
1150
1473
 
1151
 
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1474
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1152
1475
{
1153
1476
  char buff[40];
1154
1477
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1155
1478
  if (!(res=val_str(&tmp)) ||
1156
1479
      str_to_datetime_with_warn(res->ptr(), res->length(),
1157
 
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
1480
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
1158
1481
    return 1;
1159
1482
  return 0;
1160
1483
}
1161
1484
 
1162
 
bool Field::get_time(DRIZZLE_TIME *ltime)
 
1485
bool Field::get_time(MYSQL_TIME *ltime)
1163
1486
{
1164
1487
  char buff[40];
1165
1488
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1176
1499
    Needs to be changed if/when we want to support different time formats.
1177
1500
*/
1178
1501
 
1179
 
int Field::store_time(DRIZZLE_TIME *ltime,
1180
 
                      enum enum_drizzle_timestamp_type type_arg __attribute__((unused)))
 
1502
int Field::store_time(MYSQL_TIME *ltime,
 
1503
                      timestamp_type type_arg __attribute__((__unused__)))
1181
1504
{
 
1505
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1182
1506
  char buff[MAX_DATE_STRING_REP_LENGTH];
1183
 
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
 
1507
  uint length= (uint) my_TIME_to_str(ltime, buff);
1184
1508
  return store(buff, length, &my_charset_bin);
1185
1509
}
1186
1510
 
1187
1511
 
1188
 
bool Field::optimize_range(uint32_t idx, uint32_t part)
 
1512
bool Field::optimize_range(uint idx, uint part)
1189
1513
{
1190
1514
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1191
1515
}
1192
1516
 
1193
1517
 
1194
 
Field *Field::new_field(MEM_ROOT *root, Table *new_table,
 
1518
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
1195
1519
                        bool keep_type __attribute__((unused)))
1196
1520
{
1197
1521
  Field *tmp;
1205
1529
  tmp->part_of_key.init(0);
1206
1530
  tmp->part_of_sortkey.init(0);
1207
1531
  tmp->unireg_check= Field::NONE;
1208
 
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
 
1532
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
 
1533
                ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1209
1534
  tmp->reset_fields();
1210
1535
  return tmp;
1211
1536
}
1212
1537
 
1213
1538
 
1214
 
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
1215
 
                            unsigned char *new_ptr, unsigned char *new_null_ptr,
1216
 
                            uint32_t new_null_bit)
 
1539
Field *Field::new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
1540
                            uchar *new_ptr, uchar *new_null_ptr,
 
1541
                            uint new_null_bit)
1217
1542
{
1218
1543
  Field *tmp;
1219
1544
  if ((tmp= new_field(root, new_table, table == new_table)))
1226
1551
}
1227
1552
 
1228
1553
 
1229
 
/* This is used to generate a field in Table from TABLE_SHARE */
 
1554
/* This is used to generate a field in TABLE from TABLE_SHARE */
1230
1555
 
1231
 
Field *Field::clone(MEM_ROOT *root, Table *new_table)
 
1556
Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
1232
1557
{
1233
1558
  Field *tmp;
1234
1559
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1242
1567
 
1243
1568
 
1244
1569
/****************************************************************************
 
1570
  Field_null, a field that always return NULL
 
1571
****************************************************************************/
 
1572
 
 
1573
void Field_null::sql_type(String &res) const
 
1574
{
 
1575
  res.set_ascii(STRING_WITH_LEN("null"));
 
1576
}
 
1577
 
 
1578
 
 
1579
/****************************************************************************
 
1580
** Field_new_decimal
 
1581
****************************************************************************/
 
1582
 
 
1583
Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
 
1584
                                     uint32 len_arg, uchar *null_ptr_arg,
 
1585
                                     uchar null_bit_arg,
 
1586
                                     enum utype unireg_check_arg,
 
1587
                                     const char *field_name_arg,
 
1588
                                     uint8 dec_arg,bool zero_arg,
 
1589
                                     bool unsigned_arg)
 
1590
  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1591
             unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
 
1592
{
 
1593
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
1594
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
 
1595
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
 
1596
              (dec <= DECIMAL_MAX_SCALE));
 
1597
  bin_size= my_decimal_get_binary_size(precision, dec);
 
1598
}
 
1599
 
 
1600
 
 
1601
Field_new_decimal::Field_new_decimal(uint32 len_arg,
 
1602
                                     bool maybe_null_arg,
 
1603
                                     const char *name,
 
1604
                                     uint8 dec_arg,
 
1605
                                     bool unsigned_arg)
 
1606
  :Field_num((uchar*) 0, len_arg,
 
1607
             maybe_null_arg ? (uchar*) "": 0, 0,
 
1608
             NONE, name, dec_arg, 0, unsigned_arg)
 
1609
{
 
1610
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
1611
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
 
1612
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
 
1613
              (dec <= DECIMAL_MAX_SCALE));
 
1614
  bin_size= my_decimal_get_binary_size(precision, dec);
 
1615
}
 
1616
 
 
1617
 
 
1618
int Field_new_decimal::reset(void)
 
1619
{
 
1620
  store_value(&decimal_zero);
 
1621
  return 0;
 
1622
}
 
1623
 
 
1624
 
 
1625
/**
 
1626
  Generate max/min decimal value in case of overflow.
 
1627
 
 
1628
  @param decimal_value     buffer for value
 
1629
  @param sign              sign of value which caused overflow
 
1630
*/
 
1631
 
 
1632
void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
 
1633
                                              bool sign)
 
1634
{
 
1635
  DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
 
1636
  max_my_decimal(decimal_value, precision, decimals());
 
1637
  if (sign)
 
1638
  {
 
1639
    if (unsigned_flag)
 
1640
      my_decimal_set_zero(decimal_value);
 
1641
    else
 
1642
      decimal_value->sign(TRUE);
 
1643
  }
 
1644
  DBUG_VOID_RETURN;
 
1645
}
 
1646
 
 
1647
 
 
1648
/**
 
1649
  Store decimal value in the binary buffer.
 
1650
 
 
1651
  Checks if decimal_value fits into field size.
 
1652
  If it does, stores the decimal in the buffer using binary format.
 
1653
  Otherwise sets maximal number that can be stored in the field.
 
1654
 
 
1655
  @param decimal_value   my_decimal
 
1656
 
 
1657
  @retval
 
1658
    0 ok
 
1659
  @retval
 
1660
    1 error
 
1661
*/
 
1662
 
 
1663
bool Field_new_decimal::store_value(const my_decimal *decimal_value)
 
1664
{
 
1665
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1666
  int error= 0;
 
1667
  DBUG_ENTER("Field_new_decimal::store_value");
 
1668
#ifndef DBUG_OFF
 
1669
  {
 
1670
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
 
1671
    DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
 
1672
  }
 
1673
#endif
 
1674
 
 
1675
  /* check that we do not try to write negative value in unsigned field */
 
1676
  if (unsigned_flag && decimal_value->sign())
 
1677
  {
 
1678
    DBUG_PRINT("info", ("unsigned overflow"));
 
1679
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1680
    error= 1;
 
1681
    decimal_value= &decimal_zero;
 
1682
  }
 
1683
#ifndef DBUG_OFF
 
1684
  {
 
1685
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
 
1686
    DBUG_PRINT("info", ("saving with precision %d  scale: %d  value %s",
 
1687
                        (int)precision, (int)dec,
 
1688
                        dbug_decimal_as_string(dbug_buff, decimal_value)));
 
1689
  }
 
1690
#endif
 
1691
 
 
1692
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
1693
                                         decimal_value, ptr, precision, dec)))
 
1694
  {
 
1695
    my_decimal buff;
 
1696
    DBUG_PRINT("info", ("overflow"));
 
1697
    set_value_on_overflow(&buff, decimal_value->sign());
 
1698
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
 
1699
    error= 1;
 
1700
  }
 
1701
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
 
1702
                                          bin_size););
 
1703
  DBUG_RETURN(error);
 
1704
}
 
1705
 
 
1706
 
 
1707
int Field_new_decimal::store(const char *from, uint length,
 
1708
                             CHARSET_INFO *charset_arg)
 
1709
{
 
1710
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1711
  int err;
 
1712
  my_decimal decimal_value;
 
1713
  DBUG_ENTER("Field_new_decimal::store(char*)");
 
1714
 
 
1715
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
 
1716
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
 
1717
                           from, length, charset_arg,
 
1718
                           &decimal_value)) &&
 
1719
      table->in_use->abort_on_warning)
 
1720
  {
 
1721
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
 
1722
    String from_as_str;
 
1723
    from_as_str.copy(from, length, &my_charset_bin);
 
1724
 
 
1725
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_ERROR,
 
1726
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
1727
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
1728
                        "decimal", from_as_str.c_ptr(), field_name,
 
1729
                        (ulong) table->in_use->row_count);
 
1730
 
 
1731
    DBUG_RETURN(err);
 
1732
  }
 
1733
 
 
1734
  switch (err) {
 
1735
  case E_DEC_TRUNCATED:
 
1736
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
 
1737
    break;
 
1738
  case E_DEC_OVERFLOW:
 
1739
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1740
    set_value_on_overflow(&decimal_value, decimal_value.sign());
 
1741
    break;
 
1742
  case E_DEC_BAD_NUM:
 
1743
    {
 
1744
      /* Because "from" is not NUL-terminated and we use %s in the ER() */
 
1745
      String from_as_str;
 
1746
      from_as_str.copy(from, length, &my_charset_bin);
 
1747
 
 
1748
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
 
1749
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
1750
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
1751
                          "decimal", from_as_str.c_ptr(), field_name,
 
1752
                        (ulong) table->in_use->row_count);
 
1753
    my_decimal_set_zero(&decimal_value);
 
1754
 
 
1755
    break;
 
1756
    }
 
1757
  }
 
1758
 
 
1759
#ifndef DBUG_OFF
 
1760
  char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
 
1761
  DBUG_PRINT("enter", ("value: %s",
 
1762
                       dbug_decimal_as_string(dbug_buff, &decimal_value)));
 
1763
#endif
 
1764
  store_value(&decimal_value);
 
1765
  DBUG_RETURN(err);
 
1766
}
 
1767
 
 
1768
 
 
1769
/**
 
1770
  @todo
 
1771
  Fix following when double2my_decimal when double2decimal
 
1772
  will return E_DEC_TRUNCATED always correctly
 
1773
*/
 
1774
 
 
1775
int Field_new_decimal::store(double nr)
 
1776
{
 
1777
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1778
  my_decimal decimal_value;
 
1779
  int err;
 
1780
  DBUG_ENTER("Field_new_decimal::store(double)");
 
1781
 
 
1782
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
 
1783
                         &decimal_value);
 
1784
  if (err)
 
1785
  {
 
1786
    if (check_overflow(err))
 
1787
      set_value_on_overflow(&decimal_value, decimal_value.sign());
 
1788
    /* Only issue a warning if store_value doesn't issue an warning */
 
1789
    table->in_use->got_warning= 0;
 
1790
  }
 
1791
  if (store_value(&decimal_value))
 
1792
    err= 1;
 
1793
  else if (err && !table->in_use->got_warning)
 
1794
    err= warn_if_overflow(err);
 
1795
  DBUG_RETURN(err);
 
1796
}
 
1797
 
 
1798
 
 
1799
int Field_new_decimal::store(longlong nr, bool unsigned_val)
 
1800
{
 
1801
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1802
  my_decimal decimal_value;
 
1803
  int err;
 
1804
 
 
1805
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
1806
                           nr, unsigned_val, &decimal_value)))
 
1807
  {
 
1808
    if (check_overflow(err))
 
1809
      set_value_on_overflow(&decimal_value, decimal_value.sign());
 
1810
    /* Only issue a warning if store_value doesn't issue an warning */
 
1811
    table->in_use->got_warning= 0;
 
1812
  }
 
1813
  if (store_value(&decimal_value))
 
1814
    err= 1;
 
1815
  else if (err && !table->in_use->got_warning)
 
1816
    err= warn_if_overflow(err);
 
1817
  return err;
 
1818
}
 
1819
 
 
1820
 
 
1821
int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
 
1822
{
 
1823
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1824
  return store_value(decimal_value);
 
1825
}
 
1826
 
 
1827
 
 
1828
int Field_new_decimal::store_time(MYSQL_TIME *ltime,
 
1829
                                  timestamp_type t_type __attribute__((__unused__)))
 
1830
{
 
1831
    my_decimal decimal_value;
 
1832
    return store_value(date2my_decimal(ltime, &decimal_value));
 
1833
}
 
1834
 
 
1835
 
 
1836
double Field_new_decimal::val_real(void)
 
1837
{
 
1838
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1839
  double dbl;
 
1840
  my_decimal decimal_value;
 
1841
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
1842
  return dbl;
 
1843
}
 
1844
 
 
1845
 
 
1846
longlong Field_new_decimal::val_int(void)
 
1847
{
 
1848
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1849
  longlong i;
 
1850
  my_decimal decimal_value;
 
1851
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
1852
                 unsigned_flag, &i);
 
1853
  return i;
 
1854
}
 
1855
 
 
1856
 
 
1857
my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
 
1858
{
 
1859
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1860
  DBUG_ENTER("Field_new_decimal::val_decimal");
 
1861
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
 
1862
                    precision, dec);
 
1863
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
 
1864
                                          bin_size););
 
1865
  DBUG_RETURN(decimal_value);
 
1866
}
 
1867
 
 
1868
 
 
1869
String *Field_new_decimal::val_str(String *val_buffer,
 
1870
                                   String *val_ptr __attribute__((unused)))
 
1871
{
 
1872
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1873
  my_decimal decimal_value;
 
1874
  uint fixed_precision= zerofill ? precision : 0;
 
1875
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
1876
                    fixed_precision, dec, '0', val_buffer);
 
1877
  return val_buffer;
 
1878
}
 
1879
 
 
1880
 
 
1881
int Field_new_decimal::cmp(const uchar *a,const uchar*b)
 
1882
{
 
1883
  return memcmp(a, b, bin_size);
 
1884
}
 
1885
 
 
1886
 
 
1887
void Field_new_decimal::sort_string(uchar *buff,
 
1888
                                    uint length __attribute__((unused)))
 
1889
{
 
1890
  memcpy(buff, ptr, bin_size);
 
1891
}
 
1892
 
 
1893
 
 
1894
void Field_new_decimal::sql_type(String &str) const
 
1895
{
 
1896
  CHARSET_INFO *cs= str.charset();
 
1897
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
 
1898
                                "decimal(%d,%d)", precision, (int)dec));
 
1899
  add_zerofill_and_unsigned(str);
 
1900
}
 
1901
 
 
1902
 
 
1903
/**
 
1904
   Save the field metadata for new decimal fields.
 
1905
 
 
1906
   Saves the precision in the first byte and decimals() in the second
 
1907
   byte of the field metadata array at index of *metadata_ptr and 
 
1908
   *(metadata_ptr + 1).
 
1909
 
 
1910
   @param   metadata_ptr   First byte of field metadata
 
1911
 
 
1912
   @returns number of bytes written to metadata_ptr
 
1913
*/
 
1914
int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
 
1915
{
 
1916
  *metadata_ptr= precision;
 
1917
  *(metadata_ptr + 1)= decimals();
 
1918
  return 2;
 
1919
}
 
1920
 
 
1921
 
 
1922
/**
 
1923
   Returns the number of bytes field uses in row-based replication 
 
1924
   row packed size.
 
1925
 
 
1926
   This method is used in row-based replication to determine the number
 
1927
   of bytes that the field consumes in the row record format. This is
 
1928
   used to skip fields in the master that do not exist on the slave.
 
1929
 
 
1930
   @param   field_metadata   Encoded size in field metadata
 
1931
 
 
1932
   @returns The size of the field based on the field metadata.
 
1933
*/
 
1934
uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
 
1935
{
 
1936
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
 
1937
  uint const source_decimal= field_metadata & 0x00ff; 
 
1938
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
1939
                                                     source_decimal);
 
1940
  return (source_size);
 
1941
}
 
1942
 
 
1943
 
 
1944
/**
 
1945
   Check to see if field size is compatible with destination.
 
1946
 
 
1947
   This method is used in row-based replication to verify that the slave's
 
1948
   field size is less than or equal to the master's field size. The 
 
1949
   encoded field metadata (from the master or source) is decoded and compared
 
1950
   to the size of this field (the slave or destination). 
 
1951
 
 
1952
   @param   field_metadata   Encoded size in field metadata
 
1953
 
 
1954
   @retval 0 if this field's size is < the source field's size
 
1955
   @retval 1 if this field's size is >= the source field's size
 
1956
*/
 
1957
int Field_new_decimal::compatible_field_size(uint field_metadata)
 
1958
{
 
1959
  int compatible= 0;
 
1960
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
 
1961
  uint const source_decimal= field_metadata & 0x00ff; 
 
1962
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
1963
                                                     source_decimal);
 
1964
  uint const destination_size= row_pack_length();
 
1965
  compatible= (source_size <= destination_size);
 
1966
  if (compatible)
 
1967
    compatible= (source_precision <= precision) &&
 
1968
                (source_decimal <= decimals());
 
1969
  return (compatible);
 
1970
}
 
1971
 
 
1972
 
 
1973
uint Field_new_decimal::is_equal(Create_field *new_field)
 
1974
{
 
1975
  return ((new_field->sql_type == real_type()) &&
 
1976
          ((new_field->flags & UNSIGNED_FLAG) == 
 
1977
           (uint) (flags & UNSIGNED_FLAG)) &&
 
1978
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
 
1979
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
 
1980
          (new_field->length == max_display_length()) &&
 
1981
          (new_field->decimals == dec));
 
1982
}
 
1983
 
 
1984
 
 
1985
/**
 
1986
   Unpack a decimal field from row data.
 
1987
 
 
1988
   This method is used to unpack a decimal or numeric field from a master
 
1989
   whose size of the field is less than that of the slave.
 
1990
  
 
1991
   @param   to         Destination of the data
 
1992
   @param   from       Source of the data
 
1993
   @param   param_data Precision (upper) and decimal (lower) values
 
1994
 
 
1995
   @return  New pointer into memory based on from + length of the data
 
1996
*/
 
1997
const uchar *
 
1998
Field_new_decimal::unpack(uchar* to,
 
1999
                          const uchar *from,
 
2000
                          uint param_data,
 
2001
                          bool low_byte_first)
 
2002
{
 
2003
  if (param_data == 0)
 
2004
    return Field::unpack(to, from, param_data, low_byte_first);
 
2005
 
 
2006
  uint from_precision= (param_data & 0xff00) >> 8U;
 
2007
  uint from_decimal= param_data & 0x00ff;
 
2008
  uint length=pack_length();
 
2009
  uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
 
2010
  uint len= (param_data && (from_pack_len < length)) ?
 
2011
            from_pack_len : length;
 
2012
  if ((from_pack_len && (from_pack_len < length)) ||
 
2013
      (from_precision < precision) ||
 
2014
      (from_decimal < decimals()))
 
2015
  {
 
2016
    /*
 
2017
      If the master's data is smaller than the slave, we need to convert
 
2018
      the binary to decimal then resize the decimal converting it back to
 
2019
      a decimal and write that to the raw data buffer.
 
2020
    */
 
2021
    decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
 
2022
    decimal_t dec;
 
2023
    dec.len= from_precision;
 
2024
    dec.buf= dec_buf;
 
2025
    /*
 
2026
      Note: bin2decimal does not change the length of the field. So it is
 
2027
      just the first step the resizing operation. The second step does the
 
2028
      resizing using the precision and decimals from the slave.
 
2029
    */
 
2030
    bin2decimal((uchar *)from, &dec, from_precision, from_decimal);
 
2031
    decimal2bin(&dec, to, precision, decimals());
 
2032
  }
 
2033
  else
 
2034
    memcpy(to, from, len); // Sizes are the same, just copy the data.
 
2035
  return from+len;
 
2036
}
 
2037
 
 
2038
/****************************************************************************
1245
2039
** tiny int
1246
2040
****************************************************************************/
1247
2041
 
1248
 
int Field_tiny::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
2042
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
1249
2043
{
 
2044
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1250
2045
  int error;
1251
 
  int64_t rnd;
 
2046
  longlong rnd;
1252
2047
  
1253
2048
  error= get_int(cs, from, len, &rnd, 255, -128, 127);
1254
2049
  ptr[0]= unsigned_flag ? (char) (uint64_t) rnd : (char) rnd;
1258
2053
 
1259
2054
int Field_tiny::store(double nr)
1260
2055
{
 
2056
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1261
2057
  int error= 0;
1262
2058
  nr=rint(nr);
1263
2059
  if (unsigned_flag)
1265
2061
    if (nr < 0.0)
1266
2062
    {
1267
2063
      *ptr=0;
1268
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2064
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1269
2065
      error= 1;
1270
2066
    }
1271
2067
    else if (nr > 255.0)
1272
2068
    {
1273
2069
      *ptr=(char) 255;
1274
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2070
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1275
2071
      error= 1;
1276
2072
    }
1277
2073
    else
1282
2078
    if (nr < -128.0)
1283
2079
    {
1284
2080
      *ptr= (char) -128;
1285
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2081
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1286
2082
      error= 1;
1287
2083
    }
1288
2084
    else if (nr > 127.0)
1289
2085
    {
1290
2086
      *ptr=127;
1291
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2087
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1292
2088
      error= 1;
1293
2089
    }
1294
2090
    else
1298
2094
}
1299
2095
 
1300
2096
 
1301
 
int Field_tiny::store(int64_t nr, bool unsigned_val)
 
2097
int Field_tiny::store(longlong nr, bool unsigned_val)
1302
2098
{
 
2099
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1303
2100
  int error= 0;
1304
2101
 
1305
2102
  if (unsigned_flag)
1307
2104
    if (nr < 0 && !unsigned_val)
1308
2105
    {
1309
2106
      *ptr= 0;
1310
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2107
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1311
2108
      error= 1;
1312
2109
    }
1313
2110
    else if ((uint64_t) nr > (uint64_t) 255)
1314
2111
    {
1315
2112
      *ptr= (char) 255;
1316
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2113
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1317
2114
      error= 1;
1318
2115
    }
1319
2116
    else
1326
2123
    if (nr < -128)
1327
2124
    {
1328
2125
      *ptr= (char) -128;
1329
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2126
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1330
2127
      error= 1;
1331
2128
    }
1332
2129
    else if (nr > 127)
1333
2130
    {
1334
2131
      *ptr=127;
1335
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2132
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1336
2133
      error= 1;
1337
2134
    }
1338
2135
    else
1344
2141
 
1345
2142
double Field_tiny::val_real(void)
1346
2143
{
 
2144
  ASSERT_COLUMN_MARKED_FOR_READ;
1347
2145
  int tmp= unsigned_flag ? (int) ptr[0] :
1348
2146
    (int) ((signed char*) ptr)[0];
1349
2147
  return (double) tmp;
1350
2148
}
1351
2149
 
1352
2150
 
1353
 
int64_t Field_tiny::val_int(void)
 
2151
longlong Field_tiny::val_int(void)
1354
2152
{
 
2153
  ASSERT_COLUMN_MARKED_FOR_READ;
1355
2154
  int tmp= unsigned_flag ? (int) ptr[0] :
1356
2155
    (int) ((signed char*) ptr)[0];
1357
 
  return (int64_t) tmp;
 
2156
  return (longlong) tmp;
1358
2157
}
1359
2158
 
1360
2159
 
1361
2160
String *Field_tiny::val_str(String *val_buffer,
1362
2161
                            String *val_ptr __attribute__((unused)))
1363
2162
{
1364
 
  const CHARSET_INFO * const cs= &my_charset_bin;
1365
 
  uint32_t length;
1366
 
  uint32_t mlength=cmax(field_length+1,5*cs->mbmaxlen);
 
2163
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2164
  CHARSET_INFO *cs= &my_charset_bin;
 
2165
  uint length;
 
2166
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
1367
2167
  val_buffer->alloc(mlength);
1368
2168
  char *to=(char*) val_buffer->ptr();
1369
2169
 
1370
2170
  if (unsigned_flag)
1371
 
    length= (uint32_t) cs->cset->long10_to_str(cs,to,mlength, 10,
 
2171
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
1372
2172
                                           (long) *ptr);
1373
2173
  else
1374
 
    length= (uint32_t) cs->cset->long10_to_str(cs,to,mlength,-10,
 
2174
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
1375
2175
                                           (long) *((signed char*) ptr));
1376
2176
  
1377
2177
  val_buffer->length(length);
1378
 
 
 
2178
  if (zerofill)
 
2179
    prepend_zeros(val_buffer);
1379
2180
  return val_buffer;
1380
2181
}
1381
2182
 
1382
2183
bool Field_tiny::send_binary(Protocol *protocol)
1383
2184
{
1384
 
  return protocol->store_tiny((int64_t) (int8_t) ptr[0]);
 
2185
  return protocol->store_tiny((longlong) (int8) ptr[0]);
1385
2186
}
1386
2187
 
1387
 
int Field_tiny::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
2188
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
1388
2189
{
1389
2190
  signed char a,b;
1390
2191
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
1391
2192
  if (unsigned_flag)
1392
 
    return ((unsigned char) a < (unsigned char) b) ? -1 : ((unsigned char) a > (unsigned char) b) ? 1 : 0;
 
2193
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
1393
2194
  return (a < b) ? -1 : (a > b) ? 1 : 0;
1394
2195
}
1395
2196
 
1396
 
void Field_tiny::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
 
2197
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
1397
2198
{
1398
2199
  if (unsigned_flag)
1399
2200
    *to= *ptr;
1400
2201
  else
1401
 
    to[0] = (char) (ptr[0] ^ (unsigned char) 128);      /* Revers signbit */
 
2202
    to[0] = (char) (ptr[0] ^ (uchar) 128);      /* Revers signbit */
1402
2203
}
1403
2204
 
1404
2205
void Field_tiny::sql_type(String &res) const
1405
2206
{
1406
 
  const CHARSET_INFO * const cs=res.charset();
 
2207
  CHARSET_INFO *cs=res.charset();
1407
2208
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1408
2209
                          "tinyint(%d)",(int) field_length));
1409
 
  add_unsigned(res);
1410
 
}
1411
 
 
 
2210
  add_zerofill_and_unsigned(res);
 
2211
}
 
2212
 
 
2213
/****************************************************************************
 
2214
 Field type short int (2 byte)
 
2215
****************************************************************************/
 
2216
 
 
2217
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
 
2218
{
 
2219
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2220
  int store_tmp;
 
2221
  int error;
 
2222
  longlong rnd;
 
2223
  
 
2224
  error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
 
2225
  store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
 
2226
#ifdef WORDS_BIGENDIAN
 
2227
  if (table->s->db_low_byte_first)
 
2228
  {
 
2229
    int2store(ptr, store_tmp);
 
2230
  }
 
2231
  else
 
2232
#endif
 
2233
    shortstore(ptr, (short) store_tmp);
 
2234
  return error;
 
2235
}
 
2236
 
 
2237
 
 
2238
int Field_short::store(double nr)
 
2239
{
 
2240
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2241
  int error= 0;
 
2242
  int16 res;
 
2243
  nr=rint(nr);
 
2244
  if (unsigned_flag)
 
2245
  {
 
2246
    if (nr < 0)
 
2247
    {
 
2248
      res=0;
 
2249
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2250
      error= 1;
 
2251
    }
 
2252
    else if (nr > (double) UINT_MAX16)
 
2253
    {
 
2254
      res=(int16) UINT_MAX16;
 
2255
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2256
      error= 1;
 
2257
    }
 
2258
    else
 
2259
      res=(int16) (uint16) nr;
 
2260
  }
 
2261
  else
 
2262
  {
 
2263
    if (nr < (double) INT_MIN16)
 
2264
    {
 
2265
      res=INT_MIN16;
 
2266
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2267
      error= 1;
 
2268
    }
 
2269
    else if (nr > (double) INT_MAX16)
 
2270
    {
 
2271
      res=INT_MAX16;
 
2272
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2273
      error= 1;
 
2274
    }
 
2275
    else
 
2276
      res=(int16) (int) nr;
 
2277
  }
 
2278
#ifdef WORDS_BIGENDIAN
 
2279
  if (table->s->db_low_byte_first)
 
2280
  {
 
2281
    int2store(ptr,res);
 
2282
  }
 
2283
  else
 
2284
#endif
 
2285
    shortstore(ptr,res);
 
2286
  return error;
 
2287
}
 
2288
 
 
2289
 
 
2290
int Field_short::store(longlong nr, bool unsigned_val)
 
2291
{
 
2292
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2293
  int error= 0;
 
2294
  int16 res;
 
2295
 
 
2296
  if (unsigned_flag)
 
2297
  {
 
2298
    if (nr < 0L && !unsigned_val)
 
2299
    {
 
2300
      res=0;
 
2301
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2302
      error= 1;
 
2303
    }
 
2304
    else if ((uint64_t) nr > (uint64_t) UINT_MAX16)
 
2305
    {
 
2306
      res=(int16) UINT_MAX16;
 
2307
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2308
      error= 1;
 
2309
    }
 
2310
    else
 
2311
      res=(int16) (uint16) nr;
 
2312
  }
 
2313
  else
 
2314
  {
 
2315
    if (nr < 0 && unsigned_val)
 
2316
      nr= UINT_MAX16+1;                         // Generate overflow
 
2317
 
 
2318
    if (nr < INT_MIN16)
 
2319
    {
 
2320
      res=INT_MIN16;
 
2321
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2322
      error= 1;
 
2323
    }
 
2324
    else if (nr > (longlong) INT_MAX16)
 
2325
    {
 
2326
      res=INT_MAX16;
 
2327
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2328
      error= 1;
 
2329
    }
 
2330
    else
 
2331
      res=(int16) nr;
 
2332
  }
 
2333
#ifdef WORDS_BIGENDIAN
 
2334
  if (table->s->db_low_byte_first)
 
2335
  {
 
2336
    int2store(ptr,res);
 
2337
  }
 
2338
  else
 
2339
#endif
 
2340
    shortstore(ptr,res);
 
2341
  return error;
 
2342
}
 
2343
 
 
2344
 
 
2345
double Field_short::val_real(void)
 
2346
{
 
2347
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2348
  short j;
 
2349
#ifdef WORDS_BIGENDIAN
 
2350
  if (table->s->db_low_byte_first)
 
2351
    j=sint2korr(ptr);
 
2352
  else
 
2353
#endif
 
2354
    shortget(j,ptr);
 
2355
  return unsigned_flag ? (double) (unsigned short) j : (double) j;
 
2356
}
 
2357
 
 
2358
longlong Field_short::val_int(void)
 
2359
{
 
2360
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2361
  short j;
 
2362
#ifdef WORDS_BIGENDIAN
 
2363
  if (table->s->db_low_byte_first)
 
2364
    j=sint2korr(ptr);
 
2365
  else
 
2366
#endif
 
2367
    shortget(j,ptr);
 
2368
  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
 
2369
}
 
2370
 
 
2371
 
 
2372
String *Field_short::val_str(String *val_buffer,
 
2373
                             String *val_ptr __attribute__((unused)))
 
2374
{
 
2375
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2376
  CHARSET_INFO *cs= &my_charset_bin;
 
2377
  uint length;
 
2378
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
 
2379
  val_buffer->alloc(mlength);
 
2380
  char *to=(char*) val_buffer->ptr();
 
2381
  short j;
 
2382
#ifdef WORDS_BIGENDIAN
 
2383
  if (table->s->db_low_byte_first)
 
2384
    j=sint2korr(ptr);
 
2385
  else
 
2386
#endif
 
2387
    shortget(j,ptr);
 
2388
 
 
2389
  if (unsigned_flag)
 
2390
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
 
2391
                                          (long) (uint16) j);
 
2392
  else
 
2393
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
 
2394
  val_buffer->length(length);
 
2395
  if (zerofill)
 
2396
    prepend_zeros(val_buffer);
 
2397
  return val_buffer;
 
2398
}
 
2399
 
 
2400
 
 
2401
bool Field_short::send_binary(Protocol *protocol)
 
2402
{
 
2403
  return protocol->store_short(Field_short::val_int());
 
2404
}
 
2405
 
 
2406
 
 
2407
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
2408
{
 
2409
  short a,b;
 
2410
#ifdef WORDS_BIGENDIAN
 
2411
  if (table->s->db_low_byte_first)
 
2412
  {
 
2413
    a=sint2korr(a_ptr);
 
2414
    b=sint2korr(b_ptr);
 
2415
  }
 
2416
  else
 
2417
#endif
 
2418
  {
 
2419
    shortget(a,a_ptr);
 
2420
    shortget(b,b_ptr);
 
2421
  }
 
2422
 
 
2423
  if (unsigned_flag)
 
2424
    return ((unsigned short) a < (unsigned short) b) ? -1 :
 
2425
    ((unsigned short) a > (unsigned short) b) ? 1 : 0;
 
2426
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
2427
}
 
2428
 
 
2429
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
 
2430
{
 
2431
#ifdef WORDS_BIGENDIAN
 
2432
  if (!table->s->db_low_byte_first)
 
2433
  {
 
2434
    if (unsigned_flag)
 
2435
      to[0] = ptr[0];
 
2436
    else
 
2437
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
2438
    to[1]   = ptr[1];
 
2439
  }
 
2440
  else
 
2441
#endif
 
2442
  {
 
2443
    if (unsigned_flag)
 
2444
      to[0] = ptr[1];
 
2445
    else
 
2446
      to[0] = (char) (ptr[1] ^ 128);            /* Revers signbit */
 
2447
    to[1]   = ptr[0];
 
2448
  }
 
2449
}
 
2450
 
 
2451
void Field_short::sql_type(String &res) const
 
2452
{
 
2453
  CHARSET_INFO *cs=res.charset();
 
2454
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2455
                          "smallint(%d)",(int) field_length));
 
2456
  add_zerofill_and_unsigned(res);
 
2457
}
 
2458
 
 
2459
 
 
2460
/****************************************************************************
 
2461
** long int
 
2462
****************************************************************************/
 
2463
 
 
2464
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
 
2465
{
 
2466
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2467
  long store_tmp;
 
2468
  int error;
 
2469
  longlong rnd;
 
2470
  
 
2471
  error= get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
 
2472
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
 
2473
#ifdef WORDS_BIGENDIAN
 
2474
  if (table->s->db_low_byte_first)
 
2475
  {
 
2476
    int4store(ptr, store_tmp);
 
2477
  }
 
2478
  else
 
2479
#endif
 
2480
    longstore(ptr, store_tmp);
 
2481
  return error;
 
2482
}
 
2483
 
 
2484
 
 
2485
int Field_long::store(double nr)
 
2486
{
 
2487
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2488
  int error= 0;
 
2489
  int32 res;
 
2490
  nr=rint(nr);
 
2491
  if (unsigned_flag)
 
2492
  {
 
2493
    if (nr < 0)
 
2494
    {
 
2495
      res=0;
 
2496
      error= 1;
 
2497
    }
 
2498
    else if (nr > (double) UINT_MAX32)
 
2499
    {
 
2500
      res= INT_MAX32;
 
2501
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2502
      error= 1;
 
2503
    }
 
2504
    else
 
2505
      res=(int32) (ulong) nr;
 
2506
  }
 
2507
  else
 
2508
  {
 
2509
    if (nr < (double) INT_MIN32)
 
2510
    {
 
2511
      res=(int32) INT_MIN32;
 
2512
      error= 1;
 
2513
    }
 
2514
    else if (nr > (double) INT_MAX32)
 
2515
    {
 
2516
      res=(int32) INT_MAX32;
 
2517
      error= 1;
 
2518
    }
 
2519
    else
 
2520
      res=(int32) (longlong) nr;
 
2521
  }
 
2522
  if (error)
 
2523
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2524
 
 
2525
#ifdef WORDS_BIGENDIAN
 
2526
  if (table->s->db_low_byte_first)
 
2527
  {
 
2528
    int4store(ptr,res);
 
2529
  }
 
2530
  else
 
2531
#endif
 
2532
    longstore(ptr,res);
 
2533
  return error;
 
2534
}
 
2535
 
 
2536
 
 
2537
int Field_long::store(longlong nr, bool unsigned_val)
 
2538
{
 
2539
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2540
  int error= 0;
 
2541
  int32 res;
 
2542
 
 
2543
  if (unsigned_flag)
 
2544
  {
 
2545
    if (nr < 0 && !unsigned_val)
 
2546
    {
 
2547
      res=0;
 
2548
      error= 1;
 
2549
    }
 
2550
    else if ((uint64_t) nr >= (1LL << 32))
 
2551
    {
 
2552
      res=(int32) (uint32) ~0L;
 
2553
      error= 1;
 
2554
    }
 
2555
    else
 
2556
      res=(int32) (uint32) nr;
 
2557
  }
 
2558
  else
 
2559
  {
 
2560
    if (nr < 0 && unsigned_val)
 
2561
      nr= ((longlong) INT_MAX32) + 1;           // Generate overflow
 
2562
    if (nr < (longlong) INT_MIN32) 
 
2563
    {
 
2564
      res=(int32) INT_MIN32;
 
2565
      error= 1;
 
2566
    }
 
2567
    else if (nr > (longlong) INT_MAX32)
 
2568
    {
 
2569
      res=(int32) INT_MAX32;
 
2570
      error= 1;
 
2571
    }
 
2572
    else
 
2573
      res=(int32) nr;
 
2574
  }
 
2575
  if (error)
 
2576
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2577
 
 
2578
#ifdef WORDS_BIGENDIAN
 
2579
  if (table->s->db_low_byte_first)
 
2580
  {
 
2581
    int4store(ptr,res);
 
2582
  }
 
2583
  else
 
2584
#endif
 
2585
    longstore(ptr,res);
 
2586
  return error;
 
2587
}
 
2588
 
 
2589
 
 
2590
double Field_long::val_real(void)
 
2591
{
 
2592
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2593
  int32 j;
 
2594
#ifdef WORDS_BIGENDIAN
 
2595
  if (table->s->db_low_byte_first)
 
2596
    j=sint4korr(ptr);
 
2597
  else
 
2598
#endif
 
2599
    longget(j,ptr);
 
2600
  return unsigned_flag ? (double) (uint32) j : (double) j;
 
2601
}
 
2602
 
 
2603
longlong Field_long::val_int(void)
 
2604
{
 
2605
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2606
  int32 j;
 
2607
  /* See the comment in Field_long::store(long long) */
 
2608
  DBUG_ASSERT(table->in_use == current_thd);
 
2609
#ifdef WORDS_BIGENDIAN
 
2610
  if (table->s->db_low_byte_first)
 
2611
    j=sint4korr(ptr);
 
2612
  else
 
2613
#endif
 
2614
    longget(j,ptr);
 
2615
  return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
 
2616
}
 
2617
 
 
2618
String *Field_long::val_str(String *val_buffer,
 
2619
                            String *val_ptr __attribute__((unused)))
 
2620
{
 
2621
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2622
  CHARSET_INFO *cs= &my_charset_bin;
 
2623
  uint length;
 
2624
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
 
2625
  val_buffer->alloc(mlength);
 
2626
  char *to=(char*) val_buffer->ptr();
 
2627
  int32 j;
 
2628
#ifdef WORDS_BIGENDIAN
 
2629
  if (table->s->db_low_byte_first)
 
2630
    j=sint4korr(ptr);
 
2631
  else
 
2632
#endif
 
2633
    longget(j,ptr);
 
2634
 
 
2635
  if (unsigned_flag)
 
2636
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
 
2637
  else
 
2638
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
 
2639
  val_buffer->length(length);
 
2640
  if (zerofill)
 
2641
    prepend_zeros(val_buffer);
 
2642
  return val_buffer;
 
2643
}
 
2644
 
 
2645
 
 
2646
bool Field_long::send_binary(Protocol *protocol)
 
2647
{
 
2648
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2649
  return protocol->store_long(Field_long::val_int());
 
2650
}
 
2651
 
 
2652
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
2653
{
 
2654
  int32 a,b;
 
2655
#ifdef WORDS_BIGENDIAN
 
2656
  if (table->s->db_low_byte_first)
 
2657
  {
 
2658
    a=sint4korr(a_ptr);
 
2659
    b=sint4korr(b_ptr);
 
2660
  }
 
2661
  else
 
2662
#endif
 
2663
  {
 
2664
    longget(a,a_ptr);
 
2665
    longget(b,b_ptr);
 
2666
  }
 
2667
  if (unsigned_flag)
 
2668
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
 
2669
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
2670
}
 
2671
 
 
2672
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
 
2673
{
 
2674
#ifdef WORDS_BIGENDIAN
 
2675
  if (!table->s->db_low_byte_first)
 
2676
  {
 
2677
    if (unsigned_flag)
 
2678
      to[0] = ptr[0];
 
2679
    else
 
2680
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
2681
    to[1]   = ptr[1];
 
2682
    to[2]   = ptr[2];
 
2683
    to[3]   = ptr[3];
 
2684
  }
 
2685
  else
 
2686
#endif
 
2687
  {
 
2688
    if (unsigned_flag)
 
2689
      to[0] = ptr[3];
 
2690
    else
 
2691
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
 
2692
    to[1]   = ptr[2];
 
2693
    to[2]   = ptr[1];
 
2694
    to[3]   = ptr[0];
 
2695
  }
 
2696
}
 
2697
 
 
2698
 
 
2699
void Field_long::sql_type(String &res) const
 
2700
{
 
2701
  CHARSET_INFO *cs=res.charset();
 
2702
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2703
                          "int(%d)",(int) field_length));
 
2704
  add_zerofill_and_unsigned(res);
 
2705
}
 
2706
 
 
2707
/****************************************************************************
 
2708
 Field type longlong int (8 bytes)
 
2709
****************************************************************************/
 
2710
 
 
2711
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
 
2712
{
 
2713
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2714
  int error= 0;
 
2715
  char *end;
 
2716
  uint64_t tmp;
 
2717
 
 
2718
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
 
2719
  if (error == MY_ERRNO_ERANGE)
 
2720
  {
 
2721
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2722
    error= 1;
 
2723
  }
 
2724
  else if (table->in_use->count_cuted_fields && 
 
2725
           check_int(cs, from, len, end, error))
 
2726
    error= 1;
 
2727
  else
 
2728
    error= 0;
 
2729
#ifdef WORDS_BIGENDIAN
 
2730
  if (table->s->db_low_byte_first)
 
2731
  {
 
2732
    int8store(ptr,tmp);
 
2733
  }
 
2734
  else
 
2735
#endif
 
2736
    longlongstore(ptr,tmp);
 
2737
  return error;
 
2738
}
 
2739
 
 
2740
 
 
2741
int Field_longlong::store(double nr)
 
2742
{
 
2743
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2744
  int error= 0;
 
2745
  longlong res;
 
2746
 
 
2747
  nr= rint(nr);
 
2748
  if (unsigned_flag)
 
2749
  {
 
2750
    if (nr < 0)
 
2751
    {
 
2752
      res=0;
 
2753
      error= 1;
 
2754
    }
 
2755
    else if (nr >= (double) ULONGLONG_MAX)
 
2756
    {
 
2757
      res= ~(longlong) 0;
 
2758
      error= 1;
 
2759
    }
 
2760
    else
 
2761
      res=(longlong) (uint64_t) nr;
 
2762
  }
 
2763
  else
 
2764
  {
 
2765
    if (nr <= (double) LONGLONG_MIN)
 
2766
    {
 
2767
      res= LONGLONG_MIN;
 
2768
      error= (nr < (double) LONGLONG_MIN);
 
2769
    }
 
2770
    else if (nr >= (double) (uint64_t) LONGLONG_MAX)
 
2771
    {
 
2772
      res= LONGLONG_MAX;
 
2773
      error= (nr > (double) LONGLONG_MAX);
 
2774
    }
 
2775
    else
 
2776
      res=(longlong) nr;
 
2777
  }
 
2778
  if (error)
 
2779
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2780
 
 
2781
#ifdef WORDS_BIGENDIAN
 
2782
  if (table->s->db_low_byte_first)
 
2783
  {
 
2784
    int8store(ptr,res);
 
2785
  }
 
2786
  else
 
2787
#endif
 
2788
    longlongstore(ptr,res);
 
2789
  return error;
 
2790
}
 
2791
 
 
2792
 
 
2793
int Field_longlong::store(longlong nr, bool unsigned_val)
 
2794
{
 
2795
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2796
  int error= 0;
 
2797
 
 
2798
  if (nr < 0)                                   // Only possible error
 
2799
  {
 
2800
    /*
 
2801
      if field is unsigned and value is signed (< 0) or
 
2802
      if field is signed and value is unsigned we have an overflow
 
2803
    */
 
2804
    if (unsigned_flag != unsigned_val)
 
2805
    {
 
2806
      nr= unsigned_flag ? (uint64_t) 0 : (uint64_t) LONGLONG_MAX;
 
2807
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2808
      error= 1;
 
2809
    }
 
2810
  }
 
2811
 
 
2812
#ifdef WORDS_BIGENDIAN
 
2813
  if (table->s->db_low_byte_first)
 
2814
  {
 
2815
    int8store(ptr,nr);
 
2816
  }
 
2817
  else
 
2818
#endif
 
2819
    longlongstore(ptr,nr);
 
2820
  return error;
 
2821
}
 
2822
 
 
2823
 
 
2824
double Field_longlong::val_real(void)
 
2825
{
 
2826
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2827
  longlong j;
 
2828
#ifdef WORDS_BIGENDIAN
 
2829
  if (table->s->db_low_byte_first)
 
2830
  {
 
2831
    j=sint8korr(ptr);
 
2832
  }
 
2833
  else
 
2834
#endif
 
2835
    longlongget(j,ptr);
 
2836
  /* The following is open coded to avoid a bug in gcc 3.3 */
 
2837
  if (unsigned_flag)
 
2838
  {
 
2839
    uint64_t tmp= (uint64_t) j;
 
2840
    return ulonglong2double(tmp);
 
2841
  }
 
2842
  return (double) j;
 
2843
}
 
2844
 
 
2845
 
 
2846
longlong Field_longlong::val_int(void)
 
2847
{
 
2848
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2849
  longlong j;
 
2850
#ifdef WORDS_BIGENDIAN
 
2851
  if (table->s->db_low_byte_first)
 
2852
    j=sint8korr(ptr);
 
2853
  else
 
2854
#endif
 
2855
    longlongget(j,ptr);
 
2856
  return j;
 
2857
}
 
2858
 
 
2859
 
 
2860
String *Field_longlong::val_str(String *val_buffer,
 
2861
                                String *val_ptr __attribute__((unused)))
 
2862
{
 
2863
  CHARSET_INFO *cs= &my_charset_bin;
 
2864
  uint length;
 
2865
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
 
2866
  val_buffer->alloc(mlength);
 
2867
  char *to=(char*) val_buffer->ptr();
 
2868
  longlong j;
 
2869
#ifdef WORDS_BIGENDIAN
 
2870
  if (table->s->db_low_byte_first)
 
2871
    j=sint8korr(ptr);
 
2872
  else
 
2873
#endif
 
2874
    longlongget(j,ptr);
 
2875
 
 
2876
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
 
2877
                                        unsigned_flag ? 10 : -10, j);
 
2878
  val_buffer->length(length);
 
2879
  if (zerofill)
 
2880
    prepend_zeros(val_buffer);
 
2881
  return val_buffer;
 
2882
}
 
2883
 
 
2884
 
 
2885
bool Field_longlong::send_binary(Protocol *protocol)
 
2886
{
 
2887
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2888
  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
 
2889
}
 
2890
 
 
2891
 
 
2892
int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
2893
{
 
2894
  longlong a,b;
 
2895
#ifdef WORDS_BIGENDIAN
 
2896
  if (table->s->db_low_byte_first)
 
2897
  {
 
2898
    a=sint8korr(a_ptr);
 
2899
    b=sint8korr(b_ptr);
 
2900
  }
 
2901
  else
 
2902
#endif
 
2903
  {
 
2904
    longlongget(a,a_ptr);
 
2905
    longlongget(b,b_ptr);
 
2906
  }
 
2907
  if (unsigned_flag)
 
2908
    return ((uint64_t) a < (uint64_t) b) ? -1 :
 
2909
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
2910
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
2911
}
 
2912
 
 
2913
void Field_longlong::sort_string(uchar *to,uint length __attribute__((unused)))
 
2914
{
 
2915
#ifdef WORDS_BIGENDIAN
 
2916
  if (!table->s->db_low_byte_first)
 
2917
  {
 
2918
    if (unsigned_flag)
 
2919
      to[0] = ptr[0];
 
2920
    else
 
2921
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
2922
    to[1]   = ptr[1];
 
2923
    to[2]   = ptr[2];
 
2924
    to[3]   = ptr[3];
 
2925
    to[4]   = ptr[4];
 
2926
    to[5]   = ptr[5];
 
2927
    to[6]   = ptr[6];
 
2928
    to[7]   = ptr[7];
 
2929
  }
 
2930
  else
 
2931
#endif
 
2932
  {
 
2933
    if (unsigned_flag)
 
2934
      to[0] = ptr[7];
 
2935
    else
 
2936
      to[0] = (char) (ptr[7] ^ 128);            /* Revers signbit */
 
2937
    to[1]   = ptr[6];
 
2938
    to[2]   = ptr[5];
 
2939
    to[3]   = ptr[4];
 
2940
    to[4]   = ptr[3];
 
2941
    to[5]   = ptr[2];
 
2942
    to[6]   = ptr[1];
 
2943
    to[7]   = ptr[0];
 
2944
  }
 
2945
}
 
2946
 
 
2947
 
 
2948
void Field_longlong::sql_type(String &res) const
 
2949
{
 
2950
  CHARSET_INFO *cs=res.charset();
 
2951
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2952
                          "bigint(%d)",(int) field_length));
 
2953
  add_zerofill_and_unsigned(res);
 
2954
}
 
2955
 
 
2956
 
 
2957
/*
 
2958
  Floating-point numbers
 
2959
 */
 
2960
 
 
2961
uchar *
 
2962
Field_real::pack(uchar *to, const uchar *from,
 
2963
                 uint max_length, bool low_byte_first)
 
2964
{
 
2965
  DBUG_ENTER("Field_real::pack");
 
2966
  DBUG_ASSERT(max_length >= pack_length());
 
2967
  DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
 
2968
#ifdef WORDS_BIGENDIAN
 
2969
  if (low_byte_first != table->s->db_low_byte_first)
 
2970
  {
 
2971
    const uchar *dptr= from + pack_length();
 
2972
    while (dptr-- > from)
 
2973
      *to++ = *dptr;
 
2974
    DBUG_RETURN(to);
 
2975
  }
 
2976
  else
 
2977
#endif
 
2978
    DBUG_RETURN(Field::pack(to, from, max_length, low_byte_first));
 
2979
}
 
2980
 
 
2981
const uchar *
 
2982
Field_real::unpack(uchar *to, const uchar *from,
 
2983
                   uint param_data, bool low_byte_first)
 
2984
{
 
2985
  DBUG_ENTER("Field_real::unpack");
 
2986
  DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
 
2987
#ifdef WORDS_BIGENDIAN
 
2988
  if (low_byte_first != table->s->db_low_byte_first)
 
2989
  {
 
2990
    const uchar *dptr= from + pack_length();
 
2991
    while (dptr-- > from)
 
2992
      *to++ = *dptr;
 
2993
    DBUG_RETURN(from + pack_length());
 
2994
  }
 
2995
  else
 
2996
#endif
 
2997
    DBUG_RETURN(Field::unpack(to, from, param_data, low_byte_first));
 
2998
}
 
2999
 
 
3000
/****************************************************************************
 
3001
  single precision float
 
3002
****************************************************************************/
 
3003
 
 
3004
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
 
3005
{
 
3006
  int error;
 
3007
  char *end;
 
3008
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
 
3009
  if (error || (!len || (((uint)(end-from) != len) && table->in_use->count_cuted_fields)))
 
3010
  {
 
3011
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3012
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
 
3013
    error= error ? 1 : 2;
 
3014
  }
 
3015
  Field_float::store(nr);
 
3016
  return error;
 
3017
}
 
3018
 
 
3019
 
 
3020
int Field_float::store(double nr)
 
3021
{
 
3022
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3023
  int error= truncate(&nr, FLT_MAX);
 
3024
  float j= (float)nr;
 
3025
 
 
3026
#ifdef WORDS_BIGENDIAN
 
3027
  if (table->s->db_low_byte_first)
 
3028
  {
 
3029
    float4store(ptr,j);
 
3030
  }
 
3031
  else
 
3032
#endif
 
3033
    memcpy_fixed(ptr,(uchar*) &j,sizeof(j));
 
3034
  return error;
 
3035
}
 
3036
 
 
3037
 
 
3038
int Field_float::store(longlong nr, bool unsigned_val)
 
3039
{
 
3040
  return Field_float::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
 
3041
                            (double) nr);
 
3042
}
 
3043
 
 
3044
 
 
3045
double Field_float::val_real(void)
 
3046
{
 
3047
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3048
  float j;
 
3049
#ifdef WORDS_BIGENDIAN
 
3050
  if (table->s->db_low_byte_first)
 
3051
  {
 
3052
    float4get(j,ptr);
 
3053
  }
 
3054
  else
 
3055
#endif
 
3056
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
 
3057
  return ((double) j);
 
3058
}
 
3059
 
 
3060
longlong Field_float::val_int(void)
 
3061
{
 
3062
  float j;
 
3063
#ifdef WORDS_BIGENDIAN
 
3064
  if (table->s->db_low_byte_first)
 
3065
  {
 
3066
    float4get(j,ptr);
 
3067
  }
 
3068
  else
 
3069
#endif
 
3070
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
 
3071
  return (longlong) rint(j);
 
3072
}
 
3073
 
 
3074
 
 
3075
String *Field_float::val_str(String *val_buffer,
 
3076
                             String *val_ptr __attribute__((unused)))
 
3077
{
 
3078
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3079
  float nr;
 
3080
#ifdef WORDS_BIGENDIAN
 
3081
  if (table->s->db_low_byte_first)
 
3082
  {
 
3083
    float4get(nr,ptr);
 
3084
  }
 
3085
  else
 
3086
#endif
 
3087
    memcpy_fixed((uchar*) &nr,ptr,sizeof(nr));
 
3088
 
 
3089
  uint to_length=max(field_length,70);
 
3090
  val_buffer->alloc(to_length);
 
3091
  char *to=(char*) val_buffer->ptr();
 
3092
  size_t len;
 
3093
 
 
3094
  if (dec >= NOT_FIXED_DEC)
 
3095
    len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
 
3096
  else
 
3097
  {
 
3098
    /*
 
3099
      We are safe here because the buffer length is >= 70, and
 
3100
      fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
 
3101
      will be not longer than 69 chars + terminating '\0'.
 
3102
    */
 
3103
    len= my_fcvt(nr, dec, to, NULL);
 
3104
  }
 
3105
  val_buffer->length((uint) len);
 
3106
  if (zerofill)
 
3107
    prepend_zeros(val_buffer);
 
3108
  return val_buffer;
 
3109
}
 
3110
 
 
3111
 
 
3112
int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3113
{
 
3114
  float a,b;
 
3115
#ifdef WORDS_BIGENDIAN
 
3116
  if (table->s->db_low_byte_first)
 
3117
  {
 
3118
    float4get(a,a_ptr);
 
3119
    float4get(b,b_ptr);
 
3120
  }
 
3121
  else
 
3122
#endif
 
3123
  {
 
3124
    memcpy_fixed(&a,a_ptr,sizeof(float));
 
3125
    memcpy_fixed(&b,b_ptr,sizeof(float));
 
3126
  }
 
3127
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3128
}
 
3129
 
 
3130
#define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
 
3131
 
 
3132
void Field_float::sort_string(uchar *to,uint length __attribute__((unused)))
 
3133
{
 
3134
  float nr;
 
3135
#ifdef WORDS_BIGENDIAN
 
3136
  if (table->s->db_low_byte_first)
 
3137
  {
 
3138
    float4get(nr,ptr);
 
3139
  }
 
3140
  else
 
3141
#endif
 
3142
    memcpy_fixed(&nr,ptr,sizeof(float));
 
3143
 
 
3144
  uchar *tmp= to;
 
3145
  if (nr == (float) 0.0)
 
3146
  {                                             /* Change to zero string */
 
3147
    tmp[0]=(uchar) 128;
 
3148
    bzero((char*) tmp+1,sizeof(nr)-1);
 
3149
  }
 
3150
  else
 
3151
  {
 
3152
#ifdef WORDS_BIGENDIAN
 
3153
    memcpy_fixed(tmp,&nr,sizeof(nr));
 
3154
#else
 
3155
    tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
 
3156
#endif
 
3157
    if (tmp[0] & 128)                           /* Negative */
 
3158
    {                                           /* make complement */
 
3159
      uint i;
 
3160
      for (i=0 ; i < sizeof(nr); i++)
 
3161
        tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
 
3162
    }
 
3163
    else
 
3164
    {
 
3165
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
 
3166
                       (ushort) 32768);
 
3167
      exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
 
3168
      tmp[0]= (uchar) (exp_part >> 8);
 
3169
      tmp[1]= (uchar) exp_part;
 
3170
    }
 
3171
  }
 
3172
}
 
3173
 
 
3174
 
 
3175
bool Field_float::send_binary(Protocol *protocol)
 
3176
{
 
3177
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3178
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
 
3179
}
 
3180
 
 
3181
 
 
3182
/**
 
3183
   Save the field metadata for float fields.
 
3184
 
 
3185
   Saves the pack length in the first byte.
 
3186
 
 
3187
   @param   metadata_ptr   First byte of field metadata
 
3188
 
 
3189
   @returns number of bytes written to metadata_ptr
 
3190
*/
 
3191
int Field_float::do_save_field_metadata(uchar *metadata_ptr)
 
3192
{
 
3193
  *metadata_ptr= pack_length();
 
3194
  return 1;
 
3195
}
 
3196
 
 
3197
 
 
3198
void Field_float::sql_type(String &res) const
 
3199
{
 
3200
  if (dec == NOT_FIXED_DEC)
 
3201
  {
 
3202
    res.set_ascii(STRING_WITH_LEN("float"));
 
3203
  }
 
3204
  else
 
3205
  {
 
3206
    CHARSET_INFO *cs= res.charset();
 
3207
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
3208
                            "float(%d,%d)",(int) field_length,dec));
 
3209
  }
 
3210
  add_zerofill_and_unsigned(res);
 
3211
}
 
3212
 
 
3213
 
 
3214
/****************************************************************************
 
3215
  double precision floating point numbers
 
3216
****************************************************************************/
 
3217
 
 
3218
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
 
3219
{
 
3220
  int error;
 
3221
  char *end;
 
3222
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
 
3223
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
 
3224
  {
 
3225
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3226
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
 
3227
    error= error ? 1 : 2;
 
3228
  }
 
3229
  Field_double::store(nr);
 
3230
  return error;
 
3231
}
 
3232
 
 
3233
 
 
3234
int Field_double::store(double nr)
 
3235
{
 
3236
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3237
  int error= truncate(&nr, DBL_MAX);
 
3238
 
 
3239
#ifdef WORDS_BIGENDIAN
 
3240
  if (table->s->db_low_byte_first)
 
3241
  {
 
3242
    float8store(ptr,nr);
 
3243
  }
 
3244
  else
 
3245
#endif
 
3246
    doublestore(ptr,nr);
 
3247
  return error;
 
3248
}
 
3249
 
 
3250
 
 
3251
int Field_double::store(longlong nr, bool unsigned_val)
 
3252
{
 
3253
  return Field_double::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
 
3254
                             (double) nr);
 
3255
}
 
3256
 
 
3257
/*
 
3258
  If a field has fixed length, truncate the double argument pointed to by 'nr'
 
3259
  appropriately.
 
3260
  Also ensure that the argument is within [-max_value; max_value] range.
 
3261
*/
 
3262
 
 
3263
int Field_real::truncate(double *nr, double max_value)
 
3264
{
 
3265
  int error= 1;
 
3266
  double res= *nr;
 
3267
  
 
3268
  if (isnan(res))
 
3269
  {
 
3270
    res= 0;
 
3271
    set_null();
 
3272
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3273
    goto end;
 
3274
  }
 
3275
  else if (unsigned_flag && res < 0)
 
3276
  {
 
3277
    res= 0;
 
3278
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3279
    goto end;
 
3280
  }
 
3281
 
 
3282
  if (!not_fixed)
 
3283
  {
 
3284
    uint order= field_length - dec;
 
3285
    uint step= array_elements(log_10) - 1;
 
3286
    max_value= 1.0;
 
3287
    for (; order > step; order-= step)
 
3288
      max_value*= log_10[step];
 
3289
    max_value*= log_10[order];
 
3290
    max_value-= 1.0 / log_10[dec];
 
3291
 
 
3292
    /* Check for infinity so we don't get NaN in calculations */
 
3293
    if (!my_isinf(res))
 
3294
    {
 
3295
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
 
3296
      res= floor(res) + tmp;
 
3297
    }
 
3298
  }
 
3299
  
 
3300
  if (res < -max_value)
 
3301
  {
 
3302
   res= -max_value;
 
3303
   set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3304
  }
 
3305
  else if (res > max_value)
 
3306
  {
 
3307
    res= max_value;
 
3308
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3309
  }
 
3310
  else
 
3311
    error= 0;
 
3312
 
 
3313
end:
 
3314
  *nr= res;
 
3315
  return error;
 
3316
}
 
3317
 
 
3318
 
 
3319
int Field_real::store_decimal(const my_decimal *dm)
 
3320
{
 
3321
  double dbl;
 
3322
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
 
3323
  return store(dbl);
 
3324
}
 
3325
 
 
3326
double Field_double::val_real(void)
 
3327
{
 
3328
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3329
  double j;
 
3330
#ifdef WORDS_BIGENDIAN
 
3331
  if (table->s->db_low_byte_first)
 
3332
  {
 
3333
    float8get(j,ptr);
 
3334
  }
 
3335
  else
 
3336
#endif
 
3337
    doubleget(j,ptr);
 
3338
  return j;
 
3339
}
 
3340
 
 
3341
longlong Field_double::val_int(void)
 
3342
{
 
3343
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3344
  double j;
 
3345
  longlong res;
 
3346
#ifdef WORDS_BIGENDIAN
 
3347
  if (table->s->db_low_byte_first)
 
3348
  {
 
3349
    float8get(j,ptr);
 
3350
  }
 
3351
  else
 
3352
#endif
 
3353
    doubleget(j,ptr);
 
3354
  /* Check whether we fit into longlong range */
 
3355
  if (j <= (double) LONGLONG_MIN)
 
3356
  {
 
3357
    res= (longlong) LONGLONG_MIN;
 
3358
    goto warn;
 
3359
  }
 
3360
  if (j >= (double) (uint64_t) LONGLONG_MAX)
 
3361
  {
 
3362
    res= (longlong) LONGLONG_MAX;
 
3363
    goto warn;
 
3364
  }
 
3365
  return (longlong) rint(j);
 
3366
 
 
3367
warn:
 
3368
  {
 
3369
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
3370
    String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
 
3371
    str= val_str(&tmp, 0);
 
3372
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
3373
                        ER_TRUNCATED_WRONG_VALUE,
 
3374
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
 
3375
                        str->c_ptr());
 
3376
  }
 
3377
  return res;
 
3378
}
 
3379
 
 
3380
 
 
3381
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
 
3382
{
 
3383
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3384
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
 
3385
  return decimal_value;
 
3386
}
 
3387
 
 
3388
 
 
3389
String *Field_double::val_str(String *val_buffer,
 
3390
                              String *val_ptr __attribute__((unused)))
 
3391
{
 
3392
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3393
  double nr;
 
3394
#ifdef WORDS_BIGENDIAN
 
3395
  if (table->s->db_low_byte_first)
 
3396
  {
 
3397
    float8get(nr,ptr);
 
3398
  }
 
3399
  else
 
3400
#endif
 
3401
    doubleget(nr,ptr);
 
3402
 
 
3403
  uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
 
3404
  val_buffer->alloc(to_length);
 
3405
  char *to=(char*) val_buffer->ptr();
 
3406
  size_t len;
 
3407
 
 
3408
  if (dec >= NOT_FIXED_DEC)
 
3409
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
 
3410
  else
 
3411
    len= my_fcvt(nr, dec, to, NULL);
 
3412
 
 
3413
  val_buffer->length((uint) len);
 
3414
  if (zerofill)
 
3415
    prepend_zeros(val_buffer);
 
3416
  return val_buffer;
 
3417
}
 
3418
 
 
3419
bool Field_double::send_binary(Protocol *protocol)
 
3420
{
 
3421
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
 
3422
}
 
3423
 
 
3424
 
 
3425
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3426
{
 
3427
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3428
  double a,b;
 
3429
#ifdef WORDS_BIGENDIAN
 
3430
  if (table->s->db_low_byte_first)
 
3431
  {
 
3432
    float8get(a,a_ptr);
 
3433
    float8get(b,b_ptr);
 
3434
  }
 
3435
  else
 
3436
#endif
 
3437
  {
 
3438
    doubleget(a, a_ptr);
 
3439
    doubleget(b, b_ptr);
 
3440
  }
 
3441
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3442
}
 
3443
 
 
3444
 
 
3445
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
 
3446
 
 
3447
/* The following should work for IEEE */
 
3448
 
 
3449
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
 
3450
{
 
3451
  double nr;
 
3452
#ifdef WORDS_BIGENDIAN
 
3453
  if (table->s->db_low_byte_first)
 
3454
  {
 
3455
    float8get(nr,ptr);
 
3456
  }
 
3457
  else
 
3458
#endif
 
3459
    doubleget(nr,ptr);
 
3460
  change_double_for_sort(nr, to);
 
3461
}
 
3462
 
 
3463
 
 
3464
/**
 
3465
   Save the field metadata for double fields.
 
3466
 
 
3467
   Saves the pack length in the first byte of the field metadata array
 
3468
   at index of *metadata_ptr.
 
3469
 
 
3470
   @param   metadata_ptr   First byte of field metadata
 
3471
 
 
3472
   @returns number of bytes written to metadata_ptr
 
3473
*/
 
3474
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
 
3475
{
 
3476
  *metadata_ptr= pack_length();
 
3477
  return 1;
 
3478
}
 
3479
 
 
3480
 
 
3481
void Field_double::sql_type(String &res) const
 
3482
{
 
3483
  CHARSET_INFO *cs=res.charset();
 
3484
  if (dec == NOT_FIXED_DEC)
 
3485
  {
 
3486
    res.set_ascii(STRING_WITH_LEN("double"));
 
3487
  }
 
3488
  else
 
3489
  {
 
3490
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
3491
                            "double(%d,%d)",(int) field_length,dec));
 
3492
  }
 
3493
  add_zerofill_and_unsigned(res);
 
3494
}
 
3495
 
 
3496
 
 
3497
/**
 
3498
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to 
 
3499
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix 
 
3500
  Epoch in UTC.
 
3501
  
 
3502
  Up to one of timestamps columns in the table can be automatically 
 
3503
  set on row update and/or have NOW() as default value.
 
3504
  TABLE::timestamp_field points to Field object for such timestamp with 
 
3505
  auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
 
3506
  field, and is used by handler code which performs updates required.
 
3507
  
 
3508
  Actually SQL-99 says that we should allow niladic functions (like NOW())
 
3509
  as defaults for any field. Current limitations (only NOW() and only 
 
3510
  for one TIMESTAMP field) are because of restricted binary .frm format 
 
3511
  and should go away in the future.
 
3512
  
 
3513
  Also because of this limitation of binary .frm format we use 5 different
 
3514
  unireg_check values with TIMESTAMP field to distinguish various cases of
 
3515
  DEFAULT or ON UPDATE values. These values are:
 
3516
  
 
3517
  TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
 
3518
    auto-set-on-update (or now() as default) in this table before, then this 
 
3519
    field has NOW() as default and is updated when row changes, else it is 
 
3520
    field which has 0 as default value and is not automatically updated.
 
3521
  TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
 
3522
    automatically (TIMESTAMP DEFAULT NOW())
 
3523
  TIMESTAMP_UN_FIELD - field which is set on update automatically but has not 
 
3524
    NOW() as default (but it may has 0 or some other const timestamp as 
 
3525
    default) (TIMESTAMP ON UPDATE NOW()).
 
3526
  TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on 
 
3527
    update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
 
3528
  NONE - field which is not auto-set on update with some other than NOW() 
 
3529
    default value (TIMESTAMP DEFAULT 0).
 
3530
 
 
3531
  Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are 
 
3532
  left only for preserving ability to read old tables. Such fields replaced 
 
3533
  with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is 
 
3534
  because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for 
 
3535
  "TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such 
 
3536
  specification too but ignored default value for first timestamp, which of 
 
3537
  course is non-standard.) In most cases user won't notice any change, only
 
3538
  exception is different behavior of old/new timestamps during ALTER TABLE.
 
3539
 */
 
3540
 
 
3541
Field_timestamp::Field_timestamp(uchar *ptr_arg,
 
3542
                                 uint32 len_arg __attribute__((__unused__)),
 
3543
                                 uchar *null_ptr_arg, uchar null_bit_arg,
 
3544
                                 enum utype unireg_check_arg,
 
3545
                                 const char *field_name_arg,
 
3546
                                 TABLE_SHARE *share,
 
3547
                                 CHARSET_INFO *cs)
 
3548
  :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
 
3549
             unireg_check_arg, field_name_arg, cs)
 
3550
{
 
3551
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
3552
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
3553
  if (!share->timestamp_field && unireg_check != NONE)
 
3554
  {
 
3555
    /* This timestamp has auto-update */
 
3556
    share->timestamp_field= this;
 
3557
    flags|= TIMESTAMP_FLAG;
 
3558
    if (unireg_check != TIMESTAMP_DN_FIELD)
 
3559
      flags|= ON_UPDATE_NOW_FLAG;
 
3560
  }
 
3561
}
 
3562
 
 
3563
 
 
3564
Field_timestamp::Field_timestamp(bool maybe_null_arg,
 
3565
                                 const char *field_name_arg,
 
3566
                                 CHARSET_INFO *cs)
 
3567
  :Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
 
3568
             maybe_null_arg ? (uchar*) "": 0, 0,
 
3569
             NONE, field_name_arg, cs)
 
3570
{
 
3571
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
3572
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
3573
    if (unireg_check != TIMESTAMP_DN_FIELD)
 
3574
      flags|= ON_UPDATE_NOW_FLAG;
 
3575
}
 
3576
 
 
3577
 
 
3578
/**
 
3579
  Get auto-set type for TIMESTAMP field.
 
3580
 
 
3581
  Returns value indicating during which operations this TIMESTAMP field
 
3582
  should be auto-set to current timestamp.
 
3583
*/
 
3584
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
 
3585
{
 
3586
  switch (unireg_check)
 
3587
  {
 
3588
  case TIMESTAMP_DN_FIELD:
 
3589
    return TIMESTAMP_AUTO_SET_ON_INSERT;
 
3590
  case TIMESTAMP_UN_FIELD:
 
3591
    return TIMESTAMP_AUTO_SET_ON_UPDATE;
 
3592
  case TIMESTAMP_OLD_FIELD:
 
3593
    /*
 
3594
      Although we can have several such columns in legacy tables this
 
3595
      function should be called only for first of them (i.e. the one
 
3596
      having auto-set property).
 
3597
    */
 
3598
    DBUG_ASSERT(table->timestamp_field == this);
 
3599
    /* Fall-through */
 
3600
  case TIMESTAMP_DNUN_FIELD:
 
3601
    return TIMESTAMP_AUTO_SET_ON_BOTH;
 
3602
  default:
 
3603
    /*
 
3604
      Normally this function should not be called for TIMESTAMPs without
 
3605
      auto-set property.
 
3606
    */
 
3607
    DBUG_ASSERT(0);
 
3608
    return TIMESTAMP_NO_AUTO_SET;
 
3609
  }
 
3610
}
 
3611
 
 
3612
 
 
3613
int Field_timestamp::store(const char *from,
 
3614
                           uint len,
 
3615
                           CHARSET_INFO *cs __attribute__((__unused__)))
 
3616
{
 
3617
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3618
  MYSQL_TIME l_time;
 
3619
  my_time_t tmp= 0;
 
3620
  int error;
 
3621
  bool have_smth_to_conv;
 
3622
  bool in_dst_time_gap;
 
3623
  THD *thd= table ? table->in_use : current_thd;
 
3624
 
 
3625
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
 
3626
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 1, &error) >
 
3627
                      MYSQL_TIMESTAMP_ERROR);
 
3628
 
 
3629
  if (error || !have_smth_to_conv)
 
3630
  {
 
3631
    error= 1;
 
3632
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
3633
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
 
3634
  }
 
3635
 
 
3636
  /* Only convert a correct date (not a zero date) */
 
3637
  if (have_smth_to_conv && l_time.month)
 
3638
  {
 
3639
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
 
3640
    {
 
3641
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3642
                           ER_WARN_DATA_OUT_OF_RANGE,
 
3643
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
 
3644
      error= 1;
 
3645
    }
 
3646
    else if (in_dst_time_gap)
 
3647
    {
 
3648
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3649
                           ER_WARN_INVALID_TIMESTAMP,
 
3650
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
 
3651
      error= 1;
 
3652
    }
 
3653
  }
 
3654
  store_timestamp(tmp);
 
3655
  return error;
 
3656
}
 
3657
 
 
3658
 
 
3659
int Field_timestamp::store(double nr)
 
3660
{
 
3661
  int error= 0;
 
3662
  if (nr < 0 || nr > 99991231235959.0)
 
3663
  {
 
3664
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3665
                         ER_WARN_DATA_OUT_OF_RANGE,
 
3666
                         nr, MYSQL_TIMESTAMP_DATETIME);
 
3667
    nr= 0;                                      // Avoid overflow on buff
 
3668
    error= 1;
 
3669
  }
 
3670
  error|= Field_timestamp::store((longlong) rint(nr), FALSE);
 
3671
  return error;
 
3672
}
 
3673
 
 
3674
 
 
3675
int Field_timestamp::store(longlong nr,
 
3676
                           bool unsigned_val __attribute__((__unused__)))
 
3677
{
 
3678
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3679
  MYSQL_TIME l_time;
 
3680
  my_time_t timestamp= 0;
 
3681
  int error;
 
3682
  bool in_dst_time_gap;
 
3683
  THD *thd= table ? table->in_use : current_thd;
 
3684
 
 
3685
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
 
3686
  longlong tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
 
3687
                                                 MODE_NO_ZERO_DATE) |
 
3688
                                   MODE_NO_ZERO_IN_DATE, &error);
 
3689
  if (tmp == -1LL)
 
3690
  {
 
3691
    error= 2;
 
3692
  }
 
3693
 
 
3694
  if (!error && tmp)
 
3695
  {
 
3696
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
 
3697
    {
 
3698
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3699
                           ER_WARN_DATA_OUT_OF_RANGE,
 
3700
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
3701
      error= 1;
 
3702
    }
 
3703
    if (in_dst_time_gap)
 
3704
    {
 
3705
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3706
                           ER_WARN_INVALID_TIMESTAMP,
 
3707
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
3708
      error= 1;
 
3709
    }
 
3710
  } else if (error)
 
3711
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3712
                         WARN_DATA_TRUNCATED,
 
3713
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
3714
 
 
3715
  store_timestamp(timestamp);
 
3716
  return error;
 
3717
}
 
3718
 
 
3719
double Field_timestamp::val_real(void)
 
3720
{
 
3721
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3722
  return (double) Field_timestamp::val_int();
 
3723
}
 
3724
 
 
3725
longlong Field_timestamp::val_int(void)
 
3726
{
 
3727
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3728
  uint32 temp;
 
3729
  MYSQL_TIME time_tmp;
 
3730
  THD  *thd= table ? table->in_use : current_thd;
 
3731
 
 
3732
  thd->time_zone_used= 1;
 
3733
#ifdef WORDS_BIGENDIAN
 
3734
  if (table && table->s->db_low_byte_first)
 
3735
    temp=uint4korr(ptr);
 
3736
  else
 
3737
#endif
 
3738
    longget(temp,ptr);
 
3739
 
 
3740
  if (temp == 0L)                               // No time
 
3741
    return(0);                                  /* purecov: inspected */
 
3742
  
 
3743
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
 
3744
  
 
3745
  return time_tmp.year * 10000000000LL + time_tmp.month * 100000000LL +
 
3746
         time_tmp.day * 1000000L + time_tmp.hour * 10000L +
 
3747
         time_tmp.minute * 100 + time_tmp.second;
 
3748
}
 
3749
 
 
3750
 
 
3751
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
 
3752
{
 
3753
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3754
  uint32 temp, temp2;
 
3755
  MYSQL_TIME time_tmp;
 
3756
  THD *thd= table ? table->in_use : current_thd;
 
3757
  char *to;
 
3758
 
 
3759
  val_buffer->alloc(field_length+1);
 
3760
  to= (char*) val_buffer->ptr();
 
3761
  val_buffer->length(field_length);
 
3762
 
 
3763
  thd->time_zone_used= 1;
 
3764
#ifdef WORDS_BIGENDIAN
 
3765
  if (table && table->s->db_low_byte_first)
 
3766
    temp=uint4korr(ptr);
 
3767
  else
 
3768
#endif
 
3769
    longget(temp,ptr);
 
3770
 
 
3771
  if (temp == 0L)
 
3772
  {                                   /* Zero time is "000000" */
 
3773
    val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_bin);
 
3774
    return val_ptr;
 
3775
  }
 
3776
  val_buffer->set_charset(&my_charset_bin);     // Safety
 
3777
  
 
3778
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
 
3779
 
 
3780
  temp= time_tmp.year % 100;
 
3781
  if (temp < YY_PART_YEAR - 1)
 
3782
  {
 
3783
    *to++= '2';
 
3784
    *to++= '0';
 
3785
  }
 
3786
  else
 
3787
  {
 
3788
    *to++= '1';
 
3789
    *to++= '9';
 
3790
  }
 
3791
  temp2=temp/10; temp=temp-temp2*10;
 
3792
  *to++= (char) ('0'+(char) (temp2));
 
3793
  *to++= (char) ('0'+(char) (temp));
 
3794
  *to++= '-';
 
3795
  temp=time_tmp.month;
 
3796
  temp2=temp/10; temp=temp-temp2*10;
 
3797
  *to++= (char) ('0'+(char) (temp2));
 
3798
  *to++= (char) ('0'+(char) (temp));
 
3799
  *to++= '-';
 
3800
  temp=time_tmp.day;
 
3801
  temp2=temp/10; temp=temp-temp2*10;
 
3802
  *to++= (char) ('0'+(char) (temp2));
 
3803
  *to++= (char) ('0'+(char) (temp));
 
3804
  *to++= ' ';
 
3805
  temp=time_tmp.hour;
 
3806
  temp2=temp/10; temp=temp-temp2*10;
 
3807
  *to++= (char) ('0'+(char) (temp2));
 
3808
  *to++= (char) ('0'+(char) (temp));
 
3809
  *to++= ':';
 
3810
  temp=time_tmp.minute;
 
3811
  temp2=temp/10; temp=temp-temp2*10;
 
3812
  *to++= (char) ('0'+(char) (temp2));
 
3813
  *to++= (char) ('0'+(char) (temp));
 
3814
  *to++= ':';
 
3815
  temp=time_tmp.second;
 
3816
  temp2=temp/10; temp=temp-temp2*10;
 
3817
  *to++= (char) ('0'+(char) (temp2));
 
3818
  *to++= (char) ('0'+(char) (temp));
 
3819
  *to= 0;
 
3820
  return val_buffer;
 
3821
}
 
3822
 
 
3823
 
 
3824
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
3825
{
 
3826
  long temp;
 
3827
  THD *thd= table ? table->in_use : current_thd;
 
3828
  thd->time_zone_used= 1;
 
3829
#ifdef WORDS_BIGENDIAN
 
3830
  if (table && table->s->db_low_byte_first)
 
3831
    temp=uint4korr(ptr);
 
3832
  else
 
3833
#endif
 
3834
    longget(temp,ptr);
 
3835
  if (temp == 0L)
 
3836
  {                                   /* Zero time is "000000" */
 
3837
    if (fuzzydate & TIME_NO_ZERO_DATE)
 
3838
      return 1;
 
3839
    bzero((char*) ltime,sizeof(*ltime));
 
3840
  }
 
3841
  else
 
3842
  {
 
3843
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
 
3844
  }
 
3845
  return 0;
 
3846
}
 
3847
 
 
3848
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
 
3849
{
 
3850
  return Field_timestamp::get_date(ltime,0);
 
3851
}
 
3852
 
 
3853
 
 
3854
bool Field_timestamp::send_binary(Protocol *protocol)
 
3855
{
 
3856
  MYSQL_TIME tm;
 
3857
  Field_timestamp::get_date(&tm, 0);
 
3858
  return protocol->store(&tm);
 
3859
}
 
3860
 
 
3861
 
 
3862
int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3863
{
 
3864
  int32 a,b;
 
3865
#ifdef WORDS_BIGENDIAN
 
3866
  if (table && table->s->db_low_byte_first)
 
3867
  {
 
3868
    a=sint4korr(a_ptr);
 
3869
    b=sint4korr(b_ptr);
 
3870
  }
 
3871
  else
 
3872
#endif
 
3873
  {
 
3874
  longget(a,a_ptr);
 
3875
  longget(b,b_ptr);
 
3876
  }
 
3877
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
 
3878
}
 
3879
 
 
3880
 
 
3881
void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
 
3882
{
 
3883
#ifdef WORDS_BIGENDIAN
 
3884
  if (!table || !table->s->db_low_byte_first)
 
3885
  {
 
3886
    to[0] = ptr[0];
 
3887
    to[1] = ptr[1];
 
3888
    to[2] = ptr[2];
 
3889
    to[3] = ptr[3];
 
3890
  }
 
3891
  else
 
3892
#endif
 
3893
  {
 
3894
    to[0] = ptr[3];
 
3895
    to[1] = ptr[2];
 
3896
    to[2] = ptr[1];
 
3897
    to[3] = ptr[0];
 
3898
  }
 
3899
}
 
3900
 
 
3901
 
 
3902
void Field_timestamp::sql_type(String &res) const
 
3903
{
 
3904
  res.set_ascii(STRING_WITH_LEN("timestamp"));
 
3905
}
 
3906
 
 
3907
 
 
3908
void Field_timestamp::set_time()
 
3909
{
 
3910
  THD *thd= table ? table->in_use : current_thd;
 
3911
  long tmp= (long) thd->query_start();
 
3912
  set_notnull();
 
3913
  store_timestamp(tmp);
 
3914
}
 
3915
 
 
3916
/****************************************************************************
 
3917
** time type
 
3918
** In string context: HH:MM:SS
 
3919
** In number context: HHMMSS
 
3920
** Stored as a 3 byte unsigned int
 
3921
****************************************************************************/
 
3922
 
 
3923
int Field_time::store(const char *from,
 
3924
                      uint len,
 
3925
                      CHARSET_INFO *cs __attribute__((__unused__)))
 
3926
{
 
3927
  MYSQL_TIME ltime;
 
3928
  long tmp;
 
3929
  int error= 0;
 
3930
  int warning;
 
3931
 
 
3932
  if (str_to_time(from, len, &ltime, &warning))
 
3933
  {
 
3934
    tmp=0L;
 
3935
    error= 2;
 
3936
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
3937
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
 
3938
  }
 
3939
  else
 
3940
  {
 
3941
    if (warning & MYSQL_TIME_WARN_TRUNCATED)
 
3942
    {
 
3943
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3944
                           WARN_DATA_TRUNCATED,
 
3945
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
 
3946
      error= 1;
 
3947
    }
 
3948
    if (warning & MYSQL_TIME_WARN_OUT_OF_RANGE)
 
3949
    {
 
3950
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3951
                           ER_WARN_DATA_OUT_OF_RANGE,
 
3952
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
 
3953
      error= 1;
 
3954
    }
 
3955
    if (ltime.month)
 
3956
      ltime.day=0;
 
3957
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
 
3958
  }
 
3959
  
 
3960
  if (ltime.neg)
 
3961
    tmp= -tmp;
 
3962
  int3store(ptr,tmp);
 
3963
  return error;
 
3964
}
 
3965
 
 
3966
 
 
3967
int Field_time::store_time(MYSQL_TIME *ltime,
 
3968
                           timestamp_type time_type __attribute__((__unused__)))
 
3969
{
 
3970
  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
 
3971
            (ltime->minute * 100 + ltime->second);
 
3972
  if (ltime->neg)
 
3973
    tmp= -tmp;
 
3974
  return Field_time::store((longlong) tmp, FALSE);
 
3975
}
 
3976
 
 
3977
 
 
3978
int Field_time::store(double nr)
 
3979
{
 
3980
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3981
  long tmp;
 
3982
  int error= 0;
 
3983
  if (nr > (double)TIME_MAX_VALUE)
 
3984
  {
 
3985
    tmp= TIME_MAX_VALUE;
 
3986
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3987
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
 
3988
    error= 1;
 
3989
  }
 
3990
  else if (nr < (double)-TIME_MAX_VALUE)
 
3991
  {
 
3992
    tmp= -TIME_MAX_VALUE;
 
3993
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3994
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
 
3995
    error= 1;
 
3996
  }
 
3997
  else
 
3998
  {
 
3999
    tmp=(long) floor(fabs(nr));                 // Remove fractions
 
4000
    if (nr < 0)
 
4001
      tmp= -tmp;
 
4002
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
4003
    {
 
4004
      tmp=0;
 
4005
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
4006
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
 
4007
                           MYSQL_TIMESTAMP_TIME);
 
4008
      error= 1;
 
4009
    }
 
4010
  }
 
4011
  int3store(ptr,tmp);
 
4012
  return error;
 
4013
}
 
4014
 
 
4015
 
 
4016
int Field_time::store(longlong nr, bool unsigned_val)
 
4017
{
 
4018
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4019
  long tmp;
 
4020
  int error= 0;
 
4021
  if (nr < (longlong) -TIME_MAX_VALUE && !unsigned_val)
 
4022
  {
 
4023
    tmp= -TIME_MAX_VALUE;
 
4024
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
4025
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
 
4026
                         MYSQL_TIMESTAMP_TIME, 1);
 
4027
    error= 1;
 
4028
  }
 
4029
  else if (nr > (longlong) TIME_MAX_VALUE || (nr < 0 && unsigned_val))
 
4030
  {
 
4031
    tmp= TIME_MAX_VALUE;
 
4032
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
4033
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
 
4034
                         MYSQL_TIMESTAMP_TIME, 1);
 
4035
    error= 1;
 
4036
  }
 
4037
  else
 
4038
  {
 
4039
    tmp=(long) nr;
 
4040
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
4041
    {
 
4042
      tmp=0;
 
4043
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
4044
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
 
4045
                           MYSQL_TIMESTAMP_TIME, 1);
 
4046
      error= 1;
 
4047
    }
 
4048
  }
 
4049
  int3store(ptr,tmp);
 
4050
  return error;
 
4051
}
 
4052
 
 
4053
 
 
4054
double Field_time::val_real(void)
 
4055
{
 
4056
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4057
  uint32 j= (uint32) uint3korr(ptr);
 
4058
  return (double) j;
 
4059
}
 
4060
 
 
4061
longlong Field_time::val_int(void)
 
4062
{
 
4063
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4064
  return (longlong) sint3korr(ptr);
 
4065
}
 
4066
 
 
4067
 
 
4068
/**
 
4069
  @note
 
4070
  This function is multi-byte safe as the result string is always of type
 
4071
  my_charset_bin
 
4072
*/
 
4073
 
 
4074
String *Field_time::val_str(String *val_buffer,
 
4075
                            String *val_ptr __attribute__((unused)))
 
4076
{
 
4077
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4078
  MYSQL_TIME ltime;
 
4079
  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
 
4080
  long tmp=(long) sint3korr(ptr);
 
4081
  ltime.neg= 0;
 
4082
  if (tmp < 0)
 
4083
  {
 
4084
    tmp= -tmp;
 
4085
    ltime.neg= 1;
 
4086
  }
 
4087
  ltime.day= (uint) 0;
 
4088
  ltime.hour= (uint) (tmp/10000);
 
4089
  ltime.minute= (uint) (tmp/100 % 100);
 
4090
  ltime.second= (uint) (tmp % 100);
 
4091
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
 
4092
  return val_buffer;
 
4093
}
 
4094
 
 
4095
 
 
4096
/**
 
4097
  @note
 
4098
  Normally we would not consider 'time' as a valid date, but we allow
 
4099
  get_date() here to be able to do things like
 
4100
  DATE_FORMAT(time, "%l.%i %p")
 
4101
*/
 
4102
 
 
4103
bool Field_time::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4104
{
 
4105
  long tmp;
 
4106
  THD *thd= table ? table->in_use : current_thd;
 
4107
  if (!(fuzzydate & TIME_FUZZY_DATE))
 
4108
  {
 
4109
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
4110
                        ER_WARN_DATA_OUT_OF_RANGE,
 
4111
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
 
4112
                        thd->row_count);
 
4113
    return 1;
 
4114
  }
 
4115
  tmp=(long) sint3korr(ptr);
 
4116
  ltime->neg=0;
 
4117
  if (tmp < 0)
 
4118
  {
 
4119
    ltime->neg= 1;
 
4120
    tmp=-tmp;
 
4121
  }
 
4122
  ltime->hour=tmp/10000;
 
4123
  tmp-=ltime->hour*10000;
 
4124
  ltime->minute=   tmp/100;
 
4125
  ltime->second= tmp % 100;
 
4126
  ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
 
4127
  return 0;
 
4128
}
 
4129
 
 
4130
 
 
4131
bool Field_time::get_time(MYSQL_TIME *ltime)
 
4132
{
 
4133
  long tmp=(long) sint3korr(ptr);
 
4134
  ltime->neg=0;
 
4135
  if (tmp < 0)
 
4136
  {
 
4137
    ltime->neg= 1;
 
4138
    tmp=-tmp;
 
4139
  }
 
4140
  ltime->day= 0;
 
4141
  ltime->hour=   (int) (tmp/10000);
 
4142
  tmp-=ltime->hour*10000;
 
4143
  ltime->minute= (int) tmp/100;
 
4144
  ltime->second= (int) tmp % 100;
 
4145
  ltime->second_part=0;
 
4146
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
 
4147
  return 0;
 
4148
}
 
4149
 
 
4150
 
 
4151
bool Field_time::send_binary(Protocol *protocol)
 
4152
{
 
4153
  MYSQL_TIME tm;
 
4154
  Field_time::get_time(&tm);
 
4155
  tm.day= tm.hour/24;                           // Move hours to days
 
4156
  tm.hour-= tm.day*24;
 
4157
  return protocol->store_time(&tm);
 
4158
}
 
4159
 
 
4160
 
 
4161
int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4162
{
 
4163
  int32 a,b;
 
4164
  a=(int32) sint3korr(a_ptr);
 
4165
  b=(int32) sint3korr(b_ptr);
 
4166
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
4167
}
 
4168
 
 
4169
void Field_time::sort_string(uchar *to,uint length __attribute__((unused)))
 
4170
{
 
4171
  to[0] = (uchar) (ptr[2] ^ 128);
 
4172
  to[1] = ptr[1];
 
4173
  to[2] = ptr[0];
 
4174
}
 
4175
 
 
4176
void Field_time::sql_type(String &res) const
 
4177
{
 
4178
  res.set_ascii(STRING_WITH_LEN("time"));
 
4179
}
 
4180
 
 
4181
/****************************************************************************
 
4182
** year type
 
4183
** Save in a byte the year 0, 1901->2155
 
4184
** Can handle 2 byte or 4 byte years!
 
4185
****************************************************************************/
 
4186
 
 
4187
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
 
4188
{
 
4189
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4190
  char *end;
 
4191
  int error;
 
4192
  longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
 
4193
 
 
4194
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 || 
 
4195
      error == MY_ERRNO_ERANGE)
 
4196
  {
 
4197
    *ptr=0;
 
4198
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4199
    return 1;
 
4200
  }
 
4201
  if (table->in_use->count_cuted_fields && 
 
4202
      (error= check_int(cs, from, len, end, error)))
 
4203
  {
 
4204
    if (error == 1)  /* empty or incorrect string */
 
4205
    {
 
4206
      *ptr= 0;
 
4207
      return 1;
 
4208
    }
 
4209
    error= 1;
 
4210
  }
 
4211
 
 
4212
  if (nr != 0 || len != 4)
 
4213
  {
 
4214
    if (nr < YY_PART_YEAR)
 
4215
      nr+=100;                                  // 2000 - 2069
 
4216
    else if (nr > 1900)
 
4217
      nr-= 1900;
 
4218
  }
 
4219
  *ptr= (char) (uchar) nr;
 
4220
  return error;
 
4221
}
 
4222
 
 
4223
 
 
4224
int Field_year::store(double nr)
 
4225
{
 
4226
  if (nr < 0.0 || nr >= 2155.0)
 
4227
  {
 
4228
    (void) Field_year::store((longlong) -1, FALSE);
 
4229
    return 1;
 
4230
  }
 
4231
  return Field_year::store((longlong) nr, FALSE);
 
4232
}
 
4233
 
 
4234
 
 
4235
int Field_year::store(longlong nr,
 
4236
                      bool unsigned_val __attribute__((__unused__)))
 
4237
{
 
4238
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4239
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
 
4240
  {
 
4241
    *ptr= 0;
 
4242
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4243
    return 1;
 
4244
  }
 
4245
  if (nr != 0 || field_length != 4)             // 0000 -> 0; 00 -> 2000
 
4246
  {
 
4247
    if (nr < YY_PART_YEAR)
 
4248
      nr+=100;                                  // 2000 - 2069
 
4249
    else if (nr > 1900)
 
4250
      nr-= 1900;
 
4251
  }
 
4252
  *ptr= (char) (uchar) nr;
 
4253
  return 0;
 
4254
}
 
4255
 
 
4256
 
 
4257
bool Field_year::send_binary(Protocol *protocol)
 
4258
{
 
4259
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4260
  uint64_t tmp= Field_year::val_int();
 
4261
  return protocol->store_short(tmp);
 
4262
}
 
4263
 
 
4264
 
 
4265
double Field_year::val_real(void)
 
4266
{
 
4267
  return (double) Field_year::val_int();
 
4268
}
 
4269
 
 
4270
 
 
4271
longlong Field_year::val_int(void)
 
4272
{
 
4273
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4274
  int tmp= (int) ptr[0];
 
4275
  if (field_length != 4)
 
4276
    tmp%=100;                                   // Return last 2 char
 
4277
  else if (tmp)
 
4278
    tmp+=1900;
 
4279
  return (longlong) tmp;
 
4280
}
 
4281
 
 
4282
 
 
4283
String *Field_year::val_str(String *val_buffer,
 
4284
                            String *val_ptr __attribute__((unused)))
 
4285
{
 
4286
  val_buffer->alloc(5);
 
4287
  val_buffer->length(field_length);
 
4288
  char *to=(char*) val_buffer->ptr();
 
4289
  sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
 
4290
  return val_buffer;
 
4291
}
 
4292
 
 
4293
 
 
4294
void Field_year::sql_type(String &res) const
 
4295
{
 
4296
  CHARSET_INFO *cs=res.charset();
 
4297
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
 
4298
                          "year(%d)",(int) field_length));
 
4299
}
 
4300
 
 
4301
 
 
4302
/****************************************************************************
 
4303
** The new date type
 
4304
** This is identical to the old date type, but stored on 3 bytes instead of 4
 
4305
** In number context: YYYYMMDD
 
4306
****************************************************************************/
 
4307
 
 
4308
/*
 
4309
  Store string into a date field
 
4310
 
 
4311
  SYNOPSIS
 
4312
    Field_newdate::store()
 
4313
    from                Date string
 
4314
    len                 Length of date field
 
4315
    cs                  Character set (not used)
 
4316
 
 
4317
  RETURN
 
4318
    0  ok
 
4319
    1  Value was cut during conversion
 
4320
    2  Wrong date string
 
4321
    3  Datetime value that was cut (warning level NOTE)
 
4322
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
 
4323
       nearly-identical class Field_date doesn't ever return 3 from its
 
4324
       store function.
 
4325
*/
 
4326
 
 
4327
int Field_newdate::store(const char *from,
 
4328
                         uint len,
 
4329
                         CHARSET_INFO *cs __attribute__((__unused__)))
 
4330
{
 
4331
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4332
  long tmp;
 
4333
  MYSQL_TIME l_time;
 
4334
  int error;
 
4335
  THD *thd= table ? table->in_use : current_thd;
 
4336
  enum enum_mysql_timestamp_type ret;
 
4337
  if ((ret= str_to_datetime(from, len, &l_time,
 
4338
                            (TIME_FUZZY_DATE |
 
4339
                             (thd->variables.sql_mode &
 
4340
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4341
                               MODE_INVALID_DATES))),
 
4342
                            &error)) <= MYSQL_TIMESTAMP_ERROR)
 
4343
  {
 
4344
    tmp= 0;
 
4345
    error= 2;
 
4346
  }
 
4347
  else
 
4348
  {
 
4349
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
4350
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
 
4351
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
4352
      error= 3;                                 // Datetime was cut (note)
 
4353
  }
 
4354
 
 
4355
  if (error)
 
4356
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
4357
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
4358
                         WARN_DATA_TRUNCATED,
 
4359
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
 
4360
 
 
4361
  int3store(ptr, tmp);
 
4362
  return error;
 
4363
}
 
4364
 
 
4365
 
 
4366
int Field_newdate::store(double nr)
 
4367
{
 
4368
  if (nr < 0.0 || nr > 99991231235959.0)
 
4369
  {
 
4370
    int3store(ptr,(int32) 0);
 
4371
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4372
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
 
4373
    return 1;
 
4374
  }
 
4375
  return Field_newdate::store((longlong) rint(nr), FALSE);
 
4376
}
 
4377
 
 
4378
 
 
4379
int Field_newdate::store(longlong nr,
 
4380
                         bool unsigned_val __attribute__((__unused__)))
 
4381
{
 
4382
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4383
  MYSQL_TIME l_time;
 
4384
  longlong tmp;
 
4385
  int error;
 
4386
  THD *thd= table ? table->in_use : current_thd;
 
4387
  if (number_to_datetime(nr, &l_time,
 
4388
                         (TIME_FUZZY_DATE |
 
4389
                          (thd->variables.sql_mode &
 
4390
                           (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4391
                            MODE_INVALID_DATES))),
 
4392
                         &error) == -1LL)
 
4393
  {
 
4394
    tmp= 0L;
 
4395
    error= 2;
 
4396
  }
 
4397
  else
 
4398
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
4399
 
 
4400
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
 
4401
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
4402
    error= 3;
 
4403
 
 
4404
  if (error)
 
4405
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
4406
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
4407
                         error == 2 ? 
 
4408
                         ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED,
 
4409
                         nr,MYSQL_TIMESTAMP_DATE, 1);
 
4410
 
 
4411
  int3store(ptr,tmp);
 
4412
  return error;
 
4413
}
 
4414
 
 
4415
 
 
4416
int Field_newdate::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
 
4417
{
 
4418
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4419
  long tmp;
 
4420
  int error= 0;
 
4421
  if (time_type == MYSQL_TIMESTAMP_DATE ||
 
4422
      time_type == MYSQL_TIMESTAMP_DATETIME)
 
4423
  {
 
4424
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
 
4425
    if (check_date(ltime, tmp != 0,
 
4426
                   (TIME_FUZZY_DATE |
 
4427
                    (current_thd->variables.sql_mode &
 
4428
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4429
                      MODE_INVALID_DATES))), &error))
 
4430
    {
 
4431
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
4432
      String str(buff, sizeof(buff), &my_charset_latin1);
 
4433
      make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
 
4434
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
4435
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
 
4436
    }
 
4437
    if (!error && ltime->time_type != MYSQL_TIMESTAMP_DATE &&
 
4438
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
 
4439
    {
 
4440
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
4441
      String str(buff, sizeof(buff), &my_charset_latin1);
 
4442
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
4443
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_NOTE,
 
4444
                           WARN_DATA_TRUNCATED,
 
4445
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
 
4446
      error= 3;
 
4447
    }
 
4448
  }
 
4449
  else
 
4450
  {
 
4451
    tmp=0;
 
4452
    error= 1;
 
4453
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
4454
  }
 
4455
  int3store(ptr,tmp);
 
4456
  return error;
 
4457
}
 
4458
 
 
4459
 
 
4460
bool Field_newdate::send_binary(Protocol *protocol)
 
4461
{
 
4462
  MYSQL_TIME tm;
 
4463
  Field_newdate::get_date(&tm,0);
 
4464
  return protocol->store_date(&tm);
 
4465
}
 
4466
 
 
4467
 
 
4468
double Field_newdate::val_real(void)
 
4469
{
 
4470
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4471
  return (double) Field_newdate::val_int();
 
4472
}
 
4473
 
 
4474
 
 
4475
longlong Field_newdate::val_int(void)
 
4476
{
 
4477
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4478
  ulong j= uint3korr(ptr);
 
4479
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
 
4480
  return (longlong) j;
 
4481
}
 
4482
 
 
4483
 
 
4484
String *Field_newdate::val_str(String *val_buffer,
 
4485
                               String *val_ptr __attribute__((unused)))
 
4486
{
 
4487
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4488
  val_buffer->alloc(field_length);
 
4489
  val_buffer->length(field_length);
 
4490
  uint32 tmp=(uint32) uint3korr(ptr);
 
4491
  int part;
 
4492
  char *pos=(char*) val_buffer->ptr()+10;
 
4493
 
 
4494
  /* Open coded to get more speed */
 
4495
  *pos--=0;                                     // End NULL
 
4496
  part=(int) (tmp & 31);
 
4497
  *pos--= (char) ('0'+part%10);
 
4498
  *pos--= (char) ('0'+part/10);
 
4499
  *pos--= '-';
 
4500
  part=(int) (tmp >> 5 & 15);
 
4501
  *pos--= (char) ('0'+part%10);
 
4502
  *pos--= (char) ('0'+part/10);
 
4503
  *pos--= '-';
 
4504
  part=(int) (tmp >> 9);
 
4505
  *pos--= (char) ('0'+part%10); part/=10;
 
4506
  *pos--= (char) ('0'+part%10); part/=10;
 
4507
  *pos--= (char) ('0'+part%10); part/=10;
 
4508
  *pos=   (char) ('0'+part);
 
4509
  return val_buffer;
 
4510
}
 
4511
 
 
4512
 
 
4513
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
 
4514
{
 
4515
  uint32 tmp=(uint32) uint3korr(ptr);
 
4516
  ltime->day=   tmp & 31;
 
4517
  ltime->month= (tmp >> 5) & 15;
 
4518
  ltime->year=  (tmp >> 9);
 
4519
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
 
4520
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
 
4521
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
 
4522
          1 : 0);
 
4523
}
 
4524
 
 
4525
 
 
4526
bool Field_newdate::get_time(MYSQL_TIME *ltime)
 
4527
{
 
4528
  return Field_newdate::get_date(ltime,0);
 
4529
}
 
4530
 
 
4531
 
 
4532
int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4533
{
 
4534
  uint32 a,b;
 
4535
  a=(uint32) uint3korr(a_ptr);
 
4536
  b=(uint32) uint3korr(b_ptr);
 
4537
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
4538
}
 
4539
 
 
4540
 
 
4541
void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
 
4542
{
 
4543
  to[0] = ptr[2];
 
4544
  to[1] = ptr[1];
 
4545
  to[2] = ptr[0];
 
4546
}
 
4547
 
 
4548
 
 
4549
void Field_newdate::sql_type(String &res) const
 
4550
{
 
4551
  res.set_ascii(STRING_WITH_LEN("date"));
 
4552
}
 
4553
 
 
4554
 
 
4555
/****************************************************************************
 
4556
** datetime type
 
4557
** In string context: YYYY-MM-DD HH:MM:DD
 
4558
** In number context: YYYYMMDDHHMMDD
 
4559
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
 
4560
****************************************************************************/
 
4561
 
 
4562
int Field_datetime::store(const char *from,
 
4563
                          uint len,
 
4564
                          CHARSET_INFO *cs __attribute__((__unused__)))
 
4565
{
 
4566
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4567
  MYSQL_TIME time_tmp;
 
4568
  int error;
 
4569
  uint64_t tmp= 0;
 
4570
  enum enum_mysql_timestamp_type func_res;
 
4571
  THD *thd= table ? table->in_use : current_thd;
 
4572
 
 
4573
  func_res= str_to_datetime(from, len, &time_tmp,
 
4574
                            (TIME_FUZZY_DATE |
 
4575
                             (thd->variables.sql_mode &
 
4576
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4577
                               MODE_INVALID_DATES))),
 
4578
                            &error);
 
4579
  if ((int) func_res > (int) MYSQL_TIMESTAMP_ERROR)
 
4580
    tmp= TIME_to_ulonglong_datetime(&time_tmp);
 
4581
  else
 
4582
    error= 1;                                 // Fix if invalid zero date
 
4583
 
 
4584
  if (error)
 
4585
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4586
                         ER_WARN_DATA_OUT_OF_RANGE,
 
4587
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
 
4588
 
 
4589
#ifdef WORDS_BIGENDIAN
 
4590
  if (table && table->s->db_low_byte_first)
 
4591
  {
 
4592
    int8store(ptr,tmp);
 
4593
  }
 
4594
  else
 
4595
#endif
 
4596
    longlongstore(ptr,tmp);
 
4597
  return error;
 
4598
}
 
4599
 
 
4600
 
 
4601
int Field_datetime::store(double nr)
 
4602
{
 
4603
  int error= 0;
 
4604
  if (nr < 0.0 || nr > 99991231235959.0)
 
4605
  {
 
4606
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
4607
                         ER_WARN_DATA_OUT_OF_RANGE,
 
4608
                         nr, MYSQL_TIMESTAMP_DATETIME);
 
4609
    nr= 0.0;
 
4610
    error= 1;
 
4611
  }
 
4612
  error|= Field_datetime::store((longlong) rint(nr), FALSE);
 
4613
  return error;
 
4614
}
 
4615
 
 
4616
 
 
4617
int Field_datetime::store(longlong nr,
 
4618
                          bool unsigned_val __attribute__((__unused__)))
 
4619
{
 
4620
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4621
  MYSQL_TIME not_used;
 
4622
  int error;
 
4623
  longlong initial_nr= nr;
 
4624
  THD *thd= table ? table->in_use : current_thd;
 
4625
 
 
4626
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
 
4627
                                         (thd->variables.sql_mode &
 
4628
                                          (MODE_NO_ZERO_IN_DATE |
 
4629
                                           MODE_NO_ZERO_DATE |
 
4630
                                           MODE_INVALID_DATES))), &error);
 
4631
 
 
4632
  if (nr == -1LL)
 
4633
  {
 
4634
    nr= 0;
 
4635
    error= 2;
 
4636
  }
 
4637
 
 
4638
  if (error)
 
4639
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4640
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
 
4641
                         WARN_DATA_TRUNCATED, initial_nr,
 
4642
                         MYSQL_TIMESTAMP_DATETIME, 1);
 
4643
 
 
4644
#ifdef WORDS_BIGENDIAN
 
4645
  if (table && table->s->db_low_byte_first)
 
4646
  {
 
4647
    int8store(ptr,nr);
 
4648
  }
 
4649
  else
 
4650
#endif
 
4651
    longlongstore(ptr,nr);
 
4652
  return error;
 
4653
}
 
4654
 
 
4655
 
 
4656
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
 
4657
{
 
4658
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4659
  longlong tmp;
 
4660
  int error= 0;
 
4661
  /*
 
4662
    We don't perform range checking here since values stored in TIME
 
4663
    structure always fit into DATETIME range.
 
4664
  */
 
4665
  if (time_type == MYSQL_TIMESTAMP_DATE ||
 
4666
      time_type == MYSQL_TIMESTAMP_DATETIME)
 
4667
  {
 
4668
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*1000000LL+
 
4669
         (ltime->hour*10000L+ltime->minute*100+ltime->second));
 
4670
    if (check_date(ltime, tmp != 0,
 
4671
                   (TIME_FUZZY_DATE |
 
4672
                    (current_thd->variables.sql_mode &
 
4673
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4674
                      MODE_INVALID_DATES))), &error))
 
4675
    {
 
4676
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
4677
      String str(buff, sizeof(buff), &my_charset_latin1);
 
4678
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
4679
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
4680
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATETIME,1);
 
4681
    }
 
4682
  }
 
4683
  else
 
4684
  {
 
4685
    tmp=0;
 
4686
    error= 1;
 
4687
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
4688
  }
 
4689
#ifdef WORDS_BIGENDIAN
 
4690
  if (table && table->s->db_low_byte_first)
 
4691
  {
 
4692
    int8store(ptr,tmp);
 
4693
  }
 
4694
  else
 
4695
#endif
 
4696
    longlongstore(ptr,tmp);
 
4697
  return error;
 
4698
}
 
4699
 
 
4700
bool Field_datetime::send_binary(Protocol *protocol)
 
4701
{
 
4702
  MYSQL_TIME tm;
 
4703
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
 
4704
  return protocol->store(&tm);
 
4705
}
 
4706
 
 
4707
 
 
4708
double Field_datetime::val_real(void)
 
4709
{
 
4710
  return (double) Field_datetime::val_int();
 
4711
}
 
4712
 
 
4713
longlong Field_datetime::val_int(void)
 
4714
{
 
4715
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4716
  longlong j;
 
4717
#ifdef WORDS_BIGENDIAN
 
4718
  if (table && table->s->db_low_byte_first)
 
4719
    j=sint8korr(ptr);
 
4720
  else
 
4721
#endif
 
4722
    longlongget(j,ptr);
 
4723
  return j;
 
4724
}
 
4725
 
 
4726
 
 
4727
String *Field_datetime::val_str(String *val_buffer,
 
4728
                                String *val_ptr __attribute__((unused)))
 
4729
{
 
4730
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4731
  val_buffer->alloc(field_length);
 
4732
  val_buffer->length(field_length);
 
4733
  uint64_t tmp;
 
4734
  long part1,part2;
 
4735
  char *pos;
 
4736
  int part3;
 
4737
 
 
4738
#ifdef WORDS_BIGENDIAN
 
4739
  if (table && table->s->db_low_byte_first)
 
4740
    tmp=sint8korr(ptr);
 
4741
  else
 
4742
#endif
 
4743
    longlongget(tmp,ptr);
 
4744
 
 
4745
  /*
 
4746
    Avoid problem with slow longlong arithmetic and sprintf
 
4747
  */
 
4748
 
 
4749
  part1=(long) (tmp/1000000LL);
 
4750
  part2=(long) (tmp - (uint64_t) part1*1000000LL);
 
4751
 
 
4752
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
 
4753
  *pos--=0;
 
4754
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
 
4755
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
 
4756
  *pos--= ':';
 
4757
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4758
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4759
  *pos--= ':';
 
4760
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4761
  *pos--= (char) ('0'+(char) part3);
 
4762
  *pos--= ' ';
 
4763
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
4764
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
4765
  *pos--= '-';
 
4766
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
4767
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
 
4768
  *pos--= '-';
 
4769
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4770
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4771
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4772
  *pos=(char) ('0'+(char) part3);
 
4773
  return val_buffer;
 
4774
}
 
4775
 
 
4776
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4777
{
 
4778
  longlong tmp=Field_datetime::val_int();
 
4779
  uint32 part1,part2;
 
4780
  part1=(uint32) (tmp/1000000LL);
 
4781
  part2=(uint32) (tmp - (uint64_t) part1*1000000LL);
 
4782
 
 
4783
  ltime->time_type=     MYSQL_TIMESTAMP_DATETIME;
 
4784
  ltime->neg=           0;
 
4785
  ltime->second_part=   0;
 
4786
  ltime->second=        (int) (part2%100);
 
4787
  ltime->minute=        (int) (part2/100%100);
 
4788
  ltime->hour=          (int) (part2/10000);
 
4789
  ltime->day=           (int) (part1%100);
 
4790
  ltime->month=         (int) (part1/100%100);
 
4791
  ltime->year=          (int) (part1/10000);
 
4792
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
 
4793
}
 
4794
 
 
4795
bool Field_datetime::get_time(MYSQL_TIME *ltime)
 
4796
{
 
4797
  return Field_datetime::get_date(ltime,0);
 
4798
}
 
4799
 
 
4800
int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4801
{
 
4802
  longlong a,b;
 
4803
#ifdef WORDS_BIGENDIAN
 
4804
  if (table && table->s->db_low_byte_first)
 
4805
  {
 
4806
    a=sint8korr(a_ptr);
 
4807
    b=sint8korr(b_ptr);
 
4808
  }
 
4809
  else
 
4810
#endif
 
4811
  {
 
4812
    longlongget(a,a_ptr);
 
4813
    longlongget(b,b_ptr);
 
4814
  }
 
4815
  return ((uint64_t) a < (uint64_t) b) ? -1 :
 
4816
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
4817
}
 
4818
 
 
4819
void Field_datetime::sort_string(uchar *to,uint length __attribute__((unused)))
 
4820
{
 
4821
#ifdef WORDS_BIGENDIAN
 
4822
  if (!table || !table->s->db_low_byte_first)
 
4823
  {
 
4824
    to[0] = ptr[0];
 
4825
    to[1] = ptr[1];
 
4826
    to[2] = ptr[2];
 
4827
    to[3] = ptr[3];
 
4828
    to[4] = ptr[4];
 
4829
    to[5] = ptr[5];
 
4830
    to[6] = ptr[6];
 
4831
    to[7] = ptr[7];
 
4832
  }
 
4833
  else
 
4834
#endif
 
4835
  {
 
4836
    to[0] = ptr[7];
 
4837
    to[1] = ptr[6];
 
4838
    to[2] = ptr[5];
 
4839
    to[3] = ptr[4];
 
4840
    to[4] = ptr[3];
 
4841
    to[5] = ptr[2];
 
4842
    to[6] = ptr[1];
 
4843
    to[7] = ptr[0];
 
4844
  }
 
4845
}
 
4846
 
 
4847
 
 
4848
void Field_datetime::sql_type(String &res) const
 
4849
{
 
4850
  res.set_ascii(STRING_WITH_LEN("datetime"));
 
4851
}
 
4852
 
 
4853
/****************************************************************************
 
4854
** string type
 
4855
** A string may be varchar or binary
 
4856
****************************************************************************/
1412
4857
 
1413
4858
/*
1414
4859
  Report "not well formed" or "cannot convert" error
1432
4877
      "Cannot convert character string: 'xxx' for column 't' at row 1"
1433
4878
 
1434
4879
  RETURN
1435
 
    false - If errors didn't happen
1436
 
    true  - If an error happened
 
4880
    FALSE - If errors didn't happen
 
4881
    TRUE  - If an error happened
1437
4882
*/
1438
4883
 
1439
 
bool
 
4884
static bool
1440
4885
check_string_copy_error(Field_str *field,
1441
4886
                        const char *well_formed_error_pos,
1442
4887
                        const char *cannot_convert_error_pos,
1443
4888
                        const char *end,
1444
 
                        const CHARSET_INFO * const cs)
 
4889
                        CHARSET_INFO *cs)
1445
4890
{
1446
4891
  const char *pos, *end_orig;
1447
4892
  char tmp[64], *t;
1448
4893
  
1449
4894
  if (!(pos= well_formed_error_pos) &&
1450
4895
      !(pos= cannot_convert_error_pos))
1451
 
    return false;
 
4896
    return FALSE;
1452
4897
 
1453
4898
  end_orig= end;
1454
4899
  set_if_smaller(end, pos + 6);
1487
4932
  *t= '\0';
1488
4933
  push_warning_printf(field->table->in_use, 
1489
4934
                      field->table->in_use->abort_on_warning ?
1490
 
                      DRIZZLE_ERROR::WARN_LEVEL_ERROR :
1491
 
                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
4935
                      MYSQL_ERROR::WARN_LEVEL_ERROR :
 
4936
                      MYSQL_ERROR::WARN_LEVEL_WARN,
1492
4937
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
1493
4938
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1494
4939
                      "string", tmp, field->field_name,
1495
 
                      (uint32_t) field->table->in_use->row_count);
1496
 
  return true;
 
4940
                      (ulong) field->table->in_use->row_count);
 
4941
  return TRUE;
1497
4942
}
1498
4943
 
1499
4944
 
1523
4968
    if (test_if_important_data(field_charset, ptr, end))
1524
4969
    {
1525
4970
      if (table->in_use->abort_on_warning)
1526
 
        set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
4971
        set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
1527
4972
      else
1528
 
        set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
4973
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1529
4974
    }
1530
4975
    else /* If we lost only spaces then produce a NOTE, not a WARNING */
1531
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
4976
      set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1532
4977
    return 2;
1533
4978
  }
1534
4979
  return 0;
1535
4980
}
1536
4981
 
1537
4982
 
 
4983
        /* Copy a string and fill with space */
 
4984
 
 
4985
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
 
4986
{
 
4987
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4988
  uint copy_length;
 
4989
  const char *well_formed_error_pos;
 
4990
  const char *cannot_convert_error_pos;
 
4991
  const char *from_end_pos;
 
4992
 
 
4993
  /* See the comment for Field_long::store(long long) */
 
4994
  DBUG_ASSERT(table->in_use == current_thd);
 
4995
 
 
4996
  copy_length= well_formed_copy_nchars(field_charset,
 
4997
                                       (char*) ptr, field_length,
 
4998
                                       cs, from, length,
 
4999
                                       field_length / field_charset->mbmaxlen,
 
5000
                                       &well_formed_error_pos,
 
5001
                                       &cannot_convert_error_pos,
 
5002
                                       &from_end_pos);
 
5003
 
 
5004
  /* Append spaces if the string was shorter than the field. */
 
5005
  if (copy_length < field_length)
 
5006
    field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
 
5007
                              field_length-copy_length,
 
5008
                              field_charset->pad_char);
 
5009
 
 
5010
  if (check_string_copy_error(this, well_formed_error_pos,
 
5011
                              cannot_convert_error_pos, from + length, cs))
 
5012
    return 2;
 
5013
 
 
5014
  return report_if_important_data(from_end_pos, from + length);
 
5015
}
 
5016
 
 
5017
 
1538
5018
/**
1539
 
  Store double value in Field_varstring.
 
5019
  Store double value in Field_string or Field_varstring.
1540
5020
 
1541
5021
  Pretty prints double number into field_length characters buffer.
1542
5022
 
1545
5025
 
1546
5026
int Field_str::store(double nr)
1547
5027
{
 
5028
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1548
5029
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
1549
 
  uint32_t local_char_length= field_length / charset()->mbmaxlen;
 
5030
  uint local_char_length= field_length / charset()->mbmaxlen;
1550
5031
  size_t length;
1551
 
  bool error;
 
5032
  my_bool error;
1552
5033
 
1553
5034
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
1554
5035
  if (error)
1555
5036
  {
1556
5037
    if (table->in_use->abort_on_warning)
1557
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
5038
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
1558
5039
    else
1559
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
5040
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1560
5041
  }
1561
5042
  return store(buff, length, charset());
1562
5043
}
1563
5044
 
1564
5045
 
1565
 
uint32_t Field::is_equal(Create_field *new_field)
 
5046
uint Field::is_equal(Create_field *new_field)
1566
5047
{
1567
5048
  return (new_field->sql_type == real_type());
1568
5049
}
1570
5051
 
1571
5052
/* If one of the fields is binary and the other one isn't return 1 else 0 */
1572
5053
 
1573
 
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32_t flag_arg)
 
5054
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32 flag_arg)
1574
5055
{
1575
5056
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
1576
5057
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
1579
5060
}
1580
5061
 
1581
5062
 
1582
 
uint32_t Field_str::is_equal(Create_field *new_field)
 
5063
uint Field_str::is_equal(Create_field *new_field)
1583
5064
{
1584
5065
  if (compare_str_field_flags(new_field, flags))
1585
5066
    return 0;
1590
5071
}
1591
5072
 
1592
5073
 
 
5074
int Field_string::store(longlong nr, bool unsigned_val)
 
5075
{
 
5076
  char buff[64];
 
5077
  int  l;
 
5078
  CHARSET_INFO *cs=charset();
 
5079
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
 
5080
                                   unsigned_val ? 10 : -10, nr);
 
5081
  return Field_string::store(buff,(uint)l,cs);
 
5082
}
 
5083
 
 
5084
 
1593
5085
int Field_longstr::store_decimal(const my_decimal *d)
1594
5086
{
1595
5087
  char buff[DECIMAL_MAX_STR_LENGTH+1];
1598
5090
  return store(str.ptr(), str.length(), str.charset());
1599
5091
}
1600
5092
 
1601
 
uint32_t Field_longstr::max_data_length() const
 
5093
uint32 Field_longstr::max_data_length() const
1602
5094
{
1603
5095
  return field_length + (field_length > 255 ? 2 : 1);
1604
5096
}
1605
5097
 
1606
5098
 
 
5099
double Field_string::val_real(void)
 
5100
{
 
5101
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5102
  int error;
 
5103
  char *end;
 
5104
  CHARSET_INFO *cs= charset();
 
5105
  double result;
 
5106
  
 
5107
  result=  my_strntod(cs,(char*) ptr,field_length,&end,&error);
 
5108
  if (!table->in_use->no_errors &&
 
5109
      (error || (field_length != (uint32)(end - (char*) ptr) && 
 
5110
                 !check_if_only_end_space(cs, end,
 
5111
                                          (char*) ptr + field_length))))
 
5112
  {
 
5113
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
5114
    String tmp(buf, sizeof(buf), cs);
 
5115
    tmp.copy((char*) ptr, field_length, cs);
 
5116
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
5117
                        ER_TRUNCATED_WRONG_VALUE, 
 
5118
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
5119
                        "DOUBLE", tmp.c_ptr());
 
5120
  }
 
5121
  return result;
 
5122
}
 
5123
 
 
5124
 
 
5125
longlong Field_string::val_int(void)
 
5126
{
 
5127
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5128
  int error;
 
5129
  char *end;
 
5130
  CHARSET_INFO *cs= charset();
 
5131
  longlong result;
 
5132
 
 
5133
  result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
 
5134
  if (!table->in_use->no_errors &&
 
5135
      (error || (field_length != (uint32)(end - (char*) ptr) && 
 
5136
                 !check_if_only_end_space(cs, end,
 
5137
                                          (char*) ptr + field_length))))
 
5138
  {
 
5139
    char buf[LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE];
 
5140
    String tmp(buf, sizeof(buf), cs);
 
5141
    tmp.copy((char*) ptr, field_length, cs);
 
5142
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
5143
                        ER_TRUNCATED_WRONG_VALUE, 
 
5144
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
5145
                        "INTEGER", tmp.c_ptr());
 
5146
  }
 
5147
  return result;
 
5148
}
 
5149
 
 
5150
 
 
5151
String *Field_string::val_str(String *val_buffer __attribute__((unused)),
 
5152
                              String *val_ptr)
 
5153
{
 
5154
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5155
  /* See the comment for Field_long::store(long long) */
 
5156
  DBUG_ASSERT(table->in_use == current_thd);
 
5157
  uint length;
 
5158
  if (table->in_use->variables.sql_mode &
 
5159
      MODE_PAD_CHAR_TO_FULL_LENGTH)
 
5160
    length= my_charpos(field_charset, ptr, ptr + field_length, field_length);
 
5161
  else
 
5162
    length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
 
5163
                                          field_length);
 
5164
  val_ptr->set((const char*) ptr, length, field_charset);
 
5165
  return val_ptr;
 
5166
}
 
5167
 
 
5168
 
 
5169
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
 
5170
{
 
5171
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5172
  int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
 
5173
                          charset(), decimal_value);
 
5174
  if (!table->in_use->no_errors && err)
 
5175
  {
 
5176
    char buf[DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE];
 
5177
    CHARSET_INFO *cs= charset();
 
5178
    String tmp(buf, sizeof(buf), cs);
 
5179
    tmp.copy((char*) ptr, field_length, cs);
 
5180
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
5181
                        ER_TRUNCATED_WRONG_VALUE, 
 
5182
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
5183
                        "DECIMAL", tmp.c_ptr());
 
5184
  }
 
5185
 
 
5186
  return decimal_value;
 
5187
}
 
5188
 
 
5189
 
 
5190
int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
5191
{
 
5192
  uint a_len, b_len;
 
5193
 
 
5194
  if (field_charset->mbmaxlen != 1)
 
5195
  {
 
5196
    uint char_len= field_length/field_charset->mbmaxlen;
 
5197
    a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
 
5198
    b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
 
5199
  }
 
5200
  else
 
5201
    a_len= b_len= field_length;
 
5202
  /*
 
5203
    We have to remove end space to be able to compare multi-byte-characters
 
5204
    like in latin_de 'ae' and 0xe4
 
5205
  */
 
5206
  return field_charset->coll->strnncollsp(field_charset,
 
5207
                                          a_ptr, a_len,
 
5208
                                          b_ptr, b_len,
 
5209
                                          0);
 
5210
}
 
5211
 
 
5212
 
 
5213
void Field_string::sort_string(uchar *to,uint length)
 
5214
{
 
5215
  IF_DBUG(uint tmp=) my_strnxfrm(field_charset,
 
5216
                                 to, length,
 
5217
                                 ptr, field_length);
 
5218
  DBUG_ASSERT(tmp == length);
 
5219
}
 
5220
 
 
5221
 
 
5222
void Field_string::sql_type(String &res) const
 
5223
{
 
5224
  THD *thd= table->in_use;
 
5225
  CHARSET_INFO *cs=res.charset();
 
5226
  ulong length;
 
5227
 
 
5228
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
 
5229
                             res.alloced_length(), "%s(%d)",
 
5230
                             ((type() == MYSQL_TYPE_VAR_STRING &&
 
5231
                               !thd->variables.new_mode) ?
 
5232
                              (has_charset() ? "varchar" : "varbinary") :
 
5233
                              (has_charset() ? "char" : "binary")),
 
5234
                             (int) field_length / charset()->mbmaxlen);
 
5235
  res.length(length);
 
5236
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
 
5237
      has_charset() && (charset()->state & MY_CS_BINSORT))
 
5238
    res.append(STRING_WITH_LEN(" binary"));
 
5239
}
 
5240
 
 
5241
 
 
5242
uchar *Field_string::pack(uchar *to, const uchar *from,
 
5243
                          uint max_length,
 
5244
                          bool low_byte_first __attribute__((unused)))
 
5245
{
 
5246
  uint length=      min(field_length,max_length);
 
5247
  uint local_char_length= max_length/field_charset->mbmaxlen;
 
5248
  if (length > local_char_length)
 
5249
    local_char_length= my_charpos(field_charset, from, from+length,
 
5250
                                  local_char_length);
 
5251
  set_if_smaller(length, local_char_length);
 
5252
  while (length && from[length-1] == field_charset->pad_char)
 
5253
    length--;
 
5254
 
 
5255
  // Length always stored little-endian
 
5256
  *to++= (uchar) length;
 
5257
  if (field_length > 255)
 
5258
    *to++= (uchar) (length >> 8);
 
5259
 
 
5260
  // Store the actual bytes of the string
 
5261
  memcpy(to, from, length);
 
5262
  return to+length;
 
5263
}
 
5264
 
 
5265
 
 
5266
/**
 
5267
   Unpack a string field from row data.
 
5268
 
 
5269
   This method is used to unpack a string field from a master whose size 
 
5270
   of the field is less than that of the slave. Note that there can be a
 
5271
   variety of field types represented with this class. Certain types like
 
5272
   ENUM or SET are processed differently. Hence, the upper byte of the 
 
5273
   @c param_data argument contains the result of field->real_type() from
 
5274
   the master.
 
5275
 
 
5276
   @param   to         Destination of the data
 
5277
   @param   from       Source of the data
 
5278
   @param   param_data Real type (upper) and length (lower) values
 
5279
 
 
5280
   @return  New pointer into memory based on from + length of the data
 
5281
*/
 
5282
const uchar *
 
5283
Field_string::unpack(uchar *to,
 
5284
                     const uchar *from,
 
5285
                     uint param_data,
 
5286
                     bool low_byte_first __attribute__((unused)))
 
5287
{
 
5288
  uint from_length=
 
5289
    param_data ? min(param_data & 0x00ff, field_length) : field_length;
 
5290
  uint length;
 
5291
 
 
5292
  if (from_length > 255)
 
5293
  {
 
5294
    length= uint2korr(from);
 
5295
    from+= 2;
 
5296
  }
 
5297
  else
 
5298
    length= (uint) *from++;
 
5299
 
 
5300
  memcpy(to, from, length);
 
5301
  // Pad the string with the pad character of the fields charset
 
5302
  bfill(to + length, field_length - length, field_charset->pad_char);
 
5303
  return from+length;
 
5304
}
 
5305
 
 
5306
 
 
5307
/**
 
5308
   Save the field metadata for string fields.
 
5309
 
 
5310
   Saves the real type in the first byte and the field length in the 
 
5311
   second byte of the field metadata array at index of *metadata_ptr and
 
5312
   *(metadata_ptr + 1).
 
5313
 
 
5314
   @param   metadata_ptr   First byte of field metadata
 
5315
 
 
5316
   @returns number of bytes written to metadata_ptr
 
5317
*/
 
5318
int Field_string::do_save_field_metadata(uchar *metadata_ptr)
 
5319
{
 
5320
  *metadata_ptr= real_type();
 
5321
  *(metadata_ptr + 1)= field_length;
 
5322
  return 2;
 
5323
}
 
5324
 
 
5325
 
 
5326
/*
 
5327
  Compare two packed keys
 
5328
 
 
5329
  SYNOPSIS
 
5330
    pack_cmp()
 
5331
     a                  New key
 
5332
     b                  Original key
 
5333
     length             Key length
 
5334
     insert_or_update   1 if this is an insert or update
 
5335
 
 
5336
  RETURN
 
5337
    < 0   a < b
 
5338
    0     a = b
 
5339
    > 0   a > b
 
5340
*/
 
5341
 
 
5342
int Field_string::pack_cmp(const uchar *a, const uchar *b, uint length,
 
5343
                           my_bool insert_or_update)
 
5344
{
 
5345
  uint a_length, b_length;
 
5346
  if (length > 255)
 
5347
  {
 
5348
    a_length= uint2korr(a);
 
5349
    b_length= uint2korr(b);
 
5350
    a+= 2;
 
5351
    b+= 2;
 
5352
  }
 
5353
  else
 
5354
  {
 
5355
    a_length= (uint) *a++;
 
5356
    b_length= (uint) *b++;
 
5357
  }
 
5358
  return field_charset->coll->strnncollsp(field_charset,
 
5359
                                          a, a_length,
 
5360
                                          b, b_length,
 
5361
                                          insert_or_update);
 
5362
}
 
5363
 
 
5364
 
 
5365
/**
 
5366
  Compare a packed key against row.
 
5367
 
 
5368
  @param key                    Original key
 
5369
  @param length         Key length. (May be less than field length)
 
5370
  @param insert_or_update       1 if this is an insert or update
 
5371
 
 
5372
  @return
 
5373
    < 0   row < key
 
5374
  @return
 
5375
    0     row = key
 
5376
  @return
 
5377
    > 0   row > key
 
5378
*/
 
5379
 
 
5380
int Field_string::pack_cmp(const uchar *key, uint length,
 
5381
                           my_bool insert_or_update)
 
5382
{
 
5383
  uint row_length, local_key_length;
 
5384
  uchar *end;
 
5385
  if (length > 255)
 
5386
  {
 
5387
    local_key_length= uint2korr(key);
 
5388
    key+= 2;
 
5389
  }
 
5390
  else
 
5391
    local_key_length= (uint) *key++;
 
5392
  
 
5393
  /* Only use 'length' of key, not field_length */
 
5394
  end= ptr + length;
 
5395
  while (end > ptr && end[-1] == ' ')
 
5396
    end--;
 
5397
  row_length= (uint) (end - ptr);
 
5398
 
 
5399
  return field_charset->coll->strnncollsp(field_charset,
 
5400
                                          ptr, row_length,
 
5401
                                          key, local_key_length,
 
5402
                                          insert_or_update);
 
5403
}
 
5404
 
 
5405
 
 
5406
uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
 
5407
{
 
5408
  if (length > 255)
 
5409
    return uint2korr(data_ptr)+2;
 
5410
  return (uint) *data_ptr + 1;
 
5411
}
 
5412
 
 
5413
 
 
5414
uint Field_string::max_packed_col_length(uint max_length)
 
5415
{
 
5416
  return (max_length > 255 ? 2 : 1)+max_length;
 
5417
}
 
5418
 
 
5419
 
 
5420
uint Field_string::get_key_image(uchar *buff,
 
5421
                                 uint length,
 
5422
                                 imagetype type_arg __attribute__((__unused__)))
 
5423
{
 
5424
  uint bytes = my_charpos(field_charset, (char*) ptr,
 
5425
                          (char*) ptr + field_length,
 
5426
                          length / field_charset->mbmaxlen);
 
5427
  memcpy(buff, ptr, bytes);
 
5428
  if (bytes < length)
 
5429
    field_charset->cset->fill(field_charset, (char*) buff + bytes,
 
5430
                              length - bytes, field_charset->pad_char);
 
5431
  return bytes;
 
5432
}
 
5433
 
 
5434
 
 
5435
Field *Field_string::new_field(MEM_ROOT *root, struct st_table *new_table,
 
5436
                               bool keep_type)
 
5437
{
 
5438
  Field *field;
 
5439
  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
 
5440
    field= Field::new_field(root, new_table, keep_type);
 
5441
  else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
 
5442
                                       new_table->s, charset())))
 
5443
  {
 
5444
    /*
 
5445
      Old VARCHAR field which should be modified to a VARCHAR on copy
 
5446
      This is done to ensure that ALTER TABLE will convert old VARCHAR fields
 
5447
      to now VARCHAR fields.
 
5448
    */
 
5449
    field->init(new_table);
 
5450
    /*
 
5451
      Normally orig_table is different from table only if field was created
 
5452
      via ::new_field.  Here we alter the type of field, so ::new_field is
 
5453
      not applicable. But we still need to preserve the original field
 
5454
      metadata for the client-server protocol.
 
5455
    */
 
5456
    field->orig_table= orig_table;
 
5457
  }
 
5458
  return field;
 
5459
}
 
5460
 
 
5461
 
 
5462
/****************************************************************************
 
5463
  VARCHAR type
 
5464
  Data in field->ptr is stored as:
 
5465
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
 
5466
    data
 
5467
 
 
5468
  NOTE:
 
5469
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
 
5470
  stored with a 2 byte prefix. (Just like blob keys).
 
5471
 
 
5472
  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
 
5473
  The exception is if there is a prefix key field that is part of a long
 
5474
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
 
5475
  is 2.
 
5476
****************************************************************************/
 
5477
 
 
5478
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
 
5479
 
 
5480
/**
 
5481
   Save the field metadata for varstring fields.
 
5482
 
 
5483
   Saves the field length in the first byte. Note: may consume
 
5484
   2 bytes. Caller must ensure second byte is contiguous with
 
5485
   first byte (e.g. array index 0,1).
 
5486
 
 
5487
   @param   metadata_ptr   First byte of field metadata
 
5488
 
 
5489
   @returns number of bytes written to metadata_ptr
 
5490
*/
 
5491
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
 
5492
{
 
5493
  char *ptr= (char *)metadata_ptr;
 
5494
  DBUG_ASSERT(field_length <= 65535);
 
5495
  int2store(ptr, field_length);
 
5496
  return 2;
 
5497
}
 
5498
 
 
5499
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
 
5500
{
 
5501
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5502
  uint copy_length;
 
5503
  const char *well_formed_error_pos;
 
5504
  const char *cannot_convert_error_pos;
 
5505
  const char *from_end_pos;
 
5506
 
 
5507
  copy_length= well_formed_copy_nchars(field_charset,
 
5508
                                       (char*) ptr + length_bytes,
 
5509
                                       field_length,
 
5510
                                       cs, from, length,
 
5511
                                       field_length / field_charset->mbmaxlen,
 
5512
                                       &well_formed_error_pos,
 
5513
                                       &cannot_convert_error_pos,
 
5514
                                       &from_end_pos);
 
5515
 
 
5516
  if (length_bytes == 1)
 
5517
    *ptr= (uchar) copy_length;
 
5518
  else
 
5519
    int2store(ptr, copy_length);
 
5520
 
 
5521
  if (check_string_copy_error(this, well_formed_error_pos,
 
5522
                              cannot_convert_error_pos, from + length, cs))
 
5523
    return 2;
 
5524
 
 
5525
  return report_if_important_data(from_end_pos, from + length);
 
5526
}
 
5527
 
 
5528
 
 
5529
int Field_varstring::store(longlong nr, bool unsigned_val)
 
5530
{
 
5531
  char buff[64];
 
5532
  uint  length;
 
5533
  length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
 
5534
                                                          buff,
 
5535
                                                          sizeof(buff),
 
5536
                                                          (unsigned_val ? 10:
 
5537
                                                           -10),
 
5538
                                                           nr);
 
5539
  return Field_varstring::store(buff, length, field_charset);
 
5540
}
 
5541
 
 
5542
 
 
5543
double Field_varstring::val_real(void)
 
5544
{
 
5545
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5546
  int not_used;
 
5547
  char *end_not_used;
 
5548
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5549
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
 
5550
                    &end_not_used, &not_used);
 
5551
}
 
5552
 
 
5553
 
 
5554
longlong Field_varstring::val_int(void)
 
5555
{
 
5556
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5557
  int not_used;
 
5558
  char *end_not_used;
 
5559
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5560
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
 
5561
                     &end_not_used, &not_used);
 
5562
}
 
5563
 
 
5564
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
 
5565
                                 String *val_ptr)
 
5566
{
 
5567
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5568
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5569
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
 
5570
  return val_ptr;
 
5571
}
 
5572
 
 
5573
 
 
5574
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
 
5575
{
 
5576
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5577
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5578
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
 
5579
                 charset(), decimal_value);
 
5580
  return decimal_value;
 
5581
}
 
5582
 
 
5583
 
 
5584
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
 
5585
                             uint max_len)
 
5586
{
 
5587
  uint a_length, b_length;
 
5588
  int diff;
 
5589
 
 
5590
  if (length_bytes == 1)
 
5591
  {
 
5592
    a_length= (uint) *a_ptr;
 
5593
    b_length= (uint) *b_ptr;
 
5594
  }
 
5595
  else
 
5596
  {
 
5597
    a_length= uint2korr(a_ptr);
 
5598
    b_length= uint2korr(b_ptr);
 
5599
  }
 
5600
  set_if_smaller(a_length, max_len);
 
5601
  set_if_smaller(b_length, max_len);
 
5602
  diff= field_charset->coll->strnncollsp(field_charset,
 
5603
                                         a_ptr+
 
5604
                                         length_bytes,
 
5605
                                         a_length,
 
5606
                                         b_ptr+
 
5607
                                         length_bytes,
 
5608
                                         b_length,0);
 
5609
  return diff;
 
5610
}
 
5611
 
 
5612
 
 
5613
/**
 
5614
  @note
 
5615
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
 
5616
*/
 
5617
 
 
5618
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
 
5619
{
 
5620
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5621
  uint local_char_length= max_key_length / field_charset->mbmaxlen;
 
5622
 
 
5623
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
 
5624
                          ptr + length_bytes + length, local_char_length);
 
5625
  set_if_smaller(length, local_char_length);
 
5626
  return field_charset->coll->strnncollsp(field_charset, 
 
5627
                                          ptr + length_bytes,
 
5628
                                          length,
 
5629
                                          key_ptr+
 
5630
                                          HA_KEY_BLOB_LENGTH,
 
5631
                                          uint2korr(key_ptr), 0);
 
5632
}
 
5633
 
 
5634
 
 
5635
/**
 
5636
  Compare to key segments (always 2 byte length prefix).
 
5637
 
 
5638
  @note
 
5639
    This is used only to compare key segments created for index_read().
 
5640
    (keys are created and compared in key.cc)
 
5641
*/
 
5642
 
 
5643
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
 
5644
{
 
5645
  return field_charset->coll->strnncollsp(field_charset,
 
5646
                                          a + HA_KEY_BLOB_LENGTH,
 
5647
                                          uint2korr(a),
 
5648
                                          b + HA_KEY_BLOB_LENGTH,
 
5649
                                          uint2korr(b),
 
5650
                                          0);
 
5651
}
 
5652
 
 
5653
 
 
5654
void Field_varstring::sort_string(uchar *to,uint length)
 
5655
{
 
5656
  uint tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5657
 
 
5658
  if (field_charset == &my_charset_bin)
 
5659
  {
 
5660
    /* Store length last in high-byte order to sort longer strings first */
 
5661
    if (length_bytes == 1)
 
5662
      to[length-1]= tot_length;
 
5663
    else
 
5664
      mi_int2store(to+length-2, tot_length);
 
5665
    length-= length_bytes;
 
5666
  }
 
5667
 
 
5668
  tot_length= my_strnxfrm(field_charset,
 
5669
                          to, length, ptr + length_bytes,
 
5670
                          tot_length);
 
5671
  DBUG_ASSERT(tot_length == length);
 
5672
}
 
5673
 
 
5674
 
 
5675
enum ha_base_keytype Field_varstring::key_type() const
 
5676
{
 
5677
  enum ha_base_keytype res;
 
5678
 
 
5679
  if (binary())
 
5680
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
 
5681
  else
 
5682
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
 
5683
  return res;
 
5684
}
 
5685
 
 
5686
 
 
5687
void Field_varstring::sql_type(String &res) const
 
5688
{
 
5689
  THD *thd= table->in_use;
 
5690
  CHARSET_INFO *cs=res.charset();
 
5691
  ulong length;
 
5692
 
 
5693
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
 
5694
                             res.alloced_length(), "%s(%d)",
 
5695
                              (has_charset() ? "varchar" : "varbinary"),
 
5696
                             (int) field_length / charset()->mbmaxlen);
 
5697
  res.length(length);
 
5698
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
 
5699
      has_charset() && (charset()->state & MY_CS_BINSORT))
 
5700
    res.append(STRING_WITH_LEN(" binary"));
 
5701
}
 
5702
 
 
5703
 
 
5704
uint32 Field_varstring::data_length()
 
5705
{
 
5706
  return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
 
5707
}
 
5708
 
 
5709
uint32 Field_varstring::used_length()
 
5710
{
 
5711
  return length_bytes == 1 ? 1 + (uint32) (uchar) *ptr : 2 + uint2korr(ptr);
 
5712
}
 
5713
 
 
5714
/*
 
5715
  Functions to create a packed row.
 
5716
  Here the number of length bytes are depending on the given max_length
 
5717
*/
 
5718
 
 
5719
uchar *Field_varstring::pack(uchar *to, const uchar *from,
 
5720
                             uint max_length,
 
5721
                             bool low_byte_first __attribute__((unused)))
 
5722
{
 
5723
  uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
 
5724
  set_if_smaller(max_length, field_length);
 
5725
  if (length > max_length)
 
5726
    length=max_length;
 
5727
 
 
5728
  /* Length always stored little-endian */
 
5729
  *to++= length & 0xFF;
 
5730
  if (max_length > 255)
 
5731
    *to++= (length >> 8) & 0xFF;
 
5732
 
 
5733
  /* Store bytes of string */
 
5734
  if (length > 0)
 
5735
    memcpy(to, from+length_bytes, length);
 
5736
  return to+length;
 
5737
}
 
5738
 
 
5739
 
 
5740
uchar *
 
5741
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
 
5742
                          bool low_byte_first __attribute__((unused)))
 
5743
{
 
5744
  uint length=  length_bytes == 1 ? (uint) *key : uint2korr(key);
 
5745
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
5746
                     max_length/field_charset->mbmaxlen : max_length);
 
5747
  key+= length_bytes;
 
5748
  if (length > local_char_length)
 
5749
  {
 
5750
    local_char_length= my_charpos(field_charset, key, key+length,
 
5751
                                  local_char_length);
 
5752
    set_if_smaller(length, local_char_length);
 
5753
  }
 
5754
  *to++= (char) (length & 255);
 
5755
  if (max_length > 255)
 
5756
    *to++= (char) (length >> 8);
 
5757
  if (length)
 
5758
    memcpy(to, key, length);
 
5759
  return to+length;
 
5760
}
 
5761
 
 
5762
 
 
5763
/**
 
5764
  Unpack a key into a record buffer.
 
5765
 
 
5766
  A VARCHAR key has a maximum size of 64K-1.
 
5767
  In its packed form, the length field is one or two bytes long,
 
5768
  depending on 'max_length'.
 
5769
 
 
5770
  @param to                          Pointer into the record buffer.
 
5771
  @param key                         Pointer to the packed key.
 
5772
  @param max_length                  Key length limit from key description.
 
5773
 
 
5774
  @return
 
5775
    Pointer to end of 'key' (To the next key part if multi-segment key)
 
5776
*/
 
5777
 
 
5778
const uchar *
 
5779
Field_varstring::unpack_key(uchar *to __attribute__((__unused__)),
 
5780
                            const uchar *key, uint max_length,
 
5781
                            bool low_byte_first __attribute__((unused)))
 
5782
{
 
5783
  /* get length of the blob key */
 
5784
  uint32 length= *key++;
 
5785
  if (max_length > 255)
 
5786
    length+= (*key++) << 8;
 
5787
 
 
5788
  /* put the length into the record buffer */
 
5789
  if (length_bytes == 1)
 
5790
    *ptr= (uchar) length;
 
5791
  else
 
5792
    int2store(ptr, length);
 
5793
  memcpy(ptr + length_bytes, key, length);
 
5794
  return key + length;
 
5795
}
 
5796
 
 
5797
/**
 
5798
  Create a packed key that will be used for storage in the index tree.
 
5799
 
 
5800
  @param to             Store packed key segment here
 
5801
  @param from           Key segment (as given to index_read())
 
5802
  @param max_length     Max length of key
 
5803
 
 
5804
  @return
 
5805
    end of key storage
 
5806
*/
 
5807
 
 
5808
uchar *
 
5809
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
 
5810
                                         bool low_byte_first __attribute__((unused)))
 
5811
{
 
5812
  /* Key length is always stored as 2 bytes */
 
5813
  uint length= uint2korr(from);
 
5814
  if (length > max_length)
 
5815
    length= max_length;
 
5816
  *to++= (char) (length & 255);
 
5817
  if (max_length > 255)
 
5818
    *to++= (char) (length >> 8);
 
5819
  if (length)
 
5820
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
 
5821
  return to+length;
 
5822
}
 
5823
 
 
5824
 
 
5825
/**
 
5826
   Unpack a varstring field from row data.
 
5827
 
 
5828
   This method is used to unpack a varstring field from a master
 
5829
   whose size of the field is less than that of the slave.
 
5830
 
 
5831
   @note
 
5832
   The string length is always packed little-endian.
 
5833
  
 
5834
   @param   to         Destination of the data
 
5835
   @param   from       Source of the data
 
5836
   @param   param_data Length bytes from the master's field data
 
5837
 
 
5838
   @return  New pointer into memory based on from + length of the data
 
5839
*/
 
5840
const uchar *
 
5841
Field_varstring::unpack(uchar *to, const uchar *from,
 
5842
                        uint param_data,
 
5843
                        bool low_byte_first __attribute__((unused)))
 
5844
{
 
5845
  uint length;
 
5846
  uint l_bytes= (param_data && (param_data < field_length)) ? 
 
5847
                (param_data <= 255) ? 1 : 2 : length_bytes;
 
5848
  if (l_bytes == 1)
 
5849
  {
 
5850
    to[0]= *from++;
 
5851
    length= to[0];
 
5852
    if (length_bytes == 2)
 
5853
      to[1]= 0;
 
5854
  }
 
5855
  else /* l_bytes == 2 */
 
5856
  {
 
5857
    length= uint2korr(from);
 
5858
    to[0]= *from++;
 
5859
    to[1]= *from++;
 
5860
  }
 
5861
  if (length)
 
5862
    memcpy(to+ length_bytes, from, length);
 
5863
  return from+length;
 
5864
}
 
5865
 
 
5866
 
 
5867
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
 
5868
                              uint key_length_arg,
 
5869
                              my_bool insert_or_update)
 
5870
{
 
5871
  uint a_length, b_length;
 
5872
  if (key_length_arg > 255)
 
5873
  {
 
5874
    a_length=uint2korr(a); a+= 2;
 
5875
    b_length=uint2korr(b); b+= 2;
 
5876
  }
 
5877
  else
 
5878
  {
 
5879
    a_length= (uint) *a++;
 
5880
    b_length= (uint) *b++;
 
5881
  }
 
5882
  return field_charset->coll->strnncollsp(field_charset,
 
5883
                                          a, a_length,
 
5884
                                          b, b_length,
 
5885
                                          insert_or_update);
 
5886
}
 
5887
 
 
5888
 
 
5889
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
 
5890
                              my_bool insert_or_update)
 
5891
{
 
5892
  uchar *a= ptr+ length_bytes;
 
5893
  uint a_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5894
  uint b_length;
 
5895
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
5896
                           key_length_arg / field_charset->mbmaxlen :
 
5897
                           key_length_arg);
 
5898
 
 
5899
  if (key_length_arg > 255)
 
5900
  {
 
5901
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
 
5902
  }
 
5903
  else
 
5904
    b_length= (uint) *b++;
 
5905
 
 
5906
  if (a_length > local_char_length)
 
5907
  {
 
5908
    local_char_length= my_charpos(field_charset, a, a+a_length,
 
5909
                                  local_char_length);
 
5910
    set_if_smaller(a_length, local_char_length);
 
5911
  }
 
5912
 
 
5913
  return field_charset->coll->strnncollsp(field_charset,
 
5914
                                          a, a_length,
 
5915
                                          b, b_length,
 
5916
                                          insert_or_update);
 
5917
}
 
5918
 
 
5919
 
 
5920
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
 
5921
{
 
5922
  if (length > 255)
 
5923
    return uint2korr(data_ptr)+2;
 
5924
  return (uint) *data_ptr + 1;
 
5925
}
 
5926
 
 
5927
 
 
5928
uint Field_varstring::max_packed_col_length(uint max_length)
 
5929
{
 
5930
  return (max_length > 255 ? 2 : 1)+max_length;
 
5931
}
 
5932
 
 
5933
uint Field_varstring::get_key_image(uchar *buff,
 
5934
                                    uint length,
 
5935
                                    imagetype type __attribute__((__unused__)))
 
5936
{
 
5937
  uint f_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5938
  uint local_char_length= length / field_charset->mbmaxlen;
 
5939
  uchar *pos= ptr+length_bytes;
 
5940
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
 
5941
                                local_char_length);
 
5942
  set_if_smaller(f_length, local_char_length);
 
5943
  /* Key is always stored with 2 bytes */
 
5944
  int2store(buff,f_length);
 
5945
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
 
5946
  if (f_length < length)
 
5947
  {
 
5948
    /*
 
5949
      Must clear this as we do a memcmp in opt_range.cc to detect
 
5950
      identical keys
 
5951
    */
 
5952
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
 
5953
  }
 
5954
  return HA_KEY_BLOB_LENGTH+f_length;
 
5955
}
 
5956
 
 
5957
 
 
5958
void Field_varstring::set_key_image(const uchar *buff,uint length)
 
5959
{
 
5960
  length= uint2korr(buff);                      // Real length is here
 
5961
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
5962
                                field_charset);
 
5963
}
 
5964
 
 
5965
 
 
5966
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
 
5967
                                uint32 max_length)
 
5968
{
 
5969
  uint32 a_length,b_length;
 
5970
 
 
5971
  if (length_bytes == 1)
 
5972
  {
 
5973
    a_length= (uint) *a_ptr;
 
5974
    b_length= (uint) *b_ptr;
 
5975
  }
 
5976
  else
 
5977
  {
 
5978
    a_length= uint2korr(a_ptr);
 
5979
    b_length= uint2korr(b_ptr);
 
5980
  }
 
5981
  set_if_smaller(a_length, max_length);
 
5982
  set_if_smaller(b_length, max_length);
 
5983
  if (a_length != b_length)
 
5984
    return 1;
 
5985
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
 
5986
}
 
5987
 
 
5988
 
 
5989
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
 
5990
                                  bool keep_type)
 
5991
{
 
5992
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
 
5993
                                                            keep_type);
 
5994
  if (res)
 
5995
    res->length_bytes= length_bytes;
 
5996
  return res;
 
5997
}
 
5998
 
 
5999
 
 
6000
Field *Field_varstring::new_key_field(MEM_ROOT *root,
 
6001
                                      struct st_table *new_table,
 
6002
                                      uchar *new_ptr, uchar *new_null_ptr,
 
6003
                                      uint new_null_bit)
 
6004
{
 
6005
  Field_varstring *res;
 
6006
  if ((res= (Field_varstring*) Field::new_key_field(root,
 
6007
                                                    new_table,
 
6008
                                                    new_ptr,
 
6009
                                                    new_null_ptr,
 
6010
                                                    new_null_bit)))
 
6011
  {
 
6012
    /* Keys length prefixes are always packed with 2 bytes */
 
6013
    res->length_bytes= 2;
 
6014
  }
 
6015
  return res;
 
6016
}
 
6017
 
 
6018
 
 
6019
uint Field_varstring::is_equal(Create_field *new_field)
 
6020
{
 
6021
  if (new_field->sql_type == real_type() &&
 
6022
      new_field->charset == field_charset)
 
6023
  {
 
6024
    if (new_field->length == max_display_length())
 
6025
      return IS_EQUAL_YES;
 
6026
    if (new_field->length > max_display_length() &&
 
6027
        ((new_field->length <= 255 && max_display_length() <= 255) ||
 
6028
         (new_field->length > 255 && max_display_length() > 255)))
 
6029
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
 
6030
  }
 
6031
  return IS_EQUAL_NO;
 
6032
}
 
6033
 
 
6034
 
 
6035
void Field_varstring::hash(ulong *nr, ulong *nr2)
 
6036
{
 
6037
  if (is_null())
 
6038
  {
 
6039
    *nr^= (*nr << 1) | 1;
 
6040
  }
 
6041
  else
 
6042
  {
 
6043
    uint len=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
6044
    CHARSET_INFO *cs= charset();
 
6045
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
 
6046
  }
 
6047
}
 
6048
 
 
6049
 
 
6050
/****************************************************************************
 
6051
** blob type
 
6052
** A blob is saved as a length and a pointer. The length is stored in the
 
6053
** packlength slot and may be from 1-4.
 
6054
****************************************************************************/
 
6055
 
 
6056
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
6057
                       enum utype unireg_check_arg, const char *field_name_arg,
 
6058
                       TABLE_SHARE *share, uint blob_pack_length,
 
6059
                       CHARSET_INFO *cs)
 
6060
  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
 
6061
                 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
 
6062
                 cs),
 
6063
   packlength(blob_pack_length)
 
6064
{
 
6065
  flags|= BLOB_FLAG;
 
6066
  share->blob_fields++;
 
6067
  /* TODO: why do not fill table->s->blob_field array here? */
 
6068
}
 
6069
 
 
6070
 
 
6071
void Field_blob::store_length(uchar *i_ptr,
 
6072
                              uint i_packlength,
 
6073
                              uint32 i_number,
 
6074
                              bool low_byte_first __attribute__((__unused__)))
 
6075
{
 
6076
  switch (i_packlength) {
 
6077
  case 1:
 
6078
    i_ptr[0]= (uchar) i_number;
 
6079
    break;
 
6080
  case 2:
 
6081
#ifdef WORDS_BIGENDIAN
 
6082
    if (low_byte_first)
 
6083
    {
 
6084
      int2store(i_ptr,(unsigned short) i_number);
 
6085
    }
 
6086
    else
 
6087
#endif
 
6088
      shortstore(i_ptr,(unsigned short) i_number);
 
6089
    break;
 
6090
  case 3:
 
6091
    int3store(i_ptr,i_number);
 
6092
    break;
 
6093
  case 4:
 
6094
#ifdef WORDS_BIGENDIAN
 
6095
    if (low_byte_first)
 
6096
    {
 
6097
      int4store(i_ptr,i_number);
 
6098
    }
 
6099
    else
 
6100
#endif
 
6101
      longstore(i_ptr,i_number);
 
6102
  }
 
6103
}
 
6104
 
 
6105
 
 
6106
uint32 Field_blob::get_length(const uchar *pos,
 
6107
                              uint packlength_arg,
 
6108
                              bool low_byte_first __attribute__((__unused__)))
 
6109
{
 
6110
  switch (packlength_arg) {
 
6111
  case 1:
 
6112
    return (uint32) pos[0];
 
6113
  case 2:
 
6114
    {
 
6115
      uint16 tmp;
 
6116
#ifdef WORDS_BIGENDIAN
 
6117
      if (low_byte_first)
 
6118
        tmp=sint2korr(pos);
 
6119
      else
 
6120
#endif
 
6121
        shortget(tmp,pos);
 
6122
      return (uint32) tmp;
 
6123
    }
 
6124
  case 3:
 
6125
    return (uint32) uint3korr(pos);
 
6126
  case 4:
 
6127
    {
 
6128
      uint32 tmp;
 
6129
#ifdef WORDS_BIGENDIAN
 
6130
      if (low_byte_first)
 
6131
        tmp=uint4korr(pos);
 
6132
      else
 
6133
#endif
 
6134
        longget(tmp,pos);
 
6135
      return (uint32) tmp;
 
6136
    }
 
6137
  }
 
6138
  return 0;                                     // Impossible
 
6139
}
 
6140
 
 
6141
 
 
6142
/**
 
6143
  Put a blob length field into a record buffer.
 
6144
 
 
6145
  Depending on the maximum length of a blob, its length field is
 
6146
  put into 1 to 4 bytes. This is a property of the blob object,
 
6147
  described by 'packlength'.
 
6148
 
 
6149
  @param pos                 Pointer into the record buffer.
 
6150
  @param length              The length value to put.
 
6151
*/
 
6152
 
 
6153
void Field_blob::put_length(uchar *pos, uint32 length)
 
6154
{
 
6155
  switch (packlength) {
 
6156
  case 1:
 
6157
    *pos= (char) length;
 
6158
    break;
 
6159
  case 2:
 
6160
    int2store(pos, length);
 
6161
    break;
 
6162
  case 3:
 
6163
    int3store(pos, length);
 
6164
    break;
 
6165
  case 4:
 
6166
    int4store(pos, length);
 
6167
    break;
 
6168
  }
 
6169
}
 
6170
 
 
6171
 
 
6172
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
 
6173
{
 
6174
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
6175
  uint copy_length, new_length;
 
6176
  const char *well_formed_error_pos;
 
6177
  const char *cannot_convert_error_pos;
 
6178
  const char *from_end_pos, *tmp;
 
6179
  char buff[STRING_BUFFER_USUAL_SIZE];
 
6180
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
6181
 
 
6182
  if (!length)
 
6183
  {
 
6184
    bzero(ptr,Field_blob::pack_length());
 
6185
    return 0;
 
6186
  }
 
6187
 
 
6188
  if (from == value.ptr())
 
6189
  {
 
6190
    uint32 dummy_offset;
 
6191
    if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
 
6192
    {
 
6193
      Field_blob::store_length(length);
 
6194
      bmove(ptr+packlength,(char*) &from,sizeof(char*));
 
6195
      return 0;
 
6196
    }
 
6197
    if (tmpstr.copy(from, length, cs))
 
6198
      goto oom_error;
 
6199
    from= tmpstr.ptr();
 
6200
  }
 
6201
 
 
6202
  new_length= min(max_data_length(), field_charset->mbmaxlen * length);
 
6203
  if (value.alloc(new_length))
 
6204
    goto oom_error;
 
6205
 
 
6206
 
 
6207
  if (f_is_hex_escape(flags))
 
6208
  {
 
6209
    copy_length= my_copy_with_hex_escaping(field_charset,
 
6210
                                           (char*) value.ptr(), new_length,
 
6211
                                            from, length);
 
6212
    Field_blob::store_length(copy_length);
 
6213
    tmp= value.ptr();
 
6214
    bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
 
6215
    return 0;
 
6216
  }
 
6217
  /*
 
6218
    "length" is OK as "nchars" argument to well_formed_copy_nchars as this
 
6219
    is never used to limit the length of the data. The cut of long data
 
6220
    is done with the new_length value.
 
6221
  */
 
6222
  copy_length= well_formed_copy_nchars(field_charset,
 
6223
                                       (char*) value.ptr(), new_length,
 
6224
                                       cs, from, length,
 
6225
                                       length,
 
6226
                                       &well_formed_error_pos,
 
6227
                                       &cannot_convert_error_pos,
 
6228
                                       &from_end_pos);
 
6229
 
 
6230
  Field_blob::store_length(copy_length);
 
6231
  tmp= value.ptr();
 
6232
  bmove(ptr+packlength,(uchar*) &tmp,sizeof(char*));
 
6233
 
 
6234
  if (check_string_copy_error(this, well_formed_error_pos,
 
6235
                              cannot_convert_error_pos, from + length, cs))
 
6236
    return 2;
 
6237
 
 
6238
  return report_if_important_data(from_end_pos, from + length);
 
6239
 
 
6240
oom_error:
 
6241
  /* Fatal OOM error */
 
6242
  bzero(ptr,Field_blob::pack_length());
 
6243
  return -1; 
 
6244
}
 
6245
 
 
6246
 
 
6247
int Field_blob::store(double nr)
 
6248
{
 
6249
  CHARSET_INFO *cs=charset();
 
6250
  value.set_real(nr, NOT_FIXED_DEC, cs);
 
6251
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
 
6252
}
 
6253
 
 
6254
 
 
6255
int Field_blob::store(longlong nr, bool unsigned_val)
 
6256
{
 
6257
  CHARSET_INFO *cs=charset();
 
6258
  value.set_int(nr, unsigned_val, cs);
 
6259
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
 
6260
}
 
6261
 
 
6262
 
 
6263
double Field_blob::val_real(void)
 
6264
{
 
6265
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6266
  int not_used;
 
6267
  char *end_not_used, *blob;
 
6268
  uint32 length;
 
6269
  CHARSET_INFO *cs;
 
6270
 
 
6271
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6272
  if (!blob)
 
6273
    return 0.0;
 
6274
  length= get_length(ptr);
 
6275
  cs= charset();
 
6276
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
 
6277
}
 
6278
 
 
6279
 
 
6280
longlong Field_blob::val_int(void)
 
6281
{
 
6282
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6283
  int not_used;
 
6284
  char *blob;
 
6285
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6286
  if (!blob)
 
6287
    return 0;
 
6288
  uint32 length=get_length(ptr);
 
6289
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
 
6290
}
 
6291
 
 
6292
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
 
6293
                            String *val_ptr)
 
6294
{
 
6295
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6296
  char *blob;
 
6297
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6298
  if (!blob)
 
6299
    val_ptr->set("",0,charset());       // A bit safer than ->length(0)
 
6300
  else
 
6301
    val_ptr->set((const char*) blob,get_length(ptr),charset());
 
6302
  return val_ptr;
 
6303
}
 
6304
 
 
6305
 
 
6306
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
 
6307
{
 
6308
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6309
  const char *blob;
 
6310
  size_t length;
 
6311
  memcpy_fixed(&blob, ptr+packlength, sizeof(const uchar*));
 
6312
  if (!blob)
 
6313
  {
 
6314
    blob= "";
 
6315
    length= 0;
 
6316
  }
 
6317
  else
 
6318
    length= get_length(ptr);
 
6319
 
 
6320
  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
 
6321
                 decimal_value);
 
6322
  return decimal_value;
 
6323
}
 
6324
 
 
6325
 
 
6326
int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
 
6327
                    uint32 b_length)
 
6328
{
 
6329
  return field_charset->coll->strnncollsp(field_charset, 
 
6330
                                          a, a_length, b, b_length,
 
6331
                                          0);
 
6332
}
 
6333
 
 
6334
 
 
6335
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
 
6336
                        uint max_length)
 
6337
{
 
6338
  uchar *blob1,*blob2;
 
6339
  memcpy_fixed(&blob1,a_ptr+packlength,sizeof(char*));
 
6340
  memcpy_fixed(&blob2,b_ptr+packlength,sizeof(char*));
 
6341
  uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
 
6342
  set_if_smaller(a_len, max_length);
 
6343
  set_if_smaller(b_len, max_length);
 
6344
  return Field_blob::cmp(blob1,a_len,blob2,b_len);
 
6345
}
 
6346
 
 
6347
 
 
6348
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
 
6349
                           uint32 max_length)
 
6350
{
 
6351
  char *a,*b;
 
6352
  uint diff;
 
6353
  uint32 a_length,b_length;
 
6354
  memcpy_fixed(&a,a_ptr+packlength,sizeof(char*));
 
6355
  memcpy_fixed(&b,b_ptr+packlength,sizeof(char*));
 
6356
  a_length=get_length(a_ptr);
 
6357
  if (a_length > max_length)
 
6358
    a_length=max_length;
 
6359
  b_length=get_length(b_ptr);
 
6360
  if (b_length > max_length)
 
6361
    b_length=max_length;
 
6362
  diff=memcmp(a,b,min(a_length,b_length));
 
6363
  return diff ? diff : (int) (a_length - b_length);
 
6364
}
 
6365
 
 
6366
 
 
6367
/* The following is used only when comparing a key */
 
6368
 
 
6369
uint Field_blob::get_key_image(uchar *buff,
 
6370
                               uint length,
 
6371
                               imagetype type_arg __attribute__((__unused__)))
 
6372
{
 
6373
  uint32 blob_length= get_length(ptr);
 
6374
  uchar *blob;
 
6375
 
 
6376
  get_ptr(&blob);
 
6377
  uint local_char_length= length / field_charset->mbmaxlen;
 
6378
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
 
6379
                          local_char_length);
 
6380
  set_if_smaller(blob_length, local_char_length);
 
6381
 
 
6382
  if ((uint32) length > blob_length)
 
6383
  {
 
6384
    /*
 
6385
      Must clear this as we do a memcmp in opt_range.cc to detect
 
6386
      identical keys
 
6387
    */
 
6388
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
 
6389
    length=(uint) blob_length;
 
6390
  }
 
6391
  int2store(buff,length);
 
6392
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
 
6393
  return HA_KEY_BLOB_LENGTH+length;
 
6394
}
 
6395
 
 
6396
 
 
6397
void Field_blob::set_key_image(const uchar *buff,uint length)
 
6398
{
 
6399
  length= uint2korr(buff);
 
6400
  (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
6401
                           field_charset);
 
6402
}
 
6403
 
 
6404
 
 
6405
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
 
6406
{
 
6407
  uchar *blob1;
 
6408
  uint blob_length=get_length(ptr);
 
6409
  memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
 
6410
  CHARSET_INFO *cs= charset();
 
6411
  uint local_char_length= max_key_length / cs->mbmaxlen;
 
6412
  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
 
6413
                                local_char_length);
 
6414
  set_if_smaller(blob_length, local_char_length);
 
6415
  return Field_blob::cmp(blob1, blob_length,
 
6416
                         key_ptr+HA_KEY_BLOB_LENGTH,
 
6417
                         uint2korr(key_ptr));
 
6418
}
 
6419
 
 
6420
int Field_blob::key_cmp(const uchar *a,const uchar *b)
 
6421
{
 
6422
  return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
 
6423
                         b+HA_KEY_BLOB_LENGTH, uint2korr(b));
 
6424
}
 
6425
 
 
6426
 
 
6427
/**
 
6428
   Save the field metadata for blob fields.
 
6429
 
 
6430
   Saves the pack length in the first byte of the field metadata array
 
6431
   at index of *metadata_ptr.
 
6432
 
 
6433
   @param   metadata_ptr   First byte of field metadata
 
6434
 
 
6435
   @returns number of bytes written to metadata_ptr
 
6436
*/
 
6437
int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
 
6438
{
 
6439
  *metadata_ptr= pack_length_no_ptr();
 
6440
  return 1;
 
6441
}
 
6442
 
 
6443
 
 
6444
uint32 Field_blob::sort_length() const
 
6445
{
 
6446
  return (uint32) (current_thd->variables.max_sort_length + 
 
6447
                   (field_charset == &my_charset_bin ? 0 : packlength));
 
6448
}
 
6449
 
 
6450
 
 
6451
void Field_blob::sort_string(uchar *to,uint length)
 
6452
{
 
6453
  uchar *blob;
 
6454
  uint blob_length=get_length();
 
6455
 
 
6456
  if (!blob_length)
 
6457
    bzero(to,length);
 
6458
  else
 
6459
  {
 
6460
    if (field_charset == &my_charset_bin)
 
6461
    {
 
6462
      uchar *pos;
 
6463
 
 
6464
      /*
 
6465
        Store length of blob last in blob to shorter blobs before longer blobs
 
6466
      */
 
6467
      length-= packlength;
 
6468
      pos= to+length;
 
6469
 
 
6470
      switch (packlength) {
 
6471
      case 1:
 
6472
        *pos= (char) blob_length;
 
6473
        break;
 
6474
      case 2:
 
6475
        mi_int2store(pos, blob_length);
 
6476
        break;
 
6477
      case 3:
 
6478
        mi_int3store(pos, blob_length);
 
6479
        break;
 
6480
      case 4:
 
6481
        mi_int4store(pos, blob_length);
 
6482
        break;
 
6483
      }
 
6484
    }
 
6485
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6486
    
 
6487
    blob_length=my_strnxfrm(field_charset,
 
6488
                            to, length, blob, blob_length);
 
6489
    DBUG_ASSERT(blob_length == length);
 
6490
  }
 
6491
}
 
6492
 
 
6493
 
 
6494
void Field_blob::sql_type(String &res) const
 
6495
{
 
6496
  const char *str;
 
6497
  uint length;
 
6498
  switch (packlength) {
 
6499
  default: str="tiny"; length=4; break;
 
6500
  case 2:  str="";     length=0; break;
 
6501
  case 3:  str="medium"; length= 6; break;
 
6502
  case 4:  str="long";  length=4; break;
 
6503
  }
 
6504
  res.set_ascii(str,length);
 
6505
  if (charset() == &my_charset_bin)
 
6506
    res.append(STRING_WITH_LEN("blob"));
 
6507
  else
 
6508
  {
 
6509
    res.append(STRING_WITH_LEN("text"));
 
6510
  }
 
6511
}
 
6512
 
 
6513
uchar *Field_blob::pack(uchar *to, const uchar *from,
 
6514
                        uint max_length, bool low_byte_first)
 
6515
{
 
6516
  DBUG_ENTER("Field_blob::pack");
 
6517
  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
 
6518
                       " max_length: %u; low_byte_first: %d",
 
6519
                       (ulong) to, (ulong) from,
 
6520
                       max_length, low_byte_first));
 
6521
  DBUG_DUMP("record", from, table->s->reclength);
 
6522
  uchar *save= ptr;
 
6523
  ptr= (uchar*) from;
 
6524
  uint32 length=get_length();                   // Length of from string
 
6525
 
 
6526
  /*
 
6527
    Store max length, which will occupy packlength bytes. If the max
 
6528
    length given is smaller than the actual length of the blob, we
 
6529
    just store the initial bytes of the blob.
 
6530
  */
 
6531
  store_length(to, packlength, min(length, max_length), low_byte_first);
 
6532
 
 
6533
  /*
 
6534
    Store the actual blob data, which will occupy 'length' bytes.
 
6535
   */
 
6536
  if (length > 0)
 
6537
  {
 
6538
    get_ptr((uchar**) &from);
 
6539
    memcpy(to+packlength, from,length);
 
6540
  }
 
6541
  ptr=save;                                     // Restore org row pointer
 
6542
  DBUG_DUMP("packed", to, packlength + length);
 
6543
  DBUG_RETURN(to+packlength+length);
 
6544
}
 
6545
 
 
6546
 
 
6547
/**
 
6548
   Unpack a blob field from row data.
 
6549
 
 
6550
   This method is used to unpack a blob field from a master whose size of 
 
6551
   the field is less than that of the slave. Note: This method is included
 
6552
   to satisfy inheritance rules, but is not needed for blob fields. It
 
6553
   simply is used as a pass-through to the original unpack() method for
 
6554
   blob fields.
 
6555
 
 
6556
   @param   to         Destination of the data
 
6557
   @param   from       Source of the data
 
6558
   @param   param_data @c TRUE if base types should be stored in little-
 
6559
                       endian format, @c FALSE if native format should
 
6560
                       be used.
 
6561
 
 
6562
   @return  New pointer into memory based on from + length of the data
 
6563
*/
 
6564
const uchar *Field_blob::unpack(uchar *to __attribute__((__unused__)),
 
6565
                                const uchar *from,
 
6566
                                uint param_data,
 
6567
                                bool low_byte_first)
 
6568
{
 
6569
  DBUG_ENTER("Field_blob::unpack");
 
6570
  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
 
6571
                       " param_data: %u; low_byte_first: %d",
 
6572
                       (ulong) to, (ulong) from, param_data, low_byte_first));
 
6573
  uint const master_packlength=
 
6574
    param_data > 0 ? param_data & 0xFF : packlength;
 
6575
  uint32 const length= get_length(from, master_packlength, low_byte_first);
 
6576
  DBUG_DUMP("packed", from, length + master_packlength);
 
6577
  bitmap_set_bit(table->write_set, field_index);
 
6578
  store(reinterpret_cast<const char*>(from) + master_packlength,
 
6579
        length, field_charset);
 
6580
  DBUG_DUMP("record", to, table->s->reclength);
 
6581
  DBUG_RETURN(from + master_packlength + length);
 
6582
}
 
6583
 
 
6584
/* Keys for blobs are like keys on varchars */
 
6585
 
 
6586
int Field_blob::pack_cmp(const uchar *a, const uchar *b, uint key_length_arg,
 
6587
                         my_bool insert_or_update)
 
6588
{
 
6589
  uint a_length, b_length;
 
6590
  if (key_length_arg > 255)
 
6591
  {
 
6592
    a_length=uint2korr(a); a+=2;
 
6593
    b_length=uint2korr(b); b+=2;
 
6594
  }
 
6595
  else
 
6596
  {
 
6597
    a_length= (uint) *a++;
 
6598
    b_length= (uint) *b++;
 
6599
  }
 
6600
  return field_charset->coll->strnncollsp(field_charset,
 
6601
                                          a, a_length,
 
6602
                                          b, b_length,
 
6603
                                          insert_or_update);
 
6604
}
 
6605
 
 
6606
 
 
6607
int Field_blob::pack_cmp(const uchar *b, uint key_length_arg,
 
6608
                         my_bool insert_or_update)
 
6609
{
 
6610
  uchar *a;
 
6611
  uint a_length, b_length;
 
6612
  memcpy_fixed(&a,ptr+packlength,sizeof(char*));
 
6613
  if (!a)
 
6614
    return key_length_arg > 0 ? -1 : 0;
 
6615
 
 
6616
  a_length= get_length(ptr);
 
6617
  if (key_length_arg > 255)
 
6618
  {
 
6619
    b_length= uint2korr(b); b+=2;
 
6620
  }
 
6621
  else
 
6622
    b_length= (uint) *b++;
 
6623
  return field_charset->coll->strnncollsp(field_charset,
 
6624
                                          a, a_length,
 
6625
                                          b, b_length,
 
6626
                                          insert_or_update);
 
6627
}
 
6628
 
 
6629
/** Create a packed key that will be used for storage from a MySQL row. */
 
6630
 
 
6631
uchar *
 
6632
Field_blob::pack_key(uchar *to, const uchar *from, uint max_length,
 
6633
                     bool low_byte_first __attribute__((unused)))
 
6634
{
 
6635
  uchar *save= ptr;
 
6636
  ptr= (uchar*) from;
 
6637
  uint32 length=get_length();        // Length of from string
 
6638
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
6639
                           max_length/field_charset->mbmaxlen : max_length);
 
6640
  if (length)
 
6641
    get_ptr((uchar**) &from);
 
6642
  if (length > local_char_length)
 
6643
    local_char_length= my_charpos(field_charset, from, from+length,
 
6644
                                  local_char_length);
 
6645
  set_if_smaller(length, local_char_length);
 
6646
  *to++= (uchar) length;
 
6647
  if (max_length > 255)                         // 2 byte length
 
6648
    *to++= (uchar) (length >> 8);
 
6649
  memcpy(to, from, length);
 
6650
  ptr=save;                                     // Restore org row pointer
 
6651
  return to+length;
 
6652
}
 
6653
 
 
6654
 
 
6655
/**
 
6656
  Unpack a blob key into a record buffer.
 
6657
 
 
6658
  A blob key has a maximum size of 64K-1.
 
6659
  In its packed form, the length field is one or two bytes long,
 
6660
  depending on 'max_length'.
 
6661
  Depending on the maximum length of a blob, its length field is
 
6662
  put into 1 to 4 bytes. This is a property of the blob object,
 
6663
  described by 'packlength'.
 
6664
  Blobs are internally stored apart from the record buffer, which
 
6665
  contains a pointer to the blob buffer.
 
6666
 
 
6667
 
 
6668
  @param to                          Pointer into the record buffer.
 
6669
  @param from                        Pointer to the packed key.
 
6670
  @param max_length                  Key length limit from key description.
 
6671
 
 
6672
  @return
 
6673
    Pointer into 'from' past the last byte copied from packed key.
 
6674
*/
 
6675
 
 
6676
const uchar *
 
6677
Field_blob::unpack_key(uchar *to, const uchar *from, uint max_length,
 
6678
                       bool low_byte_first __attribute__((unused)))
 
6679
{
 
6680
  /* get length of the blob key */
 
6681
  uint32 length= *from++;
 
6682
  if (max_length > 255)
 
6683
    length+= *from++ << 8;
 
6684
 
 
6685
  /* put the length into the record buffer */
 
6686
  put_length(to, length);
 
6687
 
 
6688
  /* put the address of the blob buffer or NULL */
 
6689
  if (length)
 
6690
    memcpy_fixed(to + packlength, &from, sizeof(from));
 
6691
  else
 
6692
    bzero(to + packlength, sizeof(from));
 
6693
 
 
6694
  /* point to first byte of next field in 'from' */
 
6695
  return from + length;
 
6696
}
 
6697
 
 
6698
 
 
6699
/** Create a packed key that will be used for storage from a MySQL key. */
 
6700
 
 
6701
uchar *
 
6702
Field_blob::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
 
6703
                                    bool low_byte_first __attribute__((unused)))
 
6704
{
 
6705
  uint length=uint2korr(from);
 
6706
  if (length > max_length)
 
6707
    length=max_length;
 
6708
  *to++= (char) (length & 255);
 
6709
  if (max_length > 255)
 
6710
    *to++= (char) (length >> 8);
 
6711
  if (length)
 
6712
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
 
6713
  return to+length;
 
6714
}
 
6715
 
 
6716
 
 
6717
uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
 
6718
{
 
6719
  if (length > 255)
 
6720
    return uint2korr(data_ptr)+2;
 
6721
  return (uint) *data_ptr + 1;
 
6722
}
 
6723
 
 
6724
 
 
6725
uint Field_blob::max_packed_col_length(uint max_length)
 
6726
{
 
6727
  return (max_length > 255 ? 2 : 1)+max_length;
 
6728
}
 
6729
 
 
6730
 
 
6731
uint Field_blob::is_equal(Create_field *new_field)
 
6732
{
 
6733
  if (compare_str_field_flags(new_field, flags))
 
6734
    return 0;
 
6735
 
 
6736
  return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
 
6737
          && new_field->charset == field_charset &&
 
6738
          ((Field_blob *)new_field->field)->max_data_length() ==
 
6739
          max_data_length());
 
6740
}
 
6741
 
 
6742
 
 
6743
/****************************************************************************
 
6744
** enum type.
 
6745
** This is a string which only can have a selection of different values.
 
6746
** If one uses this string in a number context one gets the type number.
 
6747
****************************************************************************/
 
6748
 
 
6749
enum ha_base_keytype Field_enum::key_type() const
 
6750
{
 
6751
  switch (packlength) {
 
6752
  default: return HA_KEYTYPE_BINARY;
 
6753
  case 2: return HA_KEYTYPE_USHORT_INT;
 
6754
  case 3: return HA_KEYTYPE_UINT24;
 
6755
  case 4: return HA_KEYTYPE_ULONG_INT;
 
6756
  case 8: return HA_KEYTYPE_ULONGLONG;
 
6757
  }
 
6758
}
 
6759
 
 
6760
void Field_enum::store_type(uint64_t value)
 
6761
{
 
6762
  switch (packlength) {
 
6763
  case 1: ptr[0]= (uchar) value;  break;
 
6764
  case 2:
 
6765
#ifdef WORDS_BIGENDIAN
 
6766
  if (table->s->db_low_byte_first)
 
6767
  {
 
6768
    int2store(ptr,(unsigned short) value);
 
6769
  }
 
6770
  else
 
6771
#endif
 
6772
    shortstore(ptr,(unsigned short) value);
 
6773
  break;
 
6774
  case 3: int3store(ptr,(long) value); break;
 
6775
  case 4:
 
6776
#ifdef WORDS_BIGENDIAN
 
6777
  if (table->s->db_low_byte_first)
 
6778
  {
 
6779
    int4store(ptr,value);
 
6780
  }
 
6781
  else
 
6782
#endif
 
6783
    longstore(ptr,(long) value);
 
6784
  break;
 
6785
  case 8:
 
6786
#ifdef WORDS_BIGENDIAN
 
6787
  if (table->s->db_low_byte_first)
 
6788
  {
 
6789
    int8store(ptr,value);
 
6790
  }
 
6791
  else
 
6792
#endif
 
6793
    longlongstore(ptr,value); break;
 
6794
  }
 
6795
}
 
6796
 
 
6797
 
 
6798
/**
 
6799
  @note
 
6800
    Storing a empty string in a enum field gives a warning
 
6801
    (if there isn't a empty value in the enum)
 
6802
*/
 
6803
 
 
6804
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
 
6805
{
 
6806
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
6807
  int err= 0;
 
6808
  uint32 not_used;
 
6809
  char buff[STRING_BUFFER_USUAL_SIZE];
 
6810
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
6811
 
 
6812
  /* Convert character set if necessary */
 
6813
  if (String::needs_conversion(length, cs, field_charset, &not_used))
 
6814
  { 
 
6815
    uint dummy_errors;
 
6816
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
 
6817
    from= tmpstr.ptr();
 
6818
    length=  tmpstr.length();
 
6819
  }
 
6820
 
 
6821
  /* Remove end space */
 
6822
  length= field_charset->cset->lengthsp(field_charset, from, length);
 
6823
  uint tmp=find_type2(typelib, from, length, field_charset);
 
6824
  if (!tmp)
 
6825
  {
 
6826
    if (length < 6) // Can't be more than 99999 enums
 
6827
    {
 
6828
      /* This is for reading numbers with LOAD DATA INFILE */
 
6829
      char *end;
 
6830
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
 
6831
      if (err || end != from+length || tmp > typelib->count)
 
6832
      {
 
6833
        tmp=0;
 
6834
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6835
      }
 
6836
      if (!table->in_use->count_cuted_fields)
 
6837
        err= 0;
 
6838
    }
 
6839
    else
 
6840
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6841
  }
 
6842
  store_type((uint64_t) tmp);
 
6843
  return err;
 
6844
}
 
6845
 
 
6846
 
 
6847
int Field_enum::store(double nr)
 
6848
{
 
6849
  return Field_enum::store((longlong) nr, FALSE);
 
6850
}
 
6851
 
 
6852
 
 
6853
int Field_enum::store(longlong nr,
 
6854
                      bool unsigned_val __attribute__((__unused__)))
 
6855
{
 
6856
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
6857
  int error= 0;
 
6858
  if ((uint64_t) nr > typelib->count || nr == 0)
 
6859
  {
 
6860
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6861
    if (nr != 0 || table->in_use->count_cuted_fields)
 
6862
    {
 
6863
      nr= 0;
 
6864
      error= 1;
 
6865
    }
 
6866
  }
 
6867
  store_type((uint64_t) (uint) nr);
 
6868
  return error;
 
6869
}
 
6870
 
 
6871
 
 
6872
double Field_enum::val_real(void)
 
6873
{
 
6874
  return (double) Field_enum::val_int();
 
6875
}
 
6876
 
 
6877
 
 
6878
longlong Field_enum::val_int(void)
 
6879
{
 
6880
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6881
  switch (packlength) {
 
6882
  case 1:
 
6883
    return (longlong) ptr[0];
 
6884
  case 2:
 
6885
  {
 
6886
    uint16 tmp;
 
6887
#ifdef WORDS_BIGENDIAN
 
6888
    if (table->s->db_low_byte_first)
 
6889
      tmp=sint2korr(ptr);
 
6890
    else
 
6891
#endif
 
6892
      shortget(tmp,ptr);
 
6893
    return (longlong) tmp;
 
6894
  }
 
6895
  case 3:
 
6896
    return (longlong) uint3korr(ptr);
 
6897
  case 4:
 
6898
  {
 
6899
    uint32 tmp;
 
6900
#ifdef WORDS_BIGENDIAN
 
6901
    if (table->s->db_low_byte_first)
 
6902
      tmp=uint4korr(ptr);
 
6903
    else
 
6904
#endif
 
6905
      longget(tmp,ptr);
 
6906
    return (longlong) tmp;
 
6907
  }
 
6908
  case 8:
 
6909
  {
 
6910
    longlong tmp;
 
6911
#ifdef WORDS_BIGENDIAN
 
6912
    if (table->s->db_low_byte_first)
 
6913
      tmp=sint8korr(ptr);
 
6914
    else
 
6915
#endif
 
6916
      longlongget(tmp,ptr);
 
6917
    return tmp;
 
6918
  }
 
6919
  }
 
6920
  return 0;                                     // impossible
 
6921
}
 
6922
 
 
6923
 
 
6924
/**
 
6925
   Save the field metadata for enum fields.
 
6926
 
 
6927
   Saves the real type in the first byte and the pack length in the 
 
6928
   second byte of the field metadata array at index of *metadata_ptr and
 
6929
   *(metadata_ptr + 1).
 
6930
 
 
6931
   @param   metadata_ptr   First byte of field metadata
 
6932
 
 
6933
   @returns number of bytes written to metadata_ptr
 
6934
*/
 
6935
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
 
6936
{
 
6937
  *metadata_ptr= real_type();
 
6938
  *(metadata_ptr + 1)= pack_length();
 
6939
  return 2;
 
6940
}
 
6941
 
 
6942
 
 
6943
String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
 
6944
                            String *val_ptr)
 
6945
{
 
6946
  uint tmp=(uint) Field_enum::val_int();
 
6947
  if (!tmp || tmp > typelib->count)
 
6948
    val_ptr->set("", 0, field_charset);
 
6949
  else
 
6950
    val_ptr->set((const char*) typelib->type_names[tmp-1],
 
6951
                 typelib->type_lengths[tmp-1],
 
6952
                 field_charset);
 
6953
  return val_ptr;
 
6954
}
 
6955
 
 
6956
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
6957
{
 
6958
  uchar *old= ptr;
 
6959
  ptr= (uchar*) a_ptr;
 
6960
  uint64_t a=Field_enum::val_int();
 
6961
  ptr= (uchar*) b_ptr;
 
6962
  uint64_t b=Field_enum::val_int();
 
6963
  ptr= old;
 
6964
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
6965
}
 
6966
 
 
6967
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
 
6968
{
 
6969
  uint64_t value=Field_enum::val_int();
 
6970
  to+=packlength-1;
 
6971
  for (uint i=0 ; i < packlength ; i++)
 
6972
  {
 
6973
    *to-- = (uchar) (value & 255);
 
6974
    value>>=8;
 
6975
  }
 
6976
}
 
6977
 
 
6978
 
 
6979
void Field_enum::sql_type(String &res) const
 
6980
{
 
6981
  char buffer[255];
 
6982
  String enum_item(buffer, sizeof(buffer), res.charset());
 
6983
 
 
6984
  res.length(0);
 
6985
  res.append(STRING_WITH_LEN("enum("));
 
6986
 
 
6987
  bool flag=0;
 
6988
  uint *len= typelib->type_lengths;
 
6989
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
 
6990
  {
 
6991
    uint dummy_errors;
 
6992
    if (flag)
 
6993
      res.append(',');
 
6994
    /* convert to res.charset() == utf8, then quote */
 
6995
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
 
6996
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
 
6997
    flag= 1;
 
6998
  }
 
6999
  res.append(')');
 
7000
}
 
7001
 
 
7002
 
 
7003
Field *Field_enum::new_field(MEM_ROOT *root, struct st_table *new_table,
 
7004
                             bool keep_type)
 
7005
{
 
7006
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
 
7007
  if (res)
 
7008
    res->typelib= copy_typelib(root, typelib);
 
7009
  return res;
 
7010
}
 
7011
 
 
7012
 
 
7013
/*
 
7014
   set type.
 
7015
   This is a string which can have a collection of different values.
 
7016
   Each string value is separated with a ','.
 
7017
   For example "One,two,five"
 
7018
   If one uses this string in a number context one gets the bits as a longlong
 
7019
   number.
 
7020
*/
 
7021
 
 
7022
 
 
7023
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
 
7024
{
 
7025
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
7026
  bool got_warning= 0;
 
7027
  int err= 0;
 
7028
  char *not_used;
 
7029
  uint not_used2;
 
7030
  uint32 not_used_offset;
 
7031
  char buff[STRING_BUFFER_USUAL_SIZE];
 
7032
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
7033
 
 
7034
  /* Convert character set if necessary */
 
7035
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
 
7036
  { 
 
7037
    uint dummy_errors;
 
7038
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
 
7039
    from= tmpstr.ptr();
 
7040
    length=  tmpstr.length();
 
7041
  }
 
7042
  uint64_t tmp= find_set(typelib, from, length, field_charset,
 
7043
                          &not_used, &not_used2, &got_warning);
 
7044
  if (!tmp && length && length < 22)
 
7045
  {
 
7046
    /* This is for reading numbers with LOAD DATA INFILE */
 
7047
    char *end;
 
7048
    tmp=my_strntoull(cs,from,length,10,&end,&err);
 
7049
    if (err || end != from+length ||
 
7050
        tmp > (uint64_t) (((longlong) 1 << typelib->count) - (longlong) 1))
 
7051
    {
 
7052
      tmp=0;      
 
7053
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
7054
    }
 
7055
  }
 
7056
  else if (got_warning)
 
7057
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
7058
  store_type(tmp);
 
7059
  return err;
 
7060
}
 
7061
 
 
7062
 
 
7063
int Field_set::store(longlong nr,
 
7064
                     bool unsigned_val __attribute__((__unused__)))
 
7065
{
 
7066
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
7067
  int error= 0;
 
7068
  uint64_t max_nr= set_bits(uint64_t, typelib->count);
 
7069
  if ((uint64_t) nr > max_nr)
 
7070
  {
 
7071
    nr&= max_nr;
 
7072
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
7073
    error=1;
 
7074
  }
 
7075
  store_type((uint64_t) nr);
 
7076
  return error;
 
7077
}
 
7078
 
 
7079
 
 
7080
String *Field_set::val_str(String *val_buffer,
 
7081
                           String *val_ptr __attribute__((unused)))
 
7082
{
 
7083
  uint64_t tmp=(uint64_t) Field_enum::val_int();
 
7084
  uint bitnr=0;
 
7085
 
 
7086
  val_buffer->length(0);
 
7087
  val_buffer->set_charset(field_charset);
 
7088
  while (tmp && bitnr < (uint) typelib->count)
 
7089
  {
 
7090
    if (tmp & 1)
 
7091
    {
 
7092
      if (val_buffer->length())
 
7093
        val_buffer->append(&field_separator, 1, &my_charset_latin1);
 
7094
      String str(typelib->type_names[bitnr],
 
7095
                 typelib->type_lengths[bitnr],
 
7096
                 field_charset);
 
7097
      val_buffer->append(str);
 
7098
    }
 
7099
    tmp>>=1;
 
7100
    bitnr++;
 
7101
  }
 
7102
  return val_buffer;
 
7103
}
 
7104
 
 
7105
 
 
7106
void Field_set::sql_type(String &res) const
 
7107
{
 
7108
  char buffer[255];
 
7109
  String set_item(buffer, sizeof(buffer), res.charset());
 
7110
 
 
7111
  res.length(0);
 
7112
  res.append(STRING_WITH_LEN("set("));
 
7113
 
 
7114
  bool flag=0;
 
7115
  uint *len= typelib->type_lengths;
 
7116
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
 
7117
  {
 
7118
    uint dummy_errors;
 
7119
    if (flag)
 
7120
      res.append(',');
 
7121
    /* convert to res.charset() == utf8, then quote */
 
7122
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
 
7123
    append_unescaped(&res, set_item.ptr(), set_item.length());
 
7124
    flag= 1;
 
7125
  }
 
7126
  res.append(')');
 
7127
}
 
7128
 
1607
7129
/**
1608
7130
  @retval
1609
7131
    1  if the fields are equally defined
1631
7153
 
1632
7154
  if (typelib->count < from_lib->count)
1633
7155
    return 0;
1634
 
  for (uint32_t i=0 ; i < from_lib->count ; i++)
 
7156
  for (uint i=0 ; i < from_lib->count ; i++)
1635
7157
    if (my_strnncoll(field_charset,
1636
 
                     (const unsigned char*)typelib->type_names[i],
 
7158
                     (const uchar*)typelib->type_names[i],
1637
7159
                     strlen(typelib->type_names[i]),
1638
 
                     (const unsigned char*)from_lib->type_names[i],
 
7160
                     (const uchar*)from_lib->type_names[i],
1639
7161
                     strlen(from_lib->type_names[i])))
1640
7162
      return 0;
1641
7163
  return 1;
1652
7174
  Field_num *from_num= (Field_num*) field;
1653
7175
 
1654
7176
  if (unsigned_flag != from_num->unsigned_flag ||
 
7177
      (zerofill && !from_num->zerofill && !zero_pack()) ||
1655
7178
      dec != from_num->dec)
1656
7179
    return 0;
1657
7180
  return 1;
1658
7181
}
1659
7182
 
1660
7183
 
1661
 
uint32_t Field_num::is_equal(Create_field *new_field)
 
7184
uint Field_num::is_equal(Create_field *new_field)
1662
7185
{
1663
7186
  return ((new_field->sql_type == real_type()) &&
1664
 
          ((new_field->flags & UNSIGNED_FLAG) == (uint32_t) (flags &
 
7187
          ((new_field->flags & UNSIGNED_FLAG) == (uint) (flags &
1665
7188
                                                         UNSIGNED_FLAG)) &&
1666
7189
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
1667
 
           (uint32_t) (flags & AUTO_INCREMENT_FLAG)) &&
 
7190
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
1668
7191
          (new_field->length <= max_display_length()));
1669
7192
}
1670
7193
 
1680
7203
void Create_field::create_length_to_internal_length(void)
1681
7204
{
1682
7205
  switch (sql_type) {
1683
 
  case DRIZZLE_TYPE_BLOB:
1684
 
  case DRIZZLE_TYPE_VARCHAR:
 
7206
  case MYSQL_TYPE_BLOB:
 
7207
  case MYSQL_TYPE_VAR_STRING:
 
7208
  case MYSQL_TYPE_STRING:
 
7209
  case MYSQL_TYPE_VARCHAR:
1685
7210
    length*= charset->mbmaxlen;
1686
7211
    key_length= length;
1687
7212
    pack_length= calc_pack_length(sql_type, length);
1688
7213
    break;
1689
 
  case DRIZZLE_TYPE_ENUM:
 
7214
  case MYSQL_TYPE_ENUM:
 
7215
  case MYSQL_TYPE_SET:
1690
7216
    /* Pack_length already calculated in sql_parse.cc */
1691
7217
    length*= charset->mbmaxlen;
1692
7218
    key_length= pack_length;
1693
7219
    break;
1694
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7220
  case MYSQL_TYPE_NEWDECIMAL:
1695
7221
    key_length= pack_length=
1696
7222
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1697
7223
                                                                decimals,
1710
7236
  Init for a tmp table field. To be extended if need be.
1711
7237
*/
1712
7238
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1713
 
                                      uint32_t length_arg, uint32_t decimals_arg,
 
7239
                                      uint32 length_arg, uint32 decimals_arg,
1714
7240
                                      bool maybe_null, bool is_unsigned)
1715
7241
{
1716
7242
  field_name= "";
1743
7269
  @param fld_charset           Field charset
1744
7270
 
1745
7271
  @retval
1746
 
    false on success
 
7272
    FALSE on success
1747
7273
  @retval
1748
 
    true  on error
 
7274
    TRUE  on error
1749
7275
*/
1750
7276
 
1751
 
bool Create_field::init(THD *thd __attribute__((unused)), char *fld_name, enum_field_types fld_type,
 
7277
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
1752
7278
                        char *fld_length, char *fld_decimals,
1753
 
                        uint32_t fld_type_modifier, Item *fld_default_value,
 
7279
                        uint fld_type_modifier, Item *fld_default_value,
1754
7280
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
1755
7281
                        char *fld_change, List<String> *fld_interval_list,
1756
 
                        const CHARSET_INFO * const fld_charset,
1757
 
                        uint32_t fld_geom_type __attribute__((unused)),
 
7282
                        CHARSET_INFO *fld_charset,
 
7283
                        uint fld_geom_type __attribute__((__unused__)),
1758
7284
                        enum column_format_type column_format)
1759
7285
{
1760
 
  uint32_t sign_len, allowed_type_modifier= 0;
1761
 
  uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
 
7286
  uint sign_len, allowed_type_modifier= 0;
 
7287
  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
 
7288
 
 
7289
  DBUG_ENTER("Create_field::init()");
1762
7290
 
1763
7291
  field= 0;
1764
7292
  field_name= fld_name;
1765
7293
  def= fld_default_value;
1766
7294
  flags= fld_type_modifier;
1767
 
  flags|= (((uint32_t)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
 
7295
  flags|= (((uint)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1768
7296
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1769
7297
                 Field::NEXT_NUMBER : Field::NONE);
1770
 
  decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
 
7298
  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
1771
7299
  if (decimals >= NOT_FIXED_DEC)
1772
7300
  {
1773
7301
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1774
7302
             NOT_FIXED_DEC-1);
1775
 
    return(true);
 
7303
    DBUG_RETURN(TRUE);
1776
7304
  }
1777
7305
 
1778
7306
  sql_type= fld_type;
1789
7317
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1790
7318
  */
1791
7319
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1792
 
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
 
7320
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != MYSQL_TYPE_TIMESTAMP)
1793
7321
    flags|= NO_DEFAULT_VALUE_FLAG;
1794
7322
 
1795
 
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
 
7323
  if (fld_length && !(length= (uint) atoi(fld_length)))
1796
7324
    fld_length= 0; /* purecov: inspected */
1797
7325
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1798
7326
 
1799
7327
  switch (fld_type) {
1800
 
  case DRIZZLE_TYPE_TINY:
 
7328
  case MYSQL_TYPE_TINY:
1801
7329
    if (!fld_length)
1802
7330
      length= MAX_TINYINT_WIDTH+sign_len;
1803
7331
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1804
7332
    break;
1805
 
  case DRIZZLE_TYPE_LONG:
 
7333
  case MYSQL_TYPE_SHORT:
 
7334
    if (!fld_length)
 
7335
      length= MAX_SMALLINT_WIDTH+sign_len;
 
7336
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
7337
    break;
 
7338
  case MYSQL_TYPE_LONG:
1806
7339
    if (!fld_length)
1807
7340
      length= MAX_INT_WIDTH+sign_len;
1808
7341
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1809
7342
    break;
1810
 
  case DRIZZLE_TYPE_LONGLONG:
 
7343
  case MYSQL_TYPE_LONGLONG:
1811
7344
    if (!fld_length)
1812
7345
      length= MAX_BIGINT_WIDTH;
1813
7346
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1814
7347
    break;
1815
 
  case DRIZZLE_TYPE_NULL:
 
7348
  case MYSQL_TYPE_NULL:
1816
7349
    break;
1817
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7350
  case MYSQL_TYPE_NEWDECIMAL:
1818
7351
    my_decimal_trim(&length, &decimals);
1819
7352
    if (length > DECIMAL_MAX_PRECISION)
1820
7353
    {
1821
7354
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1822
7355
               DECIMAL_MAX_PRECISION);
1823
 
      return(true);
 
7356
      DBUG_RETURN(TRUE);
1824
7357
    }
1825
7358
    if (length < decimals)
1826
7359
    {
1827
7360
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1828
 
      return(true);
 
7361
      DBUG_RETURN(TRUE);
1829
7362
    }
1830
7363
    length=
1831
7364
      my_decimal_precision_to_length(length, decimals,
1833
7366
    pack_length=
1834
7367
      my_decimal_get_binary_size(length, decimals);
1835
7368
    break;
1836
 
  case DRIZZLE_TYPE_VARCHAR:
 
7369
  case MYSQL_TYPE_VARCHAR:
1837
7370
    /*
1838
7371
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1839
7372
      if they don't have a default value
1840
7373
    */
1841
7374
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1842
7375
    break;
1843
 
  case DRIZZLE_TYPE_BLOB:
 
7376
  case MYSQL_TYPE_STRING:
 
7377
    break;
 
7378
  case MYSQL_TYPE_BLOB:
1844
7379
    if (fld_default_value)
1845
7380
    {
1846
7381
      /* Allow empty as default value. */
1847
7382
      String str,*res;
1848
7383
      res= fld_default_value->val_str(&str);
 
7384
      /*
 
7385
        A default other than '' is always an error, and any non-NULL
 
7386
        specified default is an error in strict mode.
 
7387
      */
 
7388
      if (res->length() || (thd->variables.sql_mode &
 
7389
                            (MODE_STRICT_TRANS_TABLES |
 
7390
                             MODE_STRICT_ALL_TABLES)))
 
7391
      {
 
7392
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
 
7393
                 fld_name); /* purecov: inspected */
 
7394
        DBUG_RETURN(TRUE);
 
7395
      }
 
7396
      else
 
7397
      {
 
7398
        /*
 
7399
          Otherwise a default of '' is just a warning.
 
7400
        */
 
7401
        push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
7402
                            ER_BLOB_CANT_HAVE_DEFAULT,
 
7403
                            ER(ER_BLOB_CANT_HAVE_DEFAULT),
 
7404
                            fld_name);
 
7405
      }
 
7406
      def= 0;
1849
7407
    }
1850
7408
    flags|= BLOB_FLAG;
1851
7409
    break;
1852
 
  case DRIZZLE_TYPE_DOUBLE:
 
7410
  case MYSQL_TYPE_YEAR:
 
7411
    if (!fld_length || length != 2)
 
7412
      length= 4; /* Default length */
 
7413
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
7414
    break;
 
7415
  case MYSQL_TYPE_FLOAT:
 
7416
    /* change FLOAT(precision) to FLOAT or DOUBLE */
 
7417
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
7418
    if (fld_length && !fld_decimals)
 
7419
    {
 
7420
      uint tmp_length= length;
 
7421
      if (tmp_length > PRECISION_FOR_DOUBLE)
 
7422
      {
 
7423
        my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
 
7424
        DBUG_RETURN(TRUE);
 
7425
      }
 
7426
      else if (tmp_length > PRECISION_FOR_FLOAT)
 
7427
      {
 
7428
        sql_type= MYSQL_TYPE_DOUBLE;
 
7429
        length= DBL_DIG+7; /* -[digits].E+### */
 
7430
      }
 
7431
      else
 
7432
        length= FLT_DIG+6; /* -[digits].E+## */
 
7433
      decimals= NOT_FIXED_DEC;
 
7434
      break;
 
7435
    }
 
7436
    if (!fld_length && !fld_decimals)
 
7437
    {
 
7438
      length=  FLT_DIG+6;
 
7439
      decimals= NOT_FIXED_DEC;
 
7440
    }
 
7441
    if (length < decimals &&
 
7442
        decimals != NOT_FIXED_DEC)
 
7443
    {
 
7444
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
 
7445
      DBUG_RETURN(TRUE);
 
7446
    }
 
7447
    break;
 
7448
  case MYSQL_TYPE_DOUBLE:
1853
7449
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1854
7450
    if (!fld_length && !fld_decimals)
1855
7451
    {
1860
7456
        decimals != NOT_FIXED_DEC)
1861
7457
    {
1862
7458
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1863
 
      return(true);
 
7459
      DBUG_RETURN(TRUE);
1864
7460
    }
1865
7461
    break;
1866
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
7462
  case MYSQL_TYPE_TIMESTAMP:
1867
7463
    if (!fld_length)
1868
7464
    {
1869
7465
      /* Compressed date YYYYMMDDHHMMSS */
1876
7472
        and 19 as length of 4.1 compatible representation.
1877
7473
      */
1878
7474
      length= ((length+1)/2)*2; /* purecov: inspected */
1879
 
      length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
 
7475
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1880
7476
    }
1881
 
    flags|= UNSIGNED_FLAG;
 
7477
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
1882
7478
    if (fld_default_value)
1883
7479
    {
1884
7480
      /* Grammar allows only NOW() value for ON UPDATE clause */
1917
7513
                                              Field::NONE));
1918
7514
    }
1919
7515
    break;
1920
 
  case DRIZZLE_TYPE_NEWDATE:
1921
 
    length= 10;
1922
 
    break;
1923
 
  case DRIZZLE_TYPE_TIME:
1924
 
    length= 10;
1925
 
    break;
1926
 
  case DRIZZLE_TYPE_DATETIME:
 
7516
  case MYSQL_TYPE_DATE:
 
7517
    /* Old date type. */
 
7518
    sql_type= MYSQL_TYPE_NEWDATE;
 
7519
    /* fall trough */
 
7520
  case MYSQL_TYPE_NEWDATE:
 
7521
    length= 10;
 
7522
    break;
 
7523
  case MYSQL_TYPE_TIME:
 
7524
    length= 10;
 
7525
    break;
 
7526
  case MYSQL_TYPE_DATETIME:
1927
7527
    length= MAX_DATETIME_WIDTH;
1928
7528
    break;
1929
 
  case DRIZZLE_TYPE_ENUM:
 
7529
  case MYSQL_TYPE_SET:
 
7530
    {
 
7531
      pack_length= get_set_pack_length(fld_interval_list->elements);
 
7532
 
 
7533
      List_iterator<String> it(*fld_interval_list);
 
7534
      String *tmp;
 
7535
      while ((tmp= it++))
 
7536
        interval_list.push_back(tmp);
 
7537
      /*
 
7538
        Set fake length to 1 to pass the below conditions.
 
7539
        Real length will be set in mysql_prepare_table()
 
7540
        when we know the character set of the column
 
7541
      */
 
7542
      length= 1;
 
7543
      break;
 
7544
    }
 
7545
  case MYSQL_TYPE_ENUM:
1930
7546
    {
1931
7547
      /* Should be safe. */
1932
7548
      pack_length= get_enum_pack_length(fld_interval_list->elements);
1935
7551
      String *tmp;
1936
7552
      while ((tmp= it++))
1937
7553
        interval_list.push_back(tmp);
1938
 
      length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
 
7554
      length= 1; /* See comment for MYSQL_TYPE_SET above. */
1939
7555
      break;
1940
7556
   }
 
7557
  case MYSQL_TYPE_VAR_STRING:
 
7558
    DBUG_ASSERT(0);  /* Impossible. */
 
7559
    break;
1941
7560
  }
1942
7561
  /* Remember the value of length */
1943
7562
  char_length= length;
1944
7563
 
1945
7564
  if (!(flags & BLOB_FLAG) &&
1946
 
      ((length > max_field_charlength &&
1947
 
        fld_type != DRIZZLE_TYPE_ENUM &&
1948
 
        (fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1949
 
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
 
7565
      ((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
 
7566
        fld_type != MYSQL_TYPE_ENUM &&
 
7567
        (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
 
7568
       (!length &&
 
7569
        fld_type != MYSQL_TYPE_STRING &&
 
7570
        fld_type != MYSQL_TYPE_VARCHAR)))
1950
7571
  {
1951
 
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
 
7572
    my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
 
7573
              fld_type == MYSQL_TYPE_VARCHAR ||
 
7574
              fld_type == MYSQL_TYPE_STRING) ?  ER_TOO_BIG_FIELDLENGTH :
 
7575
                                                ER_TOO_BIG_DISPLAYWIDTH,
1952
7576
              MYF(0),
1953
7577
              fld_name, max_field_charlength); /* purecov: inspected */
1954
 
    return(true);
 
7578
    DBUG_RETURN(TRUE);
1955
7579
  }
1956
7580
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
1957
7581
  if ((~allowed_type_modifier) & fld_type_modifier)
1958
7582
  {
1959
7583
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1960
 
    return(true);
 
7584
    DBUG_RETURN(TRUE);
1961
7585
  }
1962
7586
 
1963
 
  return(false); /* success */
 
7587
  DBUG_RETURN(FALSE); /* success */
1964
7588
}
1965
7589
 
1966
7590
 
1967
 
enum_field_types get_blob_type_from_length(uint32_t length __attribute__((unused)))
 
7591
enum_field_types get_blob_type_from_length(ulong length)
1968
7592
{
1969
7593
  enum_field_types type;
1970
7594
 
1971
 
  type= DRIZZLE_TYPE_BLOB;
 
7595
  type= MYSQL_TYPE_BLOB;
1972
7596
 
1973
7597
  return type;
1974
7598
}
1978
7602
  Make a field from the .frm file info
1979
7603
*/
1980
7604
 
1981
 
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
 
7605
uint32 calc_pack_length(enum_field_types type,uint32 length)
1982
7606
{
1983
7607
  switch (type) {
1984
 
  case DRIZZLE_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
1985
 
  case DRIZZLE_TYPE_TINY        : return 1;
1986
 
  case DRIZZLE_TYPE_NEWDATE:
1987
 
  case DRIZZLE_TYPE_TIME:   return 3;
1988
 
  case DRIZZLE_TYPE_TIMESTAMP:
1989
 
  case DRIZZLE_TYPE_LONG        : return 4;
1990
 
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1991
 
  case DRIZZLE_TYPE_DATETIME:
1992
 
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1993
 
  case DRIZZLE_TYPE_NULL        : return 0;
1994
 
  case DRIZZLE_TYPE_BLOB:               return 4+portable_sizeof_char_ptr;
1995
 
  case DRIZZLE_TYPE_ENUM:
1996
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7608
  case MYSQL_TYPE_VAR_STRING:
 
7609
  case MYSQL_TYPE_STRING:
 
7610
  case MYSQL_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
 
7611
  case MYSQL_TYPE_YEAR:
 
7612
  case MYSQL_TYPE_TINY  : return 1;
 
7613
  case MYSQL_TYPE_SHORT : return 2;
 
7614
  case MYSQL_TYPE_DATE:
 
7615
  case MYSQL_TYPE_NEWDATE:
 
7616
  case MYSQL_TYPE_TIME:   return 3;
 
7617
  case MYSQL_TYPE_TIMESTAMP:
 
7618
  case MYSQL_TYPE_LONG  : return 4;
 
7619
  case MYSQL_TYPE_FLOAT : return sizeof(float);
 
7620
  case MYSQL_TYPE_DOUBLE: return sizeof(double);
 
7621
  case MYSQL_TYPE_DATETIME:
 
7622
  case MYSQL_TYPE_LONGLONG: return 8;   /* Don't crash if no longlong */
 
7623
  case MYSQL_TYPE_NULL  : return 0;
 
7624
  case MYSQL_TYPE_BLOB:         return 4+portable_sizeof_char_ptr;
 
7625
  case MYSQL_TYPE_SET:
 
7626
  case MYSQL_TYPE_ENUM:
 
7627
  case MYSQL_TYPE_NEWDECIMAL:
1997
7628
    abort(); return 0;                          // This shouldn't happen
1998
7629
  default:
1999
7630
    return 0;
2001
7632
}
2002
7633
 
2003
7634
 
2004
 
uint32_t pack_length_to_packflag(uint32_t type)
 
7635
uint pack_length_to_packflag(uint type)
2005
7636
{
2006
7637
  switch (type) {
2007
 
    case 1: return f_settype((uint32_t) DRIZZLE_TYPE_TINY);
2008
 
    case 2: assert(1);
 
7638
    case 1: return f_settype((uint) MYSQL_TYPE_TINY);
 
7639
    case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
2009
7640
    case 3: assert(1);
2010
 
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
2011
 
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
 
7641
    case 4: return f_settype((uint) MYSQL_TYPE_LONG);
 
7642
    case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
2012
7643
  }
2013
7644
  return 0;                                     // This shouldn't happen
2014
7645
}
2015
7646
 
2016
7647
 
2017
 
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
2018
 
                  unsigned char *null_pos, unsigned char null_bit,
2019
 
                  uint32_t pack_flag,
 
7648
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
 
7649
                  uchar *null_pos, uchar null_bit,
 
7650
                  uint pack_flag,
2020
7651
                  enum_field_types field_type,
2021
 
                  const CHARSET_INFO * field_charset,
 
7652
                  CHARSET_INFO *field_charset,
2022
7653
                  Field::utype unireg_check,
2023
7654
                  TYPELIB *interval,
2024
7655
                  const char *field_name)
2030
7661
  }
2031
7662
  else
2032
7663
  {
2033
 
    null_bit= ((unsigned char) 1) << null_bit;
 
7664
    null_bit= ((uchar) 1) << null_bit;
2034
7665
  }
2035
7666
 
2036
7667
  switch (field_type) {
2037
 
  case DRIZZLE_TYPE_NEWDATE:
2038
 
  case DRIZZLE_TYPE_TIME:
2039
 
  case DRIZZLE_TYPE_DATETIME:
2040
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
7668
  case MYSQL_TYPE_DATE:
 
7669
  case MYSQL_TYPE_NEWDATE:
 
7670
  case MYSQL_TYPE_TIME:
 
7671
  case MYSQL_TYPE_DATETIME:
 
7672
  case MYSQL_TYPE_TIMESTAMP:
2041
7673
    field_charset= &my_charset_bin;
2042
7674
  default: break;
2043
7675
  }
2046
7678
  {
2047
7679
    if (!f_is_packed(pack_flag))
2048
7680
    {
2049
 
      if (field_type == DRIZZLE_TYPE_VARCHAR)
 
7681
      if (field_type == MYSQL_TYPE_STRING ||
 
7682
          field_type == MYSQL_TYPE_VAR_STRING)
 
7683
        return new Field_string(ptr,field_length,null_pos,null_bit,
 
7684
                                unireg_check, field_name,
 
7685
                                field_charset);
 
7686
      if (field_type == MYSQL_TYPE_VARCHAR)
2050
7687
        return new Field_varstring(ptr,field_length,
2051
7688
                                   HA_VARCHAR_PACKLENGTH(field_length),
2052
7689
                                   null_pos,null_bit,
2056
7693
      return 0;                                 // Error
2057
7694
    }
2058
7695
 
2059
 
    uint32_t pack_length=calc_pack_length((enum_field_types)
 
7696
    uint pack_length=calc_pack_length((enum_field_types)
2060
7697
                                      f_packtype(pack_flag),
2061
7698
                                      field_length);
2062
7699
 
2070
7707
        return new Field_enum(ptr,field_length,null_pos,null_bit,
2071
7708
                                  unireg_check, field_name,
2072
7709
                                  pack_length, interval, field_charset);
 
7710
      else
 
7711
        return new Field_set(ptr,field_length,null_pos,null_bit,
 
7712
                             unireg_check, field_name,
 
7713
                             pack_length, interval, field_charset);
2073
7714
    }
2074
7715
  }
2075
7716
 
2076
7717
  switch (field_type) {
2077
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7718
  case MYSQL_TYPE_NEWDECIMAL:
2078
7719
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
2079
7720
                                 unireg_check, field_name,
2080
7721
                                 f_decimals(pack_flag),
2081
 
                                 f_is_decimal_precision(pack_flag) != 0,
 
7722
                                 f_is_zerofill(pack_flag) != 0,
2082
7723
                                 f_is_dec(pack_flag) == 0);
2083
 
  case DRIZZLE_TYPE_DOUBLE:
 
7724
  case MYSQL_TYPE_FLOAT:
 
7725
    return new Field_float(ptr,field_length,null_pos,null_bit,
 
7726
                           unireg_check, field_name,
 
7727
                           f_decimals(pack_flag),
 
7728
                           f_is_zerofill(pack_flag) != 0,
 
7729
                           f_is_dec(pack_flag)== 0);
 
7730
  case MYSQL_TYPE_DOUBLE:
2084
7731
    return new Field_double(ptr,field_length,null_pos,null_bit,
2085
7732
                            unireg_check, field_name,
2086
7733
                            f_decimals(pack_flag),
2087
 
                            false,
 
7734
                            f_is_zerofill(pack_flag) != 0,
2088
7735
                            f_is_dec(pack_flag)== 0);
2089
 
  case DRIZZLE_TYPE_TINY:
 
7736
  case MYSQL_TYPE_TINY:
2090
7737
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
2091
7738
                          unireg_check, field_name,
2092
 
                          false,
 
7739
                          f_is_zerofill(pack_flag) != 0,
2093
7740
                          f_is_dec(pack_flag) == 0);
2094
 
  case DRIZZLE_TYPE_LONG:
 
7741
  case MYSQL_TYPE_SHORT:
 
7742
    return new Field_short(ptr,field_length,null_pos,null_bit,
 
7743
                           unireg_check, field_name,
 
7744
                           f_is_zerofill(pack_flag) != 0,
 
7745
                           f_is_dec(pack_flag) == 0);
 
7746
  case MYSQL_TYPE_LONG:
2095
7747
    return new Field_long(ptr,field_length,null_pos,null_bit,
2096
7748
                           unireg_check, field_name,
2097
 
                           false,
 
7749
                           f_is_zerofill(pack_flag) != 0,
2098
7750
                           f_is_dec(pack_flag) == 0);
2099
 
  case DRIZZLE_TYPE_LONGLONG:
2100
 
    return new Field_int64_t(ptr,field_length,null_pos,null_bit,
 
7751
  case MYSQL_TYPE_LONGLONG:
 
7752
    return new Field_longlong(ptr,field_length,null_pos,null_bit,
2101
7753
                              unireg_check, field_name,
2102
 
                              false,
 
7754
                              f_is_zerofill(pack_flag) != 0,
2103
7755
                              f_is_dec(pack_flag) == 0);
2104
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
7756
  case MYSQL_TYPE_TIMESTAMP:
2105
7757
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
2106
7758
                               unireg_check, field_name, share,
2107
7759
                               field_charset);
2108
 
  case DRIZZLE_TYPE_NEWDATE:
 
7760
  case MYSQL_TYPE_YEAR:
 
7761
    return new Field_year(ptr,field_length,null_pos,null_bit,
 
7762
                          unireg_check, field_name);
 
7763
  case MYSQL_TYPE_DATE:
 
7764
  case MYSQL_TYPE_NEWDATE:
2109
7765
    return new Field_newdate(ptr,null_pos,null_bit,
2110
7766
                             unireg_check, field_name, field_charset);
2111
 
  case DRIZZLE_TYPE_TIME:
 
7767
  case MYSQL_TYPE_TIME:
2112
7768
    return new Field_time(ptr,null_pos,null_bit,
2113
7769
                          unireg_check, field_name, field_charset);
2114
 
  case DRIZZLE_TYPE_DATETIME:
 
7770
  case MYSQL_TYPE_DATETIME:
2115
7771
    return new Field_datetime(ptr,null_pos,null_bit,
2116
7772
                              unireg_check, field_name, field_charset);
2117
 
  case DRIZZLE_TYPE_NULL:
 
7773
  case MYSQL_TYPE_NULL:
2118
7774
    return new Field_null(ptr, field_length, unireg_check, field_name,
2119
7775
                          field_charset);
2120
7776
  default:                                      // Impossible (Wrong version)
2146
7802
                  portable_sizeof_char_ptr);
2147
7803
 
2148
7804
  switch (sql_type) {
2149
 
  case DRIZZLE_TYPE_BLOB:
2150
 
    sql_type= DRIZZLE_TYPE_BLOB;
 
7805
  case MYSQL_TYPE_BLOB:
 
7806
    sql_type= MYSQL_TYPE_BLOB;
2151
7807
    length/= charset->mbmaxlen;
2152
7808
    key_length/= charset->mbmaxlen;
2153
7809
    break;
 
7810
  case MYSQL_TYPE_STRING:
2154
7811
    /* Change CHAR -> VARCHAR if dynamic record length */
2155
 
  case DRIZZLE_TYPE_ENUM:
2156
 
  case DRIZZLE_TYPE_VARCHAR:
 
7812
    if (old_field->type() == MYSQL_TYPE_VAR_STRING)
 
7813
      sql_type= MYSQL_TYPE_VARCHAR;
 
7814
    /* fall through */
 
7815
 
 
7816
  case MYSQL_TYPE_ENUM:
 
7817
  case MYSQL_TYPE_SET:
 
7818
  case MYSQL_TYPE_VARCHAR:
 
7819
  case MYSQL_TYPE_VAR_STRING:
2157
7820
    /* This is corrected in create_length_to_internal_length */
2158
7821
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
2159
7822
    break;
2170
7833
 
2171
7834
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
2172
7835
      old_field->ptr && orig_field &&
2173
 
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
 
7836
      (sql_type != MYSQL_TYPE_TIMESTAMP ||                /* set def only if */
2174
7837
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
2175
7838
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
2176
7839
  {
2195
7858
}
2196
7859
 
2197
7860
 
 
7861
/**
 
7862
  maximum possible display length for blob.
 
7863
 
 
7864
  @return
 
7865
    length
 
7866
*/
 
7867
 
 
7868
uint32 Field_blob::max_display_length()
 
7869
{
 
7870
  switch (packlength)
 
7871
  {
 
7872
  case 1:
 
7873
    return 255 * field_charset->mbmaxlen;
 
7874
  case 2:
 
7875
    return 65535 * field_charset->mbmaxlen;
 
7876
  case 3:
 
7877
    return 16777215 * field_charset->mbmaxlen;
 
7878
  case 4:
 
7879
    return (uint32) 4294967295U;
 
7880
  default:
 
7881
    DBUG_ASSERT(0); // we should never go here
 
7882
    return 0;
 
7883
  }
 
7884
}
 
7885
 
 
7886
 
2198
7887
/*****************************************************************************
2199
7888
 Warning handling
2200
7889
*****************************************************************************/
2220
7909
*/
2221
7910
 
2222
7911
bool 
2223
 
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
 
7912
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
2224
7913
                   int cuted_increment)
2225
7914
{
2226
7915
  /*
2235
7924
                        thd->row_count);
2236
7925
    return 0;
2237
7926
  }
2238
 
  return level >= DRIZZLE_ERROR::WARN_LEVEL_WARN;
 
7927
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
2239
7928
}
2240
7929
 
2241
7930
 
2256
7945
*/
2257
7946
 
2258
7947
void 
2259
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
2260
 
                            unsigned int code, 
2261
 
                            const char *str, uint32_t str_length, 
2262
 
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
 
7948
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
7949
                            const char *str, uint str_length, 
 
7950
                            timestamp_type ts_type, int cuted_increment)
2263
7951
{
2264
7952
  THD *thd= table ? table->in_use : current_thd;
2265
7953
  if ((thd->really_abort_on_warning() &&
2266
 
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
 
7954
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
2267
7955
      set_warning(level, code, cuted_increment))
2268
7956
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
2269
7957
                                 field_name);
2286
7974
*/
2287
7975
 
2288
7976
void 
2289
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code, 
2290
 
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
 
7977
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
7978
                            longlong nr, timestamp_type ts_type,
2291
7979
                            int cuted_increment)
2292
7980
{
2293
7981
  THD *thd= table ? table->in_use : current_thd;
2295
7983
      set_warning(level, code, cuted_increment))
2296
7984
  {
2297
7985
    char str_nr[22];
2298
 
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
2299
 
    make_truncated_value_warning(thd, level, str_nr, (uint32_t) (str_end - str_nr), 
 
7986
    char *str_end= longlong10_to_str(nr, str_nr, -10);
 
7987
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
2300
7988
                                 ts_type, field_name);
2301
7989
  }
2302
7990
}
2317
8005
*/
2318
8006
 
2319
8007
void 
2320
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code, 
2321
 
                            double nr, enum enum_drizzle_timestamp_type ts_type)
 
8008
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
8009
                            double nr, timestamp_type ts_type)
2322
8010
{
2323
8011
  THD *thd= table ? table->in_use : current_thd;
2324
8012
  if (thd->really_abort_on_warning() ||
2326
8014
  {
2327
8015
    /* DBL_DIG is enough to print '-[digits].E+###' */
2328
8016
    char str_nr[DBL_DIG + 8];
2329
 
    uint32_t str_len= sprintf(str_nr, "%g", nr);
 
8017
    uint str_len= my_sprintf(str_nr, (str_nr, "%g", nr));
2330
8018
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
2331
8019
                                 field_name);
2332
8020
  }