~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/field.cc

  • Committer: Monty Taylor
  • Date: 2008-07-02 14:35:48 UTC
  • mto: This revision was merged to the branch mainline in revision 51.
  • Revision ID: monty@inaugust.com-20080702143548-onj30ry0sugr01uw
Removed all references to THREAD.

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>
 
32
#ifdef HAVE_FCONVERT
 
33
#include <floatingpoint.h>
 
34
#endif
27
35
 
28
36
// Maximum allowed exponent value for converting string to decimal
29
37
#define MAX_EXPONENT 1024
37
45
template class List_iterator<Create_field>;
38
46
#endif
39
47
 
 
48
uchar Field_null::null[1]={1};
 
49
const char field_separator=',';
 
50
 
 
51
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
 
52
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
 
53
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
 
54
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
 
55
((ulong) ((LL(1) << min(arg, 4) * 8) - LL(1)))
 
56
 
 
57
#define ASSERT_COLUMN_MARKED_FOR_READ DBUG_ASSERT(!table || (!table->read_set || bitmap_is_set(table->read_set, field_index)))
 
58
#define ASSERT_COLUMN_MARKED_FOR_WRITE DBUG_ASSERT(!table || (!table->write_set || bitmap_is_set(table->write_set, field_index)))
40
59
 
41
60
/*
42
61
  Rules for merging different types of fields in UNION
45
64
  following #defines describe that gap and how to canculate number of fields
46
65
  and index of field in thia array.
47
66
*/
48
 
#define FIELDTYPE_TEAR_FROM (DRIZZLE_TYPE_VARCHAR + 1)
49
 
#define FIELDTYPE_TEAR_TO   (DRIZZLE_TYPE_VIRTUAL - 1)
 
67
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_BIT + 1)
 
68
#define FIELDTYPE_TEAR_TO   (MYSQL_TYPE_NEWDECIMAL - 1)
50
69
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
51
70
inline int field_type2index (enum_field_types field_type)
52
71
{
58
77
 
59
78
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
60
79
{
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_VIRTUAL
80
 
    DRIZZLE_TYPE_VIRTUAL,
81
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
82
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
83
 
  //DRIZZLE_TYPE_BLOB
84
 
    DRIZZLE_TYPE_BLOB,
85
 
  },
86
 
  /* DRIZZLE_TYPE_TINY -> */
87
 
  {
88
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
89
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
90
 
  //DRIZZLE_TYPE_LONG
91
 
    DRIZZLE_TYPE_LONG,
92
 
  //DRIZZLE_TYPE_DOUBLE
93
 
    DRIZZLE_TYPE_DOUBLE,
94
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
95
 
    DRIZZLE_TYPE_TINY,        DRIZZLE_TYPE_VARCHAR,
96
 
  //DRIZZLE_TYPE_LONGLONG
97
 
    DRIZZLE_TYPE_LONGLONG,
98
 
  //DRIZZLE_TYPE_TIME
99
 
    DRIZZLE_TYPE_VARCHAR,
100
 
  //DRIZZLE_TYPE_DATETIME
101
 
    DRIZZLE_TYPE_VARCHAR,
102
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
103
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
104
 
  // DRIZZLE_TYPE_VIRTUAL
105
 
    DRIZZLE_TYPE_VIRTUAL,
106
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
107
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
108
 
  //DRIZZLE_TYPE_BLOB
109
 
    DRIZZLE_TYPE_BLOB,
110
 
  },
111
 
  /* DRIZZLE_TYPE_LONG -> */
112
 
  {
113
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
114
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONG,
115
 
  //DRIZZLE_TYPE_LONG
116
 
    DRIZZLE_TYPE_LONG,
117
 
  //DRIZZLE_TYPE_DOUBLE
118
 
    DRIZZLE_TYPE_DOUBLE,
119
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
120
 
    DRIZZLE_TYPE_LONG,         DRIZZLE_TYPE_VARCHAR,
121
 
  //DRIZZLE_TYPE_LONGLONG
122
 
    DRIZZLE_TYPE_LONGLONG,
123
 
  //DRIZZLE_TYPE_TIME
124
 
    DRIZZLE_TYPE_VARCHAR,
125
 
  //DRIZZLE_TYPE_DATETIME
126
 
    DRIZZLE_TYPE_VARCHAR,
127
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
128
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
129
 
  // DRIZZLE_TYPE_VIRTUAL
130
 
    DRIZZLE_TYPE_VIRTUAL,
131
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
132
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
133
 
  //DRIZZLE_TYPE_BLOB
134
 
    DRIZZLE_TYPE_BLOB,
135
 
  },
136
 
  /* DRIZZLE_TYPE_DOUBLE -> */
137
 
  {
138
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
139
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_DOUBLE,
140
 
  //DRIZZLE_TYPE_LONG
141
 
    DRIZZLE_TYPE_DOUBLE,
142
 
  //DRIZZLE_TYPE_DOUBLE
143
 
    DRIZZLE_TYPE_DOUBLE,
144
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
145
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
146
 
  //DRIZZLE_TYPE_LONGLONG
147
 
    DRIZZLE_TYPE_DOUBLE,
148
 
  //DRIZZLE_TYPE_TIME
149
 
    DRIZZLE_TYPE_VARCHAR,
150
 
  //DRIZZLE_TYPE_DATETIME
151
 
    DRIZZLE_TYPE_VARCHAR,
152
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
153
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
154
 
  // DRIZZLE_TYPE_VIRTUAL
155
 
    DRIZZLE_TYPE_VIRTUAL,
156
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
157
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
158
 
  //DRIZZLE_TYPE_BLOB
159
 
    DRIZZLE_TYPE_BLOB,
160
 
  },
161
 
  /* DRIZZLE_TYPE_NULL -> */
162
 
  {
163
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
164
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
165
 
  //DRIZZLE_TYPE_LONG
166
 
    DRIZZLE_TYPE_LONG,
167
 
  //DRIZZLE_TYPE_DOUBLE
168
 
    DRIZZLE_TYPE_DOUBLE,
169
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
170
 
    DRIZZLE_TYPE_NULL,        DRIZZLE_TYPE_TIMESTAMP,
171
 
  //DRIZZLE_TYPE_LONGLONG
172
 
    DRIZZLE_TYPE_LONGLONG,
173
 
  //DRIZZLE_TYPE_TIME
174
 
    DRIZZLE_TYPE_TIME,
175
 
  //DRIZZLE_TYPE_DATETIME
176
 
    DRIZZLE_TYPE_DATETIME,
177
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
178
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
179
 
  // DRIZZLE_TYPE_VIRTUAL
180
 
    DRIZZLE_TYPE_VIRTUAL,
181
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
182
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_ENUM,
183
 
  //DRIZZLE_TYPE_BLOB
184
 
    DRIZZLE_TYPE_BLOB,
185
 
  },
186
 
  /* DRIZZLE_TYPE_TIMESTAMP -> */
187
 
  {
188
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
189
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
190
 
  //DRIZZLE_TYPE_LONG
191
 
    DRIZZLE_TYPE_VARCHAR,
192
 
  //DRIZZLE_TYPE_DOUBLE
193
 
    DRIZZLE_TYPE_VARCHAR,
194
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
195
 
    DRIZZLE_TYPE_TIMESTAMP,   DRIZZLE_TYPE_TIMESTAMP,
196
 
  //DRIZZLE_TYPE_LONGLONG
197
 
    DRIZZLE_TYPE_VARCHAR,
198
 
  //DRIZZLE_TYPE_TIME
199
 
    DRIZZLE_TYPE_DATETIME,
200
 
  //DRIZZLE_TYPE_DATETIME
201
 
    DRIZZLE_TYPE_DATETIME,
202
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
203
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
204
 
  // DRIZZLE_TYPE_VIRTUAL
205
 
    DRIZZLE_TYPE_VIRTUAL,
206
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
207
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
208
 
  //DRIZZLE_TYPE_BLOB
209
 
    DRIZZLE_TYPE_BLOB,
210
 
  },
211
 
  /* DRIZZLE_TYPE_LONGLONG -> */
212
 
  {
213
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
214
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONGLONG,
215
 
  //DRIZZLE_TYPE_LONG
216
 
    DRIZZLE_TYPE_LONGLONG,
217
 
  //DRIZZLE_TYPE_DOUBLE
218
 
    DRIZZLE_TYPE_DOUBLE,
219
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
220
 
    DRIZZLE_TYPE_LONGLONG,    DRIZZLE_TYPE_VARCHAR,
221
 
  //DRIZZLE_TYPE_LONGLONG
222
 
    DRIZZLE_TYPE_LONGLONG,
223
 
  //DRIZZLE_TYPE_TIME
224
 
    DRIZZLE_TYPE_VARCHAR,
225
 
  //DRIZZLE_TYPE_DATETIME
226
 
    DRIZZLE_TYPE_VARCHAR,
227
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
228
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
229
 
  // DRIZZLE_TYPE_VIRTUAL
230
 
    DRIZZLE_TYPE_VIRTUAL,
231
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
232
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
233
 
  //DRIZZLE_TYPE_BLOB
234
 
    DRIZZLE_TYPE_BLOB,
235
 
  },
236
 
  /* DRIZZLE_TYPE_TIME -> */
237
 
  {
238
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
239
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
240
 
  //DRIZZLE_TYPE_LONG
241
 
    DRIZZLE_TYPE_VARCHAR,
242
 
  //DRIZZLE_TYPE_DOUBLE
243
 
    DRIZZLE_TYPE_VARCHAR,
244
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
245
 
    DRIZZLE_TYPE_TIME,        DRIZZLE_TYPE_DATETIME,
246
 
  //DRIZZLE_TYPE_LONGLONG
247
 
    DRIZZLE_TYPE_VARCHAR,
248
 
  //DRIZZLE_TYPE_TIME
249
 
    DRIZZLE_TYPE_TIME,
250
 
  //DRIZZLE_TYPE_DATETIME
251
 
    DRIZZLE_TYPE_DATETIME,
252
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
253
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
254
 
  // DRIZZLE_TYPE_VIRTUAL
255
 
    DRIZZLE_TYPE_VIRTUAL,
256
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
257
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
258
 
  //DRIZZLE_TYPE_BLOB
259
 
    DRIZZLE_TYPE_BLOB,
260
 
  },
261
 
  /* DRIZZLE_TYPE_DATETIME -> */
262
 
  {
263
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
264
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
265
 
  //DRIZZLE_TYPE_LONG
266
 
    DRIZZLE_TYPE_VARCHAR,
267
 
  //DRIZZLE_TYPE_DOUBLE
268
 
    DRIZZLE_TYPE_VARCHAR,
269
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
270
 
    DRIZZLE_TYPE_DATETIME,    DRIZZLE_TYPE_DATETIME,
271
 
  //DRIZZLE_TYPE_LONGLONG
272
 
    DRIZZLE_TYPE_VARCHAR,
273
 
  //DRIZZLE_TYPE_TIME
274
 
    DRIZZLE_TYPE_DATETIME,
275
 
  //DRIZZLE_TYPE_DATETIME
276
 
    DRIZZLE_TYPE_DATETIME,
277
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
278
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
279
 
  // DRIZZLE_TYPE_VIRTUAL
280
 
    DRIZZLE_TYPE_VIRTUAL,
281
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
282
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
283
 
  //DRIZZLE_TYPE_BLOB
284
 
    DRIZZLE_TYPE_BLOB,
285
 
  },
286
 
  /* DRIZZLE_TYPE_NEWDATE -> */
287
 
  {
288
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
289
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
290
 
  //DRIZZLE_TYPE_LONG
291
 
    DRIZZLE_TYPE_VARCHAR,
292
 
  //DRIZZLE_TYPE_DOUBLE
293
 
    DRIZZLE_TYPE_VARCHAR,
294
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
295
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_DATETIME,
296
 
  //DRIZZLE_TYPE_LONGLONG
297
 
    DRIZZLE_TYPE_VARCHAR,
298
 
  //DRIZZLE_TYPE_TIME
299
 
    DRIZZLE_TYPE_DATETIME,
300
 
  //DRIZZLE_TYPE_DATETIME
301
 
    DRIZZLE_TYPE_DATETIME,
302
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
303
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
304
 
  // DRIZZLE_TYPE_VIRTUAL
305
 
    DRIZZLE_TYPE_VIRTUAL,
306
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
307
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
308
 
  //DRIZZLE_TYPE_BLOB
309
 
    DRIZZLE_TYPE_BLOB,
310
 
  },
311
 
  /* DRIZZLE_TYPE_VARCHAR -> */
312
 
  {
313
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
314
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
315
 
  //DRIZZLE_TYPE_LONG
316
 
    DRIZZLE_TYPE_VARCHAR,
317
 
  //DRIZZLE_TYPE_DOUBLE
318
 
    DRIZZLE_TYPE_VARCHAR,
319
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
320
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
321
 
  //DRIZZLE_TYPE_LONGLONG
322
 
    DRIZZLE_TYPE_VARCHAR,
323
 
  //DRIZZLE_TYPE_TIME
324
 
    DRIZZLE_TYPE_VARCHAR,
325
 
  //DRIZZLE_TYPE_DATETIME
326
 
    DRIZZLE_TYPE_VARCHAR,
327
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
328
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
329
 
  // DRIZZLE_TYPE_VIRTUAL
330
 
    DRIZZLE_TYPE_VIRTUAL,
331
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
332
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
333
 
  //DRIZZLE_TYPE_BLOB
334
 
    DRIZZLE_TYPE_BLOB,
335
 
  },
336
 
  /* DRIZZLE_TYPE_VIRTUAL -> */
337
 
  {
338
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
339
 
    DRIZZLE_TYPE_VIRTUAL,  DRIZZLE_TYPE_VIRTUAL,
340
 
  //DRIZZLE_TYPE_LONG
341
 
    DRIZZLE_TYPE_VIRTUAL,
342
 
  //DRIZZLE_TYPE_DOUBLE
343
 
    DRIZZLE_TYPE_VIRTUAL,
344
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
345
 
    DRIZZLE_TYPE_VIRTUAL,  DRIZZLE_TYPE_VIRTUAL,
346
 
  //DRIZZLE_TYPE_LONGLONG
347
 
    DRIZZLE_TYPE_VIRTUAL,
348
 
  //DRIZZLE_TYPE_TIME
349
 
    DRIZZLE_TYPE_VIRTUAL,
350
 
  //DRIZZLE_TYPE_DATETIME
351
 
    DRIZZLE_TYPE_VIRTUAL,
352
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
353
 
    DRIZZLE_TYPE_VIRTUAL,     DRIZZLE_TYPE_VIRTUAL,
354
 
  // DRIZZLE_TYPE_VIRTUAL
355
 
    DRIZZLE_TYPE_VIRTUAL,
356
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
357
 
    DRIZZLE_TYPE_VIRTUAL,  DRIZZLE_TYPE_VIRTUAL,
358
 
  //DRIZZLE_TYPE_BLOB
359
 
    DRIZZLE_TYPE_VIRTUAL,
360
 
  },
361
 
  /* DRIZZLE_TYPE_NEWDECIMAL -> */
362
 
  {
363
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
364
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
365
 
  //DRIZZLE_TYPE_LONG
366
 
    DRIZZLE_TYPE_NEWDECIMAL,
367
 
  //DRIZZLE_TYPE_DOUBLE
368
 
    DRIZZLE_TYPE_DOUBLE,
369
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
370
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
371
 
  //DRIZZLE_TYPE_LONGLONG
372
 
    DRIZZLE_TYPE_NEWDECIMAL,
373
 
  //DRIZZLE_TYPE_TIME
374
 
    DRIZZLE_TYPE_VARCHAR,
375
 
  //DRIZZLE_TYPE_DATETIME
376
 
    DRIZZLE_TYPE_VARCHAR,
377
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
378
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
379
 
  // DRIZZLE_TYPE_VIRTUAL
380
 
    DRIZZLE_TYPE_VIRTUAL,
381
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
382
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
383
 
  //DRIZZLE_TYPE_BLOB
384
 
    DRIZZLE_TYPE_BLOB,
385
 
  },
386
 
  /* DRIZZLE_TYPE_ENUM -> */
387
 
  {
388
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
389
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
390
 
  //DRIZZLE_TYPE_LONG
391
 
    DRIZZLE_TYPE_VARCHAR,
392
 
  //DRIZZLE_TYPE_DOUBLE
393
 
    DRIZZLE_TYPE_VARCHAR,
394
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
395
 
    DRIZZLE_TYPE_ENUM,        DRIZZLE_TYPE_VARCHAR,
396
 
  //DRIZZLE_TYPE_LONGLONG
397
 
    DRIZZLE_TYPE_VARCHAR,
398
 
  //DRIZZLE_TYPE_TIME
399
 
    DRIZZLE_TYPE_VARCHAR,
400
 
  //DRIZZLE_TYPE_DATETIME
401
 
    DRIZZLE_TYPE_VARCHAR,
402
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
403
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
404
 
  // DRIZZLE_TYPE_VIRTUAL
405
 
    DRIZZLE_TYPE_VIRTUAL,
406
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
407
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
408
 
  //DRIZZLE_TYPE_BLOB
409
 
    DRIZZLE_TYPE_BLOB,
410
 
  },
411
 
  /* DRIZZLE_TYPE_BLOB -> */
412
 
  {
413
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
414
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
415
 
  //DRIZZLE_TYPE_LONG
416
 
    DRIZZLE_TYPE_BLOB,
417
 
  //DRIZZLE_TYPE_DOUBLE
418
 
    DRIZZLE_TYPE_BLOB,
419
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
420
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
421
 
  //DRIZZLE_TYPE_LONGLONG
422
 
    DRIZZLE_TYPE_BLOB,
423
 
  //DRIZZLE_TYPE_TIME
424
 
    DRIZZLE_TYPE_BLOB,
425
 
  //DRIZZLE_TYPE_DATETIME
426
 
    DRIZZLE_TYPE_BLOB,
427
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
428
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
429
 
  // DRIZZLE_TYPE_VIRTUAL
430
 
    DRIZZLE_TYPE_VIRTUAL,
431
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
432
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
433
 
  //DRIZZLE_TYPE_BLOB
434
 
    DRIZZLE_TYPE_BLOB,
435
 
  },
 
80
  /* MYSQL_TYPE_DECIMAL -> */
 
81
  {
 
82
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
83
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
84
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
85
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
86
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
87
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
88
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
89
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
90
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
91
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
 
92
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
93
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
94
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
95
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
96
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
97
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
98
  //MYSQL_TYPE_BIT          <16>-<245>
 
99
    MYSQL_TYPE_VARCHAR,
 
100
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
101
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
102
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
103
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
104
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
105
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
106
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
107
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
108
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
109
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
110
  },
 
111
  /* MYSQL_TYPE_TINY -> */
 
112
  {
 
113
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
114
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
115
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
116
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
117
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
118
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
119
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
120
    MYSQL_TYPE_TINY,        MYSQL_TYPE_VARCHAR,
 
121
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
122
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
123
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
124
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
125
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
126
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY,
 
127
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
128
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
129
  //MYSQL_TYPE_BIT          <16>-<245>
 
130
    MYSQL_TYPE_VARCHAR,
 
131
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
132
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
133
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
134
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
135
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
136
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
137
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
138
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
139
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
140
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
141
  },
 
142
  /* MYSQL_TYPE_SHORT -> */
 
143
  {
 
144
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
145
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_SHORT,
 
146
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
147
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
148
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
149
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
150
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
151
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_VARCHAR,
 
152
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
153
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
154
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
155
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
156
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
157
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_SHORT,
 
158
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
159
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
160
  //MYSQL_TYPE_BIT          <16>-<245>
 
161
    MYSQL_TYPE_VARCHAR,
 
162
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
163
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
164
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
165
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
166
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
167
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
168
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
169
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
170
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
171
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
172
  },
 
173
  /* MYSQL_TYPE_LONG -> */
 
174
  {
 
175
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
176
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONG,
 
177
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
178
    MYSQL_TYPE_LONG,        MYSQL_TYPE_LONG,
 
179
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
180
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
181
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
182
    MYSQL_TYPE_LONG,         MYSQL_TYPE_VARCHAR,
 
183
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
184
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
185
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
186
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
187
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
188
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONG,
 
189
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
190
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
191
  //MYSQL_TYPE_BIT          <16>-<245>
 
192
    MYSQL_TYPE_VARCHAR,
 
193
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
194
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
195
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
196
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
197
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
198
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
199
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
200
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
201
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
202
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
203
  },
 
204
  /* MYSQL_TYPE_FLOAT -> */
 
205
  {
 
206
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
207
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_FLOAT,
 
208
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
209
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
210
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
211
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
212
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
213
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_VARCHAR,
 
214
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
215
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_INT24,
 
216
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
217
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
218
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
219
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_FLOAT,
 
220
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
221
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
222
  //MYSQL_TYPE_BIT          <16>-<245>
 
223
    MYSQL_TYPE_VARCHAR,
 
224
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
225
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
226
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
227
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
228
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
229
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
230
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
231
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
232
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
233
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
234
  },
 
235
  /* MYSQL_TYPE_DOUBLE -> */
 
236
  {
 
237
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
238
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
239
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
240
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
241
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
242
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
243
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
244
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
245
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
246
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_INT24,
 
247
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
248
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
249
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
250
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_DOUBLE,
 
251
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
252
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
253
  //MYSQL_TYPE_BIT          <16>-<245>
 
254
    MYSQL_TYPE_VARCHAR,
 
255
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
256
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
257
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
258
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
259
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
260
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
261
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
262
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
263
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
264
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
265
  },
 
266
  /* MYSQL_TYPE_NULL -> */
 
267
  {
 
268
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
269
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
270
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
271
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
272
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
273
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
274
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
275
    MYSQL_TYPE_NULL,        MYSQL_TYPE_TIMESTAMP,
 
276
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
277
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
278
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
279
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_TIME,
 
280
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
281
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_YEAR,
 
282
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
283
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
284
  //MYSQL_TYPE_BIT          <16>-<245>
 
285
    MYSQL_TYPE_BIT,
 
286
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
287
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_ENUM,
 
288
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
289
    MYSQL_TYPE_SET,         MYSQL_TYPE_TINY_BLOB,
 
290
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
291
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
292
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
293
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
294
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
295
    MYSQL_TYPE_STRING,      MYSQL_TYPE_GEOMETRY
 
296
  },
 
297
  /* MYSQL_TYPE_TIMESTAMP -> */
 
298
  {
 
299
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
300
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
301
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
302
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
303
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
304
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
305
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
306
    MYSQL_TYPE_TIMESTAMP,   MYSQL_TYPE_TIMESTAMP,
 
307
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
308
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
309
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
310
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
311
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
312
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
313
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
314
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
315
  //MYSQL_TYPE_BIT          <16>-<245>
 
316
    MYSQL_TYPE_VARCHAR,
 
317
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
318
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
319
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
320
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
321
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
322
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
323
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
324
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
325
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
326
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
327
  },
 
328
  /* MYSQL_TYPE_LONGLONG -> */
 
329
  {
 
330
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
331
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONGLONG,
 
332
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
333
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONGLONG,
 
334
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
335
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
336
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
337
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_VARCHAR,
 
338
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
339
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONG,
 
340
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
341
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
342
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
343
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONGLONG,
 
344
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
345
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
346
  //MYSQL_TYPE_BIT          <16>-<245>
 
347
    MYSQL_TYPE_VARCHAR,
 
348
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
349
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
350
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
351
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
352
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
353
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
354
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
355
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
356
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
357
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
358
  },
 
359
  /* MYSQL_TYPE_INT24 -> */
 
360
  {
 
361
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
362
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_INT24,
 
363
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
364
    MYSQL_TYPE_INT24,       MYSQL_TYPE_LONG,
 
365
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
366
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
367
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
368
    MYSQL_TYPE_INT24,       MYSQL_TYPE_VARCHAR,
 
369
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
370
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
371
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
372
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
373
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
374
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_INT24,
 
375
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
376
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
377
  //MYSQL_TYPE_BIT          <16>-<245>
 
378
    MYSQL_TYPE_VARCHAR,
 
379
  //MYSQL_TYPE_NEWDECIMAL    MYSQL_TYPE_ENUM
 
380
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
381
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
382
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
383
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
384
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
385
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
386
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
387
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
388
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
389
  },
 
390
  /* MYSQL_TYPE_DATE -> */
 
391
  {
 
392
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
393
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
394
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
395
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
396
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
397
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
398
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
399
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
400
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
401
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
402
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
403
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
404
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
405
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
406
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
407
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
408
  //MYSQL_TYPE_BIT          <16>-<245>
 
409
    MYSQL_TYPE_VARCHAR,
 
410
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
411
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
412
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
413
    MYSQL_TYPE_VARCHAR,  MYSQL_TYPE_TINY_BLOB,
 
414
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
415
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
416
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
417
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
418
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
419
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
420
  },
 
421
  /* MYSQL_TYPE_TIME -> */
 
422
  {
 
423
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
424
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
425
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
426
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
427
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
428
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
429
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
430
    MYSQL_TYPE_TIME,        MYSQL_TYPE_DATETIME,
 
431
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
432
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
433
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
434
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_TIME,
 
435
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
436
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
437
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
438
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
439
  //MYSQL_TYPE_BIT          <16>-<245>
 
440
    MYSQL_TYPE_VARCHAR,
 
441
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
442
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
443
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
444
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
445
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
446
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
447
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
448
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
449
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
450
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
451
  },
 
452
  /* MYSQL_TYPE_DATETIME -> */
 
453
  {
 
454
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
455
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
456
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
457
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
458
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
459
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
460
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
461
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
462
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
463
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
464
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
465
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
466
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
467
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
468
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
469
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
470
  //MYSQL_TYPE_BIT          <16>-<245>
 
471
    MYSQL_TYPE_VARCHAR,
 
472
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
473
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
474
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
475
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
476
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
477
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
478
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
479
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
480
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
481
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
482
  },
 
483
  /* MYSQL_TYPE_YEAR -> */
 
484
  {
 
485
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
486
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_TINY,
 
487
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
488
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
489
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
490
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
491
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
492
    MYSQL_TYPE_YEAR,        MYSQL_TYPE_VARCHAR,
 
493
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
494
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
495
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
496
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
497
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
498
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_YEAR,
 
499
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
500
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
501
  //MYSQL_TYPE_BIT          <16>-<245>
 
502
    MYSQL_TYPE_VARCHAR,
 
503
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
504
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
505
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
506
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
507
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
508
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
509
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
510
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
511
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
512
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
513
  },
 
514
  /* MYSQL_TYPE_NEWDATE -> */
 
515
  {
 
516
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
517
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
518
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
519
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
520
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
521
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
522
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
523
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
524
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
525
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
526
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
527
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
528
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
529
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
530
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
531
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
532
  //MYSQL_TYPE_BIT          <16>-<245>
 
533
    MYSQL_TYPE_VARCHAR,
 
534
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
535
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
536
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
537
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
538
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
539
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
540
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
541
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
542
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
543
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
544
  },
 
545
  /* MYSQL_TYPE_VARCHAR -> */
 
546
  {
 
547
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
548
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
549
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
550
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
551
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
552
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
553
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
554
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
555
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
556
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
557
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
558
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
559
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
560
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
561
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
562
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
563
  //MYSQL_TYPE_BIT          <16>-<245>
 
564
    MYSQL_TYPE_VARCHAR,
 
565
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
566
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
567
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
568
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
569
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
570
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
571
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
572
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
573
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
574
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR
 
575
  },
 
576
  /* MYSQL_TYPE_BIT -> */
 
577
  {
 
578
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
579
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
580
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
581
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
582
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
583
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
584
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
585
    MYSQL_TYPE_BIT,         MYSQL_TYPE_VARCHAR,
 
586
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
587
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
588
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
589
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
590
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
591
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
592
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
593
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
594
  //MYSQL_TYPE_BIT          <16>-<245>
 
595
    MYSQL_TYPE_BIT,
 
596
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
597
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
598
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
599
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
600
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
601
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
602
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
603
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
604
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
605
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
606
  },
 
607
  /* MYSQL_TYPE_NEWDECIMAL -> */
 
608
  {
 
609
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
610
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
611
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
612
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
613
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
614
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
615
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
616
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
617
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
618
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
619
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
620
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
621
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
622
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_NEWDECIMAL,
 
623
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
624
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
625
  //MYSQL_TYPE_BIT          <16>-<245>
 
626
    MYSQL_TYPE_VARCHAR,
 
627
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
628
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
629
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
630
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
631
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
632
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
633
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
634
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
635
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
636
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
637
  },
 
638
  /* MYSQL_TYPE_ENUM -> */
 
639
  {
 
640
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
641
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
642
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
643
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
644
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
645
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
646
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
647
    MYSQL_TYPE_ENUM,        MYSQL_TYPE_VARCHAR,
 
648
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
649
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
650
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
651
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
652
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
653
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
654
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
655
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
656
  //MYSQL_TYPE_BIT          <16>-<245>
 
657
    MYSQL_TYPE_VARCHAR,
 
658
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
659
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
660
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
661
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
662
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
663
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
664
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
665
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
666
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
667
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
668
  },
 
669
  /* MYSQL_TYPE_SET -> */
 
670
  {
 
671
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
672
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
673
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
674
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
675
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
676
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
677
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
678
    MYSQL_TYPE_SET,         MYSQL_TYPE_VARCHAR,
 
679
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
680
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
681
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
682
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
683
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
684
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
685
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
686
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
687
  //MYSQL_TYPE_BIT          <16>-<245>
 
688
    MYSQL_TYPE_VARCHAR,
 
689
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
690
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
691
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
692
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
693
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
694
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
695
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
696
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
697
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
698
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
 
699
  },
 
700
  /* MYSQL_TYPE_TINY_BLOB -> */
 
701
  {
 
702
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
703
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
704
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
705
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
706
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
707
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
708
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
709
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
710
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
711
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
712
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
713
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
714
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
715
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
716
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
717
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
718
  //MYSQL_TYPE_BIT          <16>-<245>
 
719
    MYSQL_TYPE_TINY_BLOB,
 
720
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
721
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
722
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
723
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
 
724
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
725
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
726
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
727
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_TINY_BLOB,
 
728
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
729
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB
 
730
  },
 
731
  /* MYSQL_TYPE_MEDIUM_BLOB -> */
 
732
  {
 
733
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
734
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
735
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
736
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
737
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
738
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
739
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
740
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
741
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
742
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
743
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
744
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
745
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
746
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
747
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
748
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
749
  //MYSQL_TYPE_BIT          <16>-<245>
 
750
    MYSQL_TYPE_MEDIUM_BLOB,
 
751
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
752
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
753
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
754
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
755
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
756
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
757
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
758
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
 
759
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
760
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB
 
761
  },
 
762
  /* MYSQL_TYPE_LONG_BLOB -> */
 
763
  {
 
764
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
765
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
766
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
767
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
768
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
769
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
770
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
771
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
772
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
773
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
774
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
775
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
776
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
777
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
778
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
779
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
780
  //MYSQL_TYPE_BIT          <16>-<245>
 
781
    MYSQL_TYPE_LONG_BLOB,
 
782
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
783
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
784
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
785
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
786
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
787
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
788
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
789
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
 
790
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
791
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB
 
792
  },
 
793
  /* MYSQL_TYPE_BLOB -> */
 
794
  {
 
795
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
796
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
797
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
798
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
799
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
800
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
801
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
802
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
803
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
804
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
805
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
806
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
807
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
808
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
809
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
810
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
811
  //MYSQL_TYPE_BIT          <16>-<245>
 
812
    MYSQL_TYPE_BLOB,
 
813
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
814
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
815
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
816
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
817
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
818
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
819
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
820
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
821
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
822
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB
 
823
  },
 
824
  /* MYSQL_TYPE_VAR_STRING -> */
 
825
  {
 
826
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
827
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
828
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
829
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
830
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
831
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
832
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
833
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
834
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
835
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
836
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
837
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
838
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
839
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
840
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
841
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
842
  //MYSQL_TYPE_BIT          <16>-<245>
 
843
    MYSQL_TYPE_VARCHAR,
 
844
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
845
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
846
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
847
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
848
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
849
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
850
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
851
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
852
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
853
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR
 
854
  },
 
855
  /* MYSQL_TYPE_STRING -> */
 
856
  {
 
857
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
858
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
859
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
860
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
861
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
862
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
863
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
864
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
865
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
866
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
867
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
868
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
869
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
870
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
871
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
872
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR,
 
873
  //MYSQL_TYPE_BIT          <16>-<245>
 
874
    MYSQL_TYPE_STRING,
 
875
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
876
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
877
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
878
    MYSQL_TYPE_STRING,      MYSQL_TYPE_TINY_BLOB,
 
879
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
880
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
881
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
882
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
883
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
884
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING
 
885
  },
 
886
  /* MYSQL_TYPE_GEOMETRY -> */
 
887
  {
 
888
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
889
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
890
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
891
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
892
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
893
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
894
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
895
    MYSQL_TYPE_GEOMETRY,    MYSQL_TYPE_VARCHAR,
 
896
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
897
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
898
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
899
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
900
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
901
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
902
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
903
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
904
  //MYSQL_TYPE_BIT          <16>-<245>
 
905
    MYSQL_TYPE_VARCHAR,
 
906
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
907
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
908
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
909
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
 
910
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
911
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
 
912
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
913
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
914
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
915
    MYSQL_TYPE_STRING,      MYSQL_TYPE_GEOMETRY
 
916
  }
436
917
};
437
918
 
438
919
/**
448
929
enum_field_types Field::field_type_merge(enum_field_types a,
449
930
                                         enum_field_types b)
450
931
{
451
 
  assert(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
452
 
  assert(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
 
932
  DBUG_ASSERT(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
 
933
  DBUG_ASSERT(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
453
934
  return field_types_merge_rules[field_type2index(a)]
454
935
                                [field_type2index(b)];
455
936
}
457
938
 
458
939
static Item_result field_types_result_type [FIELDTYPE_NUM]=
459
940
{
460
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
 
941
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
461
942
  DECIMAL_RESULT,           INT_RESULT,
462
 
  //DRIZZLE_TYPE_LONG
463
 
  INT_RESULT,
464
 
  //DRIZZLE_TYPE_DOUBLE
465
 
  REAL_RESULT,
466
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
467
 
  STRING_RESULT,            STRING_RESULT,
468
 
  //DRIZZLE_TYPE_LONGLONG
469
 
  INT_RESULT,
470
 
  //DRIZZLE_TYPE_TIME
471
 
  STRING_RESULT,
472
 
  //DRIZZLE_TYPE_DATETIME
473
 
  STRING_RESULT,
474
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
475
 
  STRING_RESULT,            STRING_RESULT,
476
 
  //DRIZZLE_TYPE_VIRTUAL
477
 
  STRING_RESULT,
478
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
 
943
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
944
  INT_RESULT,               INT_RESULT,
 
945
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
946
  REAL_RESULT,              REAL_RESULT,
 
947
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
948
  STRING_RESULT,            STRING_RESULT,
 
949
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
950
  INT_RESULT,               INT_RESULT,
 
951
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
952
  STRING_RESULT,            STRING_RESULT,
 
953
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
954
  STRING_RESULT,            INT_RESULT,
 
955
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
956
  STRING_RESULT,            STRING_RESULT,
 
957
  //MYSQL_TYPE_BIT          <16>-<245>
 
958
  STRING_RESULT,
 
959
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
479
960
  DECIMAL_RESULT,           STRING_RESULT,
480
 
  //DRIZZLE_TYPE_BLOB
481
 
  STRING_RESULT,
 
961
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
 
962
  STRING_RESULT,            STRING_RESULT,
 
963
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
 
964
  STRING_RESULT,            STRING_RESULT,
 
965
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
966
  STRING_RESULT,            STRING_RESULT,
 
967
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
 
968
  STRING_RESULT,            STRING_RESULT
482
969
};
483
970
 
484
971
 
494
981
    strend      String end
495
982
 
496
983
  RETURN
497
 
    false - If string does not have important data
498
 
    true  - If string has some important data
 
984
    FALSE - If string does not have important data
 
985
    TRUE  - If string has some important data
499
986
*/
500
987
 
501
 
bool
502
 
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
503
 
                       const char *strend)
 
988
static bool
 
989
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
504
990
{
505
991
  if (cs != &my_charset_bin)
506
992
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
519
1005
 
520
1006
Item_result Field::result_merge_type(enum_field_types field_type)
521
1007
{
522
 
  assert(field_type < FIELDTYPE_TEAR_FROM || field_type
 
1008
  DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM || field_type
523
1009
              > FIELDTYPE_TEAR_TO);
524
1010
  return field_types_result_type[field_type2index(field_type)];
525
1011
}
538
1024
  @param type  field type
539
1025
 
540
1026
  @retval
541
 
    true  Type can have a prefixed key
 
1027
    TRUE  Type can have a prefixed key
542
1028
  @retval
543
 
    false Type can not have a prefixed key
 
1029
    FALSE Type can not have a prefixed key
544
1030
*/
545
1031
 
546
1032
bool Field::type_can_have_key_part(enum enum_field_types type)
547
1033
{
548
1034
  switch (type) {
549
 
  case DRIZZLE_TYPE_VARCHAR:
550
 
  case DRIZZLE_TYPE_BLOB:
551
 
    return true;
 
1035
  case MYSQL_TYPE_VARCHAR:
 
1036
  case MYSQL_TYPE_TINY_BLOB:
 
1037
  case MYSQL_TYPE_MEDIUM_BLOB:
 
1038
  case MYSQL_TYPE_LONG_BLOB:
 
1039
  case MYSQL_TYPE_BLOB:
 
1040
  case MYSQL_TYPE_VAR_STRING:
 
1041
  case MYSQL_TYPE_STRING:
 
1042
    return TRUE;
552
1043
  default:
553
 
    return false;
554
 
  }
555
 
}
556
 
 
 
1044
    return FALSE;
 
1045
  }
 
1046
}
 
1047
 
 
1048
 
 
1049
/**
 
1050
  Numeric fields base class constructor.
 
1051
*/
 
1052
Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
1053
                     uchar null_bit_arg, utype unireg_check_arg,
 
1054
                     const char *field_name_arg,
 
1055
                     uint8 dec_arg, bool zero_arg, bool unsigned_arg)
 
1056
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1057
         unireg_check_arg, field_name_arg),
 
1058
  dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
 
1059
{
 
1060
  if (zerofill)
 
1061
    flags|=ZEROFILL_FLAG;
 
1062
  if (unsigned_flag)
 
1063
    flags|=UNSIGNED_FLAG;
 
1064
}
 
1065
 
 
1066
 
 
1067
void Field_num::prepend_zeros(String *value)
 
1068
{
 
1069
  int diff;
 
1070
  if ((diff= (int) (field_length - value->length())) > 0)
 
1071
  {
 
1072
    bmove_upp((uchar*) value->ptr()+field_length,
 
1073
              (uchar*) value->ptr()+value->length(),
 
1074
              value->length());
 
1075
    bfill((uchar*) value->ptr(),diff,'0');
 
1076
    value->length(field_length);
 
1077
    (void) value->c_ptr_quick();                // Avoid warnings in purify
 
1078
  }
 
1079
}
 
1080
 
 
1081
/**
 
1082
  Test if given number is a int.
 
1083
 
 
1084
  @todo
 
1085
    Make this multi-byte-character safe
 
1086
 
 
1087
  @param str            String to test
 
1088
  @param length        Length of 'str'
 
1089
  @param int_end        Pointer to char after last used digit
 
1090
  @param cs             Character set
 
1091
 
 
1092
  @note
 
1093
    This is called after one has called strntoull10rnd() function.
 
1094
 
 
1095
  @retval
 
1096
    0   OK
 
1097
  @retval
 
1098
    1   error: empty string or wrong integer.
 
1099
  @retval
 
1100
    2   error: garbage at the end of string.
 
1101
*/
 
1102
 
 
1103
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
 
1104
                         const char *int_end, int error)
 
1105
{
 
1106
  /* Test if we get an empty string or wrong integer */
 
1107
  if (str == int_end || error == MY_ERRNO_EDOM)
 
1108
  {
 
1109
    char buff[128];
 
1110
    String tmp(buff, (uint32) sizeof(buff), system_charset_info);
 
1111
    tmp.copy(str, length, system_charset_info);
 
1112
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
 
1113
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
 
1114
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
1115
                        "integer", tmp.c_ptr(), field_name,
 
1116
                        (ulong) table->in_use->row_count);
 
1117
    return 1;
 
1118
  }
 
1119
  /* Test if we have garbage at the end of the given string. */
 
1120
  if (test_if_important_data(cs, int_end, str + length))
 
1121
  {
 
1122
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
1123
    return 2;
 
1124
  }
 
1125
  return 0;
 
1126
}
 
1127
 
 
1128
 
 
1129
/*
 
1130
  Conver a string to an integer then check bounds.
 
1131
  
 
1132
  SYNOPSIS
 
1133
    Field_num::get_int
 
1134
    cs            Character set
 
1135
    from          String to convert
 
1136
    len           Length of the string
 
1137
    rnd           OUT longlong value
 
1138
    unsigned_max  max unsigned value
 
1139
    signed_min    min signed value
 
1140
    signed_max    max signed value
 
1141
 
 
1142
  DESCRIPTION
 
1143
    The function calls strntoull10rnd() to get an integer value then
 
1144
    check bounds and errors returned. In case of any error a warning
 
1145
    is raised.
 
1146
 
 
1147
  RETURN
 
1148
    0   ok
 
1149
    1   error
 
1150
*/
 
1151
 
 
1152
bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len,
 
1153
                        longlong *rnd, uint64_t unsigned_max, 
 
1154
                        longlong signed_min, longlong signed_max)
 
1155
{
 
1156
  char *end;
 
1157
  int error;
 
1158
  
 
1159
  *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
 
1160
                                            unsigned_flag, &end,
 
1161
                                            &error);
 
1162
  if (unsigned_flag)
 
1163
  {
 
1164
 
 
1165
    if ((((uint64_t) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max)) ||
 
1166
        error == MY_ERRNO_ERANGE)
 
1167
    {
 
1168
      goto out_of_range;
 
1169
    }
 
1170
  }
 
1171
  else
 
1172
  {
 
1173
    if (*rnd < signed_min)
 
1174
    {
 
1175
      *rnd= signed_min;
 
1176
      goto out_of_range;
 
1177
    }
 
1178
    else if (*rnd > signed_max)
 
1179
    {
 
1180
      *rnd= signed_max;
 
1181
      goto out_of_range;
 
1182
    }
 
1183
  }
 
1184
  if (table->in_use->count_cuted_fields &&
 
1185
      check_int(cs, from, len, end, error))
 
1186
    return 1;
 
1187
  return 0;
 
1188
 
 
1189
out_of_range:
 
1190
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1191
  return 1;
 
1192
}
557
1193
 
558
1194
/**
559
1195
  Process decimal library return codes and issue warnings for overflow and
571
1207
{
572
1208
  if (op_result == E_DEC_OVERFLOW)
573
1209
  {
574
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1210
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
575
1211
    return 1;
576
1212
  }
577
1213
  if (op_result == E_DEC_TRUNCATED)
578
1214
  {
579
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
1215
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
580
1216
    /* We return 0 here as this is not a critical issue */
581
1217
  }
582
1218
  return 0;
584
1220
 
585
1221
 
586
1222
#ifdef NOT_USED
587
 
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
 
1223
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
588
1224
{
589
1225
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
590
1226
 
644
1280
  This is used for printing bit_fields as numbers while debugging.
645
1281
*/
646
1282
 
647
 
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
 
1283
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
648
1284
{
649
 
  const CHARSET_INFO * const cs= &my_charset_bin;
650
 
  uint32_t length;
651
 
  int64_t value= val_int();
 
1285
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1286
  CHARSET_INFO *cs= &my_charset_bin;
 
1287
  uint length;
 
1288
  longlong value= val_int();
652
1289
 
653
1290
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
654
1291
    return 0;
655
 
  length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
 
1292
  length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
656
1293
                                                MY_INT64_NUM_DECIMAL_DIGITS,
657
1294
                                                unsigned_val ? 10 : -10,
658
1295
                                                value);
662
1299
 
663
1300
 
664
1301
/// This is used as a table name when the table structure is not set up
665
 
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
666
 
             unsigned char null_bit_arg,
 
1302
Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
 
1303
             uchar null_bit_arg,
667
1304
             utype unireg_check_arg, const char *field_name_arg)
668
1305
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
669
1306
   table(0), orig_table(0), table_name(0),
671
1308
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
672
1309
   part_of_sortkey(0), unireg_check(unireg_check_arg),
673
1310
   field_length(length_arg), null_bit(null_bit_arg), 
674
 
   is_created_from_null_item(false),
675
 
   vcol_info(NULL), is_stored(true)
 
1311
   is_created_from_null_item(FALSE)
676
1312
{
677
1313
  flags=null_ptr ? 0: NOT_NULL_FLAG;
678
1314
  comment.str= (char*) "";
681
1317
}
682
1318
 
683
1319
 
684
 
void Field::hash(uint32_t *nr, uint32_t *nr2)
 
1320
void Field::hash(ulong *nr, ulong *nr2)
685
1321
{
686
1322
  if (is_null())
687
1323
  {
689
1325
  }
690
1326
  else
691
1327
  {
692
 
    uint32_t len= pack_length();
693
 
    const CHARSET_INFO * const cs= charset();
 
1328
    uint len= pack_length();
 
1329
    CHARSET_INFO *cs= charset();
694
1330
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
695
1331
  }
696
1332
}
698
1334
size_t
699
1335
Field::do_last_null_byte() const
700
1336
{
701
 
  assert(null_ptr == NULL || null_ptr >= table->record[0]);
 
1337
  DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
702
1338
  if (null_ptr)
703
1339
    return (size_t) (null_ptr - table->record[0]) + 1;
704
1340
  return LAST_NULL_BYTE_UNDEF;
710
1346
  memcpy(ptr,ptr+row_offset,pack_length());
711
1347
  if (null_ptr)
712
1348
  {
713
 
    *null_ptr= (unsigned char) ((null_ptr[0] & (unsigned char) ~(uint32_t) null_bit) | (null_ptr[row_offset] & (unsigned char) null_bit));
 
1349
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | (null_ptr[row_offset] & (uchar) null_bit));
714
1350
  }
715
1351
}
716
1352
 
737
1373
   @retval 0 if this field's size is < the source field's size
738
1374
   @retval 1 if this field's size is >= the source field's size
739
1375
*/
740
 
int Field::compatible_field_size(uint32_t field_metadata)
 
1376
int Field::compatible_field_size(uint field_metadata)
741
1377
{
742
 
  uint32_t const source_size= pack_length_from_metadata(field_metadata);
743
 
  uint32_t const destination_size= row_pack_length();
 
1378
  uint const source_size= pack_length_from_metadata(field_metadata);
 
1379
  uint const destination_size= row_pack_length();
744
1380
  return (source_size <= destination_size);
745
1381
}
746
1382
 
747
1383
 
748
 
int Field::store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
 
1384
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
749
1385
                 enum_check_fields check_level)
750
1386
{
751
1387
  int res;
789
1425
   the data.
790
1426
 
791
1427
   @param low_byte_first
792
 
   @c true if integers should be stored little-endian, @c false if
 
1428
   @c TRUE if integers should be stored little-endian, @c FALSE if
793
1429
   native format should be used. Note that for little-endian machines,
794
1430
   the value of this flag is a moot point since the native format is
795
1431
   little-endian.
796
1432
*/
797
 
unsigned char *
798
 
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
 
1433
uchar *
 
1434
Field::pack(uchar *to, const uchar *from, uint max_length,
799
1435
            bool low_byte_first __attribute__((unused)))
800
1436
{
801
 
  uint32_t length= pack_length();
 
1437
  uint32 length= pack_length();
802
1438
  set_if_smaller(length, max_length);
803
1439
  memcpy(to, from, length);
804
1440
  return to+length;
834
1470
 
835
1471
   @return  New pointer into memory based on from + length of the data
836
1472
*/
837
 
const unsigned char *
838
 
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
 
1473
const uchar *
 
1474
Field::unpack(uchar* to, const uchar *from, uint param_data,
839
1475
              bool low_byte_first __attribute__((unused)))
840
1476
{
841
 
  uint32_t length=pack_length();
 
1477
  uint length=pack_length();
842
1478
  int from_type= 0;
843
1479
  /*
844
1480
    If from length is > 255, it has encoded data in the upper bits. Need
858
1494
    return from+length;
859
1495
  }
860
1496
 
861
 
  uint32_t len= (param_data && (param_data < length)) ?
 
1497
  uint len= (param_data && (param_data < length)) ?
862
1498
            param_data : length;
863
1499
 
864
1500
  memcpy(to, from, param_data > length ? length : len);
866
1502
}
867
1503
 
868
1504
 
869
 
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((unused)))
 
1505
my_decimal *Field::val_decimal(my_decimal *decimal)
870
1506
{
871
1507
  /* This never have to be called */
872
 
  assert(0);
 
1508
  DBUG_ASSERT(0);
873
1509
  return 0;
874
1510
}
875
1511
 
876
1512
 
 
1513
void Field_num::add_zerofill_and_unsigned(String &res) const
 
1514
{
 
1515
  if (unsigned_flag)
 
1516
    res.append(STRING_WITH_LEN(" unsigned"));
 
1517
  if (zerofill)
 
1518
    res.append(STRING_WITH_LEN(" zerofill"));
 
1519
}
 
1520
 
 
1521
 
877
1522
void Field::make_field(Send_field *field)
878
1523
{
879
1524
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
903
1548
 
904
1549
 
905
1550
/**
906
 
  Conversion from decimal to int64_t with checking overflow and
 
1551
  Conversion from decimal to longlong with checking overflow and
907
1552
  setting correct value (min/max) in case of overflow.
908
1553
 
909
1554
  @param val             value which have to be converted
913
1558
  @return
914
1559
    value converted from val
915
1560
*/
916
 
int64_t Field::convert_decimal2int64_t(const my_decimal *val,
917
 
                                         bool unsigned_flag __attribute__((unused)), int *err)
 
1561
longlong Field::convert_decimal2longlong(const my_decimal *val,
 
1562
                                         bool unsigned_flag, int *err)
918
1563
{
919
 
  int64_t i;
920
 
  if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
921
 
                                      ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
922
 
                                      val, false, &i)))
923
 
  {
924
 
    i= (val->sign() ? INT64_MIN : INT64_MAX);
 
1564
  longlong i;
 
1565
  if (unsigned_flag)
 
1566
  {
 
1567
    if (val->sign())
 
1568
    {
 
1569
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1570
      i= 0;
 
1571
      *err= 1;
 
1572
    }
 
1573
    else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
 
1574
                                           ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
 
1575
                                           val, TRUE, &i)))
 
1576
    {
 
1577
      i= ~(longlong) 0;
 
1578
      *err= 1;
 
1579
    }
 
1580
  }
 
1581
  else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
 
1582
                                         ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
 
1583
                                         val, FALSE, &i)))
 
1584
  {
 
1585
    i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
925
1586
    *err= 1;
926
1587
  }
927
1588
  return i;
928
1589
}
929
1590
 
930
 
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
931
 
{
932
 
  uint32_t store_length;
 
1591
 
 
1592
/**
 
1593
  Storing decimal in integer fields.
 
1594
 
 
1595
  @param val       value for storing
 
1596
 
 
1597
  @note
 
1598
    This method is used by all integer fields, real/decimal redefine it
 
1599
 
 
1600
  @retval
 
1601
    0     OK
 
1602
  @retval
 
1603
    !=0  error
 
1604
*/
 
1605
 
 
1606
int Field_num::store_decimal(const my_decimal *val)
 
1607
{
 
1608
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1609
  int err= 0;
 
1610
  longlong i= convert_decimal2longlong(val, unsigned_flag, &err);
 
1611
  return test(err | store(i, unsigned_flag));
 
1612
}
 
1613
 
 
1614
 
 
1615
/**
 
1616
  Return decimal value of integer field.
 
1617
 
 
1618
  @param decimal_value     buffer for storing decimal value
 
1619
 
 
1620
  @note
 
1621
    This method is used by all integer fields, real/decimal redefine it.
 
1622
    All longlong values fit in our decimal buffer which cal store 8*9=72
 
1623
    digits of integer number
 
1624
 
 
1625
  @return
 
1626
    pointer to decimal buffer with value of field
 
1627
*/
 
1628
 
 
1629
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
 
1630
{
 
1631
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1632
  DBUG_ASSERT(result_type() == INT_RESULT);
 
1633
  longlong nr= val_int();
 
1634
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
1635
  return decimal_value;
 
1636
}
 
1637
 
 
1638
 
 
1639
Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
1640
                     uchar null_bit_arg, utype unireg_check_arg,
 
1641
                     const char *field_name_arg, CHARSET_INFO *charset_arg)
 
1642
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1643
         unireg_check_arg, field_name_arg)
 
1644
{
 
1645
  field_charset= charset_arg;
 
1646
  if (charset_arg->state & MY_CS_BINSORT)
 
1647
    flags|=BINARY_FLAG;
 
1648
  field_derivation= DERIVATION_IMPLICIT;
 
1649
}
 
1650
 
 
1651
 
 
1652
void Field_num::make_field(Send_field *field)
 
1653
{
 
1654
  Field::make_field(field);
 
1655
  field->decimals= dec;
 
1656
}
 
1657
 
 
1658
/**
 
1659
  Decimal representation of Field_str.
 
1660
 
 
1661
  @param d         value for storing
 
1662
 
 
1663
  @note
 
1664
    Field_str is the base class for fields like Field_enum,
 
1665
    Field_date and some similar. Some dates use fraction and also
 
1666
    string value should be converted to floating point value according
 
1667
    our rules, so we use double to store value of decimal in string.
 
1668
 
 
1669
  @todo
 
1670
    use decimal2string?
 
1671
 
 
1672
  @retval
 
1673
    0     OK
 
1674
  @retval
 
1675
    !=0  error
 
1676
*/
 
1677
 
 
1678
int Field_str::store_decimal(const my_decimal *d)
 
1679
{
 
1680
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1681
  double val;
 
1682
  /* TODO: use decimal2string? */
 
1683
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
 
1684
                                            ~E_DEC_OVERFLOW, d, &val));
 
1685
  return err | store(val);
 
1686
}
 
1687
 
 
1688
 
 
1689
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
 
1690
{
 
1691
  ASSERT_COLUMN_MARKED_FOR_READ;
 
1692
  longlong nr= val_int();
 
1693
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
 
1694
  return decimal_value;
 
1695
}
 
1696
 
 
1697
 
 
1698
uint Field::fill_cache_field(CACHE_FIELD *copy)
 
1699
{
 
1700
  uint store_length;
933
1701
  copy->str=ptr;
934
1702
  copy->length=pack_length();
935
1703
  copy->blob_field=0;
940
1708
    copy->length-= table->s->blob_ptr_size;
941
1709
    return copy->length;
942
1710
  }
 
1711
  else if (!zero_pack() &&
 
1712
           (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
 
1713
            copy->length < 256))
 
1714
  {
 
1715
    copy->strip=1;                              /* Remove end space */
 
1716
    store_length= 2;
 
1717
  }
943
1718
  else
944
1719
  {
945
1720
    copy->strip=0;
949
1724
}
950
1725
 
951
1726
 
952
 
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1727
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
953
1728
{
954
1729
  char buff[40];
955
1730
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
956
1731
  if (!(res=val_str(&tmp)) ||
957
1732
      str_to_datetime_with_warn(res->ptr(), res->length(),
958
 
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
1733
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
959
1734
    return 1;
960
1735
  return 0;
961
1736
}
962
1737
 
963
 
bool Field::get_time(DRIZZLE_TIME *ltime)
 
1738
bool Field::get_time(MYSQL_TIME *ltime)
964
1739
{
965
1740
  char buff[40];
966
1741
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
977
1752
    Needs to be changed if/when we want to support different time formats.
978
1753
*/
979
1754
 
980
 
int Field::store_time(DRIZZLE_TIME *ltime,
981
 
                      enum enum_drizzle_timestamp_type type_arg __attribute__((unused)))
 
1755
int Field::store_time(MYSQL_TIME *ltime, timestamp_type type_arg)
982
1756
{
 
1757
  ASSERT_COLUMN_MARKED_FOR_WRITE;
983
1758
  char buff[MAX_DATE_STRING_REP_LENGTH];
984
 
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
 
1759
  uint length= (uint) my_TIME_to_str(ltime, buff);
985
1760
  return store(buff, length, &my_charset_bin);
986
1761
}
987
1762
 
988
1763
 
989
 
bool Field::optimize_range(uint32_t idx, uint32_t part)
 
1764
bool Field::optimize_range(uint idx, uint part)
990
1765
{
991
1766
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
992
1767
}
993
1768
 
994
1769
 
995
 
Field *Field::new_field(MEM_ROOT *root, Table *new_table,
 
1770
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
996
1771
                        bool keep_type __attribute__((unused)))
997
1772
{
998
1773
  Field *tmp;
1006
1781
  tmp->part_of_key.init(0);
1007
1782
  tmp->part_of_sortkey.init(0);
1008
1783
  tmp->unireg_check= Field::NONE;
1009
 
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
 
1784
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
 
1785
                ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1010
1786
  tmp->reset_fields();
1011
1787
  return tmp;
1012
1788
}
1013
1789
 
1014
1790
 
1015
 
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
1016
 
                            unsigned char *new_ptr, unsigned char *new_null_ptr,
1017
 
                            uint32_t new_null_bit)
 
1791
Field *Field::new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
1792
                            uchar *new_ptr, uchar *new_null_ptr,
 
1793
                            uint new_null_bit)
1018
1794
{
1019
1795
  Field *tmp;
1020
1796
  if ((tmp= new_field(root, new_table, table == new_table)))
1027
1803
}
1028
1804
 
1029
1805
 
1030
 
/* This is used to generate a field in Table from TABLE_SHARE */
 
1806
/* This is used to generate a field in TABLE from TABLE_SHARE */
1031
1807
 
1032
 
Field *Field::clone(MEM_ROOT *root, Table *new_table)
 
1808
Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
1033
1809
{
1034
1810
  Field *tmp;
1035
1811
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1043
1819
 
1044
1820
 
1045
1821
/****************************************************************************
 
1822
  Field_null, a field that always return NULL
 
1823
****************************************************************************/
 
1824
 
 
1825
void Field_null::sql_type(String &res) const
 
1826
{
 
1827
  res.set_ascii(STRING_WITH_LEN("null"));
 
1828
}
 
1829
 
 
1830
 
 
1831
/****************************************************************************
 
1832
  Functions for the Field_decimal class
 
1833
  This is an number stored as a pre-space (or pre-zero) string
 
1834
****************************************************************************/
 
1835
 
 
1836
int
 
1837
Field_decimal::reset(void)
 
1838
{
 
1839
  Field_decimal::store(STRING_WITH_LEN("0"),&my_charset_bin);
 
1840
  return 0;
 
1841
}
 
1842
 
 
1843
void Field_decimal::overflow(bool negative)
 
1844
{
 
1845
  uint len=field_length;
 
1846
  uchar *to=ptr, filler= '9';
 
1847
 
 
1848
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1849
  if (negative)
 
1850
  {
 
1851
    if (!unsigned_flag)
 
1852
    {
 
1853
      /* Put - sign as a first digit so we'll have -999..999 or 999..999 */
 
1854
      *to++ = '-';
 
1855
      len--;
 
1856
    }
 
1857
    else
 
1858
    {
 
1859
      filler= '0';                              // Fill up with 0
 
1860
      if (!zerofill)
 
1861
      {
 
1862
        /*
 
1863
          Handle unsigned integer without zerofill, in which case
 
1864
          the number should be of format '   0' or '   0.000'
 
1865
        */
 
1866
        uint whole_part=field_length- (dec ? dec+2 : 1);
 
1867
        // Fill with spaces up to the first digit
 
1868
        bfill(to, whole_part, ' ');
 
1869
        to+=  whole_part;
 
1870
        len-= whole_part;
 
1871
        // The main code will also handle the 0 before the decimal point
 
1872
      }
 
1873
    }
 
1874
  }
 
1875
  bfill(to, len, filler);
 
1876
  if (dec)
 
1877
    ptr[field_length-dec-1]='.';
 
1878
  return;
 
1879
}
 
1880
 
 
1881
 
 
1882
int Field_decimal::store(const char *from_arg, uint len, CHARSET_INFO *cs)
 
1883
{
 
1884
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
1885
  char buff[STRING_BUFFER_USUAL_SIZE];
 
1886
  String tmp(buff,sizeof(buff), &my_charset_bin);
 
1887
  const uchar *from= (uchar*) from_arg;
 
1888
 
 
1889
  /* Convert character set if the old one is multi uchar */
 
1890
  if (cs->mbmaxlen > 1)
 
1891
  { 
 
1892
    uint dummy_errors;
 
1893
    tmp.copy((char*) from, len, cs, &my_charset_bin, &dummy_errors);
 
1894
    from= (uchar*) tmp.ptr();
 
1895
    len=  tmp.length();
 
1896
  }
 
1897
 
 
1898
  const uchar *end= from+len;
 
1899
  /* The pointer where the field value starts (i.e., "where to write") */
 
1900
  uchar *to= ptr;
 
1901
  uint tmp_dec, tmp_uint;
 
1902
  /*
 
1903
    The sign of the number : will be 0 (means positive but sign not
 
1904
    specified), '+' or '-'
 
1905
  */
 
1906
  uchar sign_char=0;
 
1907
  /* The pointers where prezeros start and stop */
 
1908
  const uchar *pre_zeros_from, *pre_zeros_end;
 
1909
  /* The pointers where digits at the left of '.' start and stop */
 
1910
  const uchar *int_digits_from, *int_digits_end;
 
1911
  /* The pointers where digits at the right of '.' start and stop */
 
1912
  const uchar *frac_digits_from, *frac_digits_end;
 
1913
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
 
1914
  char expo_sign_char=0;
 
1915
  uint exponent=0;                                // value of the exponent
 
1916
  /*
 
1917
    Pointers used when digits move from the left of the '.' to the
 
1918
    right of the '.' (explained below)
 
1919
  */
 
1920
  const uchar *int_digits_tail_from= NULL;
 
1921
  /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
 
1922
  uint int_digits_added_zeros= 0;
 
1923
  /*
 
1924
    Pointer used when digits move from the right of the '.' to the left
 
1925
    of the '.'
 
1926
  */
 
1927
  const uchar *frac_digits_head_end= NULL;
 
1928
  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
 
1929
  uint frac_digits_added_zeros= 0;
 
1930
  uchar *pos, *tmp_left_pos, *tmp_right_pos;
 
1931
  /* Pointers that are used as limits (begin and end of the field buffer) */
 
1932
  uchar *left_wall, *right_wall;
 
1933
  uchar tmp_char;
 
1934
  /*
 
1935
    To remember if table->in_use->cuted_fields has already been incremented,
 
1936
    to do that only once
 
1937
  */
 
1938
  bool is_cuted_fields_incr=0;
 
1939
 
 
1940
  /*
 
1941
    There are three steps in this function :
 
1942
    - parse the input string
 
1943
    - modify the position of digits around the decimal dot '.' 
 
1944
      according to the exponent value (if specified)
 
1945
    - write the formatted number
 
1946
  */
 
1947
 
 
1948
  if ((tmp_dec=dec))
 
1949
    tmp_dec++;
 
1950
 
 
1951
  /* skip pre-space */
 
1952
  while (from != end && my_isspace(&my_charset_bin,*from))
 
1953
    from++;
 
1954
  if (from == end)
 
1955
  {
 
1956
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
1957
    is_cuted_fields_incr=1;
 
1958
  }
 
1959
  else if (*from == '+' || *from == '-')        // Found some sign ?
 
1960
  {
 
1961
    sign_char= *from++;
 
1962
    /*
 
1963
      We allow "+" for unsigned decimal unless defined different
 
1964
      Both options allowed as one may wish not to have "+" for unsigned numbers
 
1965
      because of data processing issues
 
1966
    */ 
 
1967
    if (unsigned_flag)  
 
1968
    { 
 
1969
      if (sign_char=='-')
 
1970
      {
 
1971
        Field_decimal::overflow(1);
 
1972
        return 1;
 
1973
      }
 
1974
      /* 
 
1975
         Defining this will not store "+" for unsigned decimal type even if
 
1976
         it is passed in numeric string. This will make some tests to fail
 
1977
      */         
 
1978
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
 
1979
      else 
 
1980
        sign_char=0;
 
1981
#endif  
 
1982
    }
 
1983
  }
 
1984
 
 
1985
  pre_zeros_from= from;
 
1986
  for (; from!=end && *from == '0'; from++) ;   // Read prezeros
 
1987
  pre_zeros_end=int_digits_from=from;      
 
1988
  /* Read non zero digits at the left of '.'*/
 
1989
  for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
 
1990
  int_digits_end=from;
 
1991
  if (from!=end && *from == '.')                // Some '.' ?
 
1992
    from++;
 
1993
  frac_digits_from= from;
 
1994
  /* Read digits at the right of '.' */
 
1995
  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
 
1996
  frac_digits_end=from;
 
1997
  // Some exponentiation symbol ?
 
1998
  if (from != end && (*from == 'e' || *from == 'E'))
 
1999
  {   
 
2000
    from++;
 
2001
    if (from != end && (*from == '+' || *from == '-'))  // Some exponent sign ?
 
2002
      expo_sign_char= *from++;
 
2003
    else
 
2004
      expo_sign_char= '+';
 
2005
    /*
 
2006
      Read digits of the exponent and compute its value.  We must care about
 
2007
      'exponent' overflow, because as unsigned arithmetic is "modulo", big 
 
2008
      exponents will become small (e.g. 1e4294967296 will become 1e0, and the 
 
2009
      field will finally contain 1 instead of its max possible value).
 
2010
    */
 
2011
    for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
 
2012
    {
 
2013
      exponent=10*exponent+(*from-'0');
 
2014
      if (exponent>MAX_EXPONENT)
 
2015
        break;
 
2016
    }
 
2017
  }
 
2018
  
 
2019
  /*
 
2020
    We only have to generate warnings if count_cuted_fields is set.
 
2021
    This is to avoid extra checks of the number when they are not needed.
 
2022
    Even if this flag is not set, it's OK to increment warnings, if
 
2023
    it makes the code easer to read.
 
2024
  */
 
2025
 
 
2026
  if (table->in_use->count_cuted_fields)
 
2027
  {
 
2028
    // Skip end spaces
 
2029
    for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
 
2030
    if (from != end)                     // If still something left, warn
 
2031
    {
 
2032
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
2033
      is_cuted_fields_incr=1;
 
2034
    }
 
2035
  }
 
2036
  
 
2037
  /*
 
2038
    Now "move" digits around the decimal dot according to the exponent value,
 
2039
    and add necessary zeros.
 
2040
    Examples :
 
2041
    - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
 
2042
    - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
 
2043
    between '.' and '1'
 
2044
    - 1234.5E-3 : '234' moves at the right of '.'
 
2045
    These moves are implemented with pointers which point at the begin
 
2046
    and end of each moved segment. Examples :
 
2047
    - 1234.5E-3 : before the code below is executed, the int_digits part is
 
2048
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
 
2049
    below, the int_digits part is from '1' to '1', the frac_digits_head
 
2050
    part is from '2' to '4', and the frac_digits part from '5' to '5'.
 
2051
    - 1234.5E3 : before the code below is executed, the int_digits part is
 
2052
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
 
2053
    below, the int_digits part is from '1' to '4', the int_digits_tail
 
2054
    part is from '5' to '5', the frac_digits part is empty, and
 
2055
    int_digits_added_zeros=2 (to make 1234500).
 
2056
  */
 
2057
  
 
2058
  /* 
 
2059
     Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
 
2060
     as int_digits_added_zeros<=exponent<4G and 
 
2061
     (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
 
2062
     (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
 
2063
  */
 
2064
 
 
2065
  if (!expo_sign_char)
 
2066
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
 
2067
  else if (expo_sign_char == '-') 
 
2068
  {
 
2069
    tmp_uint=min(exponent,(uint)(int_digits_end-int_digits_from));
 
2070
    frac_digits_added_zeros=exponent-tmp_uint;
 
2071
    int_digits_end -= tmp_uint;
 
2072
    frac_digits_head_end=int_digits_end+tmp_uint;
 
2073
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);     
 
2074
  }
 
2075
  else // (expo_sign_char=='+') 
 
2076
  {
 
2077
    tmp_uint=min(exponent,(uint)(frac_digits_end-frac_digits_from));
 
2078
    int_digits_added_zeros=exponent-tmp_uint;
 
2079
    int_digits_tail_from=frac_digits_from;
 
2080
    frac_digits_from=frac_digits_from+tmp_uint;
 
2081
    /*
 
2082
      We "eat" the heading zeros of the 
 
2083
      int_digits.int_digits_tail.int_digits_added_zeros concatenation
 
2084
      (for example 0.003e3 must become 3 and not 0003)
 
2085
    */
 
2086
    if (int_digits_from == int_digits_end) 
 
2087
    {
 
2088
      /*
 
2089
        There was nothing in the int_digits part, so continue
 
2090
        eating int_digits_tail zeros
 
2091
      */
 
2092
      for (; int_digits_tail_from != frac_digits_from &&
 
2093
             *int_digits_tail_from == '0'; int_digits_tail_from++) ;
 
2094
      if (int_digits_tail_from == frac_digits_from) 
 
2095
      {
 
2096
        // there were only zeros in int_digits_tail too
 
2097
        int_digits_added_zeros=0;
 
2098
      }
 
2099
    }
 
2100
    tmp_uint= (uint) (tmp_dec+(int_digits_end-int_digits_from)+
 
2101
               (uint)(frac_digits_from-int_digits_tail_from)+
 
2102
               int_digits_added_zeros);
 
2103
  }
 
2104
  
 
2105
  /*
 
2106
    Now write the formated number
 
2107
    
 
2108
    First the digits of the int_% parts.
 
2109
    Do we have enough room to write these digits ?
 
2110
    If the sign is defined and '-', we need one position for it
 
2111
  */
 
2112
 
 
2113
  if (field_length < tmp_uint + (int) (sign_char == '-')) 
 
2114
  {
 
2115
    // too big number, change to max or min number
 
2116
    Field_decimal::overflow(sign_char == '-');
 
2117
    return 1;
 
2118
  }
 
2119
 
 
2120
  /*
 
2121
    Tmp_left_pos is the position where the leftmost digit of
 
2122
    the int_% parts will be written
 
2123
  */
 
2124
  tmp_left_pos=pos=to+(uint)(field_length-tmp_uint);
 
2125
  
 
2126
  // Write all digits of the int_% parts
 
2127
  while (int_digits_from != int_digits_end)
 
2128
    *pos++ = *int_digits_from++ ;
 
2129
 
 
2130
  if (expo_sign_char == '+')
 
2131
  {    
 
2132
    while (int_digits_tail_from != frac_digits_from)
 
2133
      *pos++= *int_digits_tail_from++;
 
2134
    while (int_digits_added_zeros-- >0)
 
2135
      *pos++= '0';  
 
2136
  }
 
2137
  /*
 
2138
    Note the position where the rightmost digit of the int_% parts has been
 
2139
    written (this is to later check if the int_% parts contained nothing,
 
2140
    meaning an extra 0 is needed).
 
2141
  */
 
2142
  tmp_right_pos=pos;
 
2143
 
 
2144
  /*
 
2145
    Step back to the position of the leftmost digit of the int_% parts,
 
2146
    to write sign and fill with zeros or blanks or prezeros.
 
2147
  */
 
2148
  pos=tmp_left_pos-1;
 
2149
  if (zerofill)
 
2150
  {
 
2151
    left_wall=to-1;
 
2152
    while (pos > left_wall)                     // Fill with zeros
 
2153
      *pos--='0';
 
2154
  }
 
2155
  else
 
2156
  {
 
2157
    left_wall=to+(sign_char != 0)-1;
 
2158
    if (!expo_sign_char)        // If exponent was specified, ignore prezeros
 
2159
    {
 
2160
      for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
 
2161
           pre_zeros_from++)
 
2162
        *pos--= '0';
 
2163
    }
 
2164
    if (pos == tmp_right_pos-1)
 
2165
      *pos--= '0';              // no 0 has ever been written, so write one
 
2166
    left_wall= to-1;
 
2167
    if (sign_char && pos != left_wall)
 
2168
    {
 
2169
      /* Write sign if possible (it is if sign is '-') */
 
2170
      *pos--= sign_char;
 
2171
    }
 
2172
    while (pos != left_wall)
 
2173
      *pos--=' ';  //fill with blanks
 
2174
  }
 
2175
  
 
2176
  /*
 
2177
    Write digits of the frac_% parts ;
 
2178
    Depending on table->in_use->count_cutted_fields, we may also want
 
2179
    to know if some non-zero tail of these parts will
 
2180
    be truncated (for example, 0.002->0.00 will generate a warning,
 
2181
    while 0.000->0.00 will not)
 
2182
    (and 0E1000000000 will not, while 1E-1000000000 will)
 
2183
  */
 
2184
      
 
2185
  pos=to+(uint)(field_length-tmp_dec);  // Calculate post to '.'
 
2186
  right_wall=to+field_length;
 
2187
  if (pos != right_wall) 
 
2188
    *pos++='.';
 
2189
 
 
2190
  if (expo_sign_char == '-')
 
2191
  {
 
2192
    while (frac_digits_added_zeros-- > 0)
 
2193
    {
 
2194
      if (pos == right_wall) 
 
2195
      {
 
2196
        if (table->in_use->count_cuted_fields && !is_cuted_fields_incr) 
 
2197
          break; // Go on below to see if we lose non zero digits
 
2198
        return 0;
 
2199
      }
 
2200
      *pos++='0';
 
2201
    }
 
2202
    while (int_digits_end != frac_digits_head_end)
 
2203
    {
 
2204
      tmp_char= *int_digits_end++;
 
2205
      if (pos == right_wall)
 
2206
      {
 
2207
        if (tmp_char != '0')                    // Losing a non zero digit ?
 
2208
        {
 
2209
          if (!is_cuted_fields_incr)
 
2210
            set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
2211
                        WARN_DATA_TRUNCATED, 1);
 
2212
          return 0;
 
2213
        }
 
2214
        continue;
 
2215
      }
 
2216
      *pos++= tmp_char;
 
2217
    }
 
2218
  }
 
2219
 
 
2220
  for (;frac_digits_from!=frac_digits_end;) 
 
2221
  {
 
2222
    tmp_char= *frac_digits_from++;
 
2223
    if (pos == right_wall)
 
2224
    {
 
2225
      if (tmp_char != '0')                      // Losing a non zero digit ?
 
2226
      {
 
2227
        if (!is_cuted_fields_incr)
 
2228
        {
 
2229
          /*
 
2230
            This is a note, not a warning, as we don't want to abort
 
2231
            when we cut decimals in strict mode
 
2232
          */
 
2233
          set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
 
2234
        }
 
2235
        return 0;
 
2236
      }
 
2237
      continue;
 
2238
    }
 
2239
    *pos++= tmp_char;
 
2240
  }
 
2241
      
 
2242
  while (pos != right_wall)
 
2243
   *pos++='0';                  // Fill with zeros at right of '.'
 
2244
  return 0;
 
2245
}
 
2246
 
 
2247
 
 
2248
int Field_decimal::store(double nr)
 
2249
{
 
2250
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2251
  if (unsigned_flag && nr < 0)
 
2252
  {
 
2253
    overflow(1);
 
2254
    return 1;
 
2255
  }
 
2256
  
 
2257
  if (!isfinite(nr)) // Handle infinity as special case
 
2258
  {
 
2259
    overflow(nr < 0.0);
 
2260
    return 1;
 
2261
  }
 
2262
 
 
2263
  register uint i;
 
2264
  size_t length;
 
2265
  uchar fyllchar,*to;
 
2266
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
2267
 
 
2268
  fyllchar = zerofill ? (char) '0' : (char) ' ';
 
2269
  length= my_fcvt(nr, dec, buff, NULL);
 
2270
 
 
2271
  if (length > field_length)
 
2272
  {
 
2273
    overflow(nr < 0.0);
 
2274
    return 1;
 
2275
  }
 
2276
  else
 
2277
  {
 
2278
    to=ptr;
 
2279
    for (i=field_length-length ; i-- > 0 ;)
 
2280
      *to++ = fyllchar;
 
2281
    memcpy(to,buff,length);
 
2282
    return 0;
 
2283
  }
 
2284
}
 
2285
 
 
2286
 
 
2287
int Field_decimal::store(longlong nr, bool unsigned_val)
 
2288
{
 
2289
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2290
  char buff[22];
 
2291
  uint length, int_part;
 
2292
  char fyllchar;
 
2293
  uchar *to;
 
2294
 
 
2295
  if (nr < 0 && unsigned_flag && !unsigned_val)
 
2296
  {
 
2297
    overflow(1);
 
2298
    return 1;
 
2299
  }
 
2300
  length= (uint) (longlong10_to_str(nr,buff,unsigned_val ? 10 : -10) - buff);
 
2301
  int_part= field_length- (dec  ? dec+1 : 0);
 
2302
 
 
2303
  if (length > int_part)
 
2304
  {
 
2305
    overflow(!unsigned_val && nr < 0L);         /* purecov: inspected */
 
2306
    return 1;
 
2307
  }
 
2308
 
 
2309
  fyllchar = zerofill ? (char) '0' : (char) ' ';
 
2310
  to= ptr;
 
2311
  for (uint i=int_part-length ; i-- > 0 ;)
 
2312
    *to++ = fyllchar;
 
2313
  memcpy(to,buff,length);
 
2314
  if (dec)
 
2315
  {
 
2316
    to[length]='.';
 
2317
    bfill(to+length+1,dec,'0');
 
2318
  }
 
2319
  return 0;
 
2320
}
 
2321
 
 
2322
 
 
2323
double Field_decimal::val_real(void)
 
2324
{
 
2325
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2326
  int not_used;
 
2327
  char *end_not_used;
 
2328
  return my_strntod(&my_charset_bin, (char*) ptr, field_length, &end_not_used,
 
2329
                    &not_used);
 
2330
}
 
2331
 
 
2332
longlong Field_decimal::val_int(void)
 
2333
{
 
2334
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2335
  int not_used;
 
2336
  if (unsigned_flag)
 
2337
    return my_strntoull(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
 
2338
                        &not_used);
 
2339
  return my_strntoll(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
 
2340
                     &not_used);
 
2341
}
 
2342
 
 
2343
 
 
2344
String *Field_decimal::val_str(String *val_buffer __attribute__((unused)),
 
2345
                               String *val_ptr)
 
2346
{
 
2347
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2348
  uchar *str;
 
2349
  size_t tmp_length;
 
2350
 
 
2351
  for (str=ptr ; *str == ' ' ; str++) ;
 
2352
  val_ptr->set_charset(&my_charset_bin);
 
2353
  tmp_length= (size_t) (str-ptr);
 
2354
  if (field_length < tmp_length)                // Error in data
 
2355
    val_ptr->length(0);
 
2356
  else
 
2357
    val_ptr->set_ascii((const char*) str, field_length-tmp_length);
 
2358
  return val_ptr;
 
2359
}
 
2360
 
 
2361
/**
 
2362
  Should be able to handle at least the following fixed decimal formats:
 
2363
  5.00 , -1.0,  05,  -05, +5 with optional pre/end space
 
2364
*/
 
2365
 
 
2366
int Field_decimal::cmp(const uchar *a_ptr,const uchar *b_ptr)
 
2367
{
 
2368
  const uchar *end;
 
2369
  int swap=0;
 
2370
  /* First remove prefixes '0', ' ', and '-' */
 
2371
  for (end=a_ptr+field_length;
 
2372
       a_ptr != end &&
 
2373
         (*a_ptr == *b_ptr ||
 
2374
          ((my_isspace(&my_charset_bin,*a_ptr)  || *a_ptr == '+' || 
 
2375
            *a_ptr == '0') &&
 
2376
           (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' || 
 
2377
            *b_ptr == '0')));
 
2378
       a_ptr++,b_ptr++)
 
2379
  {
 
2380
    if (*a_ptr == '-')                          // If both numbers are negative
 
2381
      swap= -1 ^ 1;                             // Swap result      
 
2382
  }
 
2383
  if (a_ptr == end)
 
2384
    return 0;
 
2385
  if (*a_ptr == '-')
 
2386
    return -1;
 
2387
  if (*b_ptr == '-')
 
2388
    return 1;
 
2389
 
 
2390
  while (a_ptr != end)
 
2391
  {
 
2392
    if (*a_ptr++ != *b_ptr++)
 
2393
      return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1); // compare digits
 
2394
  }
 
2395
  return 0;
 
2396
}
 
2397
 
 
2398
 
 
2399
void Field_decimal::sort_string(uchar *to,uint length)
 
2400
{
 
2401
  uchar *str,*end;
 
2402
  for (str=ptr,end=ptr+length;
 
2403
       str != end &&
 
2404
         ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
 
2405
           *str == '0')) ;
 
2406
       str++)
 
2407
    *to++=' ';
 
2408
  if (str == end)
 
2409
    return;                                     /* purecov: inspected */
 
2410
 
 
2411
  if (*str == '-')
 
2412
  {
 
2413
    *to++=1;                                    // Smaller than any number
 
2414
    str++;
 
2415
    while (str != end)
 
2416
      if (my_isdigit(&my_charset_bin,*str))
 
2417
        *to++= (char) ('9' - *str++);
 
2418
      else
 
2419
        *to++= *str++;
 
2420
  }
 
2421
  else memcpy(to,str,(uint) (end-str));
 
2422
}
 
2423
 
 
2424
 
 
2425
void Field_decimal::sql_type(String &res) const
 
2426
{
 
2427
  CHARSET_INFO *cs=res.charset();
 
2428
  uint tmp=field_length;
 
2429
  if (!unsigned_flag)
 
2430
    tmp--;
 
2431
  if (dec)
 
2432
    tmp--;
 
2433
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2434
                          "decimal(%d,%d)",tmp,dec));
 
2435
  add_zerofill_and_unsigned(res);
 
2436
}
 
2437
 
 
2438
 
 
2439
/****************************************************************************
 
2440
** Field_new_decimal
 
2441
****************************************************************************/
 
2442
 
 
2443
Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
 
2444
                                     uint32 len_arg, uchar *null_ptr_arg,
 
2445
                                     uchar null_bit_arg,
 
2446
                                     enum utype unireg_check_arg,
 
2447
                                     const char *field_name_arg,
 
2448
                                     uint8 dec_arg,bool zero_arg,
 
2449
                                     bool unsigned_arg)
 
2450
  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
2451
             unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
 
2452
{
 
2453
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
2454
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
 
2455
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
 
2456
              (dec <= DECIMAL_MAX_SCALE));
 
2457
  bin_size= my_decimal_get_binary_size(precision, dec);
 
2458
}
 
2459
 
 
2460
 
 
2461
Field_new_decimal::Field_new_decimal(uint32 len_arg,
 
2462
                                     bool maybe_null_arg,
 
2463
                                     const char *name,
 
2464
                                     uint8 dec_arg,
 
2465
                                     bool unsigned_arg)
 
2466
  :Field_num((uchar*) 0, len_arg,
 
2467
             maybe_null_arg ? (uchar*) "": 0, 0,
 
2468
             NONE, name, dec_arg, 0, unsigned_arg)
 
2469
{
 
2470
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
2471
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
 
2472
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
 
2473
              (dec <= DECIMAL_MAX_SCALE));
 
2474
  bin_size= my_decimal_get_binary_size(precision, dec);
 
2475
}
 
2476
 
 
2477
 
 
2478
int Field_new_decimal::reset(void)
 
2479
{
 
2480
  store_value(&decimal_zero);
 
2481
  return 0;
 
2482
}
 
2483
 
 
2484
 
 
2485
/**
 
2486
  Generate max/min decimal value in case of overflow.
 
2487
 
 
2488
  @param decimal_value     buffer for value
 
2489
  @param sign              sign of value which caused overflow
 
2490
*/
 
2491
 
 
2492
void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
 
2493
                                              bool sign)
 
2494
{
 
2495
  DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
 
2496
  max_my_decimal(decimal_value, precision, decimals());
 
2497
  if (sign)
 
2498
  {
 
2499
    if (unsigned_flag)
 
2500
      my_decimal_set_zero(decimal_value);
 
2501
    else
 
2502
      decimal_value->sign(TRUE);
 
2503
  }
 
2504
  DBUG_VOID_RETURN;
 
2505
}
 
2506
 
 
2507
 
 
2508
/**
 
2509
  Store decimal value in the binary buffer.
 
2510
 
 
2511
  Checks if decimal_value fits into field size.
 
2512
  If it does, stores the decimal in the buffer using binary format.
 
2513
  Otherwise sets maximal number that can be stored in the field.
 
2514
 
 
2515
  @param decimal_value   my_decimal
 
2516
 
 
2517
  @retval
 
2518
    0 ok
 
2519
  @retval
 
2520
    1 error
 
2521
*/
 
2522
 
 
2523
bool Field_new_decimal::store_value(const my_decimal *decimal_value)
 
2524
{
 
2525
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2526
  int error= 0;
 
2527
  DBUG_ENTER("Field_new_decimal::store_value");
 
2528
#ifndef DBUG_OFF
 
2529
  {
 
2530
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
 
2531
    DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
 
2532
  }
 
2533
#endif
 
2534
 
 
2535
  /* check that we do not try to write negative value in unsigned field */
 
2536
  if (unsigned_flag && decimal_value->sign())
 
2537
  {
 
2538
    DBUG_PRINT("info", ("unsigned overflow"));
 
2539
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2540
    error= 1;
 
2541
    decimal_value= &decimal_zero;
 
2542
  }
 
2543
#ifndef DBUG_OFF
 
2544
  {
 
2545
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
 
2546
    DBUG_PRINT("info", ("saving with precision %d  scale: %d  value %s",
 
2547
                        (int)precision, (int)dec,
 
2548
                        dbug_decimal_as_string(dbug_buff, decimal_value)));
 
2549
  }
 
2550
#endif
 
2551
 
 
2552
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
2553
                                         decimal_value, ptr, precision, dec)))
 
2554
  {
 
2555
    my_decimal buff;
 
2556
    DBUG_PRINT("info", ("overflow"));
 
2557
    set_value_on_overflow(&buff, decimal_value->sign());
 
2558
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
 
2559
    error= 1;
 
2560
  }
 
2561
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
 
2562
                                          bin_size););
 
2563
  DBUG_RETURN(error);
 
2564
}
 
2565
 
 
2566
 
 
2567
int Field_new_decimal::store(const char *from, uint length,
 
2568
                             CHARSET_INFO *charset_arg)
 
2569
{
 
2570
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2571
  int err;
 
2572
  my_decimal decimal_value;
 
2573
  DBUG_ENTER("Field_new_decimal::store(char*)");
 
2574
 
 
2575
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
 
2576
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
 
2577
                           from, length, charset_arg,
 
2578
                           &decimal_value)) &&
 
2579
      table->in_use->abort_on_warning)
 
2580
  {
 
2581
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
 
2582
    String from_as_str;
 
2583
    from_as_str.copy(from, length, &my_charset_bin);
 
2584
 
 
2585
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_ERROR,
 
2586
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
2587
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
2588
                        "decimal", from_as_str.c_ptr(), field_name,
 
2589
                        (ulong) table->in_use->row_count);
 
2590
 
 
2591
    DBUG_RETURN(err);
 
2592
  }
 
2593
 
 
2594
  switch (err) {
 
2595
  case E_DEC_TRUNCATED:
 
2596
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
 
2597
    break;
 
2598
  case E_DEC_OVERFLOW:
 
2599
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2600
    set_value_on_overflow(&decimal_value, decimal_value.sign());
 
2601
    break;
 
2602
  case E_DEC_BAD_NUM:
 
2603
    {
 
2604
      /* Because "from" is not NUL-terminated and we use %s in the ER() */
 
2605
      String from_as_str;
 
2606
      from_as_str.copy(from, length, &my_charset_bin);
 
2607
 
 
2608
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
 
2609
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
2610
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
2611
                          "decimal", from_as_str.c_ptr(), field_name,
 
2612
                        (ulong) table->in_use->row_count);
 
2613
    my_decimal_set_zero(&decimal_value);
 
2614
 
 
2615
    break;
 
2616
    }
 
2617
  }
 
2618
 
 
2619
#ifndef DBUG_OFF
 
2620
  char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
 
2621
  DBUG_PRINT("enter", ("value: %s",
 
2622
                       dbug_decimal_as_string(dbug_buff, &decimal_value)));
 
2623
#endif
 
2624
  store_value(&decimal_value);
 
2625
  DBUG_RETURN(err);
 
2626
}
 
2627
 
 
2628
 
 
2629
/**
 
2630
  @todo
 
2631
  Fix following when double2my_decimal when double2decimal
 
2632
  will return E_DEC_TRUNCATED always correctly
 
2633
*/
 
2634
 
 
2635
int Field_new_decimal::store(double nr)
 
2636
{
 
2637
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2638
  my_decimal decimal_value;
 
2639
  int err;
 
2640
  DBUG_ENTER("Field_new_decimal::store(double)");
 
2641
 
 
2642
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
 
2643
                         &decimal_value);
 
2644
  if (err)
 
2645
  {
 
2646
    if (check_overflow(err))
 
2647
      set_value_on_overflow(&decimal_value, decimal_value.sign());
 
2648
    /* Only issue a warning if store_value doesn't issue an warning */
 
2649
    table->in_use->got_warning= 0;
 
2650
  }
 
2651
  if (store_value(&decimal_value))
 
2652
    err= 1;
 
2653
  else if (err && !table->in_use->got_warning)
 
2654
    err= warn_if_overflow(err);
 
2655
  DBUG_RETURN(err);
 
2656
}
 
2657
 
 
2658
 
 
2659
int Field_new_decimal::store(longlong nr, bool unsigned_val)
 
2660
{
 
2661
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2662
  my_decimal decimal_value;
 
2663
  int err;
 
2664
 
 
2665
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
2666
                           nr, unsigned_val, &decimal_value)))
 
2667
  {
 
2668
    if (check_overflow(err))
 
2669
      set_value_on_overflow(&decimal_value, decimal_value.sign());
 
2670
    /* Only issue a warning if store_value doesn't issue an warning */
 
2671
    table->in_use->got_warning= 0;
 
2672
  }
 
2673
  if (store_value(&decimal_value))
 
2674
    err= 1;
 
2675
  else if (err && !table->in_use->got_warning)
 
2676
    err= warn_if_overflow(err);
 
2677
  return err;
 
2678
}
 
2679
 
 
2680
 
 
2681
int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
 
2682
{
 
2683
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
2684
  return store_value(decimal_value);
 
2685
}
 
2686
 
 
2687
 
 
2688
int Field_new_decimal::store_time(MYSQL_TIME *ltime, timestamp_type t_type)
 
2689
{
 
2690
    my_decimal decimal_value;
 
2691
    return store_value(date2my_decimal(ltime, &decimal_value));
 
2692
}
 
2693
 
 
2694
 
 
2695
double Field_new_decimal::val_real(void)
 
2696
{
 
2697
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2698
  double dbl;
 
2699
  my_decimal decimal_value;
 
2700
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
2701
  return dbl;
 
2702
}
 
2703
 
 
2704
 
 
2705
longlong Field_new_decimal::val_int(void)
 
2706
{
 
2707
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2708
  longlong i;
 
2709
  my_decimal decimal_value;
 
2710
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
2711
                 unsigned_flag, &i);
 
2712
  return i;
 
2713
}
 
2714
 
 
2715
 
 
2716
my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
 
2717
{
 
2718
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2719
  DBUG_ENTER("Field_new_decimal::val_decimal");
 
2720
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
 
2721
                    precision, dec);
 
2722
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
 
2723
                                          bin_size););
 
2724
  DBUG_RETURN(decimal_value);
 
2725
}
 
2726
 
 
2727
 
 
2728
String *Field_new_decimal::val_str(String *val_buffer,
 
2729
                                   String *val_ptr __attribute__((unused)))
 
2730
{
 
2731
  ASSERT_COLUMN_MARKED_FOR_READ;
 
2732
  my_decimal decimal_value;
 
2733
  uint fixed_precision= zerofill ? precision : 0;
 
2734
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
2735
                    fixed_precision, dec, '0', val_buffer);
 
2736
  return val_buffer;
 
2737
}
 
2738
 
 
2739
 
 
2740
int Field_new_decimal::cmp(const uchar *a,const uchar*b)
 
2741
{
 
2742
  return memcmp(a, b, bin_size);
 
2743
}
 
2744
 
 
2745
 
 
2746
void Field_new_decimal::sort_string(uchar *buff,
 
2747
                                    uint length __attribute__((unused)))
 
2748
{
 
2749
  memcpy(buff, ptr, bin_size);
 
2750
}
 
2751
 
 
2752
 
 
2753
void Field_new_decimal::sql_type(String &str) const
 
2754
{
 
2755
  CHARSET_INFO *cs= str.charset();
 
2756
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
 
2757
                                "decimal(%d,%d)", precision, (int)dec));
 
2758
  add_zerofill_and_unsigned(str);
 
2759
}
 
2760
 
 
2761
 
 
2762
/**
 
2763
   Save the field metadata for new decimal fields.
 
2764
 
 
2765
   Saves the precision in the first byte and decimals() in the second
 
2766
   byte of the field metadata array at index of *metadata_ptr and 
 
2767
   *(metadata_ptr + 1).
 
2768
 
 
2769
   @param   metadata_ptr   First byte of field metadata
 
2770
 
 
2771
   @returns number of bytes written to metadata_ptr
 
2772
*/
 
2773
int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
 
2774
{
 
2775
  *metadata_ptr= precision;
 
2776
  *(metadata_ptr + 1)= decimals();
 
2777
  return 2;
 
2778
}
 
2779
 
 
2780
 
 
2781
/**
 
2782
   Returns the number of bytes field uses in row-based replication 
 
2783
   row packed size.
 
2784
 
 
2785
   This method is used in row-based replication to determine the number
 
2786
   of bytes that the field consumes in the row record format. This is
 
2787
   used to skip fields in the master that do not exist on the slave.
 
2788
 
 
2789
   @param   field_metadata   Encoded size in field metadata
 
2790
 
 
2791
   @returns The size of the field based on the field metadata.
 
2792
*/
 
2793
uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
 
2794
{
 
2795
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
 
2796
  uint const source_decimal= field_metadata & 0x00ff; 
 
2797
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
2798
                                                     source_decimal);
 
2799
  return (source_size);
 
2800
}
 
2801
 
 
2802
 
 
2803
/**
 
2804
   Check to see if field size is compatible with destination.
 
2805
 
 
2806
   This method is used in row-based replication to verify that the slave's
 
2807
   field size is less than or equal to the master's field size. The 
 
2808
   encoded field metadata (from the master or source) is decoded and compared
 
2809
   to the size of this field (the slave or destination). 
 
2810
 
 
2811
   @param   field_metadata   Encoded size in field metadata
 
2812
 
 
2813
   @retval 0 if this field's size is < the source field's size
 
2814
   @retval 1 if this field's size is >= the source field's size
 
2815
*/
 
2816
int Field_new_decimal::compatible_field_size(uint field_metadata)
 
2817
{
 
2818
  int compatible= 0;
 
2819
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
 
2820
  uint const source_decimal= field_metadata & 0x00ff; 
 
2821
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
2822
                                                     source_decimal);
 
2823
  uint const destination_size= row_pack_length();
 
2824
  compatible= (source_size <= destination_size);
 
2825
  if (compatible)
 
2826
    compatible= (source_precision <= precision) &&
 
2827
                (source_decimal <= decimals());
 
2828
  return (compatible);
 
2829
}
 
2830
 
 
2831
 
 
2832
uint Field_new_decimal::is_equal(Create_field *new_field)
 
2833
{
 
2834
  return ((new_field->sql_type == real_type()) &&
 
2835
          ((new_field->flags & UNSIGNED_FLAG) == 
 
2836
           (uint) (flags & UNSIGNED_FLAG)) &&
 
2837
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
 
2838
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
 
2839
          (new_field->length == max_display_length()) &&
 
2840
          (new_field->decimals == dec));
 
2841
}
 
2842
 
 
2843
 
 
2844
/**
 
2845
   Unpack a decimal field from row data.
 
2846
 
 
2847
   This method is used to unpack a decimal or numeric field from a master
 
2848
   whose size of the field is less than that of the slave.
 
2849
  
 
2850
   @param   to         Destination of the data
 
2851
   @param   from       Source of the data
 
2852
   @param   param_data Precision (upper) and decimal (lower) values
 
2853
 
 
2854
   @return  New pointer into memory based on from + length of the data
 
2855
*/
 
2856
const uchar *
 
2857
Field_new_decimal::unpack(uchar* to,
 
2858
                          const uchar *from,
 
2859
                          uint param_data,
 
2860
                          bool low_byte_first)
 
2861
{
 
2862
  if (param_data == 0)
 
2863
    return Field::unpack(to, from, param_data, low_byte_first);
 
2864
 
 
2865
  uint from_precision= (param_data & 0xff00) >> 8U;
 
2866
  uint from_decimal= param_data & 0x00ff;
 
2867
  uint length=pack_length();
 
2868
  uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
 
2869
  uint len= (param_data && (from_pack_len < length)) ?
 
2870
            from_pack_len : length;
 
2871
  if ((from_pack_len && (from_pack_len < length)) ||
 
2872
      (from_precision < precision) ||
 
2873
      (from_decimal < decimals()))
 
2874
  {
 
2875
    /*
 
2876
      If the master's data is smaller than the slave, we need to convert
 
2877
      the binary to decimal then resize the decimal converting it back to
 
2878
      a decimal and write that to the raw data buffer.
 
2879
    */
 
2880
    decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
 
2881
    decimal_t dec;
 
2882
    dec.len= from_precision;
 
2883
    dec.buf= dec_buf;
 
2884
    /*
 
2885
      Note: bin2decimal does not change the length of the field. So it is
 
2886
      just the first step the resizing operation. The second step does the
 
2887
      resizing using the precision and decimals from the slave.
 
2888
    */
 
2889
    bin2decimal((uchar *)from, &dec, from_precision, from_decimal);
 
2890
    decimal2bin(&dec, to, precision, decimals());
 
2891
  }
 
2892
  else
 
2893
    memcpy(to, from, len); // Sizes are the same, just copy the data.
 
2894
  return from+len;
 
2895
}
 
2896
 
 
2897
/****************************************************************************
1046
2898
** tiny int
1047
2899
****************************************************************************/
1048
2900
 
1049
 
int Field_tiny::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
2901
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
1050
2902
{
 
2903
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1051
2904
  int error;
1052
 
  int64_t rnd;
 
2905
  longlong rnd;
1053
2906
  
1054
2907
  error= get_int(cs, from, len, &rnd, 255, -128, 127);
1055
 
  ptr[0]= (char) rnd;
 
2908
  ptr[0]= unsigned_flag ? (char) (uint64_t) rnd : (char) rnd;
1056
2909
  return error;
1057
2910
}
1058
2911
 
1059
2912
 
1060
2913
int Field_tiny::store(double nr)
1061
2914
{
 
2915
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1062
2916
  int error= 0;
1063
2917
  nr=rint(nr);
1064
 
 
 
2918
  if (unsigned_flag)
 
2919
  {
 
2920
    if (nr < 0.0)
 
2921
    {
 
2922
      *ptr=0;
 
2923
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2924
      error= 1;
 
2925
    }
 
2926
    else if (nr > 255.0)
 
2927
    {
 
2928
      *ptr=(char) 255;
 
2929
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2930
      error= 1;
 
2931
    }
 
2932
    else
 
2933
      *ptr=(char) nr;
 
2934
  }
 
2935
  else
1065
2936
  {
1066
2937
    if (nr < -128.0)
1067
2938
    {
1068
2939
      *ptr= (char) -128;
1069
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2940
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1070
2941
      error= 1;
1071
2942
    }
1072
2943
    else if (nr > 127.0)
1073
2944
    {
1074
2945
      *ptr=127;
1075
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2946
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1076
2947
      error= 1;
1077
2948
    }
1078
2949
    else
1082
2953
}
1083
2954
 
1084
2955
 
1085
 
int Field_tiny::store(int64_t nr, bool unsigned_val)
 
2956
int Field_tiny::store(longlong nr, bool unsigned_val)
1086
2957
{
 
2958
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1087
2959
  int error= 0;
1088
2960
 
 
2961
  if (unsigned_flag)
 
2962
  {
 
2963
    if (nr < 0 && !unsigned_val)
 
2964
    {
 
2965
      *ptr= 0;
 
2966
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2967
      error= 1;
 
2968
    }
 
2969
    else if ((uint64_t) nr > (uint64_t) 255)
 
2970
    {
 
2971
      *ptr= (char) 255;
 
2972
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2973
      error= 1;
 
2974
    }
 
2975
    else
 
2976
      *ptr=(char) nr;
 
2977
  }
 
2978
  else
1089
2979
  {
1090
2980
    if (nr < 0 && unsigned_val)
1091
2981
      nr= 256;                                    // Generate overflow
1092
2982
    if (nr < -128)
1093
2983
    {
1094
2984
      *ptr= (char) -128;
1095
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2985
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1096
2986
      error= 1;
1097
2987
    }
1098
2988
    else if (nr > 127)
1099
2989
    {
1100
2990
      *ptr=127;
1101
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2991
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1102
2992
      error= 1;
1103
2993
    }
1104
2994
    else
1110
3000
 
1111
3001
double Field_tiny::val_real(void)
1112
3002
{
1113
 
  int tmp= (int) ((signed char*) ptr)[0];
 
3003
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3004
  int tmp= unsigned_flag ? (int) ptr[0] :
 
3005
    (int) ((signed char*) ptr)[0];
1114
3006
  return (double) tmp;
1115
3007
}
1116
3008
 
1117
3009
 
1118
 
int64_t Field_tiny::val_int(void)
 
3010
longlong Field_tiny::val_int(void)
1119
3011
{
1120
 
  int tmp= (int) ((signed char*) ptr)[0];
1121
 
  return (int64_t) tmp;
 
3012
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3013
  int tmp= unsigned_flag ? (int) ptr[0] :
 
3014
    (int) ((signed char*) ptr)[0];
 
3015
  return (longlong) tmp;
1122
3016
}
1123
3017
 
1124
3018
 
1125
3019
String *Field_tiny::val_str(String *val_buffer,
1126
3020
                            String *val_ptr __attribute__((unused)))
1127
3021
{
1128
 
  const CHARSET_INFO * const cs= &my_charset_bin;
1129
 
  uint32_t length;
1130
 
  uint32_t mlength=cmax(field_length+1,5*cs->mbmaxlen);
 
3022
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3023
  CHARSET_INFO *cs= &my_charset_bin;
 
3024
  uint length;
 
3025
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
1131
3026
  val_buffer->alloc(mlength);
1132
3027
  char *to=(char*) val_buffer->ptr();
1133
3028
 
1134
 
  length= (uint32_t) cs->cset->long10_to_str(cs,to,mlength,-10,
1135
 
                                             (long) *((signed char*) ptr));
1136
 
 
 
3029
  if (unsigned_flag)
 
3030
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
 
3031
                                           (long) *ptr);
 
3032
  else
 
3033
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
 
3034
                                           (long) *((signed char*) ptr));
 
3035
  
1137
3036
  val_buffer->length(length);
1138
 
 
 
3037
  if (zerofill)
 
3038
    prepend_zeros(val_buffer);
1139
3039
  return val_buffer;
1140
3040
}
1141
3041
 
1142
3042
bool Field_tiny::send_binary(Protocol *protocol)
1143
3043
{
1144
 
  return protocol->store_tiny((int64_t) (int8_t) ptr[0]);
 
3044
  return protocol->store_tiny((longlong) (int8) ptr[0]);
1145
3045
}
1146
3046
 
1147
 
int Field_tiny::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
3047
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
1148
3048
{
1149
3049
  signed char a,b;
1150
3050
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
1151
 
 
 
3051
  if (unsigned_flag)
 
3052
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
1152
3053
  return (a < b) ? -1 : (a > b) ? 1 : 0;
1153
3054
}
1154
3055
 
1155
 
void Field_tiny::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
 
3056
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
1156
3057
{
1157
 
  to[0] = (char) (ptr[0] ^ (unsigned char) 128);        /* Revers signbit */
 
3058
  if (unsigned_flag)
 
3059
    *to= *ptr;
 
3060
  else
 
3061
    to[0] = (char) (ptr[0] ^ (uchar) 128);      /* Revers signbit */
1158
3062
}
1159
3063
 
1160
3064
void Field_tiny::sql_type(String &res) const
1161
3065
{
1162
 
  const CHARSET_INFO * const cs=res.charset();
 
3066
  CHARSET_INFO *cs=res.charset();
1163
3067
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1164
3068
                          "tinyint(%d)",(int) field_length));
1165
 
}
 
3069
  add_zerofill_and_unsigned(res);
 
3070
}
 
3071
 
 
3072
/****************************************************************************
 
3073
 Field type short int (2 byte)
 
3074
****************************************************************************/
 
3075
 
 
3076
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
 
3077
{
 
3078
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3079
  int store_tmp;
 
3080
  int error;
 
3081
  longlong rnd;
 
3082
  
 
3083
  error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
 
3084
  store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
 
3085
#ifdef WORDS_BIGENDIAN
 
3086
  if (table->s->db_low_byte_first)
 
3087
  {
 
3088
    int2store(ptr, store_tmp);
 
3089
  }
 
3090
  else
 
3091
#endif
 
3092
    shortstore(ptr, (short) store_tmp);
 
3093
  return error;
 
3094
}
 
3095
 
 
3096
 
 
3097
int Field_short::store(double nr)
 
3098
{
 
3099
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3100
  int error= 0;
 
3101
  int16 res;
 
3102
  nr=rint(nr);
 
3103
  if (unsigned_flag)
 
3104
  {
 
3105
    if (nr < 0)
 
3106
    {
 
3107
      res=0;
 
3108
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3109
      error= 1;
 
3110
    }
 
3111
    else if (nr > (double) UINT_MAX16)
 
3112
    {
 
3113
      res=(int16) UINT_MAX16;
 
3114
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3115
      error= 1;
 
3116
    }
 
3117
    else
 
3118
      res=(int16) (uint16) nr;
 
3119
  }
 
3120
  else
 
3121
  {
 
3122
    if (nr < (double) INT_MIN16)
 
3123
    {
 
3124
      res=INT_MIN16;
 
3125
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3126
      error= 1;
 
3127
    }
 
3128
    else if (nr > (double) INT_MAX16)
 
3129
    {
 
3130
      res=INT_MAX16;
 
3131
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3132
      error= 1;
 
3133
    }
 
3134
    else
 
3135
      res=(int16) (int) nr;
 
3136
  }
 
3137
#ifdef WORDS_BIGENDIAN
 
3138
  if (table->s->db_low_byte_first)
 
3139
  {
 
3140
    int2store(ptr,res);
 
3141
  }
 
3142
  else
 
3143
#endif
 
3144
    shortstore(ptr,res);
 
3145
  return error;
 
3146
}
 
3147
 
 
3148
 
 
3149
int Field_short::store(longlong nr, bool unsigned_val)
 
3150
{
 
3151
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3152
  int error= 0;
 
3153
  int16 res;
 
3154
 
 
3155
  if (unsigned_flag)
 
3156
  {
 
3157
    if (nr < 0L && !unsigned_val)
 
3158
    {
 
3159
      res=0;
 
3160
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3161
      error= 1;
 
3162
    }
 
3163
    else if ((uint64_t) nr > (uint64_t) UINT_MAX16)
 
3164
    {
 
3165
      res=(int16) UINT_MAX16;
 
3166
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3167
      error= 1;
 
3168
    }
 
3169
    else
 
3170
      res=(int16) (uint16) nr;
 
3171
  }
 
3172
  else
 
3173
  {
 
3174
    if (nr < 0 && unsigned_val)
 
3175
      nr= UINT_MAX16+1;                         // Generate overflow
 
3176
 
 
3177
    if (nr < INT_MIN16)
 
3178
    {
 
3179
      res=INT_MIN16;
 
3180
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3181
      error= 1;
 
3182
    }
 
3183
    else if (nr > (longlong) INT_MAX16)
 
3184
    {
 
3185
      res=INT_MAX16;
 
3186
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3187
      error= 1;
 
3188
    }
 
3189
    else
 
3190
      res=(int16) nr;
 
3191
  }
 
3192
#ifdef WORDS_BIGENDIAN
 
3193
  if (table->s->db_low_byte_first)
 
3194
  {
 
3195
    int2store(ptr,res);
 
3196
  }
 
3197
  else
 
3198
#endif
 
3199
    shortstore(ptr,res);
 
3200
  return error;
 
3201
}
 
3202
 
 
3203
 
 
3204
double Field_short::val_real(void)
 
3205
{
 
3206
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3207
  short j;
 
3208
#ifdef WORDS_BIGENDIAN
 
3209
  if (table->s->db_low_byte_first)
 
3210
    j=sint2korr(ptr);
 
3211
  else
 
3212
#endif
 
3213
    shortget(j,ptr);
 
3214
  return unsigned_flag ? (double) (unsigned short) j : (double) j;
 
3215
}
 
3216
 
 
3217
longlong Field_short::val_int(void)
 
3218
{
 
3219
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3220
  short j;
 
3221
#ifdef WORDS_BIGENDIAN
 
3222
  if (table->s->db_low_byte_first)
 
3223
    j=sint2korr(ptr);
 
3224
  else
 
3225
#endif
 
3226
    shortget(j,ptr);
 
3227
  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
 
3228
}
 
3229
 
 
3230
 
 
3231
String *Field_short::val_str(String *val_buffer,
 
3232
                             String *val_ptr __attribute__((unused)))
 
3233
{
 
3234
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3235
  CHARSET_INFO *cs= &my_charset_bin;
 
3236
  uint length;
 
3237
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
 
3238
  val_buffer->alloc(mlength);
 
3239
  char *to=(char*) val_buffer->ptr();
 
3240
  short j;
 
3241
#ifdef WORDS_BIGENDIAN
 
3242
  if (table->s->db_low_byte_first)
 
3243
    j=sint2korr(ptr);
 
3244
  else
 
3245
#endif
 
3246
    shortget(j,ptr);
 
3247
 
 
3248
  if (unsigned_flag)
 
3249
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
 
3250
                                          (long) (uint16) j);
 
3251
  else
 
3252
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
 
3253
  val_buffer->length(length);
 
3254
  if (zerofill)
 
3255
    prepend_zeros(val_buffer);
 
3256
  return val_buffer;
 
3257
}
 
3258
 
 
3259
 
 
3260
bool Field_short::send_binary(Protocol *protocol)
 
3261
{
 
3262
  return protocol->store_short(Field_short::val_int());
 
3263
}
 
3264
 
 
3265
 
 
3266
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3267
{
 
3268
  short a,b;
 
3269
#ifdef WORDS_BIGENDIAN
 
3270
  if (table->s->db_low_byte_first)
 
3271
  {
 
3272
    a=sint2korr(a_ptr);
 
3273
    b=sint2korr(b_ptr);
 
3274
  }
 
3275
  else
 
3276
#endif
 
3277
  {
 
3278
    shortget(a,a_ptr);
 
3279
    shortget(b,b_ptr);
 
3280
  }
 
3281
 
 
3282
  if (unsigned_flag)
 
3283
    return ((unsigned short) a < (unsigned short) b) ? -1 :
 
3284
    ((unsigned short) a > (unsigned short) b) ? 1 : 0;
 
3285
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3286
}
 
3287
 
 
3288
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
 
3289
{
 
3290
#ifdef WORDS_BIGENDIAN
 
3291
  if (!table->s->db_low_byte_first)
 
3292
  {
 
3293
    if (unsigned_flag)
 
3294
      to[0] = ptr[0];
 
3295
    else
 
3296
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
3297
    to[1]   = ptr[1];
 
3298
  }
 
3299
  else
 
3300
#endif
 
3301
  {
 
3302
    if (unsigned_flag)
 
3303
      to[0] = ptr[1];
 
3304
    else
 
3305
      to[0] = (char) (ptr[1] ^ 128);            /* Revers signbit */
 
3306
    to[1]   = ptr[0];
 
3307
  }
 
3308
}
 
3309
 
 
3310
void Field_short::sql_type(String &res) const
 
3311
{
 
3312
  CHARSET_INFO *cs=res.charset();
 
3313
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
3314
                          "smallint(%d)",(int) field_length));
 
3315
  add_zerofill_and_unsigned(res);
 
3316
}
 
3317
 
 
3318
 
 
3319
/****************************************************************************
 
3320
  Field type medium int (3 byte)
 
3321
****************************************************************************/
 
3322
 
 
3323
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
 
3324
{
 
3325
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3326
  int store_tmp;
 
3327
  int error;
 
3328
  longlong rnd;
 
3329
  
 
3330
  error= get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
 
3331
  store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
 
3332
  int3store(ptr, store_tmp);
 
3333
  return error;
 
3334
}
 
3335
 
 
3336
 
 
3337
int Field_medium::store(double nr)
 
3338
{
 
3339
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3340
  int error= 0;
 
3341
  nr=rint(nr);
 
3342
  if (unsigned_flag)
 
3343
  {
 
3344
    if (nr < 0)
 
3345
    {
 
3346
      int3store(ptr,0);
 
3347
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3348
      error= 1;
 
3349
    }
 
3350
    else if (nr >= (double) (long) (1L << 24))
 
3351
    {
 
3352
      uint32 tmp=(uint32) (1L << 24)-1L;
 
3353
      int3store(ptr,tmp);
 
3354
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3355
      error= 1;
 
3356
    }
 
3357
    else
 
3358
      int3store(ptr,(uint32) nr);
 
3359
  }
 
3360
  else
 
3361
  {
 
3362
    if (nr < (double) INT_MIN24)
 
3363
    {
 
3364
      long tmp=(long) INT_MIN24;
 
3365
      int3store(ptr,tmp);
 
3366
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3367
      error= 1;
 
3368
    }
 
3369
    else if (nr > (double) INT_MAX24)
 
3370
    {
 
3371
      long tmp=(long) INT_MAX24;
 
3372
      int3store(ptr,tmp);
 
3373
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3374
      error= 1;
 
3375
    }
 
3376
    else
 
3377
      int3store(ptr,(long) nr);
 
3378
  }
 
3379
  return error;
 
3380
}
 
3381
 
 
3382
 
 
3383
int Field_medium::store(longlong nr, bool unsigned_val)
 
3384
{
 
3385
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3386
  int error= 0;
 
3387
 
 
3388
  if (unsigned_flag)
 
3389
  {
 
3390
    if (nr < 0 && !unsigned_val)
 
3391
    {
 
3392
      int3store(ptr,0);
 
3393
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3394
      error= 1;
 
3395
    }
 
3396
    else if ((uint64_t) nr >= (uint64_t) (long) (1L << 24))
 
3397
    {
 
3398
      long tmp= (long) (1L << 24)-1L;
 
3399
      int3store(ptr,tmp);
 
3400
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3401
      error= 1;
 
3402
    }
 
3403
    else
 
3404
      int3store(ptr,(uint32) nr);
 
3405
  }
 
3406
  else
 
3407
  {
 
3408
    if (nr < 0 && unsigned_val)
 
3409
      nr= (uint64_t) (long) (1L << 24);        // Generate overflow
 
3410
 
 
3411
    if (nr < (longlong) INT_MIN24)
 
3412
    {
 
3413
      long tmp= (long) INT_MIN24;
 
3414
      int3store(ptr,tmp);
 
3415
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3416
      error= 1;
 
3417
    }
 
3418
    else if (nr > (longlong) INT_MAX24)
 
3419
    {
 
3420
      long tmp=(long) INT_MAX24;
 
3421
      int3store(ptr,tmp);
 
3422
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3423
      error= 1;
 
3424
    }
 
3425
    else
 
3426
      int3store(ptr,(long) nr);
 
3427
  }
 
3428
  return error;
 
3429
}
 
3430
 
 
3431
 
 
3432
double Field_medium::val_real(void)
 
3433
{
 
3434
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3435
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
 
3436
  return (double) j;
 
3437
}
 
3438
 
 
3439
 
 
3440
longlong Field_medium::val_int(void)
 
3441
{
 
3442
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3443
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
 
3444
  return (longlong) j;
 
3445
}
 
3446
 
 
3447
 
 
3448
String *Field_medium::val_str(String *val_buffer,
 
3449
                              String *val_ptr __attribute__((unused)))
 
3450
{
 
3451
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3452
  CHARSET_INFO *cs= &my_charset_bin;
 
3453
  uint length;
 
3454
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
 
3455
  val_buffer->alloc(mlength);
 
3456
  char *to=(char*) val_buffer->ptr();
 
3457
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
 
3458
 
 
3459
  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
 
3460
  val_buffer->length(length);
 
3461
  if (zerofill)
 
3462
    prepend_zeros(val_buffer); /* purecov: inspected */
 
3463
  return val_buffer;
 
3464
}
 
3465
 
 
3466
 
 
3467
bool Field_medium::send_binary(Protocol *protocol)
 
3468
{
 
3469
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3470
  return protocol->store_long(Field_medium::val_int());
 
3471
}
 
3472
 
 
3473
 
 
3474
int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3475
{
 
3476
  long a,b;
 
3477
  if (unsigned_flag)
 
3478
  {
 
3479
    a=uint3korr(a_ptr);
 
3480
    b=uint3korr(b_ptr);
 
3481
  }
 
3482
  else
 
3483
  {
 
3484
    a=sint3korr(a_ptr);
 
3485
    b=sint3korr(b_ptr);
 
3486
  }
 
3487
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3488
}
 
3489
 
 
3490
void Field_medium::sort_string(uchar *to,uint length __attribute__((unused)))
 
3491
{
 
3492
  if (unsigned_flag)
 
3493
    to[0] = ptr[2];
 
3494
  else
 
3495
    to[0] = (uchar) (ptr[2] ^ 128);             /* Revers signbit */
 
3496
  to[1] = ptr[1];
 
3497
  to[2] = ptr[0];
 
3498
}
 
3499
 
 
3500
 
 
3501
void Field_medium::sql_type(String &res) const
 
3502
{
 
3503
  CHARSET_INFO *cs=res.charset();
 
3504
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), 
 
3505
                          "mediumint(%d)",(int) field_length));
 
3506
  add_zerofill_and_unsigned(res);
 
3507
}
 
3508
 
 
3509
/****************************************************************************
 
3510
** long int
 
3511
****************************************************************************/
 
3512
 
 
3513
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
 
3514
{
 
3515
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3516
  long store_tmp;
 
3517
  int error;
 
3518
  longlong rnd;
 
3519
  
 
3520
  error= get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
 
3521
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
 
3522
#ifdef WORDS_BIGENDIAN
 
3523
  if (table->s->db_low_byte_first)
 
3524
  {
 
3525
    int4store(ptr, store_tmp);
 
3526
  }
 
3527
  else
 
3528
#endif
 
3529
    longstore(ptr, store_tmp);
 
3530
  return error;
 
3531
}
 
3532
 
 
3533
 
 
3534
int Field_long::store(double nr)
 
3535
{
 
3536
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3537
  int error= 0;
 
3538
  int32 res;
 
3539
  nr=rint(nr);
 
3540
  if (unsigned_flag)
 
3541
  {
 
3542
    if (nr < 0)
 
3543
    {
 
3544
      res=0;
 
3545
      error= 1;
 
3546
    }
 
3547
    else if (nr > (double) UINT_MAX32)
 
3548
    {
 
3549
      res= UINT_MAX32;
 
3550
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3551
      error= 1;
 
3552
    }
 
3553
    else
 
3554
      res=(int32) (ulong) nr;
 
3555
  }
 
3556
  else
 
3557
  {
 
3558
    if (nr < (double) INT_MIN32)
 
3559
    {
 
3560
      res=(int32) INT_MIN32;
 
3561
      error= 1;
 
3562
    }
 
3563
    else if (nr > (double) INT_MAX32)
 
3564
    {
 
3565
      res=(int32) INT_MAX32;
 
3566
      error= 1;
 
3567
    }
 
3568
    else
 
3569
      res=(int32) (longlong) nr;
 
3570
  }
 
3571
  if (error)
 
3572
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3573
 
 
3574
#ifdef WORDS_BIGENDIAN
 
3575
  if (table->s->db_low_byte_first)
 
3576
  {
 
3577
    int4store(ptr,res);
 
3578
  }
 
3579
  else
 
3580
#endif
 
3581
    longstore(ptr,res);
 
3582
  return error;
 
3583
}
 
3584
 
 
3585
 
 
3586
int Field_long::store(longlong nr, bool unsigned_val)
 
3587
{
 
3588
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3589
  int error= 0;
 
3590
  int32 res;
 
3591
 
 
3592
  if (unsigned_flag)
 
3593
  {
 
3594
    if (nr < 0 && !unsigned_val)
 
3595
    {
 
3596
      res=0;
 
3597
      error= 1;
 
3598
    }
 
3599
    else if ((uint64_t) nr >= (LL(1) << 32))
 
3600
    {
 
3601
      res=(int32) (uint32) ~0L;
 
3602
      error= 1;
 
3603
    }
 
3604
    else
 
3605
      res=(int32) (uint32) nr;
 
3606
  }
 
3607
  else
 
3608
  {
 
3609
    if (nr < 0 && unsigned_val)
 
3610
      nr= ((longlong) INT_MAX32) + 1;           // Generate overflow
 
3611
    if (nr < (longlong) INT_MIN32) 
 
3612
    {
 
3613
      res=(int32) INT_MIN32;
 
3614
      error= 1;
 
3615
    }
 
3616
    else if (nr > (longlong) INT_MAX32)
 
3617
    {
 
3618
      res=(int32) INT_MAX32;
 
3619
      error= 1;
 
3620
    }
 
3621
    else
 
3622
      res=(int32) nr;
 
3623
  }
 
3624
  if (error)
 
3625
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3626
 
 
3627
#ifdef WORDS_BIGENDIAN
 
3628
  if (table->s->db_low_byte_first)
 
3629
  {
 
3630
    int4store(ptr,res);
 
3631
  }
 
3632
  else
 
3633
#endif
 
3634
    longstore(ptr,res);
 
3635
  return error;
 
3636
}
 
3637
 
 
3638
 
 
3639
double Field_long::val_real(void)
 
3640
{
 
3641
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3642
  int32 j;
 
3643
#ifdef WORDS_BIGENDIAN
 
3644
  if (table->s->db_low_byte_first)
 
3645
    j=sint4korr(ptr);
 
3646
  else
 
3647
#endif
 
3648
    longget(j,ptr);
 
3649
  return unsigned_flag ? (double) (uint32) j : (double) j;
 
3650
}
 
3651
 
 
3652
longlong Field_long::val_int(void)
 
3653
{
 
3654
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3655
  int32 j;
 
3656
  /* See the comment in Field_long::store(long long) */
 
3657
  DBUG_ASSERT(table->in_use == current_thd);
 
3658
#ifdef WORDS_BIGENDIAN
 
3659
  if (table->s->db_low_byte_first)
 
3660
    j=sint4korr(ptr);
 
3661
  else
 
3662
#endif
 
3663
    longget(j,ptr);
 
3664
  return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
 
3665
}
 
3666
 
 
3667
String *Field_long::val_str(String *val_buffer,
 
3668
                            String *val_ptr __attribute__((unused)))
 
3669
{
 
3670
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3671
  CHARSET_INFO *cs= &my_charset_bin;
 
3672
  uint length;
 
3673
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
 
3674
  val_buffer->alloc(mlength);
 
3675
  char *to=(char*) val_buffer->ptr();
 
3676
  int32 j;
 
3677
#ifdef WORDS_BIGENDIAN
 
3678
  if (table->s->db_low_byte_first)
 
3679
    j=sint4korr(ptr);
 
3680
  else
 
3681
#endif
 
3682
    longget(j,ptr);
 
3683
 
 
3684
  if (unsigned_flag)
 
3685
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
 
3686
  else
 
3687
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
 
3688
  val_buffer->length(length);
 
3689
  if (zerofill)
 
3690
    prepend_zeros(val_buffer);
 
3691
  return val_buffer;
 
3692
}
 
3693
 
 
3694
 
 
3695
bool Field_long::send_binary(Protocol *protocol)
 
3696
{
 
3697
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3698
  return protocol->store_long(Field_long::val_int());
 
3699
}
 
3700
 
 
3701
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3702
{
 
3703
  int32 a,b;
 
3704
#ifdef WORDS_BIGENDIAN
 
3705
  if (table->s->db_low_byte_first)
 
3706
  {
 
3707
    a=sint4korr(a_ptr);
 
3708
    b=sint4korr(b_ptr);
 
3709
  }
 
3710
  else
 
3711
#endif
 
3712
  {
 
3713
    longget(a,a_ptr);
 
3714
    longget(b,b_ptr);
 
3715
  }
 
3716
  if (unsigned_flag)
 
3717
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
 
3718
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3719
}
 
3720
 
 
3721
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
 
3722
{
 
3723
#ifdef WORDS_BIGENDIAN
 
3724
  if (!table->s->db_low_byte_first)
 
3725
  {
 
3726
    if (unsigned_flag)
 
3727
      to[0] = ptr[0];
 
3728
    else
 
3729
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
3730
    to[1]   = ptr[1];
 
3731
    to[2]   = ptr[2];
 
3732
    to[3]   = ptr[3];
 
3733
  }
 
3734
  else
 
3735
#endif
 
3736
  {
 
3737
    if (unsigned_flag)
 
3738
      to[0] = ptr[3];
 
3739
    else
 
3740
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
 
3741
    to[1]   = ptr[2];
 
3742
    to[2]   = ptr[1];
 
3743
    to[3]   = ptr[0];
 
3744
  }
 
3745
}
 
3746
 
 
3747
 
 
3748
void Field_long::sql_type(String &res) const
 
3749
{
 
3750
  CHARSET_INFO *cs=res.charset();
 
3751
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
3752
                          "int(%d)",(int) field_length));
 
3753
  add_zerofill_and_unsigned(res);
 
3754
}
 
3755
 
 
3756
/****************************************************************************
 
3757
 Field type longlong int (8 bytes)
 
3758
****************************************************************************/
 
3759
 
 
3760
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
 
3761
{
 
3762
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3763
  int error= 0;
 
3764
  char *end;
 
3765
  uint64_t tmp;
 
3766
 
 
3767
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
 
3768
  if (error == MY_ERRNO_ERANGE)
 
3769
  {
 
3770
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3771
    error= 1;
 
3772
  }
 
3773
  else if (table->in_use->count_cuted_fields && 
 
3774
           check_int(cs, from, len, end, error))
 
3775
    error= 1;
 
3776
  else
 
3777
    error= 0;
 
3778
#ifdef WORDS_BIGENDIAN
 
3779
  if (table->s->db_low_byte_first)
 
3780
  {
 
3781
    int8store(ptr,tmp);
 
3782
  }
 
3783
  else
 
3784
#endif
 
3785
    longlongstore(ptr,tmp);
 
3786
  return error;
 
3787
}
 
3788
 
 
3789
 
 
3790
int Field_longlong::store(double nr)
 
3791
{
 
3792
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3793
  int error= 0;
 
3794
  longlong res;
 
3795
 
 
3796
  nr= rint(nr);
 
3797
  if (unsigned_flag)
 
3798
  {
 
3799
    if (nr < 0)
 
3800
    {
 
3801
      res=0;
 
3802
      error= 1;
 
3803
    }
 
3804
    else if (nr >= (double) ULONGLONG_MAX)
 
3805
    {
 
3806
      res= ~(longlong) 0;
 
3807
      error= 1;
 
3808
    }
 
3809
    else
 
3810
      res=(longlong) (uint64_t) nr;
 
3811
  }
 
3812
  else
 
3813
  {
 
3814
    if (nr <= (double) LONGLONG_MIN)
 
3815
    {
 
3816
      res= LONGLONG_MIN;
 
3817
      error= (nr < (double) LONGLONG_MIN);
 
3818
    }
 
3819
    else if (nr >= (double) (uint64_t) LONGLONG_MAX)
 
3820
    {
 
3821
      res= LONGLONG_MAX;
 
3822
      error= (nr > (double) LONGLONG_MAX);
 
3823
    }
 
3824
    else
 
3825
      res=(longlong) nr;
 
3826
  }
 
3827
  if (error)
 
3828
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3829
 
 
3830
#ifdef WORDS_BIGENDIAN
 
3831
  if (table->s->db_low_byte_first)
 
3832
  {
 
3833
    int8store(ptr,res);
 
3834
  }
 
3835
  else
 
3836
#endif
 
3837
    longlongstore(ptr,res);
 
3838
  return error;
 
3839
}
 
3840
 
 
3841
 
 
3842
int Field_longlong::store(longlong nr, bool unsigned_val)
 
3843
{
 
3844
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
3845
  int error= 0;
 
3846
 
 
3847
  if (nr < 0)                                   // Only possible error
 
3848
  {
 
3849
    /*
 
3850
      if field is unsigned and value is signed (< 0) or
 
3851
      if field is signed and value is unsigned we have an overflow
 
3852
    */
 
3853
    if (unsigned_flag != unsigned_val)
 
3854
    {
 
3855
      nr= unsigned_flag ? (uint64_t) 0 : (uint64_t) LONGLONG_MAX;
 
3856
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3857
      error= 1;
 
3858
    }
 
3859
  }
 
3860
 
 
3861
#ifdef WORDS_BIGENDIAN
 
3862
  if (table->s->db_low_byte_first)
 
3863
  {
 
3864
    int8store(ptr,nr);
 
3865
  }
 
3866
  else
 
3867
#endif
 
3868
    longlongstore(ptr,nr);
 
3869
  return error;
 
3870
}
 
3871
 
 
3872
 
 
3873
double Field_longlong::val_real(void)
 
3874
{
 
3875
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3876
  longlong j;
 
3877
#ifdef WORDS_BIGENDIAN
 
3878
  if (table->s->db_low_byte_first)
 
3879
  {
 
3880
    j=sint8korr(ptr);
 
3881
  }
 
3882
  else
 
3883
#endif
 
3884
    longlongget(j,ptr);
 
3885
  /* The following is open coded to avoid a bug in gcc 3.3 */
 
3886
  if (unsigned_flag)
 
3887
  {
 
3888
    uint64_t tmp= (uint64_t) j;
 
3889
    return ulonglong2double(tmp);
 
3890
  }
 
3891
  return (double) j;
 
3892
}
 
3893
 
 
3894
 
 
3895
longlong Field_longlong::val_int(void)
 
3896
{
 
3897
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3898
  longlong j;
 
3899
#ifdef WORDS_BIGENDIAN
 
3900
  if (table->s->db_low_byte_first)
 
3901
    j=sint8korr(ptr);
 
3902
  else
 
3903
#endif
 
3904
    longlongget(j,ptr);
 
3905
  return j;
 
3906
}
 
3907
 
 
3908
 
 
3909
String *Field_longlong::val_str(String *val_buffer,
 
3910
                                String *val_ptr __attribute__((unused)))
 
3911
{
 
3912
  CHARSET_INFO *cs= &my_charset_bin;
 
3913
  uint length;
 
3914
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
 
3915
  val_buffer->alloc(mlength);
 
3916
  char *to=(char*) val_buffer->ptr();
 
3917
  longlong j;
 
3918
#ifdef WORDS_BIGENDIAN
 
3919
  if (table->s->db_low_byte_first)
 
3920
    j=sint8korr(ptr);
 
3921
  else
 
3922
#endif
 
3923
    longlongget(j,ptr);
 
3924
 
 
3925
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
 
3926
                                        unsigned_flag ? 10 : -10, j);
 
3927
  val_buffer->length(length);
 
3928
  if (zerofill)
 
3929
    prepend_zeros(val_buffer);
 
3930
  return val_buffer;
 
3931
}
 
3932
 
 
3933
 
 
3934
bool Field_longlong::send_binary(Protocol *protocol)
 
3935
{
 
3936
  ASSERT_COLUMN_MARKED_FOR_READ;
 
3937
  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
 
3938
}
 
3939
 
 
3940
 
 
3941
int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3942
{
 
3943
  longlong a,b;
 
3944
#ifdef WORDS_BIGENDIAN
 
3945
  if (table->s->db_low_byte_first)
 
3946
  {
 
3947
    a=sint8korr(a_ptr);
 
3948
    b=sint8korr(b_ptr);
 
3949
  }
 
3950
  else
 
3951
#endif
 
3952
  {
 
3953
    longlongget(a,a_ptr);
 
3954
    longlongget(b,b_ptr);
 
3955
  }
 
3956
  if (unsigned_flag)
 
3957
    return ((uint64_t) a < (uint64_t) b) ? -1 :
 
3958
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
3959
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3960
}
 
3961
 
 
3962
void Field_longlong::sort_string(uchar *to,uint length __attribute__((unused)))
 
3963
{
 
3964
#ifdef WORDS_BIGENDIAN
 
3965
  if (!table->s->db_low_byte_first)
 
3966
  {
 
3967
    if (unsigned_flag)
 
3968
      to[0] = ptr[0];
 
3969
    else
 
3970
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
3971
    to[1]   = ptr[1];
 
3972
    to[2]   = ptr[2];
 
3973
    to[3]   = ptr[3];
 
3974
    to[4]   = ptr[4];
 
3975
    to[5]   = ptr[5];
 
3976
    to[6]   = ptr[6];
 
3977
    to[7]   = ptr[7];
 
3978
  }
 
3979
  else
 
3980
#endif
 
3981
  {
 
3982
    if (unsigned_flag)
 
3983
      to[0] = ptr[7];
 
3984
    else
 
3985
      to[0] = (char) (ptr[7] ^ 128);            /* Revers signbit */
 
3986
    to[1]   = ptr[6];
 
3987
    to[2]   = ptr[5];
 
3988
    to[3]   = ptr[4];
 
3989
    to[4]   = ptr[3];
 
3990
    to[5]   = ptr[2];
 
3991
    to[6]   = ptr[1];
 
3992
    to[7]   = ptr[0];
 
3993
  }
 
3994
}
 
3995
 
 
3996
 
 
3997
void Field_longlong::sql_type(String &res) const
 
3998
{
 
3999
  CHARSET_INFO *cs=res.charset();
 
4000
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
4001
                          "bigint(%d)",(int) field_length));
 
4002
  add_zerofill_and_unsigned(res);
 
4003
}
 
4004
 
 
4005
 
 
4006
/*
 
4007
  Floating-point numbers
 
4008
 */
 
4009
 
 
4010
uchar *
 
4011
Field_real::pack(uchar *to, const uchar *from,
 
4012
                 uint max_length, bool low_byte_first)
 
4013
{
 
4014
  DBUG_ENTER("Field_real::pack");
 
4015
  DBUG_ASSERT(max_length >= pack_length());
 
4016
  DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
 
4017
#ifdef WORDS_BIGENDIAN
 
4018
  if (low_byte_first != table->s->db_low_byte_first)
 
4019
  {
 
4020
    const uchar *dptr= from + pack_length();
 
4021
    while (dptr-- > from)
 
4022
      *to++ = *dptr;
 
4023
    DBUG_RETURN(to);
 
4024
  }
 
4025
  else
 
4026
#endif
 
4027
    DBUG_RETURN(Field::pack(to, from, max_length, low_byte_first));
 
4028
}
 
4029
 
 
4030
const uchar *
 
4031
Field_real::unpack(uchar *to, const uchar *from,
 
4032
                   uint param_data, bool low_byte_first)
 
4033
{
 
4034
  DBUG_ENTER("Field_real::unpack");
 
4035
  DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
 
4036
#ifdef WORDS_BIGENDIAN
 
4037
  if (low_byte_first != table->s->db_low_byte_first)
 
4038
  {
 
4039
    const uchar *dptr= from + pack_length();
 
4040
    while (dptr-- > from)
 
4041
      *to++ = *dptr;
 
4042
    DBUG_RETURN(from + pack_length());
 
4043
  }
 
4044
  else
 
4045
#endif
 
4046
    DBUG_RETURN(Field::unpack(to, from, param_data, low_byte_first));
 
4047
}
 
4048
 
 
4049
/****************************************************************************
 
4050
  single precision float
 
4051
****************************************************************************/
 
4052
 
 
4053
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
 
4054
{
 
4055
  int error;
 
4056
  char *end;
 
4057
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
 
4058
  if (error || (!len || (((uint)(end-from) != len) && table->in_use->count_cuted_fields)))
 
4059
  {
 
4060
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4061
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
 
4062
    error= error ? 1 : 2;
 
4063
  }
 
4064
  Field_float::store(nr);
 
4065
  return error;
 
4066
}
 
4067
 
 
4068
 
 
4069
int Field_float::store(double nr)
 
4070
{
 
4071
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4072
  int error= truncate(&nr, FLT_MAX);
 
4073
  float j= (float)nr;
 
4074
 
 
4075
#ifdef WORDS_BIGENDIAN
 
4076
  if (table->s->db_low_byte_first)
 
4077
  {
 
4078
    float4store(ptr,j);
 
4079
  }
 
4080
  else
 
4081
#endif
 
4082
    memcpy_fixed(ptr,(uchar*) &j,sizeof(j));
 
4083
  return error;
 
4084
}
 
4085
 
 
4086
 
 
4087
int Field_float::store(longlong nr, bool unsigned_val)
 
4088
{
 
4089
  return Field_float::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
 
4090
                            (double) nr);
 
4091
}
 
4092
 
 
4093
 
 
4094
double Field_float::val_real(void)
 
4095
{
 
4096
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4097
  float j;
 
4098
#ifdef WORDS_BIGENDIAN
 
4099
  if (table->s->db_low_byte_first)
 
4100
  {
 
4101
    float4get(j,ptr);
 
4102
  }
 
4103
  else
 
4104
#endif
 
4105
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
 
4106
  return ((double) j);
 
4107
}
 
4108
 
 
4109
longlong Field_float::val_int(void)
 
4110
{
 
4111
  float j;
 
4112
#ifdef WORDS_BIGENDIAN
 
4113
  if (table->s->db_low_byte_first)
 
4114
  {
 
4115
    float4get(j,ptr);
 
4116
  }
 
4117
  else
 
4118
#endif
 
4119
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
 
4120
  return (longlong) rint(j);
 
4121
}
 
4122
 
 
4123
 
 
4124
String *Field_float::val_str(String *val_buffer,
 
4125
                             String *val_ptr __attribute__((unused)))
 
4126
{
 
4127
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4128
  float nr;
 
4129
#ifdef WORDS_BIGENDIAN
 
4130
  if (table->s->db_low_byte_first)
 
4131
  {
 
4132
    float4get(nr,ptr);
 
4133
  }
 
4134
  else
 
4135
#endif
 
4136
    memcpy_fixed((uchar*) &nr,ptr,sizeof(nr));
 
4137
 
 
4138
  uint to_length=max(field_length,70);
 
4139
  val_buffer->alloc(to_length);
 
4140
  char *to=(char*) val_buffer->ptr();
 
4141
  size_t len;
 
4142
 
 
4143
  if (dec >= NOT_FIXED_DEC)
 
4144
    len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
 
4145
  else
 
4146
  {
 
4147
    /*
 
4148
      We are safe here because the buffer length is >= 70, and
 
4149
      fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
 
4150
      will be not longer than 69 chars + terminating '\0'.
 
4151
    */
 
4152
    len= my_fcvt(nr, dec, to, NULL);
 
4153
  }
 
4154
  val_buffer->length((uint) len);
 
4155
  if (zerofill)
 
4156
    prepend_zeros(val_buffer);
 
4157
  return val_buffer;
 
4158
}
 
4159
 
 
4160
 
 
4161
int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4162
{
 
4163
  float a,b;
 
4164
#ifdef WORDS_BIGENDIAN
 
4165
  if (table->s->db_low_byte_first)
 
4166
  {
 
4167
    float4get(a,a_ptr);
 
4168
    float4get(b,b_ptr);
 
4169
  }
 
4170
  else
 
4171
#endif
 
4172
  {
 
4173
    memcpy_fixed(&a,a_ptr,sizeof(float));
 
4174
    memcpy_fixed(&b,b_ptr,sizeof(float));
 
4175
  }
 
4176
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
4177
}
 
4178
 
 
4179
#define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
 
4180
 
 
4181
void Field_float::sort_string(uchar *to,uint length __attribute__((unused)))
 
4182
{
 
4183
  float nr;
 
4184
#ifdef WORDS_BIGENDIAN
 
4185
  if (table->s->db_low_byte_first)
 
4186
  {
 
4187
    float4get(nr,ptr);
 
4188
  }
 
4189
  else
 
4190
#endif
 
4191
    memcpy_fixed(&nr,ptr,sizeof(float));
 
4192
 
 
4193
  uchar *tmp= to;
 
4194
  if (nr == (float) 0.0)
 
4195
  {                                             /* Change to zero string */
 
4196
    tmp[0]=(uchar) 128;
 
4197
    bzero((char*) tmp+1,sizeof(nr)-1);
 
4198
  }
 
4199
  else
 
4200
  {
 
4201
#ifdef WORDS_BIGENDIAN
 
4202
    memcpy_fixed(tmp,&nr,sizeof(nr));
 
4203
#else
 
4204
    tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
 
4205
#endif
 
4206
    if (tmp[0] & 128)                           /* Negative */
 
4207
    {                                           /* make complement */
 
4208
      uint i;
 
4209
      for (i=0 ; i < sizeof(nr); i++)
 
4210
        tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
 
4211
    }
 
4212
    else
 
4213
    {
 
4214
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
 
4215
                       (ushort) 32768);
 
4216
      exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
 
4217
      tmp[0]= (uchar) (exp_part >> 8);
 
4218
      tmp[1]= (uchar) exp_part;
 
4219
    }
 
4220
  }
 
4221
}
 
4222
 
 
4223
 
 
4224
bool Field_float::send_binary(Protocol *protocol)
 
4225
{
 
4226
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4227
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
 
4228
}
 
4229
 
 
4230
 
 
4231
/**
 
4232
   Save the field metadata for float fields.
 
4233
 
 
4234
   Saves the pack length in the first byte.
 
4235
 
 
4236
   @param   metadata_ptr   First byte of field metadata
 
4237
 
 
4238
   @returns number of bytes written to metadata_ptr
 
4239
*/
 
4240
int Field_float::do_save_field_metadata(uchar *metadata_ptr)
 
4241
{
 
4242
  *metadata_ptr= pack_length();
 
4243
  return 1;
 
4244
}
 
4245
 
 
4246
 
 
4247
void Field_float::sql_type(String &res) const
 
4248
{
 
4249
  if (dec == NOT_FIXED_DEC)
 
4250
  {
 
4251
    res.set_ascii(STRING_WITH_LEN("float"));
 
4252
  }
 
4253
  else
 
4254
  {
 
4255
    CHARSET_INFO *cs= res.charset();
 
4256
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
4257
                            "float(%d,%d)",(int) field_length,dec));
 
4258
  }
 
4259
  add_zerofill_and_unsigned(res);
 
4260
}
 
4261
 
 
4262
 
 
4263
/****************************************************************************
 
4264
  double precision floating point numbers
 
4265
****************************************************************************/
 
4266
 
 
4267
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
 
4268
{
 
4269
  int error;
 
4270
  char *end;
 
4271
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
 
4272
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
 
4273
  {
 
4274
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4275
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
 
4276
    error= error ? 1 : 2;
 
4277
  }
 
4278
  Field_double::store(nr);
 
4279
  return error;
 
4280
}
 
4281
 
 
4282
 
 
4283
int Field_double::store(double nr)
 
4284
{
 
4285
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4286
  int error= truncate(&nr, DBL_MAX);
 
4287
 
 
4288
#ifdef WORDS_BIGENDIAN
 
4289
  if (table->s->db_low_byte_first)
 
4290
  {
 
4291
    float8store(ptr,nr);
 
4292
  }
 
4293
  else
 
4294
#endif
 
4295
    doublestore(ptr,nr);
 
4296
  return error;
 
4297
}
 
4298
 
 
4299
 
 
4300
int Field_double::store(longlong nr, bool unsigned_val)
 
4301
{
 
4302
  return Field_double::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
 
4303
                             (double) nr);
 
4304
}
 
4305
 
 
4306
/*
 
4307
  If a field has fixed length, truncate the double argument pointed to by 'nr'
 
4308
  appropriately.
 
4309
  Also ensure that the argument is within [-max_value; max_value] range.
 
4310
*/
 
4311
 
 
4312
int Field_real::truncate(double *nr, double max_value)
 
4313
{
 
4314
  int error= 1;
 
4315
  double res= *nr;
 
4316
  
 
4317
  if (isnan(res))
 
4318
  {
 
4319
    res= 0;
 
4320
    set_null();
 
4321
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4322
    goto end;
 
4323
  }
 
4324
  else if (unsigned_flag && res < 0)
 
4325
  {
 
4326
    res= 0;
 
4327
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4328
    goto end;
 
4329
  }
 
4330
 
 
4331
  if (!not_fixed)
 
4332
  {
 
4333
    uint order= field_length - dec;
 
4334
    uint step= array_elements(log_10) - 1;
 
4335
    max_value= 1.0;
 
4336
    for (; order > step; order-= step)
 
4337
      max_value*= log_10[step];
 
4338
    max_value*= log_10[order];
 
4339
    max_value-= 1.0 / log_10[dec];
 
4340
 
 
4341
    /* Check for infinity so we don't get NaN in calculations */
 
4342
    if (!my_isinf(res))
 
4343
    {
 
4344
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
 
4345
      res= floor(res) + tmp;
 
4346
    }
 
4347
  }
 
4348
  
 
4349
  if (res < -max_value)
 
4350
  {
 
4351
   res= -max_value;
 
4352
   set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4353
  }
 
4354
  else if (res > max_value)
 
4355
  {
 
4356
    res= max_value;
 
4357
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4358
  }
 
4359
  else
 
4360
    error= 0;
 
4361
 
 
4362
end:
 
4363
  *nr= res;
 
4364
  return error;
 
4365
}
 
4366
 
 
4367
 
 
4368
int Field_real::store_decimal(const my_decimal *dm)
 
4369
{
 
4370
  double dbl;
 
4371
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
 
4372
  return store(dbl);
 
4373
}
 
4374
 
 
4375
double Field_double::val_real(void)
 
4376
{
 
4377
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4378
  double j;
 
4379
#ifdef WORDS_BIGENDIAN
 
4380
  if (table->s->db_low_byte_first)
 
4381
  {
 
4382
    float8get(j,ptr);
 
4383
  }
 
4384
  else
 
4385
#endif
 
4386
    doubleget(j,ptr);
 
4387
  return j;
 
4388
}
 
4389
 
 
4390
longlong Field_double::val_int(void)
 
4391
{
 
4392
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4393
  double j;
 
4394
  longlong res;
 
4395
#ifdef WORDS_BIGENDIAN
 
4396
  if (table->s->db_low_byte_first)
 
4397
  {
 
4398
    float8get(j,ptr);
 
4399
  }
 
4400
  else
 
4401
#endif
 
4402
    doubleget(j,ptr);
 
4403
  /* Check whether we fit into longlong range */
 
4404
  if (j <= (double) LONGLONG_MIN)
 
4405
  {
 
4406
    res= (longlong) LONGLONG_MIN;
 
4407
    goto warn;
 
4408
  }
 
4409
  if (j >= (double) (uint64_t) LONGLONG_MAX)
 
4410
  {
 
4411
    res= (longlong) LONGLONG_MAX;
 
4412
    goto warn;
 
4413
  }
 
4414
  return (longlong) rint(j);
 
4415
 
 
4416
warn:
 
4417
  {
 
4418
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
4419
    String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
 
4420
    str= val_str(&tmp, 0);
 
4421
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
4422
                        ER_TRUNCATED_WRONG_VALUE,
 
4423
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
 
4424
                        str->c_ptr());
 
4425
  }
 
4426
  return res;
 
4427
}
 
4428
 
 
4429
 
 
4430
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
 
4431
{
 
4432
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4433
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
 
4434
  return decimal_value;
 
4435
}
 
4436
 
 
4437
 
 
4438
String *Field_double::val_str(String *val_buffer,
 
4439
                              String *val_ptr __attribute__((unused)))
 
4440
{
 
4441
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4442
  double nr;
 
4443
#ifdef WORDS_BIGENDIAN
 
4444
  if (table->s->db_low_byte_first)
 
4445
  {
 
4446
    float8get(nr,ptr);
 
4447
  }
 
4448
  else
 
4449
#endif
 
4450
    doubleget(nr,ptr);
 
4451
 
 
4452
  uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
 
4453
  val_buffer->alloc(to_length);
 
4454
  char *to=(char*) val_buffer->ptr();
 
4455
  size_t len;
 
4456
 
 
4457
  if (dec >= NOT_FIXED_DEC)
 
4458
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
 
4459
  else
 
4460
    len= my_fcvt(nr, dec, to, NULL);
 
4461
 
 
4462
  val_buffer->length((uint) len);
 
4463
  if (zerofill)
 
4464
    prepend_zeros(val_buffer);
 
4465
  return val_buffer;
 
4466
}
 
4467
 
 
4468
bool Field_double::send_binary(Protocol *protocol)
 
4469
{
 
4470
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
 
4471
}
 
4472
 
 
4473
 
 
4474
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4475
{
 
4476
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4477
  double a,b;
 
4478
#ifdef WORDS_BIGENDIAN
 
4479
  if (table->s->db_low_byte_first)
 
4480
  {
 
4481
    float8get(a,a_ptr);
 
4482
    float8get(b,b_ptr);
 
4483
  }
 
4484
  else
 
4485
#endif
 
4486
  {
 
4487
    doubleget(a, a_ptr);
 
4488
    doubleget(b, b_ptr);
 
4489
  }
 
4490
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
4491
}
 
4492
 
 
4493
 
 
4494
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
 
4495
 
 
4496
/* The following should work for IEEE */
 
4497
 
 
4498
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
 
4499
{
 
4500
  double nr;
 
4501
#ifdef WORDS_BIGENDIAN
 
4502
  if (table->s->db_low_byte_first)
 
4503
  {
 
4504
    float8get(nr,ptr);
 
4505
  }
 
4506
  else
 
4507
#endif
 
4508
    doubleget(nr,ptr);
 
4509
  change_double_for_sort(nr, to);
 
4510
}
 
4511
 
 
4512
 
 
4513
/**
 
4514
   Save the field metadata for double fields.
 
4515
 
 
4516
   Saves the pack length in the first byte of the field metadata array
 
4517
   at index of *metadata_ptr.
 
4518
 
 
4519
   @param   metadata_ptr   First byte of field metadata
 
4520
 
 
4521
   @returns number of bytes written to metadata_ptr
 
4522
*/
 
4523
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
 
4524
{
 
4525
  *metadata_ptr= pack_length();
 
4526
  return 1;
 
4527
}
 
4528
 
 
4529
 
 
4530
void Field_double::sql_type(String &res) const
 
4531
{
 
4532
  CHARSET_INFO *cs=res.charset();
 
4533
  if (dec == NOT_FIXED_DEC)
 
4534
  {
 
4535
    res.set_ascii(STRING_WITH_LEN("double"));
 
4536
  }
 
4537
  else
 
4538
  {
 
4539
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
4540
                            "double(%d,%d)",(int) field_length,dec));
 
4541
  }
 
4542
  add_zerofill_and_unsigned(res);
 
4543
}
 
4544
 
 
4545
 
 
4546
/**
 
4547
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to 
 
4548
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix 
 
4549
  Epoch in UTC.
 
4550
  
 
4551
  Up to one of timestamps columns in the table can be automatically 
 
4552
  set on row update and/or have NOW() as default value.
 
4553
  TABLE::timestamp_field points to Field object for such timestamp with 
 
4554
  auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
 
4555
  field, and is used by handler code which performs updates required.
 
4556
  
 
4557
  Actually SQL-99 says that we should allow niladic functions (like NOW())
 
4558
  as defaults for any field. Current limitations (only NOW() and only 
 
4559
  for one TIMESTAMP field) are because of restricted binary .frm format 
 
4560
  and should go away in the future.
 
4561
  
 
4562
  Also because of this limitation of binary .frm format we use 5 different
 
4563
  unireg_check values with TIMESTAMP field to distinguish various cases of
 
4564
  DEFAULT or ON UPDATE values. These values are:
 
4565
  
 
4566
  TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
 
4567
    auto-set-on-update (or now() as default) in this table before, then this 
 
4568
    field has NOW() as default and is updated when row changes, else it is 
 
4569
    field which has 0 as default value and is not automatically updated.
 
4570
  TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
 
4571
    automatically (TIMESTAMP DEFAULT NOW())
 
4572
  TIMESTAMP_UN_FIELD - field which is set on update automatically but has not 
 
4573
    NOW() as default (but it may has 0 or some other const timestamp as 
 
4574
    default) (TIMESTAMP ON UPDATE NOW()).
 
4575
  TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on 
 
4576
    update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
 
4577
  NONE - field which is not auto-set on update with some other than NOW() 
 
4578
    default value (TIMESTAMP DEFAULT 0).
 
4579
 
 
4580
  Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are 
 
4581
  left only for preserving ability to read old tables. Such fields replaced 
 
4582
  with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is 
 
4583
  because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for 
 
4584
  "TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such 
 
4585
  specification too but ignored default value for first timestamp, which of 
 
4586
  course is non-standard.) In most cases user won't notice any change, only
 
4587
  exception is different behavior of old/new timestamps during ALTER TABLE.
 
4588
 */
 
4589
 
 
4590
Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
 
4591
                                 uchar *null_ptr_arg, uchar null_bit_arg,
 
4592
                                 enum utype unireg_check_arg,
 
4593
                                 const char *field_name_arg,
 
4594
                                 TABLE_SHARE *share,
 
4595
                                 CHARSET_INFO *cs)
 
4596
  :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
 
4597
             unireg_check_arg, field_name_arg, cs)
 
4598
{
 
4599
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
4600
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
4601
  if (!share->timestamp_field && unireg_check != NONE)
 
4602
  {
 
4603
    /* This timestamp has auto-update */
 
4604
    share->timestamp_field= this;
 
4605
    flags|= TIMESTAMP_FLAG;
 
4606
    if (unireg_check != TIMESTAMP_DN_FIELD)
 
4607
      flags|= ON_UPDATE_NOW_FLAG;
 
4608
  }
 
4609
}
 
4610
 
 
4611
 
 
4612
Field_timestamp::Field_timestamp(bool maybe_null_arg,
 
4613
                                 const char *field_name_arg,
 
4614
                                 CHARSET_INFO *cs)
 
4615
  :Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
 
4616
             maybe_null_arg ? (uchar*) "": 0, 0,
 
4617
             NONE, field_name_arg, cs)
 
4618
{
 
4619
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
4620
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
4621
    if (unireg_check != TIMESTAMP_DN_FIELD)
 
4622
      flags|= ON_UPDATE_NOW_FLAG;
 
4623
}
 
4624
 
 
4625
 
 
4626
/**
 
4627
  Get auto-set type for TIMESTAMP field.
 
4628
 
 
4629
  Returns value indicating during which operations this TIMESTAMP field
 
4630
  should be auto-set to current timestamp.
 
4631
*/
 
4632
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
 
4633
{
 
4634
  switch (unireg_check)
 
4635
  {
 
4636
  case TIMESTAMP_DN_FIELD:
 
4637
    return TIMESTAMP_AUTO_SET_ON_INSERT;
 
4638
  case TIMESTAMP_UN_FIELD:
 
4639
    return TIMESTAMP_AUTO_SET_ON_UPDATE;
 
4640
  case TIMESTAMP_OLD_FIELD:
 
4641
    /*
 
4642
      Although we can have several such columns in legacy tables this
 
4643
      function should be called only for first of them (i.e. the one
 
4644
      having auto-set property).
 
4645
    */
 
4646
    DBUG_ASSERT(table->timestamp_field == this);
 
4647
    /* Fall-through */
 
4648
  case TIMESTAMP_DNUN_FIELD:
 
4649
    return TIMESTAMP_AUTO_SET_ON_BOTH;
 
4650
  default:
 
4651
    /*
 
4652
      Normally this function should not be called for TIMESTAMPs without
 
4653
      auto-set property.
 
4654
    */
 
4655
    DBUG_ASSERT(0);
 
4656
    return TIMESTAMP_NO_AUTO_SET;
 
4657
  }
 
4658
}
 
4659
 
 
4660
 
 
4661
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
 
4662
{
 
4663
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4664
  MYSQL_TIME l_time;
 
4665
  my_time_t tmp= 0;
 
4666
  int error;
 
4667
  bool have_smth_to_conv;
 
4668
  my_bool in_dst_time_gap;
 
4669
  THD *thd= table ? table->in_use : current_thd;
 
4670
 
 
4671
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
 
4672
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 1, &error) >
 
4673
                      MYSQL_TIMESTAMP_ERROR);
 
4674
 
 
4675
  if (error || !have_smth_to_conv)
 
4676
  {
 
4677
    error= 1;
 
4678
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
4679
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
 
4680
  }
 
4681
 
 
4682
  /* Only convert a correct date (not a zero date) */
 
4683
  if (have_smth_to_conv && l_time.month)
 
4684
  {
 
4685
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
 
4686
    {
 
4687
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4688
                           ER_WARN_DATA_OUT_OF_RANGE,
 
4689
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
 
4690
      error= 1;
 
4691
    }
 
4692
    else if (in_dst_time_gap)
 
4693
    {
 
4694
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4695
                           ER_WARN_INVALID_TIMESTAMP,
 
4696
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
 
4697
      error= 1;
 
4698
    }
 
4699
  }
 
4700
  store_timestamp(tmp);
 
4701
  return error;
 
4702
}
 
4703
 
 
4704
 
 
4705
int Field_timestamp::store(double nr)
 
4706
{
 
4707
  int error= 0;
 
4708
  if (nr < 0 || nr > 99991231235959.0)
 
4709
  {
 
4710
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4711
                         ER_WARN_DATA_OUT_OF_RANGE,
 
4712
                         nr, MYSQL_TIMESTAMP_DATETIME);
 
4713
    nr= 0;                                      // Avoid overflow on buff
 
4714
    error= 1;
 
4715
  }
 
4716
  error|= Field_timestamp::store((longlong) rint(nr), FALSE);
 
4717
  return error;
 
4718
}
 
4719
 
 
4720
 
 
4721
int Field_timestamp::store(longlong nr, bool unsigned_val)
 
4722
{
 
4723
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
4724
  MYSQL_TIME l_time;
 
4725
  my_time_t timestamp= 0;
 
4726
  int error;
 
4727
  my_bool in_dst_time_gap;
 
4728
  THD *thd= table ? table->in_use : current_thd;
 
4729
 
 
4730
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
 
4731
  longlong tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
 
4732
                                                 MODE_NO_ZERO_DATE) |
 
4733
                                   MODE_NO_ZERO_IN_DATE, &error);
 
4734
  if (tmp == LL(-1))
 
4735
  {
 
4736
    error= 2;
 
4737
  }
 
4738
 
 
4739
  if (!error && tmp)
 
4740
  {
 
4741
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
 
4742
    {
 
4743
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4744
                           ER_WARN_DATA_OUT_OF_RANGE,
 
4745
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
4746
      error= 1;
 
4747
    }
 
4748
    if (in_dst_time_gap)
 
4749
    {
 
4750
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4751
                           ER_WARN_INVALID_TIMESTAMP,
 
4752
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
4753
      error= 1;
 
4754
    }
 
4755
  } else if (error)
 
4756
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4757
                         WARN_DATA_TRUNCATED,
 
4758
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
4759
 
 
4760
  store_timestamp(timestamp);
 
4761
  return error;
 
4762
}
 
4763
 
 
4764
double Field_timestamp::val_real(void)
 
4765
{
 
4766
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4767
  return (double) Field_timestamp::val_int();
 
4768
}
 
4769
 
 
4770
longlong Field_timestamp::val_int(void)
 
4771
{
 
4772
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4773
  uint32 temp;
 
4774
  MYSQL_TIME time_tmp;
 
4775
  THD  *thd= table ? table->in_use : current_thd;
 
4776
 
 
4777
  thd->time_zone_used= 1;
 
4778
#ifdef WORDS_BIGENDIAN
 
4779
  if (table && table->s->db_low_byte_first)
 
4780
    temp=uint4korr(ptr);
 
4781
  else
 
4782
#endif
 
4783
    longget(temp,ptr);
 
4784
 
 
4785
  if (temp == 0L)                               // No time
 
4786
    return(0);                                  /* purecov: inspected */
 
4787
  
 
4788
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
 
4789
  
 
4790
  return time_tmp.year * LL(10000000000) + time_tmp.month * LL(100000000) +
 
4791
         time_tmp.day * 1000000L + time_tmp.hour * 10000L +
 
4792
         time_tmp.minute * 100 + time_tmp.second;
 
4793
}
 
4794
 
 
4795
 
 
4796
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
 
4797
{
 
4798
  ASSERT_COLUMN_MARKED_FOR_READ;
 
4799
  uint32 temp, temp2;
 
4800
  MYSQL_TIME time_tmp;
 
4801
  THD *thd= table ? table->in_use : current_thd;
 
4802
  char *to;
 
4803
 
 
4804
  val_buffer->alloc(field_length+1);
 
4805
  to= (char*) val_buffer->ptr();
 
4806
  val_buffer->length(field_length);
 
4807
 
 
4808
  thd->time_zone_used= 1;
 
4809
#ifdef WORDS_BIGENDIAN
 
4810
  if (table && table->s->db_low_byte_first)
 
4811
    temp=uint4korr(ptr);
 
4812
  else
 
4813
#endif
 
4814
    longget(temp,ptr);
 
4815
 
 
4816
  if (temp == 0L)
 
4817
  {                                   /* Zero time is "000000" */
 
4818
    val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_bin);
 
4819
    return val_ptr;
 
4820
  }
 
4821
  val_buffer->set_charset(&my_charset_bin);     // Safety
 
4822
  
 
4823
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
 
4824
 
 
4825
  temp= time_tmp.year % 100;
 
4826
  if (temp < YY_PART_YEAR - 1)
 
4827
  {
 
4828
    *to++= '2';
 
4829
    *to++= '0';
 
4830
  }
 
4831
  else
 
4832
  {
 
4833
    *to++= '1';
 
4834
    *to++= '9';
 
4835
  }
 
4836
  temp2=temp/10; temp=temp-temp2*10;
 
4837
  *to++= (char) ('0'+(char) (temp2));
 
4838
  *to++= (char) ('0'+(char) (temp));
 
4839
  *to++= '-';
 
4840
  temp=time_tmp.month;
 
4841
  temp2=temp/10; temp=temp-temp2*10;
 
4842
  *to++= (char) ('0'+(char) (temp2));
 
4843
  *to++= (char) ('0'+(char) (temp));
 
4844
  *to++= '-';
 
4845
  temp=time_tmp.day;
 
4846
  temp2=temp/10; temp=temp-temp2*10;
 
4847
  *to++= (char) ('0'+(char) (temp2));
 
4848
  *to++= (char) ('0'+(char) (temp));
 
4849
  *to++= ' ';
 
4850
  temp=time_tmp.hour;
 
4851
  temp2=temp/10; temp=temp-temp2*10;
 
4852
  *to++= (char) ('0'+(char) (temp2));
 
4853
  *to++= (char) ('0'+(char) (temp));
 
4854
  *to++= ':';
 
4855
  temp=time_tmp.minute;
 
4856
  temp2=temp/10; temp=temp-temp2*10;
 
4857
  *to++= (char) ('0'+(char) (temp2));
 
4858
  *to++= (char) ('0'+(char) (temp));
 
4859
  *to++= ':';
 
4860
  temp=time_tmp.second;
 
4861
  temp2=temp/10; temp=temp-temp2*10;
 
4862
  *to++= (char) ('0'+(char) (temp2));
 
4863
  *to++= (char) ('0'+(char) (temp));
 
4864
  *to= 0;
 
4865
  return val_buffer;
 
4866
}
 
4867
 
 
4868
 
 
4869
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4870
{
 
4871
  long temp;
 
4872
  THD *thd= table ? table->in_use : current_thd;
 
4873
  thd->time_zone_used= 1;
 
4874
#ifdef WORDS_BIGENDIAN
 
4875
  if (table && table->s->db_low_byte_first)
 
4876
    temp=uint4korr(ptr);
 
4877
  else
 
4878
#endif
 
4879
    longget(temp,ptr);
 
4880
  if (temp == 0L)
 
4881
  {                                   /* Zero time is "000000" */
 
4882
    if (fuzzydate & TIME_NO_ZERO_DATE)
 
4883
      return 1;
 
4884
    bzero((char*) ltime,sizeof(*ltime));
 
4885
  }
 
4886
  else
 
4887
  {
 
4888
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
 
4889
  }
 
4890
  return 0;
 
4891
}
 
4892
 
 
4893
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
 
4894
{
 
4895
  return Field_timestamp::get_date(ltime,0);
 
4896
}
 
4897
 
 
4898
 
 
4899
bool Field_timestamp::send_binary(Protocol *protocol)
 
4900
{
 
4901
  MYSQL_TIME tm;
 
4902
  Field_timestamp::get_date(&tm, 0);
 
4903
  return protocol->store(&tm);
 
4904
}
 
4905
 
 
4906
 
 
4907
int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4908
{
 
4909
  int32 a,b;
 
4910
#ifdef WORDS_BIGENDIAN
 
4911
  if (table && table->s->db_low_byte_first)
 
4912
  {
 
4913
    a=sint4korr(a_ptr);
 
4914
    b=sint4korr(b_ptr);
 
4915
  }
 
4916
  else
 
4917
#endif
 
4918
  {
 
4919
  longget(a,a_ptr);
 
4920
  longget(b,b_ptr);
 
4921
  }
 
4922
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
 
4923
}
 
4924
 
 
4925
 
 
4926
void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
 
4927
{
 
4928
#ifdef WORDS_BIGENDIAN
 
4929
  if (!table || !table->s->db_low_byte_first)
 
4930
  {
 
4931
    to[0] = ptr[0];
 
4932
    to[1] = ptr[1];
 
4933
    to[2] = ptr[2];
 
4934
    to[3] = ptr[3];
 
4935
  }
 
4936
  else
 
4937
#endif
 
4938
  {
 
4939
    to[0] = ptr[3];
 
4940
    to[1] = ptr[2];
 
4941
    to[2] = ptr[1];
 
4942
    to[3] = ptr[0];
 
4943
  }
 
4944
}
 
4945
 
 
4946
 
 
4947
void Field_timestamp::sql_type(String &res) const
 
4948
{
 
4949
  res.set_ascii(STRING_WITH_LEN("timestamp"));
 
4950
}
 
4951
 
 
4952
 
 
4953
void Field_timestamp::set_time()
 
4954
{
 
4955
  THD *thd= table ? table->in_use : current_thd;
 
4956
  long tmp= (long) thd->query_start();
 
4957
  set_notnull();
 
4958
  store_timestamp(tmp);
 
4959
}
 
4960
 
 
4961
/****************************************************************************
 
4962
** time type
 
4963
** In string context: HH:MM:SS
 
4964
** In number context: HHMMSS
 
4965
** Stored as a 3 byte unsigned int
 
4966
****************************************************************************/
 
4967
 
 
4968
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
 
4969
{
 
4970
  MYSQL_TIME ltime;
 
4971
  long tmp;
 
4972
  int error= 0;
 
4973
  int warning;
 
4974
 
 
4975
  if (str_to_time(from, len, &ltime, &warning))
 
4976
  {
 
4977
    tmp=0L;
 
4978
    error= 2;
 
4979
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
4980
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
 
4981
  }
 
4982
  else
 
4983
  {
 
4984
    if (warning & MYSQL_TIME_WARN_TRUNCATED)
 
4985
    {
 
4986
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4987
                           WARN_DATA_TRUNCATED,
 
4988
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
 
4989
      error= 1;
 
4990
    }
 
4991
    if (warning & MYSQL_TIME_WARN_OUT_OF_RANGE)
 
4992
    {
 
4993
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
4994
                           ER_WARN_DATA_OUT_OF_RANGE,
 
4995
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
 
4996
      error= 1;
 
4997
    }
 
4998
    if (ltime.month)
 
4999
      ltime.day=0;
 
5000
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
 
5001
  }
 
5002
  
 
5003
  if (ltime.neg)
 
5004
    tmp= -tmp;
 
5005
  int3store(ptr,tmp);
 
5006
  return error;
 
5007
}
 
5008
 
 
5009
 
 
5010
int Field_time::store_time(MYSQL_TIME *ltime, timestamp_type time_type)
 
5011
{
 
5012
  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
 
5013
            (ltime->minute * 100 + ltime->second);
 
5014
  if (ltime->neg)
 
5015
    tmp= -tmp;
 
5016
  return Field_time::store((longlong) tmp, FALSE);
 
5017
}
 
5018
 
 
5019
 
 
5020
int Field_time::store(double nr)
 
5021
{
 
5022
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5023
  long tmp;
 
5024
  int error= 0;
 
5025
  if (nr > (double)TIME_MAX_VALUE)
 
5026
  {
 
5027
    tmp= TIME_MAX_VALUE;
 
5028
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
5029
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
 
5030
    error= 1;
 
5031
  }
 
5032
  else if (nr < (double)-TIME_MAX_VALUE)
 
5033
  {
 
5034
    tmp= -TIME_MAX_VALUE;
 
5035
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
5036
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
 
5037
    error= 1;
 
5038
  }
 
5039
  else
 
5040
  {
 
5041
    tmp=(long) floor(fabs(nr));                 // Remove fractions
 
5042
    if (nr < 0)
 
5043
      tmp= -tmp;
 
5044
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
5045
    {
 
5046
      tmp=0;
 
5047
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
5048
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
 
5049
                           MYSQL_TIMESTAMP_TIME);
 
5050
      error= 1;
 
5051
    }
 
5052
  }
 
5053
  int3store(ptr,tmp);
 
5054
  return error;
 
5055
}
 
5056
 
 
5057
 
 
5058
int Field_time::store(longlong nr, bool unsigned_val)
 
5059
{
 
5060
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5061
  long tmp;
 
5062
  int error= 0;
 
5063
  if (nr < (longlong) -TIME_MAX_VALUE && !unsigned_val)
 
5064
  {
 
5065
    tmp= -TIME_MAX_VALUE;
 
5066
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
5067
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
 
5068
                         MYSQL_TIMESTAMP_TIME, 1);
 
5069
    error= 1;
 
5070
  }
 
5071
  else if (nr > (longlong) TIME_MAX_VALUE || (nr < 0 && unsigned_val))
 
5072
  {
 
5073
    tmp= TIME_MAX_VALUE;
 
5074
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
5075
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
 
5076
                         MYSQL_TIMESTAMP_TIME, 1);
 
5077
    error= 1;
 
5078
  }
 
5079
  else
 
5080
  {
 
5081
    tmp=(long) nr;
 
5082
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
5083
    {
 
5084
      tmp=0;
 
5085
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
5086
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
 
5087
                           MYSQL_TIMESTAMP_TIME, 1);
 
5088
      error= 1;
 
5089
    }
 
5090
  }
 
5091
  int3store(ptr,tmp);
 
5092
  return error;
 
5093
}
 
5094
 
 
5095
 
 
5096
double Field_time::val_real(void)
 
5097
{
 
5098
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5099
  uint32 j= (uint32) uint3korr(ptr);
 
5100
  return (double) j;
 
5101
}
 
5102
 
 
5103
longlong Field_time::val_int(void)
 
5104
{
 
5105
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5106
  return (longlong) sint3korr(ptr);
 
5107
}
 
5108
 
 
5109
 
 
5110
/**
 
5111
  @note
 
5112
  This function is multi-byte safe as the result string is always of type
 
5113
  my_charset_bin
 
5114
*/
 
5115
 
 
5116
String *Field_time::val_str(String *val_buffer,
 
5117
                            String *val_ptr __attribute__((unused)))
 
5118
{
 
5119
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5120
  MYSQL_TIME ltime;
 
5121
  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
 
5122
  long tmp=(long) sint3korr(ptr);
 
5123
  ltime.neg= 0;
 
5124
  if (tmp < 0)
 
5125
  {
 
5126
    tmp= -tmp;
 
5127
    ltime.neg= 1;
 
5128
  }
 
5129
  ltime.day= (uint) 0;
 
5130
  ltime.hour= (uint) (tmp/10000);
 
5131
  ltime.minute= (uint) (tmp/100 % 100);
 
5132
  ltime.second= (uint) (tmp % 100);
 
5133
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
 
5134
  return val_buffer;
 
5135
}
 
5136
 
 
5137
 
 
5138
/**
 
5139
  @note
 
5140
  Normally we would not consider 'time' as a valid date, but we allow
 
5141
  get_date() here to be able to do things like
 
5142
  DATE_FORMAT(time, "%l.%i %p")
 
5143
*/
 
5144
 
 
5145
bool Field_time::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
5146
{
 
5147
  long tmp;
 
5148
  THD *thd= table ? table->in_use : current_thd;
 
5149
  if (!(fuzzydate & TIME_FUZZY_DATE))
 
5150
  {
 
5151
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
5152
                        ER_WARN_DATA_OUT_OF_RANGE,
 
5153
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
 
5154
                        thd->row_count);
 
5155
    return 1;
 
5156
  }
 
5157
  tmp=(long) sint3korr(ptr);
 
5158
  ltime->neg=0;
 
5159
  if (tmp < 0)
 
5160
  {
 
5161
    ltime->neg= 1;
 
5162
    tmp=-tmp;
 
5163
  }
 
5164
  ltime->hour=tmp/10000;
 
5165
  tmp-=ltime->hour*10000;
 
5166
  ltime->minute=   tmp/100;
 
5167
  ltime->second= tmp % 100;
 
5168
  ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
 
5169
  return 0;
 
5170
}
 
5171
 
 
5172
 
 
5173
bool Field_time::get_time(MYSQL_TIME *ltime)
 
5174
{
 
5175
  long tmp=(long) sint3korr(ptr);
 
5176
  ltime->neg=0;
 
5177
  if (tmp < 0)
 
5178
  {
 
5179
    ltime->neg= 1;
 
5180
    tmp=-tmp;
 
5181
  }
 
5182
  ltime->day= 0;
 
5183
  ltime->hour=   (int) (tmp/10000);
 
5184
  tmp-=ltime->hour*10000;
 
5185
  ltime->minute= (int) tmp/100;
 
5186
  ltime->second= (int) tmp % 100;
 
5187
  ltime->second_part=0;
 
5188
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
 
5189
  return 0;
 
5190
}
 
5191
 
 
5192
 
 
5193
bool Field_time::send_binary(Protocol *protocol)
 
5194
{
 
5195
  MYSQL_TIME tm;
 
5196
  Field_time::get_time(&tm);
 
5197
  tm.day= tm.hour/24;                           // Move hours to days
 
5198
  tm.hour-= tm.day*24;
 
5199
  return protocol->store_time(&tm);
 
5200
}
 
5201
 
 
5202
 
 
5203
int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
5204
{
 
5205
  int32 a,b;
 
5206
  a=(int32) sint3korr(a_ptr);
 
5207
  b=(int32) sint3korr(b_ptr);
 
5208
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
5209
}
 
5210
 
 
5211
void Field_time::sort_string(uchar *to,uint length __attribute__((unused)))
 
5212
{
 
5213
  to[0] = (uchar) (ptr[2] ^ 128);
 
5214
  to[1] = ptr[1];
 
5215
  to[2] = ptr[0];
 
5216
}
 
5217
 
 
5218
void Field_time::sql_type(String &res) const
 
5219
{
 
5220
  res.set_ascii(STRING_WITH_LEN("time"));
 
5221
}
 
5222
 
 
5223
/****************************************************************************
 
5224
** year type
 
5225
** Save in a byte the year 0, 1901->2155
 
5226
** Can handle 2 byte or 4 byte years!
 
5227
****************************************************************************/
 
5228
 
 
5229
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
 
5230
{
 
5231
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5232
  char *end;
 
5233
  int error;
 
5234
  longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
 
5235
 
 
5236
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 || 
 
5237
      error == MY_ERRNO_ERANGE)
 
5238
  {
 
5239
    *ptr=0;
 
5240
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
5241
    return 1;
 
5242
  }
 
5243
  if (table->in_use->count_cuted_fields && 
 
5244
      (error= check_int(cs, from, len, end, error)))
 
5245
  {
 
5246
    if (error == 1)  /* empty or incorrect string */
 
5247
    {
 
5248
      *ptr= 0;
 
5249
      return 1;
 
5250
    }
 
5251
    error= 1;
 
5252
  }
 
5253
 
 
5254
  if (nr != 0 || len != 4)
 
5255
  {
 
5256
    if (nr < YY_PART_YEAR)
 
5257
      nr+=100;                                  // 2000 - 2069
 
5258
    else if (nr > 1900)
 
5259
      nr-= 1900;
 
5260
  }
 
5261
  *ptr= (char) (uchar) nr;
 
5262
  return error;
 
5263
}
 
5264
 
 
5265
 
 
5266
int Field_year::store(double nr)
 
5267
{
 
5268
  if (nr < 0.0 || nr >= 2155.0)
 
5269
  {
 
5270
    (void) Field_year::store((longlong) -1, FALSE);
 
5271
    return 1;
 
5272
  }
 
5273
  return Field_year::store((longlong) nr, FALSE);
 
5274
}
 
5275
 
 
5276
 
 
5277
int Field_year::store(longlong nr, bool unsigned_val)
 
5278
{
 
5279
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5280
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
 
5281
  {
 
5282
    *ptr= 0;
 
5283
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
5284
    return 1;
 
5285
  }
 
5286
  if (nr != 0 || field_length != 4)             // 0000 -> 0; 00 -> 2000
 
5287
  {
 
5288
    if (nr < YY_PART_YEAR)
 
5289
      nr+=100;                                  // 2000 - 2069
 
5290
    else if (nr > 1900)
 
5291
      nr-= 1900;
 
5292
  }
 
5293
  *ptr= (char) (uchar) nr;
 
5294
  return 0;
 
5295
}
 
5296
 
 
5297
 
 
5298
bool Field_year::send_binary(Protocol *protocol)
 
5299
{
 
5300
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5301
  uint64_t tmp= Field_year::val_int();
 
5302
  return protocol->store_short(tmp);
 
5303
}
 
5304
 
 
5305
 
 
5306
double Field_year::val_real(void)
 
5307
{
 
5308
  return (double) Field_year::val_int();
 
5309
}
 
5310
 
 
5311
 
 
5312
longlong Field_year::val_int(void)
 
5313
{
 
5314
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5315
  int tmp= (int) ptr[0];
 
5316
  if (field_length != 4)
 
5317
    tmp%=100;                                   // Return last 2 char
 
5318
  else if (tmp)
 
5319
    tmp+=1900;
 
5320
  return (longlong) tmp;
 
5321
}
 
5322
 
 
5323
 
 
5324
String *Field_year::val_str(String *val_buffer,
 
5325
                            String *val_ptr __attribute__((unused)))
 
5326
{
 
5327
  val_buffer->alloc(5);
 
5328
  val_buffer->length(field_length);
 
5329
  char *to=(char*) val_buffer->ptr();
 
5330
  sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
 
5331
  return val_buffer;
 
5332
}
 
5333
 
 
5334
 
 
5335
void Field_year::sql_type(String &res) const
 
5336
{
 
5337
  CHARSET_INFO *cs=res.charset();
 
5338
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
 
5339
                          "year(%d)",(int) field_length));
 
5340
}
 
5341
 
 
5342
 
 
5343
/****************************************************************************
 
5344
** date type
 
5345
** In string context: YYYY-MM-DD
 
5346
** In number context: YYYYMMDD
 
5347
** Stored as a 4 byte unsigned int
 
5348
****************************************************************************/
 
5349
 
 
5350
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
 
5351
{
 
5352
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5353
  MYSQL_TIME l_time;
 
5354
  uint32 tmp;
 
5355
  int error;
 
5356
  THD *thd= table ? table->in_use : current_thd;
 
5357
 
 
5358
  if (str_to_datetime(from, len, &l_time, TIME_FUZZY_DATE |
 
5359
                      (thd->variables.sql_mode &
 
5360
                       (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
5361
                        MODE_INVALID_DATES)),
 
5362
                      &error) <= MYSQL_TIMESTAMP_ERROR)
 
5363
  {
 
5364
    tmp= 0;
 
5365
    error= 2;
 
5366
  }
 
5367
  else
 
5368
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
 
5369
 
 
5370
  if (error)
 
5371
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
5372
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
 
5373
 
 
5374
#ifdef WORDS_BIGENDIAN
 
5375
  if (table && table->s->db_low_byte_first)
 
5376
  {
 
5377
    int4store(ptr,tmp);
 
5378
  }
 
5379
  else
 
5380
#endif
 
5381
    longstore(ptr,tmp);
 
5382
  return error;
 
5383
}
 
5384
 
 
5385
 
 
5386
int Field_date::store(double nr)
 
5387
{
 
5388
  longlong tmp;
 
5389
  int error= 0;
 
5390
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
 
5391
    nr=floor(nr/1000000.0);                     // Timestamp to date
 
5392
  if (nr < 0.0 || nr > 99991231.0)
 
5393
  {
 
5394
    tmp= LL(0);
 
5395
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
5396
                         ER_WARN_DATA_OUT_OF_RANGE,
 
5397
                         nr, MYSQL_TIMESTAMP_DATE);
 
5398
    error= 1;
 
5399
  }
 
5400
  else
 
5401
    tmp= (longlong) rint(nr);
 
5402
 
 
5403
  return Field_date::store(tmp, TRUE);
 
5404
}
 
5405
 
 
5406
 
 
5407
int Field_date::store(longlong nr, bool unsigned_val)
 
5408
{
 
5409
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5410
  MYSQL_TIME not_used;
 
5411
  int error;
 
5412
  longlong initial_nr= nr;
 
5413
  THD *thd= table ? table->in_use : current_thd;
 
5414
 
 
5415
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
 
5416
                                         (thd->variables.sql_mode &
 
5417
                                          (MODE_NO_ZERO_IN_DATE |
 
5418
                                           MODE_NO_ZERO_DATE |
 
5419
                                           MODE_INVALID_DATES))), &error);
 
5420
 
 
5421
  if (nr == LL(-1))
 
5422
  {
 
5423
    nr= 0;
 
5424
    error= 2;
 
5425
  }
 
5426
 
 
5427
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
 
5428
    nr= (longlong) floor(nr/1000000.0);         // Timestamp to date
 
5429
 
 
5430
  if (error)
 
5431
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
5432
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
 
5433
                         WARN_DATA_TRUNCATED, initial_nr,
 
5434
                         MYSQL_TIMESTAMP_DATETIME, 1);
 
5435
 
 
5436
#ifdef WORDS_BIGENDIAN
 
5437
  if (table && table->s->db_low_byte_first)
 
5438
  {
 
5439
    int4store(ptr, nr);
 
5440
  }
 
5441
  else
 
5442
#endif
 
5443
    longstore(ptr, nr);
 
5444
  return error;
 
5445
}
 
5446
 
 
5447
 
 
5448
bool Field_date::send_binary(Protocol *protocol)
 
5449
{
 
5450
  longlong tmp= Field_date::val_int();
 
5451
  MYSQL_TIME tm;
 
5452
  tm.year= (uint32) tmp/10000L % 10000;
 
5453
  tm.month= (uint32) tmp/100 % 100;
 
5454
  tm.day= (uint32) tmp % 100;
 
5455
  return protocol->store_date(&tm);
 
5456
}
 
5457
 
 
5458
 
 
5459
double Field_date::val_real(void)
 
5460
{
 
5461
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5462
  int32 j;
 
5463
#ifdef WORDS_BIGENDIAN
 
5464
  if (table && table->s->db_low_byte_first)
 
5465
    j=sint4korr(ptr);
 
5466
  else
 
5467
#endif
 
5468
    longget(j,ptr);
 
5469
  return (double) (uint32) j;
 
5470
}
 
5471
 
 
5472
 
 
5473
longlong Field_date::val_int(void)
 
5474
{
 
5475
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5476
  int32 j;
 
5477
#ifdef WORDS_BIGENDIAN
 
5478
  if (table && table->s->db_low_byte_first)
 
5479
    j=sint4korr(ptr);
 
5480
  else
 
5481
#endif
 
5482
    longget(j,ptr);
 
5483
  return (longlong) (uint32) j;
 
5484
}
 
5485
 
 
5486
 
 
5487
String *Field_date::val_str(String *val_buffer,
 
5488
                            String *val_ptr __attribute__((unused)))
 
5489
{
 
5490
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5491
  MYSQL_TIME ltime;
 
5492
  val_buffer->alloc(field_length);
 
5493
  int32 tmp;
 
5494
#ifdef WORDS_BIGENDIAN
 
5495
  if (table && table->s->db_low_byte_first)
 
5496
    tmp=sint4korr(ptr);
 
5497
  else
 
5498
#endif
 
5499
    longget(tmp,ptr);
 
5500
  ltime.neg= 0;
 
5501
  ltime.year= (int) ((uint32) tmp/10000L % 10000);
 
5502
  ltime.month= (int) ((uint32) tmp/100 % 100);
 
5503
  ltime.day= (int) ((uint32) tmp % 100);
 
5504
  make_date((DATE_TIME_FORMAT *) 0, &ltime, val_buffer);
 
5505
  return val_buffer;
 
5506
}
 
5507
 
 
5508
 
 
5509
bool Field_date::get_time(MYSQL_TIME *ltime)
 
5510
{
 
5511
  bzero((char *)ltime, sizeof(MYSQL_TIME));
 
5512
  return 0;
 
5513
}
 
5514
 
 
5515
 
 
5516
int Field_date::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
5517
{
 
5518
  int32 a,b;
 
5519
#ifdef WORDS_BIGENDIAN
 
5520
  if (table && table->s->db_low_byte_first)
 
5521
  {
 
5522
    a=sint4korr(a_ptr);
 
5523
    b=sint4korr(b_ptr);
 
5524
  }
 
5525
  else
 
5526
#endif
 
5527
  {
 
5528
    longget(a,a_ptr);
 
5529
    longget(b,b_ptr);
 
5530
  }
 
5531
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
 
5532
}
 
5533
 
 
5534
 
 
5535
void Field_date::sort_string(uchar *to,uint length __attribute__((unused)))
 
5536
{
 
5537
#ifdef WORDS_BIGENDIAN
 
5538
  if (!table || !table->s->db_low_byte_first)
 
5539
  {
 
5540
    to[0] = ptr[0];
 
5541
    to[1] = ptr[1];
 
5542
    to[2] = ptr[2];
 
5543
    to[3] = ptr[3];
 
5544
  }
 
5545
  else
 
5546
#endif
 
5547
  {
 
5548
    to[0] = ptr[3];
 
5549
    to[1] = ptr[2];
 
5550
    to[2] = ptr[1];
 
5551
    to[3] = ptr[0];
 
5552
  }
 
5553
}
 
5554
 
 
5555
void Field_date::sql_type(String &res) const
 
5556
{
 
5557
  res.set_ascii(STRING_WITH_LEN("date"));
 
5558
}
 
5559
 
 
5560
 
 
5561
/****************************************************************************
 
5562
** The new date type
 
5563
** This is identical to the old date type, but stored on 3 bytes instead of 4
 
5564
** In number context: YYYYMMDD
 
5565
****************************************************************************/
 
5566
 
 
5567
/*
 
5568
  Store string into a date field
 
5569
 
 
5570
  SYNOPSIS
 
5571
    Field_newdate::store()
 
5572
    from                Date string
 
5573
    len                 Length of date field
 
5574
    cs                  Character set (not used)
 
5575
 
 
5576
  RETURN
 
5577
    0  ok
 
5578
    1  Value was cut during conversion
 
5579
    2  Wrong date string
 
5580
    3  Datetime value that was cut (warning level NOTE)
 
5581
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
 
5582
       nearly-identical class Field_date doesn't ever return 3 from its
 
5583
       store function.
 
5584
*/
 
5585
 
 
5586
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
 
5587
{
 
5588
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5589
  long tmp;
 
5590
  MYSQL_TIME l_time;
 
5591
  int error;
 
5592
  THD *thd= table ? table->in_use : current_thd;
 
5593
  enum enum_mysql_timestamp_type ret;
 
5594
  if ((ret= str_to_datetime(from, len, &l_time,
 
5595
                            (TIME_FUZZY_DATE |
 
5596
                             (thd->variables.sql_mode &
 
5597
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
5598
                               MODE_INVALID_DATES))),
 
5599
                            &error)) <= MYSQL_TIMESTAMP_ERROR)
 
5600
  {
 
5601
    tmp= 0;
 
5602
    error= 2;
 
5603
  }
 
5604
  else
 
5605
  {
 
5606
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
5607
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
 
5608
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
5609
      error= 3;                                 // Datetime was cut (note)
 
5610
  }
 
5611
 
 
5612
  if (error)
 
5613
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
5614
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
5615
                         WARN_DATA_TRUNCATED,
 
5616
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
 
5617
 
 
5618
  int3store(ptr, tmp);
 
5619
  return error;
 
5620
}
 
5621
 
 
5622
 
 
5623
int Field_newdate::store(double nr)
 
5624
{
 
5625
  if (nr < 0.0 || nr > 99991231235959.0)
 
5626
  {
 
5627
    int3store(ptr,(int32) 0);
 
5628
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
5629
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
 
5630
    return 1;
 
5631
  }
 
5632
  return Field_newdate::store((longlong) rint(nr), FALSE);
 
5633
}
 
5634
 
 
5635
 
 
5636
int Field_newdate::store(longlong nr, bool unsigned_val)
 
5637
{
 
5638
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5639
  MYSQL_TIME l_time;
 
5640
  longlong tmp;
 
5641
  int error;
 
5642
  THD *thd= table ? table->in_use : current_thd;
 
5643
  if (number_to_datetime(nr, &l_time,
 
5644
                         (TIME_FUZZY_DATE |
 
5645
                          (thd->variables.sql_mode &
 
5646
                           (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
5647
                            MODE_INVALID_DATES))),
 
5648
                         &error) == LL(-1))
 
5649
  {
 
5650
    tmp= 0L;
 
5651
    error= 2;
 
5652
  }
 
5653
  else
 
5654
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
5655
 
 
5656
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
 
5657
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
5658
    error= 3;
 
5659
 
 
5660
  if (error)
 
5661
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
5662
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
5663
                         error == 2 ? 
 
5664
                         ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED,
 
5665
                         nr,MYSQL_TIMESTAMP_DATE, 1);
 
5666
 
 
5667
  int3store(ptr,tmp);
 
5668
  return error;
 
5669
}
 
5670
 
 
5671
 
 
5672
int Field_newdate::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
 
5673
{
 
5674
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5675
  long tmp;
 
5676
  int error= 0;
 
5677
  if (time_type == MYSQL_TIMESTAMP_DATE ||
 
5678
      time_type == MYSQL_TIMESTAMP_DATETIME)
 
5679
  {
 
5680
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
 
5681
    if (check_date(ltime, tmp != 0,
 
5682
                   (TIME_FUZZY_DATE |
 
5683
                    (current_thd->variables.sql_mode &
 
5684
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
5685
                      MODE_INVALID_DATES))), &error))
 
5686
    {
 
5687
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
5688
      String str(buff, sizeof(buff), &my_charset_latin1);
 
5689
      make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
 
5690
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
5691
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
 
5692
    }
 
5693
    if (!error && ltime->time_type != MYSQL_TIMESTAMP_DATE &&
 
5694
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
 
5695
    {
 
5696
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
5697
      String str(buff, sizeof(buff), &my_charset_latin1);
 
5698
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
5699
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_NOTE,
 
5700
                           WARN_DATA_TRUNCATED,
 
5701
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
 
5702
      error= 3;
 
5703
    }
 
5704
  }
 
5705
  else
 
5706
  {
 
5707
    tmp=0;
 
5708
    error= 1;
 
5709
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
5710
  }
 
5711
  int3store(ptr,tmp);
 
5712
  return error;
 
5713
}
 
5714
 
 
5715
 
 
5716
bool Field_newdate::send_binary(Protocol *protocol)
 
5717
{
 
5718
  MYSQL_TIME tm;
 
5719
  Field_newdate::get_date(&tm,0);
 
5720
  return protocol->store_date(&tm);
 
5721
}
 
5722
 
 
5723
 
 
5724
double Field_newdate::val_real(void)
 
5725
{
 
5726
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5727
  return (double) Field_newdate::val_int();
 
5728
}
 
5729
 
 
5730
 
 
5731
longlong Field_newdate::val_int(void)
 
5732
{
 
5733
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5734
  ulong j= uint3korr(ptr);
 
5735
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
 
5736
  return (longlong) j;
 
5737
}
 
5738
 
 
5739
 
 
5740
String *Field_newdate::val_str(String *val_buffer,
 
5741
                               String *val_ptr __attribute__((unused)))
 
5742
{
 
5743
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5744
  val_buffer->alloc(field_length);
 
5745
  val_buffer->length(field_length);
 
5746
  uint32 tmp=(uint32) uint3korr(ptr);
 
5747
  int part;
 
5748
  char *pos=(char*) val_buffer->ptr()+10;
 
5749
 
 
5750
  /* Open coded to get more speed */
 
5751
  *pos--=0;                                     // End NULL
 
5752
  part=(int) (tmp & 31);
 
5753
  *pos--= (char) ('0'+part%10);
 
5754
  *pos--= (char) ('0'+part/10);
 
5755
  *pos--= '-';
 
5756
  part=(int) (tmp >> 5 & 15);
 
5757
  *pos--= (char) ('0'+part%10);
 
5758
  *pos--= (char) ('0'+part/10);
 
5759
  *pos--= '-';
 
5760
  part=(int) (tmp >> 9);
 
5761
  *pos--= (char) ('0'+part%10); part/=10;
 
5762
  *pos--= (char) ('0'+part%10); part/=10;
 
5763
  *pos--= (char) ('0'+part%10); part/=10;
 
5764
  *pos=   (char) ('0'+part);
 
5765
  return val_buffer;
 
5766
}
 
5767
 
 
5768
 
 
5769
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
 
5770
{
 
5771
  uint32 tmp=(uint32) uint3korr(ptr);
 
5772
  ltime->day=   tmp & 31;
 
5773
  ltime->month= (tmp >> 5) & 15;
 
5774
  ltime->year=  (tmp >> 9);
 
5775
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
 
5776
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
 
5777
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
 
5778
          1 : 0);
 
5779
}
 
5780
 
 
5781
 
 
5782
bool Field_newdate::get_time(MYSQL_TIME *ltime)
 
5783
{
 
5784
  return Field_newdate::get_date(ltime,0);
 
5785
}
 
5786
 
 
5787
 
 
5788
int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
5789
{
 
5790
  uint32 a,b;
 
5791
  a=(uint32) uint3korr(a_ptr);
 
5792
  b=(uint32) uint3korr(b_ptr);
 
5793
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
5794
}
 
5795
 
 
5796
 
 
5797
void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
 
5798
{
 
5799
  to[0] = ptr[2];
 
5800
  to[1] = ptr[1];
 
5801
  to[2] = ptr[0];
 
5802
}
 
5803
 
 
5804
 
 
5805
void Field_newdate::sql_type(String &res) const
 
5806
{
 
5807
  res.set_ascii(STRING_WITH_LEN("date"));
 
5808
}
 
5809
 
 
5810
 
 
5811
/****************************************************************************
 
5812
** datetime type
 
5813
** In string context: YYYY-MM-DD HH:MM:DD
 
5814
** In number context: YYYYMMDDHHMMDD
 
5815
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
 
5816
****************************************************************************/
 
5817
 
 
5818
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
 
5819
{
 
5820
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5821
  MYSQL_TIME time_tmp;
 
5822
  int error;
 
5823
  uint64_t tmp= 0;
 
5824
  enum enum_mysql_timestamp_type func_res;
 
5825
  THD *thd= table ? table->in_use : current_thd;
 
5826
 
 
5827
  func_res= str_to_datetime(from, len, &time_tmp,
 
5828
                            (TIME_FUZZY_DATE |
 
5829
                             (thd->variables.sql_mode &
 
5830
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
5831
                               MODE_INVALID_DATES))),
 
5832
                            &error);
 
5833
  if ((int) func_res > (int) MYSQL_TIMESTAMP_ERROR)
 
5834
    tmp= TIME_to_ulonglong_datetime(&time_tmp);
 
5835
  else
 
5836
    error= 1;                                 // Fix if invalid zero date
 
5837
 
 
5838
  if (error)
 
5839
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
5840
                         ER_WARN_DATA_OUT_OF_RANGE,
 
5841
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
 
5842
 
 
5843
#ifdef WORDS_BIGENDIAN
 
5844
  if (table && table->s->db_low_byte_first)
 
5845
  {
 
5846
    int8store(ptr,tmp);
 
5847
  }
 
5848
  else
 
5849
#endif
 
5850
    longlongstore(ptr,tmp);
 
5851
  return error;
 
5852
}
 
5853
 
 
5854
 
 
5855
int Field_datetime::store(double nr)
 
5856
{
 
5857
  int error= 0;
 
5858
  if (nr < 0.0 || nr > 99991231235959.0)
 
5859
  {
 
5860
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
5861
                         ER_WARN_DATA_OUT_OF_RANGE,
 
5862
                         nr, MYSQL_TIMESTAMP_DATETIME);
 
5863
    nr= 0.0;
 
5864
    error= 1;
 
5865
  }
 
5866
  error|= Field_datetime::store((longlong) rint(nr), FALSE);
 
5867
  return error;
 
5868
}
 
5869
 
 
5870
 
 
5871
int Field_datetime::store(longlong nr, bool unsigned_val)
 
5872
{
 
5873
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5874
  MYSQL_TIME not_used;
 
5875
  int error;
 
5876
  longlong initial_nr= nr;
 
5877
  THD *thd= table ? table->in_use : current_thd;
 
5878
 
 
5879
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
 
5880
                                         (thd->variables.sql_mode &
 
5881
                                          (MODE_NO_ZERO_IN_DATE |
 
5882
                                           MODE_NO_ZERO_DATE |
 
5883
                                           MODE_INVALID_DATES))), &error);
 
5884
 
 
5885
  if (nr == LL(-1))
 
5886
  {
 
5887
    nr= 0;
 
5888
    error= 2;
 
5889
  }
 
5890
 
 
5891
  if (error)
 
5892
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
5893
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
 
5894
                         WARN_DATA_TRUNCATED, initial_nr,
 
5895
                         MYSQL_TIMESTAMP_DATETIME, 1);
 
5896
 
 
5897
#ifdef WORDS_BIGENDIAN
 
5898
  if (table && table->s->db_low_byte_first)
 
5899
  {
 
5900
    int8store(ptr,nr);
 
5901
  }
 
5902
  else
 
5903
#endif
 
5904
    longlongstore(ptr,nr);
 
5905
  return error;
 
5906
}
 
5907
 
 
5908
 
 
5909
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
 
5910
{
 
5911
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
5912
  longlong tmp;
 
5913
  int error= 0;
 
5914
  /*
 
5915
    We don't perform range checking here since values stored in TIME
 
5916
    structure always fit into DATETIME range.
 
5917
  */
 
5918
  if (time_type == MYSQL_TIMESTAMP_DATE ||
 
5919
      time_type == MYSQL_TIMESTAMP_DATETIME)
 
5920
  {
 
5921
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
 
5922
         (ltime->hour*10000L+ltime->minute*100+ltime->second));
 
5923
    if (check_date(ltime, tmp != 0,
 
5924
                   (TIME_FUZZY_DATE |
 
5925
                    (current_thd->variables.sql_mode &
 
5926
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
5927
                      MODE_INVALID_DATES))), &error))
 
5928
    {
 
5929
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
5930
      String str(buff, sizeof(buff), &my_charset_latin1);
 
5931
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
5932
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
5933
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATETIME,1);
 
5934
    }
 
5935
  }
 
5936
  else
 
5937
  {
 
5938
    tmp=0;
 
5939
    error= 1;
 
5940
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
5941
  }
 
5942
#ifdef WORDS_BIGENDIAN
 
5943
  if (table && table->s->db_low_byte_first)
 
5944
  {
 
5945
    int8store(ptr,tmp);
 
5946
  }
 
5947
  else
 
5948
#endif
 
5949
    longlongstore(ptr,tmp);
 
5950
  return error;
 
5951
}
 
5952
 
 
5953
bool Field_datetime::send_binary(Protocol *protocol)
 
5954
{
 
5955
  MYSQL_TIME tm;
 
5956
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
 
5957
  return protocol->store(&tm);
 
5958
}
 
5959
 
 
5960
 
 
5961
double Field_datetime::val_real(void)
 
5962
{
 
5963
  return (double) Field_datetime::val_int();
 
5964
}
 
5965
 
 
5966
longlong Field_datetime::val_int(void)
 
5967
{
 
5968
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5969
  longlong j;
 
5970
#ifdef WORDS_BIGENDIAN
 
5971
  if (table && table->s->db_low_byte_first)
 
5972
    j=sint8korr(ptr);
 
5973
  else
 
5974
#endif
 
5975
    longlongget(j,ptr);
 
5976
  return j;
 
5977
}
 
5978
 
 
5979
 
 
5980
String *Field_datetime::val_str(String *val_buffer,
 
5981
                                String *val_ptr __attribute__((unused)))
 
5982
{
 
5983
  ASSERT_COLUMN_MARKED_FOR_READ;
 
5984
  val_buffer->alloc(field_length);
 
5985
  val_buffer->length(field_length);
 
5986
  uint64_t tmp;
 
5987
  long part1,part2;
 
5988
  char *pos;
 
5989
  int part3;
 
5990
 
 
5991
#ifdef WORDS_BIGENDIAN
 
5992
  if (table && table->s->db_low_byte_first)
 
5993
    tmp=sint8korr(ptr);
 
5994
  else
 
5995
#endif
 
5996
    longlongget(tmp,ptr);
 
5997
 
 
5998
  /*
 
5999
    Avoid problem with slow longlong arithmetic and sprintf
 
6000
  */
 
6001
 
 
6002
  part1=(long) (tmp/LL(1000000));
 
6003
  part2=(long) (tmp - (uint64_t) part1*LL(1000000));
 
6004
 
 
6005
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
 
6006
  *pos--=0;
 
6007
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
 
6008
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
 
6009
  *pos--= ':';
 
6010
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
6011
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
6012
  *pos--= ':';
 
6013
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
6014
  *pos--= (char) ('0'+(char) part3);
 
6015
  *pos--= ' ';
 
6016
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
6017
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
6018
  *pos--= '-';
 
6019
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
6020
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
 
6021
  *pos--= '-';
 
6022
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
6023
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
6024
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
6025
  *pos=(char) ('0'+(char) part3);
 
6026
  return val_buffer;
 
6027
}
 
6028
 
 
6029
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
6030
{
 
6031
  longlong tmp=Field_datetime::val_int();
 
6032
  uint32 part1,part2;
 
6033
  part1=(uint32) (tmp/LL(1000000));
 
6034
  part2=(uint32) (tmp - (uint64_t) part1*LL(1000000));
 
6035
 
 
6036
  ltime->time_type=     MYSQL_TIMESTAMP_DATETIME;
 
6037
  ltime->neg=           0;
 
6038
  ltime->second_part=   0;
 
6039
  ltime->second=        (int) (part2%100);
 
6040
  ltime->minute=        (int) (part2/100%100);
 
6041
  ltime->hour=          (int) (part2/10000);
 
6042
  ltime->day=           (int) (part1%100);
 
6043
  ltime->month=         (int) (part1/100%100);
 
6044
  ltime->year=          (int) (part1/10000);
 
6045
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
 
6046
}
 
6047
 
 
6048
bool Field_datetime::get_time(MYSQL_TIME *ltime)
 
6049
{
 
6050
  return Field_datetime::get_date(ltime,0);
 
6051
}
 
6052
 
 
6053
int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
6054
{
 
6055
  longlong a,b;
 
6056
#ifdef WORDS_BIGENDIAN
 
6057
  if (table && table->s->db_low_byte_first)
 
6058
  {
 
6059
    a=sint8korr(a_ptr);
 
6060
    b=sint8korr(b_ptr);
 
6061
  }
 
6062
  else
 
6063
#endif
 
6064
  {
 
6065
    longlongget(a,a_ptr);
 
6066
    longlongget(b,b_ptr);
 
6067
  }
 
6068
  return ((uint64_t) a < (uint64_t) b) ? -1 :
 
6069
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
6070
}
 
6071
 
 
6072
void Field_datetime::sort_string(uchar *to,uint length __attribute__((unused)))
 
6073
{
 
6074
#ifdef WORDS_BIGENDIAN
 
6075
  if (!table || !table->s->db_low_byte_first)
 
6076
  {
 
6077
    to[0] = ptr[0];
 
6078
    to[1] = ptr[1];
 
6079
    to[2] = ptr[2];
 
6080
    to[3] = ptr[3];
 
6081
    to[4] = ptr[4];
 
6082
    to[5] = ptr[5];
 
6083
    to[6] = ptr[6];
 
6084
    to[7] = ptr[7];
 
6085
  }
 
6086
  else
 
6087
#endif
 
6088
  {
 
6089
    to[0] = ptr[7];
 
6090
    to[1] = ptr[6];
 
6091
    to[2] = ptr[5];
 
6092
    to[3] = ptr[4];
 
6093
    to[4] = ptr[3];
 
6094
    to[5] = ptr[2];
 
6095
    to[6] = ptr[1];
 
6096
    to[7] = ptr[0];
 
6097
  }
 
6098
}
 
6099
 
 
6100
 
 
6101
void Field_datetime::sql_type(String &res) const
 
6102
{
 
6103
  res.set_ascii(STRING_WITH_LEN("datetime"));
 
6104
}
 
6105
 
 
6106
/****************************************************************************
 
6107
** string type
 
6108
** A string may be varchar or binary
 
6109
****************************************************************************/
1166
6110
 
1167
6111
/*
1168
6112
  Report "not well formed" or "cannot convert" error
1178
6122
 
1179
6123
  NOTES
1180
6124
    As of version 5.0 both cases return the same error:
1181
 
 
 
6125
  
1182
6126
      "Invalid string value: 'xxx' for column 't' at row 1"
1183
 
 
 
6127
  
1184
6128
  Future versions will possibly introduce a new error message:
1185
6129
 
1186
6130
      "Cannot convert character string: 'xxx' for column 't' at row 1"
1187
6131
 
1188
6132
  RETURN
1189
 
    false - If errors didn't happen
1190
 
    true  - If an error happened
 
6133
    FALSE - If errors didn't happen
 
6134
    TRUE  - If an error happened
1191
6135
*/
1192
6136
 
1193
 
bool
 
6137
static bool
1194
6138
check_string_copy_error(Field_str *field,
1195
6139
                        const char *well_formed_error_pos,
1196
6140
                        const char *cannot_convert_error_pos,
1197
6141
                        const char *end,
1198
 
                        const CHARSET_INFO * const cs)
 
6142
                        CHARSET_INFO *cs)
1199
6143
{
1200
6144
  const char *pos, *end_orig;
1201
6145
  char tmp[64], *t;
1202
6146
  
1203
6147
  if (!(pos= well_formed_error_pos) &&
1204
6148
      !(pos= cannot_convert_error_pos))
1205
 
    return false;
 
6149
    return FALSE;
1206
6150
 
1207
6151
  end_orig= end;
1208
6152
  set_if_smaller(end, pos + 6);
1241
6185
  *t= '\0';
1242
6186
  push_warning_printf(field->table->in_use, 
1243
6187
                      field->table->in_use->abort_on_warning ?
1244
 
                      DRIZZLE_ERROR::WARN_LEVEL_ERROR :
1245
 
                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
6188
                      MYSQL_ERROR::WARN_LEVEL_ERROR :
 
6189
                      MYSQL_ERROR::WARN_LEVEL_WARN,
1246
6190
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
1247
6191
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1248
6192
                      "string", tmp, field->field_name,
1249
 
                      (uint32_t) field->table->in_use->row_count);
1250
 
  return true;
1251
 
}
1252
 
 
1253
 
uint32_t Field::is_equal(Create_field *new_field)
 
6193
                      (ulong) field->table->in_use->row_count);
 
6194
  return TRUE;
 
6195
}
 
6196
 
 
6197
 
 
6198
/*
 
6199
  Check if we lost any important data and send a truncation error/warning
 
6200
 
 
6201
  SYNOPSIS
 
6202
    Field_longstr::report_if_important_data()
 
6203
    ptr                      - Truncated rest of string
 
6204
    end                      - End of truncated string
 
6205
 
 
6206
  RETURN VALUES
 
6207
    0   - None was truncated (or we don't count cut fields)
 
6208
    2   - Some bytes was truncated
 
6209
 
 
6210
  NOTE
 
6211
    Check if we lost any important data (anything in a binary string,
 
6212
    or any non-space in others). If only trailing spaces was lost,
 
6213
    send a truncation note, otherwise send a truncation error.
 
6214
*/
 
6215
 
 
6216
int
 
6217
Field_longstr::report_if_important_data(const char *ptr, const char *end)
 
6218
{
 
6219
  if ((ptr < end) && table->in_use->count_cuted_fields)
 
6220
  {
 
6221
    if (test_if_important_data(field_charset, ptr, end))
 
6222
    {
 
6223
      if (table->in_use->abort_on_warning)
 
6224
        set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
6225
      else
 
6226
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6227
    }
 
6228
    else /* If we lost only spaces then produce a NOTE, not a WARNING */
 
6229
      set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
 
6230
    return 2;
 
6231
  }
 
6232
  return 0;
 
6233
}
 
6234
 
 
6235
 
 
6236
        /* Copy a string and fill with space */
 
6237
 
 
6238
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
 
6239
{
 
6240
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
6241
  uint copy_length;
 
6242
  const char *well_formed_error_pos;
 
6243
  const char *cannot_convert_error_pos;
 
6244
  const char *from_end_pos;
 
6245
 
 
6246
  /* See the comment for Field_long::store(long long) */
 
6247
  DBUG_ASSERT(table->in_use == current_thd);
 
6248
 
 
6249
  copy_length= well_formed_copy_nchars(field_charset,
 
6250
                                       (char*) ptr, field_length,
 
6251
                                       cs, from, length,
 
6252
                                       field_length / field_charset->mbmaxlen,
 
6253
                                       &well_formed_error_pos,
 
6254
                                       &cannot_convert_error_pos,
 
6255
                                       &from_end_pos);
 
6256
 
 
6257
  /* Append spaces if the string was shorter than the field. */
 
6258
  if (copy_length < field_length)
 
6259
    field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
 
6260
                              field_length-copy_length,
 
6261
                              field_charset->pad_char);
 
6262
 
 
6263
  if (check_string_copy_error(this, well_formed_error_pos,
 
6264
                              cannot_convert_error_pos, from + length, cs))
 
6265
    return 2;
 
6266
 
 
6267
  return report_if_important_data(from_end_pos, from + length);
 
6268
}
 
6269
 
 
6270
 
 
6271
/**
 
6272
  Store double value in Field_string or Field_varstring.
 
6273
 
 
6274
  Pretty prints double number into field_length characters buffer.
 
6275
 
 
6276
  @param nr            number
 
6277
*/
 
6278
 
 
6279
int Field_str::store(double nr)
 
6280
{
 
6281
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
6282
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
6283
  uint local_char_length= field_length / charset()->mbmaxlen;
 
6284
  size_t length;
 
6285
  my_bool error;
 
6286
 
 
6287
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
 
6288
  if (error)
 
6289
  {
 
6290
    if (table->in_use->abort_on_warning)
 
6291
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
6292
    else
 
6293
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6294
  }
 
6295
  return store(buff, length, charset());
 
6296
}
 
6297
 
 
6298
 
 
6299
uint Field::is_equal(Create_field *new_field)
1254
6300
{
1255
6301
  return (new_field->sql_type == real_type());
1256
6302
}
1257
6303
 
 
6304
 
 
6305
/* If one of the fields is binary and the other one isn't return 1 else 0 */
 
6306
 
 
6307
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32 flag_arg)
 
6308
{
 
6309
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
 
6310
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
 
6311
         (!(new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
 
6312
          (flag_arg & (BINCMP_FLAG | BINARY_FLAG))));
 
6313
}
 
6314
 
 
6315
 
 
6316
uint Field_str::is_equal(Create_field *new_field)
 
6317
{
 
6318
  if (compare_str_field_flags(new_field, flags))
 
6319
    return 0;
 
6320
 
 
6321
  return ((new_field->sql_type == real_type()) &&
 
6322
          new_field->charset == field_charset &&
 
6323
          new_field->length == max_display_length());
 
6324
}
 
6325
 
 
6326
 
 
6327
int Field_string::store(longlong nr, bool unsigned_val)
 
6328
{
 
6329
  char buff[64];
 
6330
  int  l;
 
6331
  CHARSET_INFO *cs=charset();
 
6332
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
 
6333
                                   unsigned_val ? 10 : -10, nr);
 
6334
  return Field_string::store(buff,(uint)l,cs);
 
6335
}
 
6336
 
 
6337
 
 
6338
int Field_longstr::store_decimal(const my_decimal *d)
 
6339
{
 
6340
  char buff[DECIMAL_MAX_STR_LENGTH+1];
 
6341
  String str(buff, sizeof(buff), &my_charset_bin);
 
6342
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
 
6343
  return store(str.ptr(), str.length(), str.charset());
 
6344
}
 
6345
 
 
6346
uint32 Field_longstr::max_data_length() const
 
6347
{
 
6348
  return field_length + (field_length > 255 ? 2 : 1);
 
6349
}
 
6350
 
 
6351
 
 
6352
double Field_string::val_real(void)
 
6353
{
 
6354
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6355
  int error;
 
6356
  char *end;
 
6357
  CHARSET_INFO *cs= charset();
 
6358
  double result;
 
6359
  
 
6360
  result=  my_strntod(cs,(char*) ptr,field_length,&end,&error);
 
6361
  if (!table->in_use->no_errors &&
 
6362
      (error || (field_length != (uint32)(end - (char*) ptr) && 
 
6363
                 !check_if_only_end_space(cs, end,
 
6364
                                          (char*) ptr + field_length))))
 
6365
  {
 
6366
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
6367
    String tmp(buf, sizeof(buf), cs);
 
6368
    tmp.copy((char*) ptr, field_length, cs);
 
6369
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
6370
                        ER_TRUNCATED_WRONG_VALUE, 
 
6371
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
6372
                        "DOUBLE", tmp.c_ptr());
 
6373
  }
 
6374
  return result;
 
6375
}
 
6376
 
 
6377
 
 
6378
longlong Field_string::val_int(void)
 
6379
{
 
6380
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6381
  int error;
 
6382
  char *end;
 
6383
  CHARSET_INFO *cs= charset();
 
6384
  longlong result;
 
6385
 
 
6386
  result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
 
6387
  if (!table->in_use->no_errors &&
 
6388
      (error || (field_length != (uint32)(end - (char*) ptr) && 
 
6389
                 !check_if_only_end_space(cs, end,
 
6390
                                          (char*) ptr + field_length))))
 
6391
  {
 
6392
    char buf[LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE];
 
6393
    String tmp(buf, sizeof(buf), cs);
 
6394
    tmp.copy((char*) ptr, field_length, cs);
 
6395
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
6396
                        ER_TRUNCATED_WRONG_VALUE, 
 
6397
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
6398
                        "INTEGER", tmp.c_ptr());
 
6399
  }
 
6400
  return result;
 
6401
}
 
6402
 
 
6403
 
 
6404
String *Field_string::val_str(String *val_buffer __attribute__((unused)),
 
6405
                              String *val_ptr)
 
6406
{
 
6407
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6408
  /* See the comment for Field_long::store(long long) */
 
6409
  DBUG_ASSERT(table->in_use == current_thd);
 
6410
  uint length;
 
6411
  if (table->in_use->variables.sql_mode &
 
6412
      MODE_PAD_CHAR_TO_FULL_LENGTH)
 
6413
    length= my_charpos(field_charset, ptr, ptr + field_length, field_length);
 
6414
  else
 
6415
    length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
 
6416
                                          field_length);
 
6417
  val_ptr->set((const char*) ptr, length, field_charset);
 
6418
  return val_ptr;
 
6419
}
 
6420
 
 
6421
 
 
6422
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
 
6423
{
 
6424
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6425
  int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
 
6426
                          charset(), decimal_value);
 
6427
  if (!table->in_use->no_errors && err)
 
6428
  {
 
6429
    char buf[DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE];
 
6430
    CHARSET_INFO *cs= charset();
 
6431
    String tmp(buf, sizeof(buf), cs);
 
6432
    tmp.copy((char*) ptr, field_length, cs);
 
6433
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
6434
                        ER_TRUNCATED_WRONG_VALUE, 
 
6435
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
6436
                        "DECIMAL", tmp.c_ptr());
 
6437
  }
 
6438
 
 
6439
  return decimal_value;
 
6440
}
 
6441
 
 
6442
 
 
6443
int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
6444
{
 
6445
  uint a_len, b_len;
 
6446
 
 
6447
  if (field_charset->mbmaxlen != 1)
 
6448
  {
 
6449
    uint char_len= field_length/field_charset->mbmaxlen;
 
6450
    a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
 
6451
    b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
 
6452
  }
 
6453
  else
 
6454
    a_len= b_len= field_length;
 
6455
  /*
 
6456
    We have to remove end space to be able to compare multi-byte-characters
 
6457
    like in latin_de 'ae' and 0xe4
 
6458
  */
 
6459
  return field_charset->coll->strnncollsp(field_charset,
 
6460
                                          a_ptr, a_len,
 
6461
                                          b_ptr, b_len,
 
6462
                                          0);
 
6463
}
 
6464
 
 
6465
 
 
6466
void Field_string::sort_string(uchar *to,uint length)
 
6467
{
 
6468
  IF_DBUG(uint tmp=) my_strnxfrm(field_charset,
 
6469
                                 to, length,
 
6470
                                 ptr, field_length);
 
6471
  DBUG_ASSERT(tmp == length);
 
6472
}
 
6473
 
 
6474
 
 
6475
void Field_string::sql_type(String &res) const
 
6476
{
 
6477
  THD *thd= table->in_use;
 
6478
  CHARSET_INFO *cs=res.charset();
 
6479
  ulong length;
 
6480
 
 
6481
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
 
6482
                             res.alloced_length(), "%s(%d)",
 
6483
                             ((type() == MYSQL_TYPE_VAR_STRING &&
 
6484
                               !thd->variables.new_mode) ?
 
6485
                              (has_charset() ? "varchar" : "varbinary") :
 
6486
                              (has_charset() ? "char" : "binary")),
 
6487
                             (int) field_length / charset()->mbmaxlen);
 
6488
  res.length(length);
 
6489
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
 
6490
      has_charset() && (charset()->state & MY_CS_BINSORT))
 
6491
    res.append(STRING_WITH_LEN(" binary"));
 
6492
}
 
6493
 
 
6494
 
 
6495
uchar *Field_string::pack(uchar *to, const uchar *from,
 
6496
                          uint max_length,
 
6497
                          bool low_byte_first __attribute__((unused)))
 
6498
{
 
6499
  uint length=      min(field_length,max_length);
 
6500
  uint local_char_length= max_length/field_charset->mbmaxlen;
 
6501
  if (length > local_char_length)
 
6502
    local_char_length= my_charpos(field_charset, from, from+length,
 
6503
                                  local_char_length);
 
6504
  set_if_smaller(length, local_char_length);
 
6505
  while (length && from[length-1] == field_charset->pad_char)
 
6506
    length--;
 
6507
 
 
6508
  // Length always stored little-endian
 
6509
  *to++= (uchar) length;
 
6510
  if (field_length > 255)
 
6511
    *to++= (uchar) (length >> 8);
 
6512
 
 
6513
  // Store the actual bytes of the string
 
6514
  memcpy(to, from, length);
 
6515
  return to+length;
 
6516
}
 
6517
 
 
6518
 
 
6519
/**
 
6520
   Unpack a string field from row data.
 
6521
 
 
6522
   This method is used to unpack a string field from a master whose size 
 
6523
   of the field is less than that of the slave. Note that there can be a
 
6524
   variety of field types represented with this class. Certain types like
 
6525
   ENUM or SET are processed differently. Hence, the upper byte of the 
 
6526
   @c param_data argument contains the result of field->real_type() from
 
6527
   the master.
 
6528
 
 
6529
   @param   to         Destination of the data
 
6530
   @param   from       Source of the data
 
6531
   @param   param_data Real type (upper) and length (lower) values
 
6532
 
 
6533
   @return  New pointer into memory based on from + length of the data
 
6534
*/
 
6535
const uchar *
 
6536
Field_string::unpack(uchar *to,
 
6537
                     const uchar *from,
 
6538
                     uint param_data,
 
6539
                     bool low_byte_first __attribute__((unused)))
 
6540
{
 
6541
  uint from_length=
 
6542
    param_data ? min(param_data & 0x00ff, field_length) : field_length;
 
6543
  uint length;
 
6544
 
 
6545
  if (from_length > 255)
 
6546
  {
 
6547
    length= uint2korr(from);
 
6548
    from+= 2;
 
6549
  }
 
6550
  else
 
6551
    length= (uint) *from++;
 
6552
 
 
6553
  memcpy(to, from, length);
 
6554
  // Pad the string with the pad character of the fields charset
 
6555
  bfill(to + length, field_length - length, field_charset->pad_char);
 
6556
  return from+length;
 
6557
}
 
6558
 
 
6559
 
 
6560
/**
 
6561
   Save the field metadata for string fields.
 
6562
 
 
6563
   Saves the real type in the first byte and the field length in the 
 
6564
   second byte of the field metadata array at index of *metadata_ptr and
 
6565
   *(metadata_ptr + 1).
 
6566
 
 
6567
   @param   metadata_ptr   First byte of field metadata
 
6568
 
 
6569
   @returns number of bytes written to metadata_ptr
 
6570
*/
 
6571
int Field_string::do_save_field_metadata(uchar *metadata_ptr)
 
6572
{
 
6573
  *metadata_ptr= real_type();
 
6574
  *(metadata_ptr + 1)= field_length;
 
6575
  return 2;
 
6576
}
 
6577
 
 
6578
 
 
6579
/*
 
6580
  Compare two packed keys
 
6581
 
 
6582
  SYNOPSIS
 
6583
    pack_cmp()
 
6584
     a                  New key
 
6585
     b                  Original key
 
6586
     length             Key length
 
6587
     insert_or_update   1 if this is an insert or update
 
6588
 
 
6589
  RETURN
 
6590
    < 0   a < b
 
6591
    0     a = b
 
6592
    > 0   a > b
 
6593
*/
 
6594
 
 
6595
int Field_string::pack_cmp(const uchar *a, const uchar *b, uint length,
 
6596
                           my_bool insert_or_update)
 
6597
{
 
6598
  uint a_length, b_length;
 
6599
  if (length > 255)
 
6600
  {
 
6601
    a_length= uint2korr(a);
 
6602
    b_length= uint2korr(b);
 
6603
    a+= 2;
 
6604
    b+= 2;
 
6605
  }
 
6606
  else
 
6607
  {
 
6608
    a_length= (uint) *a++;
 
6609
    b_length= (uint) *b++;
 
6610
  }
 
6611
  return field_charset->coll->strnncollsp(field_charset,
 
6612
                                          a, a_length,
 
6613
                                          b, b_length,
 
6614
                                          insert_or_update);
 
6615
}
 
6616
 
 
6617
 
 
6618
/**
 
6619
  Compare a packed key against row.
 
6620
 
 
6621
  @param key                    Original key
 
6622
  @param length         Key length. (May be less than field length)
 
6623
  @param insert_or_update       1 if this is an insert or update
 
6624
 
 
6625
  @return
 
6626
    < 0   row < key
 
6627
  @return
 
6628
    0     row = key
 
6629
  @return
 
6630
    > 0   row > key
 
6631
*/
 
6632
 
 
6633
int Field_string::pack_cmp(const uchar *key, uint length,
 
6634
                           my_bool insert_or_update)
 
6635
{
 
6636
  uint row_length, local_key_length;
 
6637
  uchar *end;
 
6638
  if (length > 255)
 
6639
  {
 
6640
    local_key_length= uint2korr(key);
 
6641
    key+= 2;
 
6642
  }
 
6643
  else
 
6644
    local_key_length= (uint) *key++;
 
6645
  
 
6646
  /* Only use 'length' of key, not field_length */
 
6647
  end= ptr + length;
 
6648
  while (end > ptr && end[-1] == ' ')
 
6649
    end--;
 
6650
  row_length= (uint) (end - ptr);
 
6651
 
 
6652
  return field_charset->coll->strnncollsp(field_charset,
 
6653
                                          ptr, row_length,
 
6654
                                          key, local_key_length,
 
6655
                                          insert_or_update);
 
6656
}
 
6657
 
 
6658
 
 
6659
uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
 
6660
{
 
6661
  if (length > 255)
 
6662
    return uint2korr(data_ptr)+2;
 
6663
  return (uint) *data_ptr + 1;
 
6664
}
 
6665
 
 
6666
 
 
6667
uint Field_string::max_packed_col_length(uint max_length)
 
6668
{
 
6669
  return (max_length > 255 ? 2 : 1)+max_length;
 
6670
}
 
6671
 
 
6672
 
 
6673
uint Field_string::get_key_image(uchar *buff, uint length, imagetype type_arg)
 
6674
{
 
6675
  uint bytes = my_charpos(field_charset, (char*) ptr,
 
6676
                          (char*) ptr + field_length,
 
6677
                          length / field_charset->mbmaxlen);
 
6678
  memcpy(buff, ptr, bytes);
 
6679
  if (bytes < length)
 
6680
    field_charset->cset->fill(field_charset, (char*) buff + bytes,
 
6681
                              length - bytes, field_charset->pad_char);
 
6682
  return bytes;
 
6683
}
 
6684
 
 
6685
 
 
6686
Field *Field_string::new_field(MEM_ROOT *root, struct st_table *new_table,
 
6687
                               bool keep_type)
 
6688
{
 
6689
  Field *field;
 
6690
  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
 
6691
    field= Field::new_field(root, new_table, keep_type);
 
6692
  else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
 
6693
                                       new_table->s, charset())))
 
6694
  {
 
6695
    /*
 
6696
      Old VARCHAR field which should be modified to a VARCHAR on copy
 
6697
      This is done to ensure that ALTER TABLE will convert old VARCHAR fields
 
6698
      to now VARCHAR fields.
 
6699
    */
 
6700
    field->init(new_table);
 
6701
    /*
 
6702
      Normally orig_table is different from table only if field was created
 
6703
      via ::new_field.  Here we alter the type of field, so ::new_field is
 
6704
      not applicable. But we still need to preserve the original field
 
6705
      metadata for the client-server protocol.
 
6706
    */
 
6707
    field->orig_table= orig_table;
 
6708
  }
 
6709
  return field;
 
6710
}
 
6711
 
 
6712
 
 
6713
/****************************************************************************
 
6714
  VARCHAR type
 
6715
  Data in field->ptr is stored as:
 
6716
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
 
6717
    data
 
6718
 
 
6719
  NOTE:
 
6720
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
 
6721
  stored with a 2 byte prefix. (Just like blob keys).
 
6722
 
 
6723
  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
 
6724
  The exception is if there is a prefix key field that is part of a long
 
6725
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
 
6726
  is 2.
 
6727
****************************************************************************/
 
6728
 
 
6729
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
 
6730
 
 
6731
/**
 
6732
   Save the field metadata for varstring fields.
 
6733
 
 
6734
   Saves the field length in the first byte. Note: may consume
 
6735
   2 bytes. Caller must ensure second byte is contiguous with
 
6736
   first byte (e.g. array index 0,1).
 
6737
 
 
6738
   @param   metadata_ptr   First byte of field metadata
 
6739
 
 
6740
   @returns number of bytes written to metadata_ptr
 
6741
*/
 
6742
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
 
6743
{
 
6744
  char *ptr= (char *)metadata_ptr;
 
6745
  DBUG_ASSERT(field_length <= 65535);
 
6746
  int2store(ptr, field_length);
 
6747
  return 2;
 
6748
}
 
6749
 
 
6750
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
 
6751
{
 
6752
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
6753
  uint copy_length;
 
6754
  const char *well_formed_error_pos;
 
6755
  const char *cannot_convert_error_pos;
 
6756
  const char *from_end_pos;
 
6757
 
 
6758
  copy_length= well_formed_copy_nchars(field_charset,
 
6759
                                       (char*) ptr + length_bytes,
 
6760
                                       field_length,
 
6761
                                       cs, from, length,
 
6762
                                       field_length / field_charset->mbmaxlen,
 
6763
                                       &well_formed_error_pos,
 
6764
                                       &cannot_convert_error_pos,
 
6765
                                       &from_end_pos);
 
6766
 
 
6767
  if (length_bytes == 1)
 
6768
    *ptr= (uchar) copy_length;
 
6769
  else
 
6770
    int2store(ptr, copy_length);
 
6771
 
 
6772
  if (check_string_copy_error(this, well_formed_error_pos,
 
6773
                              cannot_convert_error_pos, from + length, cs))
 
6774
    return 2;
 
6775
 
 
6776
  return report_if_important_data(from_end_pos, from + length);
 
6777
}
 
6778
 
 
6779
 
 
6780
int Field_varstring::store(longlong nr, bool unsigned_val)
 
6781
{
 
6782
  char buff[64];
 
6783
  uint  length;
 
6784
  length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
 
6785
                                                          buff,
 
6786
                                                          sizeof(buff),
 
6787
                                                          (unsigned_val ? 10:
 
6788
                                                           -10),
 
6789
                                                           nr);
 
6790
  return Field_varstring::store(buff, length, field_charset);
 
6791
}
 
6792
 
 
6793
 
 
6794
double Field_varstring::val_real(void)
 
6795
{
 
6796
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6797
  int not_used;
 
6798
  char *end_not_used;
 
6799
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
6800
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
 
6801
                    &end_not_used, &not_used);
 
6802
}
 
6803
 
 
6804
 
 
6805
longlong Field_varstring::val_int(void)
 
6806
{
 
6807
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6808
  int not_used;
 
6809
  char *end_not_used;
 
6810
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
6811
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
 
6812
                     &end_not_used, &not_used);
 
6813
}
 
6814
 
 
6815
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
 
6816
                                 String *val_ptr)
 
6817
{
 
6818
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6819
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
6820
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
 
6821
  return val_ptr;
 
6822
}
 
6823
 
 
6824
 
 
6825
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
 
6826
{
 
6827
  ASSERT_COLUMN_MARKED_FOR_READ;
 
6828
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
6829
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
 
6830
                 charset(), decimal_value);
 
6831
  return decimal_value;
 
6832
}
 
6833
 
 
6834
 
 
6835
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
 
6836
                             uint max_len)
 
6837
{
 
6838
  uint a_length, b_length;
 
6839
  int diff;
 
6840
 
 
6841
  if (length_bytes == 1)
 
6842
  {
 
6843
    a_length= (uint) *a_ptr;
 
6844
    b_length= (uint) *b_ptr;
 
6845
  }
 
6846
  else
 
6847
  {
 
6848
    a_length= uint2korr(a_ptr);
 
6849
    b_length= uint2korr(b_ptr);
 
6850
  }
 
6851
  set_if_smaller(a_length, max_len);
 
6852
  set_if_smaller(b_length, max_len);
 
6853
  diff= field_charset->coll->strnncollsp(field_charset,
 
6854
                                         a_ptr+
 
6855
                                         length_bytes,
 
6856
                                         a_length,
 
6857
                                         b_ptr+
 
6858
                                         length_bytes,
 
6859
                                         b_length,0);
 
6860
  return diff;
 
6861
}
 
6862
 
 
6863
 
 
6864
/**
 
6865
  @note
 
6866
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
 
6867
*/
 
6868
 
 
6869
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
 
6870
{
 
6871
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
6872
  uint local_char_length= max_key_length / field_charset->mbmaxlen;
 
6873
 
 
6874
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
 
6875
                          ptr + length_bytes + length, local_char_length);
 
6876
  set_if_smaller(length, local_char_length);
 
6877
  return field_charset->coll->strnncollsp(field_charset, 
 
6878
                                          ptr + length_bytes,
 
6879
                                          length,
 
6880
                                          key_ptr+
 
6881
                                          HA_KEY_BLOB_LENGTH,
 
6882
                                          uint2korr(key_ptr), 0);
 
6883
}
 
6884
 
 
6885
 
 
6886
/**
 
6887
  Compare to key segments (always 2 byte length prefix).
 
6888
 
 
6889
  @note
 
6890
    This is used only to compare key segments created for index_read().
 
6891
    (keys are created and compared in key.cc)
 
6892
*/
 
6893
 
 
6894
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
 
6895
{
 
6896
  return field_charset->coll->strnncollsp(field_charset,
 
6897
                                          a + HA_KEY_BLOB_LENGTH,
 
6898
                                          uint2korr(a),
 
6899
                                          b + HA_KEY_BLOB_LENGTH,
 
6900
                                          uint2korr(b),
 
6901
                                          0);
 
6902
}
 
6903
 
 
6904
 
 
6905
void Field_varstring::sort_string(uchar *to,uint length)
 
6906
{
 
6907
  uint tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
6908
 
 
6909
  if (field_charset == &my_charset_bin)
 
6910
  {
 
6911
    /* Store length last in high-byte order to sort longer strings first */
 
6912
    if (length_bytes == 1)
 
6913
      to[length-1]= tot_length;
 
6914
    else
 
6915
      mi_int2store(to+length-2, tot_length);
 
6916
    length-= length_bytes;
 
6917
  }
 
6918
 
 
6919
  tot_length= my_strnxfrm(field_charset,
 
6920
                          to, length, ptr + length_bytes,
 
6921
                          tot_length);
 
6922
  DBUG_ASSERT(tot_length == length);
 
6923
}
 
6924
 
 
6925
 
 
6926
enum ha_base_keytype Field_varstring::key_type() const
 
6927
{
 
6928
  enum ha_base_keytype res;
 
6929
 
 
6930
  if (binary())
 
6931
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
 
6932
  else
 
6933
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
 
6934
  return res;
 
6935
}
 
6936
 
 
6937
 
 
6938
void Field_varstring::sql_type(String &res) const
 
6939
{
 
6940
  THD *thd= table->in_use;
 
6941
  CHARSET_INFO *cs=res.charset();
 
6942
  ulong length;
 
6943
 
 
6944
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
 
6945
                             res.alloced_length(), "%s(%d)",
 
6946
                              (has_charset() ? "varchar" : "varbinary"),
 
6947
                             (int) field_length / charset()->mbmaxlen);
 
6948
  res.length(length);
 
6949
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
 
6950
      has_charset() && (charset()->state & MY_CS_BINSORT))
 
6951
    res.append(STRING_WITH_LEN(" binary"));
 
6952
}
 
6953
 
 
6954
 
 
6955
uint32 Field_varstring::data_length()
 
6956
{
 
6957
  return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
 
6958
}
 
6959
 
 
6960
uint32 Field_varstring::used_length()
 
6961
{
 
6962
  return length_bytes == 1 ? 1 + (uint32) (uchar) *ptr : 2 + uint2korr(ptr);
 
6963
}
 
6964
 
 
6965
/*
 
6966
  Functions to create a packed row.
 
6967
  Here the number of length bytes are depending on the given max_length
 
6968
*/
 
6969
 
 
6970
uchar *Field_varstring::pack(uchar *to, const uchar *from,
 
6971
                             uint max_length,
 
6972
                             bool low_byte_first __attribute__((unused)))
 
6973
{
 
6974
  uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
 
6975
  set_if_smaller(max_length, field_length);
 
6976
  if (length > max_length)
 
6977
    length=max_length;
 
6978
 
 
6979
  /* Length always stored little-endian */
 
6980
  *to++= length & 0xFF;
 
6981
  if (max_length > 255)
 
6982
    *to++= (length >> 8) & 0xFF;
 
6983
 
 
6984
  /* Store bytes of string */
 
6985
  if (length > 0)
 
6986
    memcpy(to, from+length_bytes, length);
 
6987
  return to+length;
 
6988
}
 
6989
 
 
6990
 
 
6991
uchar *
 
6992
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
 
6993
                          bool low_byte_first __attribute__((unused)))
 
6994
{
 
6995
  uint length=  length_bytes == 1 ? (uint) *key : uint2korr(key);
 
6996
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
6997
                     max_length/field_charset->mbmaxlen : max_length);
 
6998
  key+= length_bytes;
 
6999
  if (length > local_char_length)
 
7000
  {
 
7001
    local_char_length= my_charpos(field_charset, key, key+length,
 
7002
                                  local_char_length);
 
7003
    set_if_smaller(length, local_char_length);
 
7004
  }
 
7005
  *to++= (char) (length & 255);
 
7006
  if (max_length > 255)
 
7007
    *to++= (char) (length >> 8);
 
7008
  if (length)
 
7009
    memcpy(to, key, length);
 
7010
  return to+length;
 
7011
}
 
7012
 
 
7013
 
 
7014
/**
 
7015
  Unpack a key into a record buffer.
 
7016
 
 
7017
  A VARCHAR key has a maximum size of 64K-1.
 
7018
  In its packed form, the length field is one or two bytes long,
 
7019
  depending on 'max_length'.
 
7020
 
 
7021
  @param to                          Pointer into the record buffer.
 
7022
  @param key                         Pointer to the packed key.
 
7023
  @param max_length                  Key length limit from key description.
 
7024
 
 
7025
  @return
 
7026
    Pointer to end of 'key' (To the next key part if multi-segment key)
 
7027
*/
 
7028
 
 
7029
const uchar *
 
7030
Field_varstring::unpack_key(uchar *to, const uchar *key, uint max_length,
 
7031
                            bool low_byte_first __attribute__((unused)))
 
7032
{
 
7033
  /* get length of the blob key */
 
7034
  uint32 length= *key++;
 
7035
  if (max_length > 255)
 
7036
    length+= (*key++) << 8;
 
7037
 
 
7038
  /* put the length into the record buffer */
 
7039
  if (length_bytes == 1)
 
7040
    *ptr= (uchar) length;
 
7041
  else
 
7042
    int2store(ptr, length);
 
7043
  memcpy(ptr + length_bytes, key, length);
 
7044
  return key + length;
 
7045
}
 
7046
 
 
7047
/**
 
7048
  Create a packed key that will be used for storage in the index tree.
 
7049
 
 
7050
  @param to             Store packed key segment here
 
7051
  @param from           Key segment (as given to index_read())
 
7052
  @param max_length     Max length of key
 
7053
 
 
7054
  @return
 
7055
    end of key storage
 
7056
*/
 
7057
 
 
7058
uchar *
 
7059
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
 
7060
                                         bool low_byte_first __attribute__((unused)))
 
7061
{
 
7062
  /* Key length is always stored as 2 bytes */
 
7063
  uint length= uint2korr(from);
 
7064
  if (length > max_length)
 
7065
    length= max_length;
 
7066
  *to++= (char) (length & 255);
 
7067
  if (max_length > 255)
 
7068
    *to++= (char) (length >> 8);
 
7069
  if (length)
 
7070
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
 
7071
  return to+length;
 
7072
}
 
7073
 
 
7074
 
 
7075
/**
 
7076
   Unpack a varstring field from row data.
 
7077
 
 
7078
   This method is used to unpack a varstring field from a master
 
7079
   whose size of the field is less than that of the slave.
 
7080
 
 
7081
   @note
 
7082
   The string length is always packed little-endian.
 
7083
  
 
7084
   @param   to         Destination of the data
 
7085
   @param   from       Source of the data
 
7086
   @param   param_data Length bytes from the master's field data
 
7087
 
 
7088
   @return  New pointer into memory based on from + length of the data
 
7089
*/
 
7090
const uchar *
 
7091
Field_varstring::unpack(uchar *to, const uchar *from,
 
7092
                        uint param_data,
 
7093
                        bool low_byte_first __attribute__((unused)))
 
7094
{
 
7095
  uint length;
 
7096
  uint l_bytes= (param_data && (param_data < field_length)) ? 
 
7097
                (param_data <= 255) ? 1 : 2 : length_bytes;
 
7098
  if (l_bytes == 1)
 
7099
  {
 
7100
    to[0]= *from++;
 
7101
    length= to[0];
 
7102
    if (length_bytes == 2)
 
7103
      to[1]= 0;
 
7104
  }
 
7105
  else /* l_bytes == 2 */
 
7106
  {
 
7107
    length= uint2korr(from);
 
7108
    to[0]= *from++;
 
7109
    to[1]= *from++;
 
7110
  }
 
7111
  if (length)
 
7112
    memcpy(to+ length_bytes, from, length);
 
7113
  return from+length;
 
7114
}
 
7115
 
 
7116
 
 
7117
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
 
7118
                              uint key_length_arg,
 
7119
                              my_bool insert_or_update)
 
7120
{
 
7121
  uint a_length, b_length;
 
7122
  if (key_length_arg > 255)
 
7123
  {
 
7124
    a_length=uint2korr(a); a+= 2;
 
7125
    b_length=uint2korr(b); b+= 2;
 
7126
  }
 
7127
  else
 
7128
  {
 
7129
    a_length= (uint) *a++;
 
7130
    b_length= (uint) *b++;
 
7131
  }
 
7132
  return field_charset->coll->strnncollsp(field_charset,
 
7133
                                          a, a_length,
 
7134
                                          b, b_length,
 
7135
                                          insert_or_update);
 
7136
}
 
7137
 
 
7138
 
 
7139
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
 
7140
                              my_bool insert_or_update)
 
7141
{
 
7142
  uchar *a= ptr+ length_bytes;
 
7143
  uint a_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
7144
  uint b_length;
 
7145
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
7146
                           key_length_arg / field_charset->mbmaxlen :
 
7147
                           key_length_arg);
 
7148
 
 
7149
  if (key_length_arg > 255)
 
7150
  {
 
7151
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
 
7152
  }
 
7153
  else
 
7154
    b_length= (uint) *b++;
 
7155
 
 
7156
  if (a_length > local_char_length)
 
7157
  {
 
7158
    local_char_length= my_charpos(field_charset, a, a+a_length,
 
7159
                                  local_char_length);
 
7160
    set_if_smaller(a_length, local_char_length);
 
7161
  }
 
7162
 
 
7163
  return field_charset->coll->strnncollsp(field_charset,
 
7164
                                          a, a_length,
 
7165
                                          b, b_length,
 
7166
                                          insert_or_update);
 
7167
}
 
7168
 
 
7169
 
 
7170
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
 
7171
{
 
7172
  if (length > 255)
 
7173
    return uint2korr(data_ptr)+2;
 
7174
  return (uint) *data_ptr + 1;
 
7175
}
 
7176
 
 
7177
 
 
7178
uint Field_varstring::max_packed_col_length(uint max_length)
 
7179
{
 
7180
  return (max_length > 255 ? 2 : 1)+max_length;
 
7181
}
 
7182
 
 
7183
uint Field_varstring::get_key_image(uchar *buff, uint length, imagetype type)
 
7184
{
 
7185
  uint f_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
7186
  uint local_char_length= length / field_charset->mbmaxlen;
 
7187
  uchar *pos= ptr+length_bytes;
 
7188
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
 
7189
                                local_char_length);
 
7190
  set_if_smaller(f_length, local_char_length);
 
7191
  /* Key is always stored with 2 bytes */
 
7192
  int2store(buff,f_length);
 
7193
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
 
7194
  if (f_length < length)
 
7195
  {
 
7196
    /*
 
7197
      Must clear this as we do a memcmp in opt_range.cc to detect
 
7198
      identical keys
 
7199
    */
 
7200
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
 
7201
  }
 
7202
  return HA_KEY_BLOB_LENGTH+f_length;
 
7203
}
 
7204
 
 
7205
 
 
7206
void Field_varstring::set_key_image(const uchar *buff,uint length)
 
7207
{
 
7208
  length= uint2korr(buff);                      // Real length is here
 
7209
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
7210
                                field_charset);
 
7211
}
 
7212
 
 
7213
 
 
7214
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
 
7215
                                uint32 max_length)
 
7216
{
 
7217
  uint32 a_length,b_length;
 
7218
 
 
7219
  if (length_bytes == 1)
 
7220
  {
 
7221
    a_length= (uint) *a_ptr;
 
7222
    b_length= (uint) *b_ptr;
 
7223
  }
 
7224
  else
 
7225
  {
 
7226
    a_length= uint2korr(a_ptr);
 
7227
    b_length= uint2korr(b_ptr);
 
7228
  }
 
7229
  set_if_smaller(a_length, max_length);
 
7230
  set_if_smaller(b_length, max_length);
 
7231
  if (a_length != b_length)
 
7232
    return 1;
 
7233
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
 
7234
}
 
7235
 
 
7236
 
 
7237
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
 
7238
                                  bool keep_type)
 
7239
{
 
7240
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
 
7241
                                                            keep_type);
 
7242
  if (res)
 
7243
    res->length_bytes= length_bytes;
 
7244
  return res;
 
7245
}
 
7246
 
 
7247
 
 
7248
Field *Field_varstring::new_key_field(MEM_ROOT *root,
 
7249
                                      struct st_table *new_table,
 
7250
                                      uchar *new_ptr, uchar *new_null_ptr,
 
7251
                                      uint new_null_bit)
 
7252
{
 
7253
  Field_varstring *res;
 
7254
  if ((res= (Field_varstring*) Field::new_key_field(root,
 
7255
                                                    new_table,
 
7256
                                                    new_ptr,
 
7257
                                                    new_null_ptr,
 
7258
                                                    new_null_bit)))
 
7259
  {
 
7260
    /* Keys length prefixes are always packed with 2 bytes */
 
7261
    res->length_bytes= 2;
 
7262
  }
 
7263
  return res;
 
7264
}
 
7265
 
 
7266
 
 
7267
uint Field_varstring::is_equal(Create_field *new_field)
 
7268
{
 
7269
  if (new_field->sql_type == real_type() &&
 
7270
      new_field->charset == field_charset)
 
7271
  {
 
7272
    if (new_field->length == max_display_length())
 
7273
      return IS_EQUAL_YES;
 
7274
    if (new_field->length > max_display_length() &&
 
7275
        ((new_field->length <= 255 && max_display_length() <= 255) ||
 
7276
         (new_field->length > 255 && max_display_length() > 255)))
 
7277
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
 
7278
  }
 
7279
  return IS_EQUAL_NO;
 
7280
}
 
7281
 
 
7282
 
 
7283
void Field_varstring::hash(ulong *nr, ulong *nr2)
 
7284
{
 
7285
  if (is_null())
 
7286
  {
 
7287
    *nr^= (*nr << 1) | 1;
 
7288
  }
 
7289
  else
 
7290
  {
 
7291
    uint len=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
7292
    CHARSET_INFO *cs= charset();
 
7293
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
 
7294
  }
 
7295
}
 
7296
 
 
7297
 
 
7298
/****************************************************************************
 
7299
** blob type
 
7300
** A blob is saved as a length and a pointer. The length is stored in the
 
7301
** packlength slot and may be from 1-4.
 
7302
****************************************************************************/
 
7303
 
 
7304
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
7305
                       enum utype unireg_check_arg, const char *field_name_arg,
 
7306
                       TABLE_SHARE *share, uint blob_pack_length,
 
7307
                       CHARSET_INFO *cs)
 
7308
  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
 
7309
                 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
 
7310
                 cs),
 
7311
   packlength(blob_pack_length)
 
7312
{
 
7313
  flags|= BLOB_FLAG;
 
7314
  share->blob_fields++;
 
7315
  /* TODO: why do not fill table->s->blob_field array here? */
 
7316
}
 
7317
 
 
7318
 
 
7319
void Field_blob::store_length(uchar *i_ptr, 
 
7320
                              uint i_packlength, 
 
7321
                              uint32 i_number, 
 
7322
                              bool low_byte_first)
 
7323
{
 
7324
  switch (i_packlength) {
 
7325
  case 1:
 
7326
    i_ptr[0]= (uchar) i_number;
 
7327
    break;
 
7328
  case 2:
 
7329
#ifdef WORDS_BIGENDIAN
 
7330
    if (low_byte_first)
 
7331
    {
 
7332
      int2store(i_ptr,(unsigned short) i_number);
 
7333
    }
 
7334
    else
 
7335
#endif
 
7336
      shortstore(i_ptr,(unsigned short) i_number);
 
7337
    break;
 
7338
  case 3:
 
7339
    int3store(i_ptr,i_number);
 
7340
    break;
 
7341
  case 4:
 
7342
#ifdef WORDS_BIGENDIAN
 
7343
    if (low_byte_first)
 
7344
    {
 
7345
      int4store(i_ptr,i_number);
 
7346
    }
 
7347
    else
 
7348
#endif
 
7349
      longstore(i_ptr,i_number);
 
7350
  }
 
7351
}
 
7352
 
 
7353
 
 
7354
uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg, bool low_byte_first)
 
7355
{
 
7356
  switch (packlength_arg) {
 
7357
  case 1:
 
7358
    return (uint32) pos[0];
 
7359
  case 2:
 
7360
    {
 
7361
      uint16 tmp;
 
7362
#ifdef WORDS_BIGENDIAN
 
7363
      if (low_byte_first)
 
7364
        tmp=sint2korr(pos);
 
7365
      else
 
7366
#endif
 
7367
        shortget(tmp,pos);
 
7368
      return (uint32) tmp;
 
7369
    }
 
7370
  case 3:
 
7371
    return (uint32) uint3korr(pos);
 
7372
  case 4:
 
7373
    {
 
7374
      uint32 tmp;
 
7375
#ifdef WORDS_BIGENDIAN
 
7376
      if (low_byte_first)
 
7377
        tmp=uint4korr(pos);
 
7378
      else
 
7379
#endif
 
7380
        longget(tmp,pos);
 
7381
      return (uint32) tmp;
 
7382
    }
 
7383
  }
 
7384
  return 0;                                     // Impossible
 
7385
}
 
7386
 
 
7387
 
 
7388
/**
 
7389
  Put a blob length field into a record buffer.
 
7390
 
 
7391
  Depending on the maximum length of a blob, its length field is
 
7392
  put into 1 to 4 bytes. This is a property of the blob object,
 
7393
  described by 'packlength'.
 
7394
 
 
7395
  @param pos                 Pointer into the record buffer.
 
7396
  @param length              The length value to put.
 
7397
*/
 
7398
 
 
7399
void Field_blob::put_length(uchar *pos, uint32 length)
 
7400
{
 
7401
  switch (packlength) {
 
7402
  case 1:
 
7403
    *pos= (char) length;
 
7404
    break;
 
7405
  case 2:
 
7406
    int2store(pos, length);
 
7407
    break;
 
7408
  case 3:
 
7409
    int3store(pos, length);
 
7410
    break;
 
7411
  case 4:
 
7412
    int4store(pos, length);
 
7413
    break;
 
7414
  }
 
7415
}
 
7416
 
 
7417
 
 
7418
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
 
7419
{
 
7420
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
7421
  uint copy_length, new_length;
 
7422
  const char *well_formed_error_pos;
 
7423
  const char *cannot_convert_error_pos;
 
7424
  const char *from_end_pos, *tmp;
 
7425
  char buff[STRING_BUFFER_USUAL_SIZE];
 
7426
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
7427
 
 
7428
  if (!length)
 
7429
  {
 
7430
    bzero(ptr,Field_blob::pack_length());
 
7431
    return 0;
 
7432
  }
 
7433
 
 
7434
  if (from == value.ptr())
 
7435
  {
 
7436
    uint32 dummy_offset;
 
7437
    if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
 
7438
    {
 
7439
      Field_blob::store_length(length);
 
7440
      bmove(ptr+packlength,(char*) &from,sizeof(char*));
 
7441
      return 0;
 
7442
    }
 
7443
    if (tmpstr.copy(from, length, cs))
 
7444
      goto oom_error;
 
7445
    from= tmpstr.ptr();
 
7446
  }
 
7447
 
 
7448
  new_length= min(max_data_length(), field_charset->mbmaxlen * length);
 
7449
  if (value.alloc(new_length))
 
7450
    goto oom_error;
 
7451
 
 
7452
 
 
7453
  if (f_is_hex_escape(flags))
 
7454
  {
 
7455
    copy_length= my_copy_with_hex_escaping(field_charset,
 
7456
                                           (char*) value.ptr(), new_length,
 
7457
                                            from, length);
 
7458
    Field_blob::store_length(copy_length);
 
7459
    tmp= value.ptr();
 
7460
    bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
 
7461
    return 0;
 
7462
  }
 
7463
  /*
 
7464
    "length" is OK as "nchars" argument to well_formed_copy_nchars as this
 
7465
    is never used to limit the length of the data. The cut of long data
 
7466
    is done with the new_length value.
 
7467
  */
 
7468
  copy_length= well_formed_copy_nchars(field_charset,
 
7469
                                       (char*) value.ptr(), new_length,
 
7470
                                       cs, from, length,
 
7471
                                       length,
 
7472
                                       &well_formed_error_pos,
 
7473
                                       &cannot_convert_error_pos,
 
7474
                                       &from_end_pos);
 
7475
 
 
7476
  Field_blob::store_length(copy_length);
 
7477
  tmp= value.ptr();
 
7478
  bmove(ptr+packlength,(uchar*) &tmp,sizeof(char*));
 
7479
 
 
7480
  if (check_string_copy_error(this, well_formed_error_pos,
 
7481
                              cannot_convert_error_pos, from + length, cs))
 
7482
    return 2;
 
7483
 
 
7484
  return report_if_important_data(from_end_pos, from + length);
 
7485
 
 
7486
oom_error:
 
7487
  /* Fatal OOM error */
 
7488
  bzero(ptr,Field_blob::pack_length());
 
7489
  return -1; 
 
7490
}
 
7491
 
 
7492
 
 
7493
int Field_blob::store(double nr)
 
7494
{
 
7495
  CHARSET_INFO *cs=charset();
 
7496
  value.set_real(nr, NOT_FIXED_DEC, cs);
 
7497
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
 
7498
}
 
7499
 
 
7500
 
 
7501
int Field_blob::store(longlong nr, bool unsigned_val)
 
7502
{
 
7503
  CHARSET_INFO *cs=charset();
 
7504
  value.set_int(nr, unsigned_val, cs);
 
7505
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
 
7506
}
 
7507
 
 
7508
 
 
7509
double Field_blob::val_real(void)
 
7510
{
 
7511
  ASSERT_COLUMN_MARKED_FOR_READ;
 
7512
  int not_used;
 
7513
  char *end_not_used, *blob;
 
7514
  uint32 length;
 
7515
  CHARSET_INFO *cs;
 
7516
 
 
7517
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
7518
  if (!blob)
 
7519
    return 0.0;
 
7520
  length= get_length(ptr);
 
7521
  cs= charset();
 
7522
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
 
7523
}
 
7524
 
 
7525
 
 
7526
longlong Field_blob::val_int(void)
 
7527
{
 
7528
  ASSERT_COLUMN_MARKED_FOR_READ;
 
7529
  int not_used;
 
7530
  char *blob;
 
7531
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
7532
  if (!blob)
 
7533
    return 0;
 
7534
  uint32 length=get_length(ptr);
 
7535
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
 
7536
}
 
7537
 
 
7538
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
 
7539
                            String *val_ptr)
 
7540
{
 
7541
  ASSERT_COLUMN_MARKED_FOR_READ;
 
7542
  char *blob;
 
7543
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
7544
  if (!blob)
 
7545
    val_ptr->set("",0,charset());       // A bit safer than ->length(0)
 
7546
  else
 
7547
    val_ptr->set((const char*) blob,get_length(ptr),charset());
 
7548
  return val_ptr;
 
7549
}
 
7550
 
 
7551
 
 
7552
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
 
7553
{
 
7554
  ASSERT_COLUMN_MARKED_FOR_READ;
 
7555
  const char *blob;
 
7556
  size_t length;
 
7557
  memcpy_fixed(&blob, ptr+packlength, sizeof(const uchar*));
 
7558
  if (!blob)
 
7559
  {
 
7560
    blob= "";
 
7561
    length= 0;
 
7562
  }
 
7563
  else
 
7564
    length= get_length(ptr);
 
7565
 
 
7566
  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
 
7567
                 decimal_value);
 
7568
  return decimal_value;
 
7569
}
 
7570
 
 
7571
 
 
7572
int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
 
7573
                    uint32 b_length)
 
7574
{
 
7575
  return field_charset->coll->strnncollsp(field_charset, 
 
7576
                                          a, a_length, b, b_length,
 
7577
                                          0);
 
7578
}
 
7579
 
 
7580
 
 
7581
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
 
7582
                        uint max_length)
 
7583
{
 
7584
  uchar *blob1,*blob2;
 
7585
  memcpy_fixed(&blob1,a_ptr+packlength,sizeof(char*));
 
7586
  memcpy_fixed(&blob2,b_ptr+packlength,sizeof(char*));
 
7587
  uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
 
7588
  set_if_smaller(a_len, max_length);
 
7589
  set_if_smaller(b_len, max_length);
 
7590
  return Field_blob::cmp(blob1,a_len,blob2,b_len);
 
7591
}
 
7592
 
 
7593
 
 
7594
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
 
7595
                           uint32 max_length)
 
7596
{
 
7597
  char *a,*b;
 
7598
  uint diff;
 
7599
  uint32 a_length,b_length;
 
7600
  memcpy_fixed(&a,a_ptr+packlength,sizeof(char*));
 
7601
  memcpy_fixed(&b,b_ptr+packlength,sizeof(char*));
 
7602
  a_length=get_length(a_ptr);
 
7603
  if (a_length > max_length)
 
7604
    a_length=max_length;
 
7605
  b_length=get_length(b_ptr);
 
7606
  if (b_length > max_length)
 
7607
    b_length=max_length;
 
7608
  diff=memcmp(a,b,min(a_length,b_length));
 
7609
  return diff ? diff : (int) (a_length - b_length);
 
7610
}
 
7611
 
 
7612
 
 
7613
/* The following is used only when comparing a key */
 
7614
 
 
7615
uint Field_blob::get_key_image(uchar *buff,uint length, imagetype type_arg)
 
7616
{
 
7617
  uint32 blob_length= get_length(ptr);
 
7618
  uchar *blob;
 
7619
 
 
7620
  get_ptr(&blob);
 
7621
  uint local_char_length= length / field_charset->mbmaxlen;
 
7622
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
 
7623
                          local_char_length);
 
7624
  set_if_smaller(blob_length, local_char_length);
 
7625
 
 
7626
  if ((uint32) length > blob_length)
 
7627
  {
 
7628
    /*
 
7629
      Must clear this as we do a memcmp in opt_range.cc to detect
 
7630
      identical keys
 
7631
    */
 
7632
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
 
7633
    length=(uint) blob_length;
 
7634
  }
 
7635
  int2store(buff,length);
 
7636
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
 
7637
  return HA_KEY_BLOB_LENGTH+length;
 
7638
}
 
7639
 
 
7640
 
 
7641
void Field_blob::set_key_image(const uchar *buff,uint length)
 
7642
{
 
7643
  length= uint2korr(buff);
 
7644
  (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
7645
                           field_charset);
 
7646
}
 
7647
 
 
7648
 
 
7649
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
 
7650
{
 
7651
  uchar *blob1;
 
7652
  uint blob_length=get_length(ptr);
 
7653
  memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
 
7654
  CHARSET_INFO *cs= charset();
 
7655
  uint local_char_length= max_key_length / cs->mbmaxlen;
 
7656
  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
 
7657
                                local_char_length);
 
7658
  set_if_smaller(blob_length, local_char_length);
 
7659
  return Field_blob::cmp(blob1, blob_length,
 
7660
                         key_ptr+HA_KEY_BLOB_LENGTH,
 
7661
                         uint2korr(key_ptr));
 
7662
}
 
7663
 
 
7664
int Field_blob::key_cmp(const uchar *a,const uchar *b)
 
7665
{
 
7666
  return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
 
7667
                         b+HA_KEY_BLOB_LENGTH, uint2korr(b));
 
7668
}
 
7669
 
 
7670
 
 
7671
/**
 
7672
   Save the field metadata for blob fields.
 
7673
 
 
7674
   Saves the pack length in the first byte of the field metadata array
 
7675
   at index of *metadata_ptr.
 
7676
 
 
7677
   @param   metadata_ptr   First byte of field metadata
 
7678
 
 
7679
   @returns number of bytes written to metadata_ptr
 
7680
*/
 
7681
int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
 
7682
{
 
7683
  *metadata_ptr= pack_length_no_ptr();
 
7684
  return 1;
 
7685
}
 
7686
 
 
7687
 
 
7688
uint32 Field_blob::sort_length() const
 
7689
{
 
7690
  return (uint32) (current_thd->variables.max_sort_length + 
 
7691
                   (field_charset == &my_charset_bin ? 0 : packlength));
 
7692
}
 
7693
 
 
7694
 
 
7695
void Field_blob::sort_string(uchar *to,uint length)
 
7696
{
 
7697
  uchar *blob;
 
7698
  uint blob_length=get_length();
 
7699
 
 
7700
  if (!blob_length)
 
7701
    bzero(to,length);
 
7702
  else
 
7703
  {
 
7704
    if (field_charset == &my_charset_bin)
 
7705
    {
 
7706
      uchar *pos;
 
7707
 
 
7708
      /*
 
7709
        Store length of blob last in blob to shorter blobs before longer blobs
 
7710
      */
 
7711
      length-= packlength;
 
7712
      pos= to+length;
 
7713
 
 
7714
      switch (packlength) {
 
7715
      case 1:
 
7716
        *pos= (char) blob_length;
 
7717
        break;
 
7718
      case 2:
 
7719
        mi_int2store(pos, blob_length);
 
7720
        break;
 
7721
      case 3:
 
7722
        mi_int3store(pos, blob_length);
 
7723
        break;
 
7724
      case 4:
 
7725
        mi_int4store(pos, blob_length);
 
7726
        break;
 
7727
      }
 
7728
    }
 
7729
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
7730
    
 
7731
    blob_length=my_strnxfrm(field_charset,
 
7732
                            to, length, blob, blob_length);
 
7733
    DBUG_ASSERT(blob_length == length);
 
7734
  }
 
7735
}
 
7736
 
 
7737
 
 
7738
void Field_blob::sql_type(String &res) const
 
7739
{
 
7740
  const char *str;
 
7741
  uint length;
 
7742
  switch (packlength) {
 
7743
  default: str="tiny"; length=4; break;
 
7744
  case 2:  str="";     length=0; break;
 
7745
  case 3:  str="medium"; length= 6; break;
 
7746
  case 4:  str="long";  length=4; break;
 
7747
  }
 
7748
  res.set_ascii(str,length);
 
7749
  if (charset() == &my_charset_bin)
 
7750
    res.append(STRING_WITH_LEN("blob"));
 
7751
  else
 
7752
  {
 
7753
    res.append(STRING_WITH_LEN("text"));
 
7754
  }
 
7755
}
 
7756
 
 
7757
uchar *Field_blob::pack(uchar *to, const uchar *from,
 
7758
                        uint max_length, bool low_byte_first)
 
7759
{
 
7760
  DBUG_ENTER("Field_blob::pack");
 
7761
  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
 
7762
                       " max_length: %u; low_byte_first: %d",
 
7763
                       (ulong) to, (ulong) from,
 
7764
                       max_length, low_byte_first));
 
7765
  DBUG_DUMP("record", from, table->s->reclength);
 
7766
  uchar *save= ptr;
 
7767
  ptr= (uchar*) from;
 
7768
  uint32 length=get_length();                   // Length of from string
 
7769
 
 
7770
  /*
 
7771
    Store max length, which will occupy packlength bytes. If the max
 
7772
    length given is smaller than the actual length of the blob, we
 
7773
    just store the initial bytes of the blob.
 
7774
  */
 
7775
  store_length(to, packlength, min(length, max_length), low_byte_first);
 
7776
 
 
7777
  /*
 
7778
    Store the actual blob data, which will occupy 'length' bytes.
 
7779
   */
 
7780
  if (length > 0)
 
7781
  {
 
7782
    get_ptr((uchar**) &from);
 
7783
    memcpy(to+packlength, from,length);
 
7784
  }
 
7785
  ptr=save;                                     // Restore org row pointer
 
7786
  DBUG_DUMP("packed", to, packlength + length);
 
7787
  DBUG_RETURN(to+packlength+length);
 
7788
}
 
7789
 
 
7790
 
 
7791
/**
 
7792
   Unpack a blob field from row data.
 
7793
 
 
7794
   This method is used to unpack a blob field from a master whose size of 
 
7795
   the field is less than that of the slave. Note: This method is included
 
7796
   to satisfy inheritance rules, but is not needed for blob fields. It
 
7797
   simply is used as a pass-through to the original unpack() method for
 
7798
   blob fields.
 
7799
 
 
7800
   @param   to         Destination of the data
 
7801
   @param   from       Source of the data
 
7802
   @param   param_data @c TRUE if base types should be stored in little-
 
7803
                       endian format, @c FALSE if native format should
 
7804
                       be used.
 
7805
 
 
7806
   @return  New pointer into memory based on from + length of the data
 
7807
*/
 
7808
const uchar *Field_blob::unpack(uchar *to, 
 
7809
                                const uchar *from,
 
7810
                                uint param_data,
 
7811
                                bool low_byte_first)
 
7812
{
 
7813
  DBUG_ENTER("Field_blob::unpack");
 
7814
  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
 
7815
                       " param_data: %u; low_byte_first: %d",
 
7816
                       (ulong) to, (ulong) from, param_data, low_byte_first));
 
7817
  uint const master_packlength=
 
7818
    param_data > 0 ? param_data & 0xFF : packlength;
 
7819
  uint32 const length= get_length(from, master_packlength, low_byte_first);
 
7820
  DBUG_DUMP("packed", from, length + master_packlength);
 
7821
  bitmap_set_bit(table->write_set, field_index);
 
7822
  store(reinterpret_cast<const char*>(from) + master_packlength,
 
7823
        length, field_charset);
 
7824
  DBUG_DUMP("record", to, table->s->reclength);
 
7825
  DBUG_RETURN(from + master_packlength + length);
 
7826
}
 
7827
 
 
7828
/* Keys for blobs are like keys on varchars */
 
7829
 
 
7830
int Field_blob::pack_cmp(const uchar *a, const uchar *b, uint key_length_arg,
 
7831
                         my_bool insert_or_update)
 
7832
{
 
7833
  uint a_length, b_length;
 
7834
  if (key_length_arg > 255)
 
7835
  {
 
7836
    a_length=uint2korr(a); a+=2;
 
7837
    b_length=uint2korr(b); b+=2;
 
7838
  }
 
7839
  else
 
7840
  {
 
7841
    a_length= (uint) *a++;
 
7842
    b_length= (uint) *b++;
 
7843
  }
 
7844
  return field_charset->coll->strnncollsp(field_charset,
 
7845
                                          a, a_length,
 
7846
                                          b, b_length,
 
7847
                                          insert_or_update);
 
7848
}
 
7849
 
 
7850
 
 
7851
int Field_blob::pack_cmp(const uchar *b, uint key_length_arg,
 
7852
                         my_bool insert_or_update)
 
7853
{
 
7854
  uchar *a;
 
7855
  uint a_length, b_length;
 
7856
  memcpy_fixed(&a,ptr+packlength,sizeof(char*));
 
7857
  if (!a)
 
7858
    return key_length_arg > 0 ? -1 : 0;
 
7859
 
 
7860
  a_length= get_length(ptr);
 
7861
  if (key_length_arg > 255)
 
7862
  {
 
7863
    b_length= uint2korr(b); b+=2;
 
7864
  }
 
7865
  else
 
7866
    b_length= (uint) *b++;
 
7867
  return field_charset->coll->strnncollsp(field_charset,
 
7868
                                          a, a_length,
 
7869
                                          b, b_length,
 
7870
                                          insert_or_update);
 
7871
}
 
7872
 
 
7873
/** Create a packed key that will be used for storage from a MySQL row. */
 
7874
 
 
7875
uchar *
 
7876
Field_blob::pack_key(uchar *to, const uchar *from, uint max_length,
 
7877
                     bool low_byte_first __attribute__((unused)))
 
7878
{
 
7879
  uchar *save= ptr;
 
7880
  ptr= (uchar*) from;
 
7881
  uint32 length=get_length();        // Length of from string
 
7882
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
7883
                           max_length/field_charset->mbmaxlen : max_length);
 
7884
  if (length)
 
7885
    get_ptr((uchar**) &from);
 
7886
  if (length > local_char_length)
 
7887
    local_char_length= my_charpos(field_charset, from, from+length,
 
7888
                                  local_char_length);
 
7889
  set_if_smaller(length, local_char_length);
 
7890
  *to++= (uchar) length;
 
7891
  if (max_length > 255)                         // 2 byte length
 
7892
    *to++= (uchar) (length >> 8);
 
7893
  memcpy(to, from, length);
 
7894
  ptr=save;                                     // Restore org row pointer
 
7895
  return to+length;
 
7896
}
 
7897
 
 
7898
 
 
7899
/**
 
7900
  Unpack a blob key into a record buffer.
 
7901
 
 
7902
  A blob key has a maximum size of 64K-1.
 
7903
  In its packed form, the length field is one or two bytes long,
 
7904
  depending on 'max_length'.
 
7905
  Depending on the maximum length of a blob, its length field is
 
7906
  put into 1 to 4 bytes. This is a property of the blob object,
 
7907
  described by 'packlength'.
 
7908
  Blobs are internally stored apart from the record buffer, which
 
7909
  contains a pointer to the blob buffer.
 
7910
 
 
7911
 
 
7912
  @param to                          Pointer into the record buffer.
 
7913
  @param from                        Pointer to the packed key.
 
7914
  @param max_length                  Key length limit from key description.
 
7915
 
 
7916
  @return
 
7917
    Pointer into 'from' past the last byte copied from packed key.
 
7918
*/
 
7919
 
 
7920
const uchar *
 
7921
Field_blob::unpack_key(uchar *to, const uchar *from, uint max_length,
 
7922
                       bool low_byte_first __attribute__((unused)))
 
7923
{
 
7924
  /* get length of the blob key */
 
7925
  uint32 length= *from++;
 
7926
  if (max_length > 255)
 
7927
    length+= *from++ << 8;
 
7928
 
 
7929
  /* put the length into the record buffer */
 
7930
  put_length(to, length);
 
7931
 
 
7932
  /* put the address of the blob buffer or NULL */
 
7933
  if (length)
 
7934
    memcpy_fixed(to + packlength, &from, sizeof(from));
 
7935
  else
 
7936
    bzero(to + packlength, sizeof(from));
 
7937
 
 
7938
  /* point to first byte of next field in 'from' */
 
7939
  return from + length;
 
7940
}
 
7941
 
 
7942
 
 
7943
/** Create a packed key that will be used for storage from a MySQL key. */
 
7944
 
 
7945
uchar *
 
7946
Field_blob::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
 
7947
                                    bool low_byte_first __attribute__((unused)))
 
7948
{
 
7949
  uint length=uint2korr(from);
 
7950
  if (length > max_length)
 
7951
    length=max_length;
 
7952
  *to++= (char) (length & 255);
 
7953
  if (max_length > 255)
 
7954
    *to++= (char) (length >> 8);
 
7955
  if (length)
 
7956
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
 
7957
  return to+length;
 
7958
}
 
7959
 
 
7960
 
 
7961
uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
 
7962
{
 
7963
  if (length > 255)
 
7964
    return uint2korr(data_ptr)+2;
 
7965
  return (uint) *data_ptr + 1;
 
7966
}
 
7967
 
 
7968
 
 
7969
uint Field_blob::max_packed_col_length(uint max_length)
 
7970
{
 
7971
  return (max_length > 255 ? 2 : 1)+max_length;
 
7972
}
 
7973
 
 
7974
 
 
7975
uint Field_blob::is_equal(Create_field *new_field)
 
7976
{
 
7977
  if (compare_str_field_flags(new_field, flags))
 
7978
    return 0;
 
7979
 
 
7980
  return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
 
7981
          && new_field->charset == field_charset &&
 
7982
          ((Field_blob *)new_field->field)->max_data_length() ==
 
7983
          max_data_length());
 
7984
}
 
7985
 
 
7986
 
 
7987
/****************************************************************************
 
7988
** enum type.
 
7989
** This is a string which only can have a selection of different values.
 
7990
** If one uses this string in a number context one gets the type number.
 
7991
****************************************************************************/
 
7992
 
 
7993
enum ha_base_keytype Field_enum::key_type() const
 
7994
{
 
7995
  switch (packlength) {
 
7996
  default: return HA_KEYTYPE_BINARY;
 
7997
  case 2: return HA_KEYTYPE_USHORT_INT;
 
7998
  case 3: return HA_KEYTYPE_UINT24;
 
7999
  case 4: return HA_KEYTYPE_ULONG_INT;
 
8000
  case 8: return HA_KEYTYPE_ULONGLONG;
 
8001
  }
 
8002
}
 
8003
 
 
8004
void Field_enum::store_type(uint64_t value)
 
8005
{
 
8006
  switch (packlength) {
 
8007
  case 1: ptr[0]= (uchar) value;  break;
 
8008
  case 2:
 
8009
#ifdef WORDS_BIGENDIAN
 
8010
  if (table->s->db_low_byte_first)
 
8011
  {
 
8012
    int2store(ptr,(unsigned short) value);
 
8013
  }
 
8014
  else
 
8015
#endif
 
8016
    shortstore(ptr,(unsigned short) value);
 
8017
  break;
 
8018
  case 3: int3store(ptr,(long) value); break;
 
8019
  case 4:
 
8020
#ifdef WORDS_BIGENDIAN
 
8021
  if (table->s->db_low_byte_first)
 
8022
  {
 
8023
    int4store(ptr,value);
 
8024
  }
 
8025
  else
 
8026
#endif
 
8027
    longstore(ptr,(long) value);
 
8028
  break;
 
8029
  case 8:
 
8030
#ifdef WORDS_BIGENDIAN
 
8031
  if (table->s->db_low_byte_first)
 
8032
  {
 
8033
    int8store(ptr,value);
 
8034
  }
 
8035
  else
 
8036
#endif
 
8037
    longlongstore(ptr,value); break;
 
8038
  }
 
8039
}
 
8040
 
 
8041
 
 
8042
/**
 
8043
  @note
 
8044
    Storing a empty string in a enum field gives a warning
 
8045
    (if there isn't a empty value in the enum)
 
8046
*/
 
8047
 
 
8048
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
 
8049
{
 
8050
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
8051
  int err= 0;
 
8052
  uint32 not_used;
 
8053
  char buff[STRING_BUFFER_USUAL_SIZE];
 
8054
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
8055
 
 
8056
  /* Convert character set if necessary */
 
8057
  if (String::needs_conversion(length, cs, field_charset, &not_used))
 
8058
  { 
 
8059
    uint dummy_errors;
 
8060
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
 
8061
    from= tmpstr.ptr();
 
8062
    length=  tmpstr.length();
 
8063
  }
 
8064
 
 
8065
  /* Remove end space */
 
8066
  length= field_charset->cset->lengthsp(field_charset, from, length);
 
8067
  uint tmp=find_type2(typelib, from, length, field_charset);
 
8068
  if (!tmp)
 
8069
  {
 
8070
    if (length < 6) // Can't be more than 99999 enums
 
8071
    {
 
8072
      /* This is for reading numbers with LOAD DATA INFILE */
 
8073
      char *end;
 
8074
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
 
8075
      if (err || end != from+length || tmp > typelib->count)
 
8076
      {
 
8077
        tmp=0;
 
8078
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
8079
      }
 
8080
      if (!table->in_use->count_cuted_fields)
 
8081
        err= 0;
 
8082
    }
 
8083
    else
 
8084
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
8085
  }
 
8086
  store_type((uint64_t) tmp);
 
8087
  return err;
 
8088
}
 
8089
 
 
8090
 
 
8091
int Field_enum::store(double nr)
 
8092
{
 
8093
  return Field_enum::store((longlong) nr, FALSE);
 
8094
}
 
8095
 
 
8096
 
 
8097
int Field_enum::store(longlong nr, bool unsigned_val)
 
8098
{
 
8099
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
8100
  int error= 0;
 
8101
  if ((uint64_t) nr > typelib->count || nr == 0)
 
8102
  {
 
8103
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
8104
    if (nr != 0 || table->in_use->count_cuted_fields)
 
8105
    {
 
8106
      nr= 0;
 
8107
      error= 1;
 
8108
    }
 
8109
  }
 
8110
  store_type((uint64_t) (uint) nr);
 
8111
  return error;
 
8112
}
 
8113
 
 
8114
 
 
8115
double Field_enum::val_real(void)
 
8116
{
 
8117
  return (double) Field_enum::val_int();
 
8118
}
 
8119
 
 
8120
 
 
8121
longlong Field_enum::val_int(void)
 
8122
{
 
8123
  ASSERT_COLUMN_MARKED_FOR_READ;
 
8124
  switch (packlength) {
 
8125
  case 1:
 
8126
    return (longlong) ptr[0];
 
8127
  case 2:
 
8128
  {
 
8129
    uint16 tmp;
 
8130
#ifdef WORDS_BIGENDIAN
 
8131
    if (table->s->db_low_byte_first)
 
8132
      tmp=sint2korr(ptr);
 
8133
    else
 
8134
#endif
 
8135
      shortget(tmp,ptr);
 
8136
    return (longlong) tmp;
 
8137
  }
 
8138
  case 3:
 
8139
    return (longlong) uint3korr(ptr);
 
8140
  case 4:
 
8141
  {
 
8142
    uint32 tmp;
 
8143
#ifdef WORDS_BIGENDIAN
 
8144
    if (table->s->db_low_byte_first)
 
8145
      tmp=uint4korr(ptr);
 
8146
    else
 
8147
#endif
 
8148
      longget(tmp,ptr);
 
8149
    return (longlong) tmp;
 
8150
  }
 
8151
  case 8:
 
8152
  {
 
8153
    longlong tmp;
 
8154
#ifdef WORDS_BIGENDIAN
 
8155
    if (table->s->db_low_byte_first)
 
8156
      tmp=sint8korr(ptr);
 
8157
    else
 
8158
#endif
 
8159
      longlongget(tmp,ptr);
 
8160
    return tmp;
 
8161
  }
 
8162
  }
 
8163
  return 0;                                     // impossible
 
8164
}
 
8165
 
 
8166
 
 
8167
/**
 
8168
   Save the field metadata for enum fields.
 
8169
 
 
8170
   Saves the real type in the first byte and the pack length in the 
 
8171
   second byte of the field metadata array at index of *metadata_ptr and
 
8172
   *(metadata_ptr + 1).
 
8173
 
 
8174
   @param   metadata_ptr   First byte of field metadata
 
8175
 
 
8176
   @returns number of bytes written to metadata_ptr
 
8177
*/
 
8178
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
 
8179
{
 
8180
  *metadata_ptr= real_type();
 
8181
  *(metadata_ptr + 1)= pack_length();
 
8182
  return 2;
 
8183
}
 
8184
 
 
8185
 
 
8186
String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
 
8187
                            String *val_ptr)
 
8188
{
 
8189
  uint tmp=(uint) Field_enum::val_int();
 
8190
  if (!tmp || tmp > typelib->count)
 
8191
    val_ptr->set("", 0, field_charset);
 
8192
  else
 
8193
    val_ptr->set((const char*) typelib->type_names[tmp-1],
 
8194
                 typelib->type_lengths[tmp-1],
 
8195
                 field_charset);
 
8196
  return val_ptr;
 
8197
}
 
8198
 
 
8199
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
8200
{
 
8201
  uchar *old= ptr;
 
8202
  ptr= (uchar*) a_ptr;
 
8203
  uint64_t a=Field_enum::val_int();
 
8204
  ptr= (uchar*) b_ptr;
 
8205
  uint64_t b=Field_enum::val_int();
 
8206
  ptr= old;
 
8207
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
8208
}
 
8209
 
 
8210
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
 
8211
{
 
8212
  uint64_t value=Field_enum::val_int();
 
8213
  to+=packlength-1;
 
8214
  for (uint i=0 ; i < packlength ; i++)
 
8215
  {
 
8216
    *to-- = (uchar) (value & 255);
 
8217
    value>>=8;
 
8218
  }
 
8219
}
 
8220
 
 
8221
 
 
8222
void Field_enum::sql_type(String &res) const
 
8223
{
 
8224
  char buffer[255];
 
8225
  String enum_item(buffer, sizeof(buffer), res.charset());
 
8226
 
 
8227
  res.length(0);
 
8228
  res.append(STRING_WITH_LEN("enum("));
 
8229
 
 
8230
  bool flag=0;
 
8231
  uint *len= typelib->type_lengths;
 
8232
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
 
8233
  {
 
8234
    uint dummy_errors;
 
8235
    if (flag)
 
8236
      res.append(',');
 
8237
    /* convert to res.charset() == utf8, then quote */
 
8238
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
 
8239
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
 
8240
    flag= 1;
 
8241
  }
 
8242
  res.append(')');
 
8243
}
 
8244
 
 
8245
 
 
8246
Field *Field_enum::new_field(MEM_ROOT *root, struct st_table *new_table,
 
8247
                             bool keep_type)
 
8248
{
 
8249
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
 
8250
  if (res)
 
8251
    res->typelib= copy_typelib(root, typelib);
 
8252
  return res;
 
8253
}
 
8254
 
 
8255
 
 
8256
/*
 
8257
   set type.
 
8258
   This is a string which can have a collection of different values.
 
8259
   Each string value is separated with a ','.
 
8260
   For example "One,two,five"
 
8261
   If one uses this string in a number context one gets the bits as a longlong
 
8262
   number.
 
8263
*/
 
8264
 
 
8265
 
 
8266
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
 
8267
{
 
8268
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
8269
  bool got_warning= 0;
 
8270
  int err= 0;
 
8271
  char *not_used;
 
8272
  uint not_used2;
 
8273
  uint32 not_used_offset;
 
8274
  char buff[STRING_BUFFER_USUAL_SIZE];
 
8275
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
8276
 
 
8277
  /* Convert character set if necessary */
 
8278
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
 
8279
  { 
 
8280
    uint dummy_errors;
 
8281
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
 
8282
    from= tmpstr.ptr();
 
8283
    length=  tmpstr.length();
 
8284
  }
 
8285
  uint64_t tmp= find_set(typelib, from, length, field_charset,
 
8286
                          &not_used, &not_used2, &got_warning);
 
8287
  if (!tmp && length && length < 22)
 
8288
  {
 
8289
    /* This is for reading numbers with LOAD DATA INFILE */
 
8290
    char *end;
 
8291
    tmp=my_strntoull(cs,from,length,10,&end,&err);
 
8292
    if (err || end != from+length ||
 
8293
        tmp > (uint64_t) (((longlong) 1 << typelib->count) - (longlong) 1))
 
8294
    {
 
8295
      tmp=0;      
 
8296
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
8297
    }
 
8298
  }
 
8299
  else if (got_warning)
 
8300
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
8301
  store_type(tmp);
 
8302
  return err;
 
8303
}
 
8304
 
 
8305
 
 
8306
int Field_set::store(longlong nr, bool unsigned_val)
 
8307
{
 
8308
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
8309
  int error= 0;
 
8310
  uint64_t max_nr= set_bits(uint64_t, typelib->count);
 
8311
  if ((uint64_t) nr > max_nr)
 
8312
  {
 
8313
    nr&= max_nr;
 
8314
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
8315
    error=1;
 
8316
  }
 
8317
  store_type((uint64_t) nr);
 
8318
  return error;
 
8319
}
 
8320
 
 
8321
 
 
8322
String *Field_set::val_str(String *val_buffer,
 
8323
                           String *val_ptr __attribute__((unused)))
 
8324
{
 
8325
  uint64_t tmp=(uint64_t) Field_enum::val_int();
 
8326
  uint bitnr=0;
 
8327
 
 
8328
  val_buffer->length(0);
 
8329
  val_buffer->set_charset(field_charset);
 
8330
  while (tmp && bitnr < (uint) typelib->count)
 
8331
  {
 
8332
    if (tmp & 1)
 
8333
    {
 
8334
      if (val_buffer->length())
 
8335
        val_buffer->append(&field_separator, 1, &my_charset_latin1);
 
8336
      String str(typelib->type_names[bitnr],
 
8337
                 typelib->type_lengths[bitnr],
 
8338
                 field_charset);
 
8339
      val_buffer->append(str);
 
8340
    }
 
8341
    tmp>>=1;
 
8342
    bitnr++;
 
8343
  }
 
8344
  return val_buffer;
 
8345
}
 
8346
 
 
8347
 
 
8348
void Field_set::sql_type(String &res) const
 
8349
{
 
8350
  char buffer[255];
 
8351
  String set_item(buffer, sizeof(buffer), res.charset());
 
8352
 
 
8353
  res.length(0);
 
8354
  res.append(STRING_WITH_LEN("set("));
 
8355
 
 
8356
  bool flag=0;
 
8357
  uint *len= typelib->type_lengths;
 
8358
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
 
8359
  {
 
8360
    uint dummy_errors;
 
8361
    if (flag)
 
8362
      res.append(',');
 
8363
    /* convert to res.charset() == utf8, then quote */
 
8364
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
 
8365
    append_unescaped(&res, set_item.ptr(), set_item.length());
 
8366
    flag= 1;
 
8367
  }
 
8368
  res.append(')');
 
8369
}
 
8370
 
1258
8371
/**
1259
8372
  @retval
1260
8373
    1  if the fields are equally defined
1282
8395
 
1283
8396
  if (typelib->count < from_lib->count)
1284
8397
    return 0;
1285
 
  for (uint32_t i=0 ; i < from_lib->count ; i++)
 
8398
  for (uint i=0 ; i < from_lib->count ; i++)
1286
8399
    if (my_strnncoll(field_charset,
1287
 
                     (const unsigned char*)typelib->type_names[i],
 
8400
                     (const uchar*)typelib->type_names[i],
1288
8401
                     strlen(typelib->type_names[i]),
1289
 
                     (const unsigned char*)from_lib->type_names[i],
 
8402
                     (const uchar*)from_lib->type_names[i],
1290
8403
                     strlen(from_lib->type_names[i])))
1291
8404
      return 0;
1292
8405
  return 1;
1293
8406
}
1294
8407
 
 
8408
/**
 
8409
  @return
 
8410
  returns 1 if the fields are equally defined
 
8411
*/
 
8412
bool Field_num::eq_def(Field *field)
 
8413
{
 
8414
  if (!Field::eq_def(field))
 
8415
    return 0;
 
8416
  Field_num *from_num= (Field_num*) field;
 
8417
 
 
8418
  if (unsigned_flag != from_num->unsigned_flag ||
 
8419
      (zerofill && !from_num->zerofill && !zero_pack()) ||
 
8420
      dec != from_num->dec)
 
8421
    return 0;
 
8422
  return 1;
 
8423
}
 
8424
 
 
8425
 
 
8426
uint Field_num::is_equal(Create_field *new_field)
 
8427
{
 
8428
  return ((new_field->sql_type == real_type()) &&
 
8429
          ((new_field->flags & UNSIGNED_FLAG) == (uint) (flags &
 
8430
                                                         UNSIGNED_FLAG)) &&
 
8431
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
 
8432
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
 
8433
          (new_field->length <= max_display_length()));
 
8434
}
 
8435
 
 
8436
 
 
8437
/*
 
8438
  Bit field.
 
8439
 
 
8440
  We store the first 0 - 6 uneven bits among the null bits 
 
8441
  at the start of the record. The rest bytes are stored in 
 
8442
  the record itself.
 
8443
 
 
8444
  For example:
 
8445
 
 
8446
  CREATE TABLE t1 (a int, b bit(17), c bit(21) not null, d bit(8));
 
8447
  We would store data  as follows in the record:
 
8448
 
 
8449
  Byte        Bit
 
8450
  1           7 - reserve for delete
 
8451
              6 - null bit for 'a'
 
8452
              5 - null bit for 'b'
 
8453
              4 - first (high) bit of 'b'
 
8454
              3 - first (high) bit of 'c'
 
8455
              2 - second bit of 'c'
 
8456
              1 - third bit of 'c'
 
8457
              0 - forth bit of 'c'
 
8458
  2           7 - firth bit of 'c'
 
8459
              6 - null bit for 'd'
 
8460
  3 - 6       four bytes for 'a'
 
8461
  7 - 8       two bytes for 'b'
 
8462
  9 - 10      two bytes for 'c'
 
8463
  11          one byte for 'd'
 
8464
*/
 
8465
 
 
8466
Field_bit::Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
8467
                     uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
 
8468
                     enum utype unireg_check_arg, const char *field_name_arg)
 
8469
  : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
8470
          unireg_check_arg, field_name_arg),
 
8471
    bit_ptr(bit_ptr_arg), bit_ofs(bit_ofs_arg), bit_len(len_arg & 7),
 
8472
    bytes_in_rec(len_arg / 8)
 
8473
{
 
8474
  flags|= UNSIGNED_FLAG;
 
8475
  /*
 
8476
    Ensure that Field::eq() can distinguish between two different bit fields.
 
8477
    (two bit fields that are not null, may have same ptr and null_ptr)
 
8478
  */
 
8479
  if (!null_ptr_arg)
 
8480
    null_bit= bit_ofs_arg;
 
8481
}
 
8482
 
 
8483
 
 
8484
void Field_bit::hash(ulong *nr, ulong *nr2)
 
8485
{
 
8486
  if (is_null())
 
8487
  {
 
8488
    *nr^= (*nr << 1) | 1;
 
8489
  }
 
8490
  else
 
8491
  {
 
8492
    CHARSET_INFO *cs= &my_charset_bin;
 
8493
    longlong value= Field_bit::val_int();
 
8494
    uchar tmp[8];
 
8495
    mi_int8store(tmp,value);
 
8496
    cs->coll->hash_sort(cs, tmp, 8, nr, nr2);
 
8497
  }
 
8498
}
 
8499
 
 
8500
 
 
8501
size_t
 
8502
Field_bit::do_last_null_byte() const
 
8503
{
 
8504
  /*
 
8505
    Code elsewhere is assuming that bytes are 8 bits, so I'm using
 
8506
    that value instead of the correct one: CHAR_BIT.
 
8507
 
 
8508
    REFACTOR SUGGESTION (Matz): Change to use the correct number of
 
8509
    bits. On systems with CHAR_BIT > 8 (not very common), the storage
 
8510
    will lose the extra bits.
 
8511
  */
 
8512
  DBUG_PRINT("test", ("bit_ofs: %d, bit_len: %d  bit_ptr: 0x%lx",
 
8513
                      bit_ofs, bit_len, (long) bit_ptr));
 
8514
  uchar *result;
 
8515
  if (bit_len == 0)
 
8516
    result= null_ptr;
 
8517
  else if (bit_ofs + bit_len > 8)
 
8518
    result= bit_ptr + 1;
 
8519
  else
 
8520
    result= bit_ptr;
 
8521
 
 
8522
  if (result)
 
8523
    return (size_t) (result - table->record[0]) + 1;
 
8524
  return LAST_NULL_BYTE_UNDEF;
 
8525
}
 
8526
 
 
8527
 
 
8528
Field *Field_bit::new_key_field(MEM_ROOT *root,
 
8529
                                struct st_table *new_table,
 
8530
                                uchar *new_ptr, uchar *new_null_ptr,
 
8531
                                uint new_null_bit)
 
8532
{
 
8533
  Field_bit *res;
 
8534
  if ((res= (Field_bit*) Field::new_key_field(root, new_table,
 
8535
                                              new_ptr, new_null_ptr,
 
8536
                                              new_null_bit)))
 
8537
  {
 
8538
    /* Move bits normally stored in null_pointer to new_ptr */
 
8539
    res->bit_ptr= new_ptr;
 
8540
    res->bit_ofs= 0;
 
8541
    if (bit_len)
 
8542
      res->ptr++;                               // Store rest of data here
 
8543
  }
 
8544
  return res;
 
8545
}
 
8546
 
 
8547
 
 
8548
uint Field_bit::is_equal(Create_field *new_field) 
 
8549
{
 
8550
  return (new_field->sql_type == real_type() &&
 
8551
          new_field->length == max_display_length());
 
8552
}
 
8553
 
 
8554
                       
 
8555
int Field_bit::store(const char *from, uint length, CHARSET_INFO *cs)
 
8556
{
 
8557
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
8558
  int delta;
 
8559
 
 
8560
  for (; length && !*from; from++, length--) {};          // skip left 0's
 
8561
  delta= bytes_in_rec - length;
 
8562
 
 
8563
  if (delta < -1 ||
 
8564
      (delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
 
8565
      (!bit_len && delta < 0))
 
8566
  {
 
8567
    set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
 
8568
    memset(ptr, 0xff, bytes_in_rec);
 
8569
    if (table->in_use->really_abort_on_warning())
 
8570
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
8571
    else
 
8572
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
8573
    return 1;
 
8574
  }
 
8575
  /* delta is >= -1 here */
 
8576
  if (delta > 0)
 
8577
  {
 
8578
    if (bit_len)
 
8579
      clr_rec_bits(bit_ptr, bit_ofs, bit_len);
 
8580
    bzero(ptr, delta);
 
8581
    memcpy(ptr + delta, from, length);
 
8582
  }
 
8583
  else if (delta == 0)
 
8584
  {
 
8585
    if (bit_len)
 
8586
      clr_rec_bits(bit_ptr, bit_ofs, bit_len);
 
8587
    memcpy(ptr, from, length);
 
8588
  }
 
8589
  else
 
8590
  {
 
8591
    if (bit_len)
 
8592
    {
 
8593
      set_rec_bits((uchar) *from, bit_ptr, bit_ofs, bit_len);
 
8594
      from++;
 
8595
    }
 
8596
    memcpy(ptr, from, bytes_in_rec);
 
8597
  }
 
8598
  return 0;
 
8599
}
 
8600
 
 
8601
 
 
8602
int Field_bit::store(double nr)
 
8603
{
 
8604
  return Field_bit::store((longlong) nr, FALSE);
 
8605
}
 
8606
 
 
8607
 
 
8608
int Field_bit::store(longlong nr, bool unsigned_val)
 
8609
{
 
8610
  char buf[8];
 
8611
 
 
8612
  mi_int8store(buf, nr);
 
8613
  return store(buf, 8, NULL);
 
8614
}
 
8615
 
 
8616
 
 
8617
int Field_bit::store_decimal(const my_decimal *val)
 
8618
{
 
8619
  int err= 0;
 
8620
  longlong i= convert_decimal2longlong(val, 1, &err);
 
8621
  return test(err | store(i, TRUE));
 
8622
}
 
8623
 
 
8624
 
 
8625
double Field_bit::val_real(void)
 
8626
{
 
8627
  return (double) Field_bit::val_int();
 
8628
}
 
8629
 
 
8630
 
 
8631
longlong Field_bit::val_int(void)
 
8632
{
 
8633
  ASSERT_COLUMN_MARKED_FOR_READ;
 
8634
  uint64_t bits= 0;
 
8635
  if (bit_len)
 
8636
  {
 
8637
    bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
 
8638
    bits<<= (bytes_in_rec * 8);
 
8639
  }
 
8640
 
 
8641
  switch (bytes_in_rec) {
 
8642
  case 0: return bits;
 
8643
  case 1: return bits | (uint64_t) ptr[0];
 
8644
  case 2: return bits | mi_uint2korr(ptr);
 
8645
  case 3: return bits | mi_uint3korr(ptr);
 
8646
  case 4: return bits | mi_uint4korr(ptr);
 
8647
  case 5: return bits | mi_uint5korr(ptr);
 
8648
  case 6: return bits | mi_uint6korr(ptr);
 
8649
  case 7: return bits | mi_uint7korr(ptr);
 
8650
  default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
 
8651
  }
 
8652
}  
 
8653
 
 
8654
 
 
8655
String *Field_bit::val_str(String *val_buffer,
 
8656
                           String *val_ptr __attribute__((unused)))
 
8657
{
 
8658
  ASSERT_COLUMN_MARKED_FOR_READ;
 
8659
  char buff[sizeof(longlong)];
 
8660
  uint length= min(pack_length(), sizeof(longlong));
 
8661
  uint64_t bits= val_int();
 
8662
  mi_int8store(buff,bits);
 
8663
 
 
8664
  val_buffer->alloc(length);
 
8665
  memcpy_fixed((char*) val_buffer->ptr(), buff+8-length, length);
 
8666
  val_buffer->length(length);
 
8667
  val_buffer->set_charset(&my_charset_bin);
 
8668
  return val_buffer;
 
8669
}
 
8670
 
 
8671
 
 
8672
my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
 
8673
{
 
8674
  ASSERT_COLUMN_MARKED_FOR_READ;
 
8675
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
 
8676
  return deciaml_value;
 
8677
}
 
8678
 
 
8679
 
 
8680
/*
 
8681
  Compare two bit fields using pointers within the record.
 
8682
  SYNOPSIS
 
8683
    cmp_max()
 
8684
    a                 Pointer to field->ptr in first record
 
8685
    b                 Pointer to field->ptr in second record
 
8686
    max_len           Maximum length used in index
 
8687
  DESCRIPTION
 
8688
    This method is used from key_rec_cmp used by merge sorts used
 
8689
    by partitioned index read and later other similar places.
 
8690
    The a and b pointer must be pointers to the field in a record
 
8691
    (not the table->record[0] necessarily)
 
8692
*/
 
8693
int Field_bit::cmp_max(const uchar *a, const uchar *b, uint max_len)
 
8694
{
 
8695
  my_ptrdiff_t a_diff= a - ptr;
 
8696
  my_ptrdiff_t b_diff= b - ptr;
 
8697
  if (bit_len)
 
8698
  {
 
8699
    int flag;
 
8700
    uchar bits_a= get_rec_bits(bit_ptr+a_diff, bit_ofs, bit_len);
 
8701
    uchar bits_b= get_rec_bits(bit_ptr+b_diff, bit_ofs, bit_len);
 
8702
    if ((flag= (int) (bits_a - bits_b)))
 
8703
      return flag;
 
8704
  }
 
8705
  return memcmp(a, b, field_length);
 
8706
}
 
8707
 
 
8708
 
 
8709
int Field_bit::key_cmp(const uchar *str, uint length)
 
8710
{
 
8711
  if (bit_len)
 
8712
  {
 
8713
    int flag;
 
8714
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
 
8715
    if ((flag= (int) (bits - *str)))
 
8716
      return flag;
 
8717
    str++;
 
8718
    length--;
 
8719
  }
 
8720
  return memcmp(ptr, str, length);
 
8721
}
 
8722
 
 
8723
 
 
8724
int Field_bit::cmp_offset(uint row_offset)
 
8725
{
 
8726
  if (bit_len)
 
8727
  {
 
8728
    int flag;
 
8729
    uchar bits_a= get_rec_bits(bit_ptr, bit_ofs, bit_len);
 
8730
    uchar bits_b= get_rec_bits(bit_ptr + row_offset, bit_ofs, bit_len);
 
8731
    if ((flag= (int) (bits_a - bits_b)))
 
8732
      return flag;
 
8733
  }
 
8734
  return memcmp(ptr, ptr + row_offset, bytes_in_rec);
 
8735
}
 
8736
 
 
8737
 
 
8738
uint Field_bit::get_key_image(uchar *buff, uint length, imagetype type_arg)
 
8739
{
 
8740
  if (bit_len)
 
8741
  {
 
8742
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
 
8743
    *buff++= bits;
 
8744
    length--;
 
8745
  }
 
8746
  uint data_length = min(length, bytes_in_rec);
 
8747
  memcpy(buff, ptr, data_length);
 
8748
  return data_length + 1;
 
8749
}
 
8750
 
 
8751
 
 
8752
/**
 
8753
   Save the field metadata for bit fields.
 
8754
 
 
8755
   Saves the bit length in the first byte and bytes in record in the
 
8756
   second byte of the field metadata array at index of *metadata_ptr and
 
8757
   *(metadata_ptr + 1).
 
8758
 
 
8759
   @param   metadata_ptr   First byte of field metadata
 
8760
 
 
8761
   @returns number of bytes written to metadata_ptr
 
8762
*/
 
8763
int Field_bit::do_save_field_metadata(uchar *metadata_ptr)
 
8764
{
 
8765
  *metadata_ptr= bit_len;
 
8766
  *(metadata_ptr + 1)= bytes_in_rec;
 
8767
  return 2;
 
8768
}
 
8769
 
 
8770
 
 
8771
/**
 
8772
   Returns the number of bytes field uses in row-based replication 
 
8773
   row packed size.
 
8774
 
 
8775
   This method is used in row-based replication to determine the number
 
8776
   of bytes that the field consumes in the row record format. This is
 
8777
   used to skip fields in the master that do not exist on the slave.
 
8778
 
 
8779
   @param   field_metadata   Encoded size in field metadata
 
8780
 
 
8781
   @returns The size of the field based on the field metadata.
 
8782
*/
 
8783
uint Field_bit::pack_length_from_metadata(uint field_metadata)
 
8784
{
 
8785
  uint const from_len= (field_metadata >> 8U) & 0x00ff;
 
8786
  uint const from_bit_len= field_metadata & 0x00ff;
 
8787
  uint const source_size= from_len + ((from_bit_len > 0) ? 1 : 0);
 
8788
  return (source_size);
 
8789
}
 
8790
 
 
8791
 
 
8792
/**
 
8793
   Check to see if field size is compatible with destination.
 
8794
 
 
8795
   This method is used in row-based replication to verify that the slave's
 
8796
   field size is less than or equal to the master's field size. The 
 
8797
   encoded field metadata (from the master or source) is decoded and compared
 
8798
   to the size of this field (the slave or destination). 
 
8799
 
 
8800
   @param   field_metadata   Encoded size in field metadata
 
8801
 
 
8802
   @retval 0 if this field's size is < the source field's size
 
8803
   @retval 1 if this field's size is >= the source field's size
 
8804
*/
 
8805
int Field_bit::compatible_field_size(uint field_metadata)
 
8806
{
 
8807
  int compatible= 0;
 
8808
  uint const source_size= pack_length_from_metadata(field_metadata);
 
8809
  uint const destination_size= row_pack_length();
 
8810
  uint const from_bit_len= field_metadata & 0x00ff;
 
8811
  uint const from_len= (field_metadata >> 8U) & 0x00ff;
 
8812
  if ((bit_len == 0) || (from_bit_len == 0))
 
8813
    compatible= (source_size <= destination_size);
 
8814
  else if (from_bit_len > bit_len)
 
8815
    compatible= (from_len < bytes_in_rec);
 
8816
  else
 
8817
    compatible= ((from_bit_len <= bit_len) && (from_len <= bytes_in_rec));
 
8818
  return (compatible);
 
8819
}
 
8820
 
 
8821
 
 
8822
 
 
8823
void Field_bit::sql_type(String &res) const
 
8824
{
 
8825
  CHARSET_INFO *cs= res.charset();
 
8826
  ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
 
8827
                                   "bit(%d)", (int) field_length);
 
8828
  res.length((uint) length);
 
8829
}
 
8830
 
 
8831
 
 
8832
uchar *
 
8833
Field_bit::pack(uchar *to, const uchar *from, uint max_length,
 
8834
                bool low_byte_first __attribute__((unused)))
 
8835
{
 
8836
  DBUG_ASSERT(max_length > 0);
 
8837
  uint length;
 
8838
  if (bit_len > 0)
 
8839
  {
 
8840
    /*
 
8841
      We have the following:
 
8842
 
 
8843
      ptr        Points into a field in record R1
 
8844
      from       Points to a field in a record R2
 
8845
      bit_ptr    Points to the byte (in the null bytes) that holds the
 
8846
                 odd bits of R1
 
8847
      from_bitp  Points to the byte that holds the odd bits of R2
 
8848
 
 
8849
      We have the following:
 
8850
 
 
8851
          ptr - bit_ptr = from - from_bitp
 
8852
 
 
8853
      We want to isolate 'from_bitp', so this gives:
 
8854
 
 
8855
          ptr - bit_ptr - from = - from_bitp
 
8856
          - ptr + bit_ptr + from = from_bitp
 
8857
          bit_ptr + from - ptr = from_bitp
 
8858
     */
 
8859
    uchar bits= get_rec_bits(bit_ptr + (from - ptr), bit_ofs, bit_len);
 
8860
    *to++= bits;
 
8861
  }
 
8862
  length= min(bytes_in_rec, max_length - (bit_len > 0));
 
8863
  memcpy(to, from, length);
 
8864
  return to + length;
 
8865
}
 
8866
 
 
8867
 
 
8868
/**
 
8869
   Unpack a bit field from row data.
 
8870
 
 
8871
   This method is used to unpack a bit field from a master whose size
 
8872
   of the field is less than that of the slave.
 
8873
 
 
8874
   @param   to         Destination of the data
 
8875
   @param   from       Source of the data
 
8876
   @param   param_data Bit length (upper) and length (lower) values
 
8877
 
 
8878
   @return  New pointer into memory based on from + length of the data
 
8879
*/
 
8880
const uchar *
 
8881
Field_bit::unpack(uchar *to, const uchar *from, uint param_data,
 
8882
                  bool low_byte_first __attribute__((unused)))
 
8883
{
 
8884
  uint const from_len= (param_data >> 8U) & 0x00ff;
 
8885
  uint const from_bit_len= param_data & 0x00ff;
 
8886
  /*
 
8887
    If the parameter data is zero (i.e., undefined), or if the master
 
8888
    and slave have the same sizes, then use the old unpack() method.
 
8889
  */
 
8890
  if (param_data == 0 || ((from_bit_len == bit_len) && (from_len == bytes_in_rec)))
 
8891
  {
 
8892
    if (bit_len > 0)
 
8893
    {
 
8894
      /*
 
8895
        set_rec_bits is a macro, don't put the post-increment in the
 
8896
        argument since that might cause strange side-effects.
 
8897
 
 
8898
        For the choice of the second argument, see the explanation for
 
8899
        Field_bit::pack().
 
8900
      */
 
8901
      set_rec_bits(*from, bit_ptr + (to - ptr), bit_ofs, bit_len);
 
8902
      from++;
 
8903
    }
 
8904
    memcpy(to, from, bytes_in_rec);
 
8905
    return from + bytes_in_rec;
 
8906
  }
 
8907
 
 
8908
  /*
 
8909
    We are converting a smaller bit field to a larger one here.
 
8910
    To do that, we first need to construct a raw value for the original
 
8911
    bit value stored in the from buffer. Then that needs to be converted
 
8912
    to the larger field then sent to store() for writing to the field.
 
8913
    Lastly the odd bits need to be masked out if the bytes_in_rec > 0.
 
8914
    Otherwise stray bits can cause spurious values.
 
8915
  */
 
8916
  uint new_len= (field_length + 7) / 8;
 
8917
  char *value= (char *)my_alloca(new_len);
 
8918
  bzero(value, new_len);
 
8919
  uint len= from_len + ((from_bit_len > 0) ? 1 : 0);
 
8920
  memcpy(value + (new_len - len), from, len);
 
8921
  /*
 
8922
    Mask out the unused bits in the partial byte. 
 
8923
    TODO: Add code to the master to always mask these bits and remove
 
8924
          the following.
 
8925
  */
 
8926
  if ((from_bit_len > 0) && (from_len > 0))
 
8927
    value[new_len - len]= value[new_len - len] & ((1U << from_bit_len) - 1);
 
8928
  bitmap_set_bit(table->write_set,field_index);
 
8929
  store(value, new_len, system_charset_info);
 
8930
  my_afree(value);
 
8931
  return from + len;
 
8932
}
 
8933
 
 
8934
 
 
8935
void Field_bit::set_default()
 
8936
{
 
8937
  if (bit_len > 0)
 
8938
  {
 
8939
    my_ptrdiff_t const offset= table->s->default_values - table->record[0];
 
8940
    uchar bits= get_rec_bits(bit_ptr + offset, bit_ofs, bit_len);
 
8941
    set_rec_bits(bits, bit_ptr, bit_ofs, bit_len);
 
8942
  }
 
8943
  Field::set_default();
 
8944
}
 
8945
 
 
8946
/*
 
8947
  Bit field support for non-MyISAM tables.
 
8948
*/
 
8949
 
 
8950
Field_bit_as_char::Field_bit_as_char(uchar *ptr_arg, uint32 len_arg,
 
8951
                                     uchar *null_ptr_arg, uchar null_bit_arg,
 
8952
                                     enum utype unireg_check_arg,
 
8953
                                     const char *field_name_arg)
 
8954
  :Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0, 0,
 
8955
             unireg_check_arg, field_name_arg)
 
8956
{
 
8957
  flags|= UNSIGNED_FLAG;
 
8958
  bit_len= 0;
 
8959
  bytes_in_rec= (len_arg + 7) / 8;
 
8960
}
 
8961
 
 
8962
 
 
8963
int Field_bit_as_char::store(const char *from, uint length, CHARSET_INFO *cs)
 
8964
{
 
8965
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
8966
  int delta;
 
8967
  uchar bits= (uchar) (field_length & 7);
 
8968
 
 
8969
  for (; length && !*from; from++, length--) {};          // skip left 0's
 
8970
  delta= bytes_in_rec - length;
 
8971
 
 
8972
  if (delta < 0 ||
 
8973
      (delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
 
8974
  {
 
8975
    memset(ptr, 0xff, bytes_in_rec);
 
8976
    if (bits)
 
8977
      *ptr&= ((1 << bits) - 1); /* set first uchar */
 
8978
    if (table->in_use->really_abort_on_warning())
 
8979
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
8980
    else
 
8981
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
8982
    return 1;
 
8983
  }
 
8984
  bzero(ptr, delta);
 
8985
  memcpy(ptr + delta, from, length);
 
8986
  return 0;
 
8987
}
 
8988
 
 
8989
 
 
8990
void Field_bit_as_char::sql_type(String &res) const
 
8991
{
 
8992
  CHARSET_INFO *cs= res.charset();
 
8993
  ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
 
8994
                                   "bit(%d)", (int) field_length);
 
8995
  res.length((uint) length);
 
8996
}
 
8997
 
 
8998
 
1295
8999
/*****************************************************************************
1296
9000
  Handling of field and Create_field
1297
9001
*****************************************************************************/
1303
9007
void Create_field::create_length_to_internal_length(void)
1304
9008
{
1305
9009
  switch (sql_type) {
1306
 
  case DRIZZLE_TYPE_BLOB:
1307
 
  case DRIZZLE_TYPE_VARCHAR:
 
9010
  case MYSQL_TYPE_TINY_BLOB:
 
9011
  case MYSQL_TYPE_MEDIUM_BLOB:
 
9012
  case MYSQL_TYPE_LONG_BLOB:
 
9013
  case MYSQL_TYPE_BLOB:
 
9014
  case MYSQL_TYPE_VAR_STRING:
 
9015
  case MYSQL_TYPE_STRING:
 
9016
  case MYSQL_TYPE_VARCHAR:
1308
9017
    length*= charset->mbmaxlen;
1309
9018
    key_length= length;
1310
9019
    pack_length= calc_pack_length(sql_type, length);
1311
9020
    break;
1312
 
  case DRIZZLE_TYPE_ENUM:
 
9021
  case MYSQL_TYPE_ENUM:
 
9022
  case MYSQL_TYPE_SET:
1313
9023
    /* Pack_length already calculated in sql_parse.cc */
1314
9024
    length*= charset->mbmaxlen;
1315
9025
    key_length= pack_length;
1316
9026
    break;
1317
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
9027
  case MYSQL_TYPE_BIT:
 
9028
    if (f_bit_as_char(pack_flag))
 
9029
    {
 
9030
      key_length= pack_length= ((length + 7) & ~7) / 8;
 
9031
    }
 
9032
    else
 
9033
    {
 
9034
      pack_length= length / 8;
 
9035
      /* We need one extra byte to store the bits we save among the null bits */
 
9036
      key_length= pack_length + test(length & 7);
 
9037
    }
 
9038
    break;
 
9039
  case MYSQL_TYPE_NEWDECIMAL:
1318
9040
    key_length= pack_length=
1319
9041
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1320
9042
                                                                decimals,
1333
9055
  Init for a tmp table field. To be extended if need be.
1334
9056
*/
1335
9057
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1336
 
                                      uint32_t length_arg, uint32_t decimals_arg,
 
9058
                                      uint32 length_arg, uint32 decimals_arg,
1337
9059
                                      bool maybe_null, bool is_unsigned)
1338
9060
{
1339
9061
  field_name= "";
1346
9068
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1347
9069
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1348
9070
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
1349
 
  vcol_info= NULL;
1350
 
  is_stored= true;
1351
9071
}
1352
9072
 
1353
9073
 
1366
9086
  @param fld_change            Field change
1367
9087
  @param fld_interval_list     Interval list (if any)
1368
9088
  @param fld_charset           Field charset
1369
 
  @param fld_vcol_info         Virtual column data
1370
9089
 
1371
9090
  @retval
1372
 
    false on success
 
9091
    FALSE on success
1373
9092
  @retval
1374
 
    true  on error
 
9093
    TRUE  on error
1375
9094
*/
1376
9095
 
1377
 
bool Create_field::init(THD *thd __attribute__((unused)), char *fld_name, enum_field_types fld_type,
 
9096
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
1378
9097
                        char *fld_length, char *fld_decimals,
1379
 
                        uint32_t fld_type_modifier, Item *fld_default_value,
 
9098
                        uint fld_type_modifier, Item *fld_default_value,
1380
9099
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
1381
9100
                        char *fld_change, List<String> *fld_interval_list,
1382
 
                        const CHARSET_INFO * const fld_charset,
1383
 
                        uint32_t fld_geom_type __attribute__((unused)),
1384
 
                        enum column_format_type column_format,
1385
 
                        virtual_column_info *fld_vcol_info)
 
9101
                        CHARSET_INFO *fld_charset, uint fld_geom_type,
 
9102
                        enum ha_storage_media storage_type,
 
9103
                        enum column_format_type column_format)
1386
9104
{
1387
 
  uint32_t sign_len, allowed_type_modifier= 0;
1388
 
  uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
 
9105
  uint sign_len, allowed_type_modifier= 0;
 
9106
  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
1389
9107
 
 
9108
  DBUG_ENTER("Create_field::init()");
 
9109
  
1390
9110
  field= 0;
1391
9111
  field_name= fld_name;
1392
9112
  def= fld_default_value;
1393
9113
  flags= fld_type_modifier;
1394
 
  flags|= (((uint32_t)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
 
9114
  flags|= (((uint)storage_type & STORAGE_TYPE_MASK) << FIELD_STORAGE_FLAGS);
 
9115
  flags|= (((uint)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1395
9116
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1396
9117
                 Field::NEXT_NUMBER : Field::NONE);
1397
 
  decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
 
9118
  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
1398
9119
  if (decimals >= NOT_FIXED_DEC)
1399
9120
  {
1400
9121
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1401
9122
             NOT_FIXED_DEC-1);
1402
 
    return(true);
 
9123
    DBUG_RETURN(TRUE);
1403
9124
  }
1404
9125
 
1405
9126
  sql_type= fld_type;
1411
9132
  interval_list.empty();
1412
9133
 
1413
9134
  comment= *fld_comment;
1414
 
  vcol_info= fld_vcol_info;
1415
 
  is_stored= true;
1416
 
 
1417
 
  /* Initialize data for a virtual field */
1418
 
  if (fld_type == DRIZZLE_TYPE_VIRTUAL)
1419
 
  {
1420
 
    assert(vcol_info && vcol_info->expr_item);
1421
 
    is_stored= vcol_info->get_field_stored();
1422
 
    /*
1423
 
      Perform per item-type checks to determine if the expression is 
1424
 
      allowed for a virtual column.
1425
 
      Note that validation of the specific function is done later in
1426
 
      procedures open_table_from_share and fix_fields_vcol_func
1427
 
    */
1428
 
    switch (vcol_info->expr_item->type()) {
1429
 
    case Item::FUNC_ITEM:
1430
 
         if (((Item_func *)vcol_info->expr_item)->functype() == Item_func::FUNC_SP)
1431
 
         {
1432
 
           my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1433
 
           return(true);
1434
 
         }
1435
 
         break;
1436
 
    case Item::COPY_STR_ITEM:
1437
 
    case Item::FIELD_AVG_ITEM:
1438
 
    case Item::PROC_ITEM:
1439
 
    case Item::REF_ITEM:
1440
 
    case Item::FIELD_STD_ITEM:
1441
 
    case Item::FIELD_VARIANCE_ITEM:
1442
 
    case Item::INSERT_VALUE_ITEM:
1443
 
    case Item::SUBSELECT_ITEM:
1444
 
    case Item::CACHE_ITEM:
1445
 
    case Item::TYPE_HOLDER:
1446
 
    case Item::PARAM_ITEM:
1447
 
    case Item::VIEW_FIXER_ITEM: 
1448
 
         my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1449
 
         return true;
1450
 
         break;
1451
 
    default: 
1452
 
      // Continue with the field creation
1453
 
      break;
1454
 
    }
1455
 
    /*
1456
 
      Make a field created for the real type.
1457
 
      Note that "real" and virtual fields differ from each other
1458
 
      only by Field::vcol_info, which is always 0 for normal columns.
1459
 
      vcol_info is updated for fields later in procedure open_binary_frm.
1460
 
    */
1461
 
    sql_type= fld_type= vcol_info->get_real_type();
1462
 
  }
1463
 
 
1464
9135
  /*
1465
9136
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1466
9137
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1467
9138
  */
1468
9139
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1469
 
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
 
9140
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != MYSQL_TYPE_TIMESTAMP)
1470
9141
    flags|= NO_DEFAULT_VALUE_FLAG;
1471
9142
 
1472
 
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
 
9143
  if (fld_length && !(length= (uint) atoi(fld_length)))
1473
9144
    fld_length= 0; /* purecov: inspected */
1474
9145
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1475
9146
 
1476
9147
  switch (fld_type) {
1477
 
  case DRIZZLE_TYPE_TINY:
 
9148
  case MYSQL_TYPE_TINY:
1478
9149
    if (!fld_length)
1479
9150
      length= MAX_TINYINT_WIDTH+sign_len;
1480
9151
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1481
9152
    break;
1482
 
  case DRIZZLE_TYPE_LONG:
 
9153
  case MYSQL_TYPE_SHORT:
 
9154
    if (!fld_length)
 
9155
      length= MAX_SMALLINT_WIDTH+sign_len;
 
9156
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
9157
    break;
 
9158
  case MYSQL_TYPE_INT24:
 
9159
    if (!fld_length)
 
9160
      length= MAX_MEDIUMINT_WIDTH+sign_len;
 
9161
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
9162
    break;
 
9163
  case MYSQL_TYPE_LONG:
1483
9164
    if (!fld_length)
1484
9165
      length= MAX_INT_WIDTH+sign_len;
1485
9166
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1486
9167
    break;
1487
 
  case DRIZZLE_TYPE_LONGLONG:
 
9168
  case MYSQL_TYPE_LONGLONG:
1488
9169
    if (!fld_length)
1489
9170
      length= MAX_BIGINT_WIDTH;
1490
9171
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1491
9172
    break;
1492
 
  case DRIZZLE_TYPE_NULL:
 
9173
  case MYSQL_TYPE_NULL:
1493
9174
    break;
1494
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
9175
  case MYSQL_TYPE_NEWDECIMAL:
1495
9176
    my_decimal_trim(&length, &decimals);
1496
9177
    if (length > DECIMAL_MAX_PRECISION)
1497
9178
    {
1498
9179
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1499
9180
               DECIMAL_MAX_PRECISION);
1500
 
      return(true);
 
9181
      DBUG_RETURN(TRUE);
1501
9182
    }
1502
9183
    if (length < decimals)
1503
9184
    {
1504
9185
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1505
 
      return(true);
 
9186
      DBUG_RETURN(TRUE);
1506
9187
    }
1507
9188
    length=
1508
9189
      my_decimal_precision_to_length(length, decimals,
1510
9191
    pack_length=
1511
9192
      my_decimal_get_binary_size(length, decimals);
1512
9193
    break;
1513
 
  case DRIZZLE_TYPE_VARCHAR:
 
9194
  case MYSQL_TYPE_VARCHAR:
1514
9195
    /*
1515
9196
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1516
9197
      if they don't have a default value
1517
9198
    */
1518
9199
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1519
9200
    break;
1520
 
  case DRIZZLE_TYPE_BLOB:
 
9201
  case MYSQL_TYPE_STRING:
 
9202
    break;
 
9203
  case MYSQL_TYPE_BLOB:
 
9204
  case MYSQL_TYPE_TINY_BLOB:
 
9205
  case MYSQL_TYPE_LONG_BLOB:
 
9206
  case MYSQL_TYPE_MEDIUM_BLOB:
1521
9207
    if (fld_default_value)
1522
9208
    {
1523
9209
      /* Allow empty as default value. */
1524
9210
      String str,*res;
1525
9211
      res= fld_default_value->val_str(&str);
 
9212
      /*
 
9213
        A default other than '' is always an error, and any non-NULL
 
9214
        specified default is an error in strict mode.
 
9215
      */
 
9216
      if (res->length() || (thd->variables.sql_mode &
 
9217
                            (MODE_STRICT_TRANS_TABLES |
 
9218
                             MODE_STRICT_ALL_TABLES)))
 
9219
      {
 
9220
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
 
9221
                 fld_name); /* purecov: inspected */
 
9222
        DBUG_RETURN(TRUE);
 
9223
      }
 
9224
      else
 
9225
      {
 
9226
        /*
 
9227
          Otherwise a default of '' is just a warning.
 
9228
        */
 
9229
        push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
9230
                            ER_BLOB_CANT_HAVE_DEFAULT,
 
9231
                            ER(ER_BLOB_CANT_HAVE_DEFAULT),
 
9232
                            fld_name);
 
9233
      }
 
9234
      def= 0;
1526
9235
    }
1527
9236
    flags|= BLOB_FLAG;
1528
9237
    break;
1529
 
  case DRIZZLE_TYPE_DOUBLE:
 
9238
  case MYSQL_TYPE_YEAR:
 
9239
    if (!fld_length || length != 2)
 
9240
      length= 4; /* Default length */
 
9241
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
9242
    break;
 
9243
  case MYSQL_TYPE_FLOAT:
 
9244
    /* change FLOAT(precision) to FLOAT or DOUBLE */
 
9245
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
9246
    if (fld_length && !fld_decimals)
 
9247
    {
 
9248
      uint tmp_length= length;
 
9249
      if (tmp_length > PRECISION_FOR_DOUBLE)
 
9250
      {
 
9251
        my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
 
9252
        DBUG_RETURN(TRUE);
 
9253
      }
 
9254
      else if (tmp_length > PRECISION_FOR_FLOAT)
 
9255
      {
 
9256
        sql_type= MYSQL_TYPE_DOUBLE;
 
9257
        length= DBL_DIG+7; /* -[digits].E+### */
 
9258
      }
 
9259
      else
 
9260
        length= FLT_DIG+6; /* -[digits].E+## */
 
9261
      decimals= NOT_FIXED_DEC;
 
9262
      break;
 
9263
    }
 
9264
    if (!fld_length && !fld_decimals)
 
9265
    {
 
9266
      length=  FLT_DIG+6;
 
9267
      decimals= NOT_FIXED_DEC;
 
9268
    }
 
9269
    if (length < decimals &&
 
9270
        decimals != NOT_FIXED_DEC)
 
9271
    {
 
9272
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
 
9273
      DBUG_RETURN(TRUE);
 
9274
    }
 
9275
    break;
 
9276
  case MYSQL_TYPE_DOUBLE:
1530
9277
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1531
9278
    if (!fld_length && !fld_decimals)
1532
9279
    {
1537
9284
        decimals != NOT_FIXED_DEC)
1538
9285
    {
1539
9286
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1540
 
      return(true);
 
9287
      DBUG_RETURN(TRUE);
1541
9288
    }
1542
9289
    break;
1543
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
9290
  case MYSQL_TYPE_TIMESTAMP:
1544
9291
    if (!fld_length)
1545
9292
    {
1546
9293
      /* Compressed date YYYYMMDDHHMMSS */
1553
9300
        and 19 as length of 4.1 compatible representation.
1554
9301
      */
1555
9302
      length= ((length+1)/2)*2; /* purecov: inspected */
1556
 
      length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
 
9303
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1557
9304
    }
1558
 
    flags|= UNSIGNED_FLAG;
 
9305
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
1559
9306
    if (fld_default_value)
1560
9307
    {
1561
9308
      /* Grammar allows only NOW() value for ON UPDATE clause */
1594
9341
                                              Field::NONE));
1595
9342
    }
1596
9343
    break;
1597
 
  case DRIZZLE_TYPE_NEWDATE:
1598
 
    length= 10;
1599
 
    break;
1600
 
  case DRIZZLE_TYPE_TIME:
1601
 
    length= 10;
1602
 
    break;
1603
 
  case DRIZZLE_TYPE_DATETIME:
 
9344
  case MYSQL_TYPE_DATE:
 
9345
    /* Old date type. */
 
9346
    sql_type= MYSQL_TYPE_NEWDATE;
 
9347
    /* fall trough */
 
9348
  case MYSQL_TYPE_NEWDATE:
 
9349
    length= 10;
 
9350
    break;
 
9351
  case MYSQL_TYPE_TIME:
 
9352
    length= 10;
 
9353
    break;
 
9354
  case MYSQL_TYPE_DATETIME:
1604
9355
    length= MAX_DATETIME_WIDTH;
1605
9356
    break;
1606
 
  case DRIZZLE_TYPE_ENUM:
 
9357
  case MYSQL_TYPE_SET:
 
9358
    {
 
9359
      pack_length= get_set_pack_length(fld_interval_list->elements);
 
9360
 
 
9361
      List_iterator<String> it(*fld_interval_list);
 
9362
      String *tmp;
 
9363
      while ((tmp= it++))
 
9364
        interval_list.push_back(tmp);
 
9365
      /*
 
9366
        Set fake length to 1 to pass the below conditions.
 
9367
        Real length will be set in mysql_prepare_table()
 
9368
        when we know the character set of the column
 
9369
      */
 
9370
      length= 1;
 
9371
      break;
 
9372
    }
 
9373
  case MYSQL_TYPE_ENUM:
1607
9374
    {
1608
9375
      /* Should be safe. */
1609
9376
      pack_length= get_enum_pack_length(fld_interval_list->elements);
1612
9379
      String *tmp;
1613
9380
      while ((tmp= it++))
1614
9381
        interval_list.push_back(tmp);
1615
 
      length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
 
9382
      length= 1; /* See comment for MYSQL_TYPE_SET above. */
1616
9383
      break;
1617
9384
   }
1618
 
  case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1619
 
    assert(0);
 
9385
  case MYSQL_TYPE_VAR_STRING:
 
9386
    DBUG_ASSERT(0);  /* Impossible. */
 
9387
    break;
 
9388
  case MYSQL_TYPE_BIT:
 
9389
    {
 
9390
      if (!fld_length)
 
9391
        length= 1;
 
9392
      if (length > MAX_BIT_FIELD_LENGTH)
 
9393
      {
 
9394
        my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name,
 
9395
                 MAX_BIT_FIELD_LENGTH);
 
9396
        DBUG_RETURN(TRUE);
 
9397
      }
 
9398
      pack_length= (length + 7) / 8;
 
9399
      break;
 
9400
    }
 
9401
  case MYSQL_TYPE_GEOMETRY:
 
9402
  case MYSQL_TYPE_DECIMAL:
 
9403
    DBUG_ASSERT(0); /* Was obsolete */
1620
9404
  }
1621
9405
  /* Remember the value of length */
1622
9406
  char_length= length;
1623
9407
 
1624
9408
  if (!(flags & BLOB_FLAG) &&
1625
 
      ((length > max_field_charlength &&
1626
 
        fld_type != DRIZZLE_TYPE_ENUM &&
1627
 
        (fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1628
 
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
 
9409
      ((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
 
9410
        fld_type != MYSQL_TYPE_ENUM &&
 
9411
        (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
 
9412
       (!length &&
 
9413
        fld_type != MYSQL_TYPE_STRING &&
 
9414
        fld_type != MYSQL_TYPE_VARCHAR)))
1629
9415
  {
1630
 
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
 
9416
    my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
 
9417
              fld_type == MYSQL_TYPE_VARCHAR ||
 
9418
              fld_type == MYSQL_TYPE_STRING) ?  ER_TOO_BIG_FIELDLENGTH :
 
9419
                                                ER_TOO_BIG_DISPLAYWIDTH,
1631
9420
              MYF(0),
1632
9421
              fld_name, max_field_charlength); /* purecov: inspected */
1633
 
    return(true);
 
9422
    DBUG_RETURN(TRUE);
1634
9423
  }
1635
9424
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
1636
9425
  if ((~allowed_type_modifier) & fld_type_modifier)
1637
9426
  {
1638
9427
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1639
 
    return(true);
 
9428
    DBUG_RETURN(TRUE);
1640
9429
  }
1641
9430
 
1642
 
  return(false); /* success */
 
9431
  DBUG_RETURN(FALSE); /* success */
1643
9432
}
1644
9433
 
1645
9434
 
1646
 
enum_field_types get_blob_type_from_length(uint32_t length __attribute__((unused)))
 
9435
enum_field_types get_blob_type_from_length(ulong length)
1647
9436
{
1648
9437
  enum_field_types type;
1649
 
 
1650
 
  type= DRIZZLE_TYPE_BLOB;
1651
 
 
 
9438
  if (length < 256)
 
9439
    type= MYSQL_TYPE_TINY_BLOB;
 
9440
  else if (length < 65536)
 
9441
    type= MYSQL_TYPE_BLOB;
 
9442
  else if (length < 256L*256L*256L)
 
9443
    type= MYSQL_TYPE_MEDIUM_BLOB;
 
9444
  else
 
9445
    type= MYSQL_TYPE_LONG_BLOB;
1652
9446
  return type;
1653
9447
}
1654
9448
 
1657
9451
  Make a field from the .frm file info
1658
9452
*/
1659
9453
 
1660
 
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
 
9454
uint32 calc_pack_length(enum_field_types type,uint32 length)
1661
9455
{
1662
9456
  switch (type) {
1663
 
  case DRIZZLE_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
1664
 
  case DRIZZLE_TYPE_TINY        : return 1;
1665
 
  case DRIZZLE_TYPE_NEWDATE:
1666
 
  case DRIZZLE_TYPE_TIME:   return 3;
1667
 
  case DRIZZLE_TYPE_TIMESTAMP:
1668
 
  case DRIZZLE_TYPE_LONG        : return 4;
1669
 
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1670
 
  case DRIZZLE_TYPE_DATETIME:
1671
 
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1672
 
  case DRIZZLE_TYPE_NULL        : return 0;
1673
 
  case DRIZZLE_TYPE_BLOB:               return 4+portable_sizeof_char_ptr;
1674
 
  case DRIZZLE_TYPE_ENUM:
1675
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
9457
  case MYSQL_TYPE_VAR_STRING:
 
9458
  case MYSQL_TYPE_STRING:
 
9459
  case MYSQL_TYPE_DECIMAL:     return (length);
 
9460
  case MYSQL_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
 
9461
  case MYSQL_TYPE_YEAR:
 
9462
  case MYSQL_TYPE_TINY  : return 1;
 
9463
  case MYSQL_TYPE_SHORT : return 2;
 
9464
  case MYSQL_TYPE_INT24:
 
9465
  case MYSQL_TYPE_NEWDATE:
 
9466
  case MYSQL_TYPE_TIME:   return 3;
 
9467
  case MYSQL_TYPE_TIMESTAMP:
 
9468
  case MYSQL_TYPE_DATE:
 
9469
  case MYSQL_TYPE_LONG  : return 4;
 
9470
  case MYSQL_TYPE_FLOAT : return sizeof(float);
 
9471
  case MYSQL_TYPE_DOUBLE: return sizeof(double);
 
9472
  case MYSQL_TYPE_DATETIME:
 
9473
  case MYSQL_TYPE_LONGLONG: return 8;   /* Don't crash if no longlong */
 
9474
  case MYSQL_TYPE_NULL  : return 0;
 
9475
  case MYSQL_TYPE_TINY_BLOB:    return 1+portable_sizeof_char_ptr;
 
9476
  case MYSQL_TYPE_BLOB:         return 2+portable_sizeof_char_ptr;
 
9477
  case MYSQL_TYPE_MEDIUM_BLOB:  return 3+portable_sizeof_char_ptr;
 
9478
  case MYSQL_TYPE_LONG_BLOB:    return 4+portable_sizeof_char_ptr;
 
9479
  case MYSQL_TYPE_SET:
 
9480
  case MYSQL_TYPE_ENUM:
 
9481
  case MYSQL_TYPE_NEWDECIMAL:
1676
9482
    abort(); return 0;                          // This shouldn't happen
 
9483
  case MYSQL_TYPE_BIT: return length / 8;
1677
9484
  default:
1678
9485
    return 0;
1679
9486
  }
1680
9487
}
1681
9488
 
1682
9489
 
1683
 
uint32_t pack_length_to_packflag(uint32_t type)
 
9490
uint pack_length_to_packflag(uint type)
1684
9491
{
1685
9492
  switch (type) {
1686
 
    case 1: return f_settype((uint32_t) DRIZZLE_TYPE_TINY);
1687
 
    case 2: assert(1);
1688
 
    case 3: assert(1);
1689
 
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
1690
 
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
 
9493
    case 1: return f_settype((uint) MYSQL_TYPE_TINY);
 
9494
    case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
 
9495
    case 3: return f_settype((uint) MYSQL_TYPE_INT24);
 
9496
    case 4: return f_settype((uint) MYSQL_TYPE_LONG);
 
9497
    case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
1691
9498
  }
1692
9499
  return 0;                                     // This shouldn't happen
1693
9500
}
1694
9501
 
1695
9502
 
1696
 
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
1697
 
                  unsigned char *null_pos, unsigned char null_bit,
1698
 
                  uint32_t pack_flag,
 
9503
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
 
9504
                  uchar *null_pos, uchar null_bit,
 
9505
                  uint pack_flag,
1699
9506
                  enum_field_types field_type,
1700
 
                  const CHARSET_INFO * field_charset,
 
9507
                  CHARSET_INFO *field_charset,
1701
9508
                  Field::utype unireg_check,
1702
9509
                  TYPELIB *interval,
1703
9510
                  const char *field_name)
1704
9511
{
 
9512
  uchar *bit_ptr= NULL;
 
9513
  uchar bit_offset= 0;
 
9514
  if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
 
9515
  {
 
9516
    bit_ptr= null_pos;
 
9517
    bit_offset= null_bit;
 
9518
    if (f_maybe_null(pack_flag))         // if null field
 
9519
    {
 
9520
       bit_ptr+= (null_bit == 7);        // shift bit_ptr and bit_offset
 
9521
       bit_offset= (bit_offset + 1) & 7;
 
9522
    }
 
9523
  }
 
9524
 
1705
9525
  if (!f_maybe_null(pack_flag))
1706
9526
  {
1707
9527
    null_pos=0;
1709
9529
  }
1710
9530
  else
1711
9531
  {
1712
 
    null_bit= ((unsigned char) 1) << null_bit;
 
9532
    null_bit= ((uchar) 1) << null_bit;
1713
9533
  }
1714
9534
 
1715
9535
  switch (field_type) {
1716
 
  case DRIZZLE_TYPE_NEWDATE:
1717
 
  case DRIZZLE_TYPE_TIME:
1718
 
  case DRIZZLE_TYPE_DATETIME:
1719
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
9536
  case MYSQL_TYPE_DATE:
 
9537
  case MYSQL_TYPE_NEWDATE:
 
9538
  case MYSQL_TYPE_TIME:
 
9539
  case MYSQL_TYPE_DATETIME:
 
9540
  case MYSQL_TYPE_TIMESTAMP:
1720
9541
    field_charset= &my_charset_bin;
1721
9542
  default: break;
1722
9543
  }
1725
9546
  {
1726
9547
    if (!f_is_packed(pack_flag))
1727
9548
    {
1728
 
      if (field_type == DRIZZLE_TYPE_VARCHAR)
 
9549
      if (field_type == MYSQL_TYPE_STRING ||
 
9550
          field_type == MYSQL_TYPE_DECIMAL ||   // 3.23 or 4.0 string
 
9551
          field_type == MYSQL_TYPE_VAR_STRING)
 
9552
        return new Field_string(ptr,field_length,null_pos,null_bit,
 
9553
                                unireg_check, field_name,
 
9554
                                field_charset);
 
9555
      if (field_type == MYSQL_TYPE_VARCHAR)
1729
9556
        return new Field_varstring(ptr,field_length,
1730
9557
                                   HA_VARCHAR_PACKLENGTH(field_length),
1731
9558
                                   null_pos,null_bit,
1735
9562
      return 0;                                 // Error
1736
9563
    }
1737
9564
 
1738
 
    uint32_t pack_length=calc_pack_length((enum_field_types)
 
9565
    uint pack_length=calc_pack_length((enum_field_types)
1739
9566
                                      f_packtype(pack_flag),
1740
9567
                                      field_length);
1741
9568
 
1749
9576
        return new Field_enum(ptr,field_length,null_pos,null_bit,
1750
9577
                                  unireg_check, field_name,
1751
9578
                                  pack_length, interval, field_charset);
 
9579
      else
 
9580
        return new Field_set(ptr,field_length,null_pos,null_bit,
 
9581
                             unireg_check, field_name,
 
9582
                             pack_length, interval, field_charset);
1752
9583
    }
1753
9584
  }
1754
9585
 
1755
9586
  switch (field_type) {
1756
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
9587
  case MYSQL_TYPE_DECIMAL:
 
9588
    return new Field_decimal(ptr,field_length,null_pos,null_bit,
 
9589
                             unireg_check, field_name,
 
9590
                             f_decimals(pack_flag),
 
9591
                             f_is_zerofill(pack_flag) != 0,
 
9592
                             f_is_dec(pack_flag) == 0);
 
9593
  case MYSQL_TYPE_NEWDECIMAL:
1757
9594
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
1758
9595
                                 unireg_check, field_name,
1759
9596
                                 f_decimals(pack_flag),
1760
 
                                 f_is_decimal_precision(pack_flag) != 0,
 
9597
                                 f_is_zerofill(pack_flag) != 0,
1761
9598
                                 f_is_dec(pack_flag) == 0);
1762
 
  case DRIZZLE_TYPE_DOUBLE:
 
9599
  case MYSQL_TYPE_FLOAT:
 
9600
    return new Field_float(ptr,field_length,null_pos,null_bit,
 
9601
                           unireg_check, field_name,
 
9602
                           f_decimals(pack_flag),
 
9603
                           f_is_zerofill(pack_flag) != 0,
 
9604
                           f_is_dec(pack_flag)== 0);
 
9605
  case MYSQL_TYPE_DOUBLE:
1763
9606
    return new Field_double(ptr,field_length,null_pos,null_bit,
1764
9607
                            unireg_check, field_name,
1765
9608
                            f_decimals(pack_flag),
1766
 
                            false,
 
9609
                            f_is_zerofill(pack_flag) != 0,
1767
9610
                            f_is_dec(pack_flag)== 0);
1768
 
  case DRIZZLE_TYPE_TINY:
 
9611
  case MYSQL_TYPE_TINY:
1769
9612
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
1770
9613
                          unireg_check, field_name,
1771
 
                          false,
 
9614
                          f_is_zerofill(pack_flag) != 0,
1772
9615
                          f_is_dec(pack_flag) == 0);
1773
 
  case DRIZZLE_TYPE_LONG:
 
9616
  case MYSQL_TYPE_SHORT:
 
9617
    return new Field_short(ptr,field_length,null_pos,null_bit,
 
9618
                           unireg_check, field_name,
 
9619
                           f_is_zerofill(pack_flag) != 0,
 
9620
                           f_is_dec(pack_flag) == 0);
 
9621
  case MYSQL_TYPE_INT24:
 
9622
    return new Field_medium(ptr,field_length,null_pos,null_bit,
 
9623
                            unireg_check, field_name,
 
9624
                            f_is_zerofill(pack_flag) != 0,
 
9625
                            f_is_dec(pack_flag) == 0);
 
9626
  case MYSQL_TYPE_LONG:
1774
9627
    return new Field_long(ptr,field_length,null_pos,null_bit,
1775
9628
                           unireg_check, field_name,
1776
 
                           false,
 
9629
                           f_is_zerofill(pack_flag) != 0,
1777
9630
                           f_is_dec(pack_flag) == 0);
1778
 
  case DRIZZLE_TYPE_LONGLONG:
1779
 
    return new Field_int64_t(ptr,field_length,null_pos,null_bit,
 
9631
  case MYSQL_TYPE_LONGLONG:
 
9632
    return new Field_longlong(ptr,field_length,null_pos,null_bit,
1780
9633
                              unireg_check, field_name,
1781
 
                              false,
 
9634
                              f_is_zerofill(pack_flag) != 0,
1782
9635
                              f_is_dec(pack_flag) == 0);
1783
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
9636
  case MYSQL_TYPE_TIMESTAMP:
1784
9637
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
1785
9638
                               unireg_check, field_name, share,
1786
9639
                               field_charset);
1787
 
  case DRIZZLE_TYPE_NEWDATE:
 
9640
  case MYSQL_TYPE_YEAR:
 
9641
    return new Field_year(ptr,field_length,null_pos,null_bit,
 
9642
                          unireg_check, field_name);
 
9643
  case MYSQL_TYPE_DATE:
 
9644
    return new Field_date(ptr,null_pos,null_bit,
 
9645
                          unireg_check, field_name, field_charset);
 
9646
  case MYSQL_TYPE_NEWDATE:
1788
9647
    return new Field_newdate(ptr,null_pos,null_bit,
1789
9648
                             unireg_check, field_name, field_charset);
1790
 
  case DRIZZLE_TYPE_TIME:
 
9649
  case MYSQL_TYPE_TIME:
1791
9650
    return new Field_time(ptr,null_pos,null_bit,
1792
9651
                          unireg_check, field_name, field_charset);
1793
 
  case DRIZZLE_TYPE_DATETIME:
 
9652
  case MYSQL_TYPE_DATETIME:
1794
9653
    return new Field_datetime(ptr,null_pos,null_bit,
1795
9654
                              unireg_check, field_name, field_charset);
1796
 
  case DRIZZLE_TYPE_NULL:
 
9655
  case MYSQL_TYPE_NULL:
1797
9656
    return new Field_null(ptr, field_length, unireg_check, field_name,
1798
9657
                          field_charset);
1799
 
  case DRIZZLE_TYPE_VIRTUAL:                    // Must not happen
1800
 
    assert(0);
 
9658
  case MYSQL_TYPE_BIT:
 
9659
    return f_bit_as_char(pack_flag) ?
 
9660
           new Field_bit_as_char(ptr, field_length, null_pos, null_bit,
 
9661
                                 unireg_check, field_name) :
 
9662
           new Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
 
9663
                         bit_offset, unireg_check, field_name);
 
9664
 
1801
9665
  default:                                      // Impossible (Wrong version)
1802
9666
    break;
1803
9667
  }
1820
9684
  charset=    old_field->charset();             // May be NULL ptr
1821
9685
  comment=    old_field->comment;
1822
9686
  decimals=   old_field->decimals();
1823
 
  vcol_info=  old_field->vcol_info;
1824
 
  is_stored= old_field->is_stored;
1825
9687
 
1826
9688
  /* Fix if the original table had 4 byte pointer blobs */
1827
9689
  if (flags & BLOB_FLAG)
1829
9691
                  portable_sizeof_char_ptr);
1830
9692
 
1831
9693
  switch (sql_type) {
1832
 
  case DRIZZLE_TYPE_BLOB:
1833
 
    sql_type= DRIZZLE_TYPE_BLOB;
 
9694
  case MYSQL_TYPE_BLOB:
 
9695
    switch (pack_length - portable_sizeof_char_ptr) {
 
9696
    case  1: sql_type= MYSQL_TYPE_TINY_BLOB; break;
 
9697
    case  2: sql_type= MYSQL_TYPE_BLOB; break;
 
9698
    case  3: sql_type= MYSQL_TYPE_MEDIUM_BLOB; break;
 
9699
    default: sql_type= MYSQL_TYPE_LONG_BLOB; break;
 
9700
    }
1834
9701
    length/= charset->mbmaxlen;
1835
9702
    key_length/= charset->mbmaxlen;
1836
9703
    break;
 
9704
  case MYSQL_TYPE_STRING:
1837
9705
    /* Change CHAR -> VARCHAR if dynamic record length */
1838
 
  case DRIZZLE_TYPE_ENUM:
1839
 
  case DRIZZLE_TYPE_VARCHAR:
 
9706
    if (old_field->type() == MYSQL_TYPE_VAR_STRING)
 
9707
      sql_type= MYSQL_TYPE_VARCHAR;
 
9708
    /* fall through */
 
9709
 
 
9710
  case MYSQL_TYPE_ENUM:
 
9711
  case MYSQL_TYPE_SET:
 
9712
  case MYSQL_TYPE_VARCHAR:
 
9713
  case MYSQL_TYPE_VAR_STRING:
1840
9714
    /* This is corrected in create_length_to_internal_length */
1841
9715
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1842
9716
    break;
1853
9727
 
1854
9728
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
1855
9729
      old_field->ptr && orig_field &&
1856
 
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
 
9730
      (sql_type != MYSQL_TYPE_TIMESTAMP ||                /* set def only if */
1857
9731
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
1858
9732
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
1859
9733
  {
1878
9752
}
1879
9753
 
1880
9754
 
 
9755
/**
 
9756
  maximum possible display length for blob.
 
9757
 
 
9758
  @return
 
9759
    length
 
9760
*/
 
9761
 
 
9762
uint32 Field_blob::max_display_length()
 
9763
{
 
9764
  switch (packlength)
 
9765
  {
 
9766
  case 1:
 
9767
    return 255 * field_charset->mbmaxlen;
 
9768
  case 2:
 
9769
    return 65535 * field_charset->mbmaxlen;
 
9770
  case 3:
 
9771
    return 16777215 * field_charset->mbmaxlen;
 
9772
  case 4:
 
9773
    return (uint32) 4294967295U;
 
9774
  default:
 
9775
    DBUG_ASSERT(0); // we should never go here
 
9776
    return 0;
 
9777
  }
 
9778
}
 
9779
 
 
9780
 
1881
9781
/*****************************************************************************
1882
9782
 Warning handling
1883
9783
*****************************************************************************/
1903
9803
*/
1904
9804
 
1905
9805
bool 
1906
 
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
 
9806
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
1907
9807
                   int cuted_increment)
1908
9808
{
1909
9809
  /*
1918
9818
                        thd->row_count);
1919
9819
    return 0;
1920
9820
  }
1921
 
  return level >= DRIZZLE_ERROR::WARN_LEVEL_WARN;
 
9821
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
1922
9822
}
1923
9823
 
1924
9824
 
1939
9839
*/
1940
9840
 
1941
9841
void 
1942
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
1943
 
                            unsigned int code, 
1944
 
                            const char *str, uint32_t str_length, 
1945
 
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
 
9842
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
9843
                            const char *str, uint str_length, 
 
9844
                            timestamp_type ts_type, int cuted_increment)
1946
9845
{
1947
9846
  THD *thd= table ? table->in_use : current_thd;
1948
9847
  if ((thd->really_abort_on_warning() &&
1949
 
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
 
9848
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
1950
9849
      set_warning(level, code, cuted_increment))
1951
9850
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
1952
9851
                                 field_name);
1969
9868
*/
1970
9869
 
1971
9870
void 
1972
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code, 
1973
 
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
 
9871
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
9872
                            longlong nr, timestamp_type ts_type,
1974
9873
                            int cuted_increment)
1975
9874
{
1976
9875
  THD *thd= table ? table->in_use : current_thd;
1978
9877
      set_warning(level, code, cuted_increment))
1979
9878
  {
1980
9879
    char str_nr[22];
1981
 
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
1982
 
    make_truncated_value_warning(thd, level, str_nr, (uint32_t) (str_end - str_nr), 
 
9880
    char *str_end= longlong10_to_str(nr, str_nr, -10);
 
9881
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
1983
9882
                                 ts_type, field_name);
1984
9883
  }
1985
9884
}
2000
9899
*/
2001
9900
 
2002
9901
void 
2003
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code, 
2004
 
                            double nr, enum enum_drizzle_timestamp_type ts_type)
 
9902
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
9903
                            double nr, timestamp_type ts_type)
2005
9904
{
2006
9905
  THD *thd= table ? table->in_use : current_thd;
2007
9906
  if (thd->really_abort_on_warning() ||
2009
9908
  {
2010
9909
    /* DBL_DIG is enough to print '-[digits].E+###' */
2011
9910
    char str_nr[DBL_DIG + 8];
2012
 
    uint32_t str_len= sprintf(str_nr, "%g", nr);
 
9911
    uint str_len= my_sprintf(str_nr, (str_nr, "%g", nr));
2013
9912
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
2014
9913
                                 field_name);
2015
9914
  }