~drizzle-trunk/drizzle/development

520.1.25 by Monty Taylor
Removed the split.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 MySQL
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
1 by brian
clean slate
20
21
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
22
  @file
1 by brian
clean slate
23
520.1.28 by Monty Taylor
Reverted too-big formatting change.
24
  @brief
25
  This file implements classes defined in field.h
1 by brian
clean slate
26
*/
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
27
#include <drizzled/server_includes.h>
1 by brian
clean slate
28
#include "sql_select.h"
29
#include <errno.h>
549 by Monty Taylor
Took gettext.h out of header files.
30
#include <drizzled/error.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
31
#include <drizzled/virtual_column_info.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
32
#include <drizzled/field/str.h>
33
#include <drizzled/field/longstr.h>
34
#include <drizzled/field/num.h>
35
#include <drizzled/field/blob.h>
36
#include <drizzled/field/enum.h>
37
#include <drizzled/field/null.h>
38
#include <drizzled/field/date.h>
670.1.1 by Monty Taylor
Renamed fdecimal.* to decimal.*. Let's see how many things we can break!
39
#include <drizzled/field/decimal.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
40
#include <drizzled/field/real.h>
41
#include <drizzled/field/double.h>
42
#include <drizzled/field/long.h>
43
#include <drizzled/field/int64_t.h>
44
#include <drizzled/field/num.h>
45
#include <drizzled/field/timetype.h>
46
#include <drizzled/field/timestamp.h>
47
#include <drizzled/field/datetime.h>
48
#include <drizzled/field/varstring.h>
1 by brian
clean slate
49
50
51
/*****************************************************************************
52
  Instansiate templates and static variables
53
*****************************************************************************/
54
55
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
56
template class List<Create_field>;
57
template class List_iterator<Create_field>;
58
#endif
59
60
520.1.25 by Monty Taylor
Removed the split.
61
static enum_field_types
62
field_types_merge_rules [DRIZZLE_TYPE_MAX+1][DRIZZLE_TYPE_MAX+1]=
1 by brian
clean slate
63
{
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
64
  /* DRIZZLE_TYPE_TINY -> */
65
  {
520.1.25 by Monty Taylor
Removed the split.
66
    //DRIZZLE_TYPE_TINY
520.1.24 by Monty Taylor
Fixed the TINY=1 bit.
67
    DRIZZLE_TYPE_TINY,
520.1.25 by Monty Taylor
Removed the split.
68
    //DRIZZLE_TYPE_LONG
398 by Brian Aker
Remove short.
69
    DRIZZLE_TYPE_LONG,
520.1.25 by Monty Taylor
Removed the split.
70
    //DRIZZLE_TYPE_DOUBLE
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
71
    DRIZZLE_TYPE_DOUBLE,
520.1.25 by Monty Taylor
Removed the split.
72
    //DRIZZLE_TYPE_NULL
73
    DRIZZLE_TYPE_TINY,
74
    //DRIZZLE_TYPE_TIMESTAMP
75
    DRIZZLE_TYPE_VARCHAR,
76
    //DRIZZLE_TYPE_LONGLONG
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
77
    DRIZZLE_TYPE_LONGLONG,
520.1.25 by Monty Taylor
Removed the split.
78
    //DRIZZLE_TYPE_TIME
79
    DRIZZLE_TYPE_VARCHAR,
80
    //DRIZZLE_TYPE_DATETIME
81
    DRIZZLE_TYPE_VARCHAR,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
82
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
83
    DRIZZLE_TYPE_VARCHAR,
84
    //DRIZZLE_TYPE_VARCHAR
85
    DRIZZLE_TYPE_VARCHAR,
86
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
87
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
88
    //DRIZZLE_TYPE_NEWDECIMAL
89
    DRIZZLE_TYPE_NEWDECIMAL,
90
    //DRIZZLE_TYPE_ENUM
91
    DRIZZLE_TYPE_VARCHAR,
92
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
93
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
94
  },
95
  /* DRIZZLE_TYPE_LONG -> */
96
  {
520.1.25 by Monty Taylor
Removed the split.
97
    //DRIZZLE_TYPE_TINY
98
    DRIZZLE_TYPE_LONG,
99
    //DRIZZLE_TYPE_LONG
100
    DRIZZLE_TYPE_LONG,
101
    //DRIZZLE_TYPE_DOUBLE
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
102
    DRIZZLE_TYPE_DOUBLE,
520.1.25 by Monty Taylor
Removed the split.
103
    //DRIZZLE_TYPE_NULL
104
    DRIZZLE_TYPE_LONG,
105
    //DRIZZLE_TYPE_TIMESTAMP
106
    DRIZZLE_TYPE_VARCHAR,
107
    //DRIZZLE_TYPE_LONGLONG
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
108
    DRIZZLE_TYPE_LONGLONG,
520.1.25 by Monty Taylor
Removed the split.
109
    //DRIZZLE_TYPE_TIME
110
    DRIZZLE_TYPE_VARCHAR,
111
    //DRIZZLE_TYPE_DATETIME
112
    DRIZZLE_TYPE_VARCHAR,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
113
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
114
    DRIZZLE_TYPE_VARCHAR,
115
    //DRIZZLE_TYPE_VARCHAR
116
    DRIZZLE_TYPE_VARCHAR,
117
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
118
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
119
    //DRIZZLE_TYPE_NEWDECIMAL
120
    DRIZZLE_TYPE_NEWDECIMAL,
121
    //DRIZZLE_TYPE_ENUM
122
    DRIZZLE_TYPE_VARCHAR,
123
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
124
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
125
  },
126
  /* DRIZZLE_TYPE_DOUBLE -> */
127
  {
520.1.25 by Monty Taylor
Removed the split.
128
    //DRIZZLE_TYPE_TINY
129
    DRIZZLE_TYPE_DOUBLE,
130
    //DRIZZLE_TYPE_LONG
131
    DRIZZLE_TYPE_DOUBLE,
132
    //DRIZZLE_TYPE_DOUBLE
133
    DRIZZLE_TYPE_DOUBLE,
134
    //DRIZZLE_TYPE_NULL
135
    DRIZZLE_TYPE_DOUBLE,
136
    //DRIZZLE_TYPE_TIMESTAMP
137
    DRIZZLE_TYPE_VARCHAR,
138
    //DRIZZLE_TYPE_LONGLONG
139
    DRIZZLE_TYPE_DOUBLE,
140
    //DRIZZLE_TYPE_TIME
141
    DRIZZLE_TYPE_VARCHAR,
142
    //DRIZZLE_TYPE_DATETIME
143
    DRIZZLE_TYPE_VARCHAR,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
144
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
145
    DRIZZLE_TYPE_VARCHAR,
146
    //DRIZZLE_TYPE_VARCHAR
147
    DRIZZLE_TYPE_VARCHAR,
148
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
149
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
150
    //DRIZZLE_TYPE_NEWDECIMAL
151
    DRIZZLE_TYPE_DOUBLE,
152
    //DRIZZLE_TYPE_ENUM
153
    DRIZZLE_TYPE_VARCHAR,
154
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
155
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
156
  },
157
  /* DRIZZLE_TYPE_NULL -> */
158
  {
520.1.25 by Monty Taylor
Removed the split.
159
    //DRIZZLE_TYPE_TINY
520.1.24 by Monty Taylor
Fixed the TINY=1 bit.
160
    DRIZZLE_TYPE_TINY,
520.1.25 by Monty Taylor
Removed the split.
161
    //DRIZZLE_TYPE_LONG
398 by Brian Aker
Remove short.
162
    DRIZZLE_TYPE_LONG,
520.1.25 by Monty Taylor
Removed the split.
163
    //DRIZZLE_TYPE_DOUBLE
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
164
    DRIZZLE_TYPE_DOUBLE,
520.1.25 by Monty Taylor
Removed the split.
165
    //DRIZZLE_TYPE_NULL
166
    DRIZZLE_TYPE_NULL,
167
    //DRIZZLE_TYPE_TIMESTAMP
168
    DRIZZLE_TYPE_TIMESTAMP,
169
    //DRIZZLE_TYPE_LONGLONG
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
170
    DRIZZLE_TYPE_LONGLONG,
520.1.25 by Monty Taylor
Removed the split.
171
    //DRIZZLE_TYPE_TIME
399 by Brian Aker
Removed dead datatype (even dead in original server...). Mindless 6:30AM
172
    DRIZZLE_TYPE_TIME,
520.1.25 by Monty Taylor
Removed the split.
173
    //DRIZZLE_TYPE_DATETIME
222 by Brian Aker
Remove YEAR field type
174
    DRIZZLE_TYPE_DATETIME,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
175
    //DRIZZLE_TYPE_DATE
176
    DRIZZLE_TYPE_DATE,
520.1.25 by Monty Taylor
Removed the split.
177
    //DRIZZLE_TYPE_VARCHAR
178
    DRIZZLE_TYPE_VARCHAR,
179
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
180
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
181
    //DRIZZLE_TYPE_NEWDECIMAL
182
    DRIZZLE_TYPE_NEWDECIMAL,
183
    //DRIZZLE_TYPE_ENUM
184
    DRIZZLE_TYPE_ENUM,
185
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
186
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
187
  },
188
  /* DRIZZLE_TYPE_TIMESTAMP -> */
189
  {
520.1.25 by Monty Taylor
Removed the split.
190
    //DRIZZLE_TYPE_TINY
191
    DRIZZLE_TYPE_VARCHAR,
192
    //DRIZZLE_TYPE_LONG
193
    DRIZZLE_TYPE_VARCHAR,
194
    //DRIZZLE_TYPE_DOUBLE
195
    DRIZZLE_TYPE_VARCHAR,
196
    //DRIZZLE_TYPE_NULL
197
    DRIZZLE_TYPE_TIMESTAMP,
198
    //DRIZZLE_TYPE_TIMESTAMP
199
    DRIZZLE_TYPE_TIMESTAMP,
200
    //DRIZZLE_TYPE_LONGLONG
201
    DRIZZLE_TYPE_VARCHAR,
202
    //DRIZZLE_TYPE_TIME
203
    DRIZZLE_TYPE_DATETIME,
204
    //DRIZZLE_TYPE_DATETIME
205
    DRIZZLE_TYPE_DATETIME,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
206
    //DRIZZLE_TYPE_DATE
207
    DRIZZLE_TYPE_DATE,
520.1.25 by Monty Taylor
Removed the split.
208
    //DRIZZLE_TYPE_VARCHAR
209
    DRIZZLE_TYPE_VARCHAR,
210
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
211
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
212
    //DRIZZLE_TYPE_NEWDECIMAL
213
    DRIZZLE_TYPE_VARCHAR,
214
    //DRIZZLE_TYPE_ENUM
215
    DRIZZLE_TYPE_VARCHAR,
216
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
217
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
218
  },
219
  /* DRIZZLE_TYPE_LONGLONG -> */
220
  {
520.1.25 by Monty Taylor
Removed the split.
221
    //DRIZZLE_TYPE_TINY
222
    DRIZZLE_TYPE_LONGLONG,
223
    //DRIZZLE_TYPE_LONG
224
    DRIZZLE_TYPE_LONGLONG,
225
    //DRIZZLE_TYPE_DOUBLE
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
226
    DRIZZLE_TYPE_DOUBLE,
520.1.25 by Monty Taylor
Removed the split.
227
    //DRIZZLE_TYPE_NULL
228
    DRIZZLE_TYPE_LONGLONG,
229
    //DRIZZLE_TYPE_TIMESTAMP
230
    DRIZZLE_TYPE_VARCHAR,
231
    //DRIZZLE_TYPE_LONGLONG
232
    DRIZZLE_TYPE_LONGLONG,
233
    //DRIZZLE_TYPE_TIME
234
    DRIZZLE_TYPE_VARCHAR,
235
    //DRIZZLE_TYPE_DATETIME
236
    DRIZZLE_TYPE_VARCHAR,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
237
    //DRIZZLE_TYPE_DATE
238
    DRIZZLE_TYPE_DATE,
520.1.25 by Monty Taylor
Removed the split.
239
    //DRIZZLE_TYPE_VARCHAR
240
    DRIZZLE_TYPE_VARCHAR,
241
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
242
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
243
    //DRIZZLE_TYPE_NEWDECIMAL
244
    //DRIZZLE_TYPE_ENUM
245
    DRIZZLE_TYPE_NEWDECIMAL,
246
    DRIZZLE_TYPE_VARCHAR,
247
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
248
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
249
  },
250
  /* DRIZZLE_TYPE_TIME -> */
251
  {
520.1.25 by Monty Taylor
Removed the split.
252
    //DRIZZLE_TYPE_TINY
253
    DRIZZLE_TYPE_VARCHAR,
254
    //DRIZZLE_TYPE_LONG
255
    DRIZZLE_TYPE_VARCHAR,
256
    //DRIZZLE_TYPE_DOUBLE
257
    DRIZZLE_TYPE_VARCHAR,
258
    //DRIZZLE_TYPE_NULL
259
    DRIZZLE_TYPE_TIME,
260
    //DRIZZLE_TYPE_TIMESTAMP
261
    DRIZZLE_TYPE_DATETIME,
262
    //DRIZZLE_TYPE_LONGLONG
263
    DRIZZLE_TYPE_VARCHAR,
264
    //DRIZZLE_TYPE_TIME
265
    DRIZZLE_TYPE_TIME,
266
    //DRIZZLE_TYPE_DATETIME
267
    DRIZZLE_TYPE_DATETIME,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
268
    //DRIZZLE_TYPE_DATE
269
    DRIZZLE_TYPE_DATE,
520.1.25 by Monty Taylor
Removed the split.
270
    //DRIZZLE_TYPE_VARCHAR
271
    DRIZZLE_TYPE_VARCHAR,
272
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
273
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
274
    //DRIZZLE_TYPE_NEWDECIMAL
275
    DRIZZLE_TYPE_VARCHAR,
276
    //DRIZZLE_TYPE_ENUM
277
    DRIZZLE_TYPE_VARCHAR,
278
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
279
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
280
  },
281
  /* DRIZZLE_TYPE_DATETIME -> */
282
  {
520.1.25 by Monty Taylor
Removed the split.
283
    //DRIZZLE_TYPE_TINY
284
    DRIZZLE_TYPE_VARCHAR,
285
    //DRIZZLE_TYPE_LONG
286
    DRIZZLE_TYPE_VARCHAR,
287
    //DRIZZLE_TYPE_DOUBLE
288
    DRIZZLE_TYPE_VARCHAR,
289
    //DRIZZLE_TYPE_NULL
290
    DRIZZLE_TYPE_DATETIME,
291
    //DRIZZLE_TYPE_TIMESTAMP
292
    DRIZZLE_TYPE_DATETIME,
293
    //DRIZZLE_TYPE_LONGLONG
294
    DRIZZLE_TYPE_VARCHAR,
295
    //DRIZZLE_TYPE_TIME
296
    DRIZZLE_TYPE_DATETIME,
297
    //DRIZZLE_TYPE_DATETIME
298
    DRIZZLE_TYPE_DATETIME,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
299
    //DRIZZLE_TYPE_DATE
300
    DRIZZLE_TYPE_DATE,
520.1.25 by Monty Taylor
Removed the split.
301
    //DRIZZLE_TYPE_VARCHAR
302
    DRIZZLE_TYPE_VARCHAR,
303
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
304
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
305
    //DRIZZLE_TYPE_NEWDECIMAL
306
    DRIZZLE_TYPE_VARCHAR,
307
    //DRIZZLE_TYPE_ENUM
308
    DRIZZLE_TYPE_VARCHAR,
309
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
310
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
311
  },
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
312
  /* DRIZZLE_TYPE_DATE -> */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
313
  {
520.1.25 by Monty Taylor
Removed the split.
314
    //DRIZZLE_TYPE_TINY
315
    DRIZZLE_TYPE_VARCHAR,
316
    //DRIZZLE_TYPE_LONG
317
    DRIZZLE_TYPE_VARCHAR,
318
    //DRIZZLE_TYPE_DOUBLE
319
    DRIZZLE_TYPE_VARCHAR,
320
    //DRIZZLE_TYPE_NULL
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
321
    DRIZZLE_TYPE_DATE,
520.1.25 by Monty Taylor
Removed the split.
322
    //DRIZZLE_TYPE_TIMESTAMP
323
    DRIZZLE_TYPE_DATETIME,
324
    //DRIZZLE_TYPE_LONGLONG
325
    DRIZZLE_TYPE_VARCHAR,
326
    //DRIZZLE_TYPE_TIME
327
    DRIZZLE_TYPE_DATETIME,
328
    //DRIZZLE_TYPE_DATETIME
329
    DRIZZLE_TYPE_DATETIME,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
330
    //DRIZZLE_TYPE_DATE
331
    DRIZZLE_TYPE_DATE,
520.1.25 by Monty Taylor
Removed the split.
332
    //DRIZZLE_TYPE_VARCHAR
333
    DRIZZLE_TYPE_VARCHAR,
334
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
335
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
336
    //DRIZZLE_TYPE_NEWDECIMAL
337
    DRIZZLE_TYPE_VARCHAR,
338
    //DRIZZLE_TYPE_ENUM
339
    DRIZZLE_TYPE_VARCHAR,
340
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
341
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
342
  },
343
  /* DRIZZLE_TYPE_VARCHAR -> */
344
  {
520.1.25 by Monty Taylor
Removed the split.
345
    //DRIZZLE_TYPE_TINY
346
    DRIZZLE_TYPE_VARCHAR,
347
    //DRIZZLE_TYPE_LONG
348
    DRIZZLE_TYPE_VARCHAR,
349
    //DRIZZLE_TYPE_DOUBLE
350
    DRIZZLE_TYPE_VARCHAR,
351
    //DRIZZLE_TYPE_NULL
352
    DRIZZLE_TYPE_VARCHAR,
353
    //DRIZZLE_TYPE_TIMESTAMP
354
    DRIZZLE_TYPE_VARCHAR,
355
    //DRIZZLE_TYPE_LONGLONG
356
    DRIZZLE_TYPE_VARCHAR,
357
    //DRIZZLE_TYPE_TIME
358
    DRIZZLE_TYPE_VARCHAR,
359
    //DRIZZLE_TYPE_DATETIME
360
    DRIZZLE_TYPE_VARCHAR,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
361
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
362
    DRIZZLE_TYPE_VARCHAR,
363
    //DRIZZLE_TYPE_VARCHAR
364
    DRIZZLE_TYPE_VARCHAR,
365
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
366
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
367
    //DRIZZLE_TYPE_NEWDECIMAL
368
    DRIZZLE_TYPE_VARCHAR,
369
    //DRIZZLE_TYPE_ENUM
370
    DRIZZLE_TYPE_VARCHAR,
371
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
372
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
373
  },
383.7.1 by Andrey Zhakov
Initial submit of code and tests
374
  /* DRIZZLE_TYPE_VIRTUAL -> */
375
  {
520.1.25 by Monty Taylor
Removed the split.
376
    //DRIZZLE_TYPE_TINY
377
    DRIZZLE_TYPE_VIRTUAL,
378
    //DRIZZLE_TYPE_LONG
379
    DRIZZLE_TYPE_VIRTUAL,
380
    //DRIZZLE_TYPE_DOUBLE
381
    DRIZZLE_TYPE_VIRTUAL,
382
    //DRIZZLE_TYPE_NULL
383
    DRIZZLE_TYPE_VIRTUAL,
384
    //DRIZZLE_TYPE_TIMESTAMP
385
    DRIZZLE_TYPE_VIRTUAL,
386
    //DRIZZLE_TYPE_LONGLONG
387
    DRIZZLE_TYPE_VIRTUAL,
388
    //DRIZZLE_TYPE_TIME
389
    DRIZZLE_TYPE_VIRTUAL,
390
    //DRIZZLE_TYPE_DATETIME
391
    DRIZZLE_TYPE_VIRTUAL,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
392
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
393
    DRIZZLE_TYPE_VIRTUAL,
394
    //DRIZZLE_TYPE_VARCHAR
395
    DRIZZLE_TYPE_VIRTUAL,
396
    //DRIZZLE_TYPE_VIRTUAL
397
    DRIZZLE_TYPE_VIRTUAL,
398
    //DRIZZLE_TYPE_NEWDECIMAL
399
    DRIZZLE_TYPE_VIRTUAL,
400
    //DRIZZLE_TYPE_ENUM
401
    DRIZZLE_TYPE_VIRTUAL,
402
    //DRIZZLE_TYPE_BLOB
383.7.1 by Andrey Zhakov
Initial submit of code and tests
403
    DRIZZLE_TYPE_VIRTUAL,
404
  },
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
405
  /* DRIZZLE_TYPE_NEWDECIMAL -> */
406
  {
520.1.25 by Monty Taylor
Removed the split.
407
    //DRIZZLE_TYPE_TINY
408
    DRIZZLE_TYPE_NEWDECIMAL,
409
    //DRIZZLE_TYPE_LONG
410
    DRIZZLE_TYPE_NEWDECIMAL,
411
    //DRIZZLE_TYPE_DOUBLE
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
412
    DRIZZLE_TYPE_DOUBLE,
520.1.25 by Monty Taylor
Removed the split.
413
    //DRIZZLE_TYPE_NULL
414
    DRIZZLE_TYPE_NEWDECIMAL,
415
    //DRIZZLE_TYPE_TIMESTAMP
416
    DRIZZLE_TYPE_VARCHAR,
417
    //DRIZZLE_TYPE_LONGLONG
418
    DRIZZLE_TYPE_NEWDECIMAL,
419
    //DRIZZLE_TYPE_TIME
420
    DRIZZLE_TYPE_VARCHAR,
421
    //DRIZZLE_TYPE_DATETIME
422
    DRIZZLE_TYPE_VARCHAR,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
423
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
424
    DRIZZLE_TYPE_VARCHAR,
425
    //DRIZZLE_TYPE_VARCHAR
426
    DRIZZLE_TYPE_VARCHAR,
427
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
428
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
429
    //DRIZZLE_TYPE_NEWDECIMAL
430
    DRIZZLE_TYPE_NEWDECIMAL,
431
    //DRIZZLE_TYPE_ENUM
432
    DRIZZLE_TYPE_VARCHAR,
433
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
434
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
435
  },
436
  /* DRIZZLE_TYPE_ENUM -> */
437
  {
520.1.25 by Monty Taylor
Removed the split.
438
    //DRIZZLE_TYPE_TINY
439
    DRIZZLE_TYPE_VARCHAR,
440
    //DRIZZLE_TYPE_LONG
441
    DRIZZLE_TYPE_VARCHAR,
442
    //DRIZZLE_TYPE_DOUBLE
443
    DRIZZLE_TYPE_VARCHAR,
444
    //DRIZZLE_TYPE_NULL
445
    DRIZZLE_TYPE_ENUM,
446
    //DRIZZLE_TYPE_TIMESTAMP
447
    DRIZZLE_TYPE_VARCHAR,
448
    //DRIZZLE_TYPE_LONGLONG
449
    DRIZZLE_TYPE_VARCHAR,
450
    //DRIZZLE_TYPE_TIME
451
    DRIZZLE_TYPE_VARCHAR,
452
    //DRIZZLE_TYPE_DATETIME
453
    DRIZZLE_TYPE_VARCHAR,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
454
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
455
    DRIZZLE_TYPE_VARCHAR,
456
    //DRIZZLE_TYPE_VARCHAR
457
    DRIZZLE_TYPE_VARCHAR,
458
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
459
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
460
    //DRIZZLE_TYPE_NEWDECIMAL
461
    DRIZZLE_TYPE_VARCHAR,
462
    //DRIZZLE_TYPE_ENUM
463
    DRIZZLE_TYPE_VARCHAR,
464
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
465
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
466
  },
467
  /* DRIZZLE_TYPE_BLOB -> */
468
  {
520.1.25 by Monty Taylor
Removed the split.
469
    //DRIZZLE_TYPE_TINY
470
    DRIZZLE_TYPE_BLOB,
471
    //DRIZZLE_TYPE_LONG
472
    DRIZZLE_TYPE_BLOB,
473
    //DRIZZLE_TYPE_DOUBLE
474
    DRIZZLE_TYPE_BLOB,
475
    //DRIZZLE_TYPE_NULL
476
    DRIZZLE_TYPE_BLOB,
477
    //DRIZZLE_TYPE_TIMESTAMP
478
    DRIZZLE_TYPE_BLOB,
479
    //DRIZZLE_TYPE_LONGLONG
480
    DRIZZLE_TYPE_BLOB,
481
    //DRIZZLE_TYPE_TIME
482
    DRIZZLE_TYPE_BLOB,
483
    //DRIZZLE_TYPE_DATETIME
484
    DRIZZLE_TYPE_BLOB,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
485
    //DRIZZLE_TYPE_DATE
520.1.25 by Monty Taylor
Removed the split.
486
    DRIZZLE_TYPE_BLOB,
487
    //DRIZZLE_TYPE_VARCHAR
488
    DRIZZLE_TYPE_BLOB,
489
    //DRIZZLE_TYPE_VIRTUAL
383.7.1 by Andrey Zhakov
Initial submit of code and tests
490
    DRIZZLE_TYPE_VIRTUAL,
520.1.25 by Monty Taylor
Removed the split.
491
    //DRIZZLE_TYPE_NEWDECIMAL
492
    DRIZZLE_TYPE_BLOB,
493
    //DRIZZLE_TYPE_ENUM
494
    DRIZZLE_TYPE_BLOB,
495
    //DRIZZLE_TYPE_BLOB
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
496
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
497
  },
1 by brian
clean slate
498
};
499
500
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
501
  Return type of which can carry value of both given types in UNION result.
502
503
  @param a  type for merging
504
  @param b  type for merging
505
506
  @return
507
    type of field
1 by brian
clean slate
508
*/
509
510
enum_field_types Field::field_type_merge(enum_field_types a,
511
                                         enum_field_types b)
512
{
520.1.25 by Monty Taylor
Removed the split.
513
  assert(a <= DRIZZLE_TYPE_MAX);
514
  assert(b <= DRIZZLE_TYPE_MAX);
515
  return field_types_merge_rules[a][b];
1 by brian
clean slate
516
}
517
518
520.1.25 by Monty Taylor
Removed the split.
519
static Item_result field_types_result_type [DRIZZLE_TYPE_MAX+1]=
1 by brian
clean slate
520
{
520.1.24 by Monty Taylor
Fixed the TINY=1 bit.
521
  //DRIZZLE_TYPE_TINY
522
  INT_RESULT,
398 by Brian Aker
Remove short.
523
  //DRIZZLE_TYPE_LONG
524
  INT_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
525
  //DRIZZLE_TYPE_DOUBLE
166 by Brian Aker
Removal of FLOAT type
526
  REAL_RESULT,
520.1.26 by Monty Taylor
Fixed some formatting.
527
  //DRIZZLE_TYPE_NULL
528
  STRING_RESULT,
529
  //DRIZZLE_TYPE_TIMESTAMP
530
  STRING_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
531
  //DRIZZLE_TYPE_LONGLONG
167 by Brian Aker
Remove old 3byte int
532
  INT_RESULT,
399 by Brian Aker
Removed dead datatype (even dead in original server...). Mindless 6:30AM
533
  //DRIZZLE_TYPE_TIME
534
  STRING_RESULT,
222 by Brian Aker
Remove YEAR field type
535
  //DRIZZLE_TYPE_DATETIME
536
  STRING_RESULT,
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
537
  //DRIZZLE_TYPE_DATE
520.1.26 by Monty Taylor
Fixed some formatting.
538
  STRING_RESULT,
539
  //DRIZZLE_TYPE_VARCHAR
540
  STRING_RESULT,
383.7.1 by Andrey Zhakov
Initial submit of code and tests
541
  //DRIZZLE_TYPE_VIRTUAL
542
  STRING_RESULT,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
543
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
544
  DECIMAL_RESULT,           STRING_RESULT,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
545
  //DRIZZLE_TYPE_BLOB
546
  STRING_RESULT,
1 by brian
clean slate
547
};
548
549
550
/*
551
  Test if the given string contains important data:
552
  not spaces for character string,
553
  or any data for binary string.
554
555
  SYNOPSIS
520.1.28 by Monty Taylor
Reverted too-big formatting change.
556
    test_if_important_data()
557
    cs          Character set
558
    str         String to test
559
    strend      String end
1 by brian
clean slate
560
561
  RETURN
520.1.28 by Monty Taylor
Reverted too-big formatting change.
562
    false - If string does not have important data
563
    true  - If string has some important data
1 by brian
clean slate
564
*/
565
483.1.4 by Lee
break out Field_longstr
566
bool
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
567
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
568
                       const char *strend)
1 by brian
clean slate
569
{
570
  if (cs != &my_charset_bin)
571
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
572
  return (str < strend);
573
}
574
575
576
Item_result Field::result_merge_type(enum_field_types field_type)
577
{
520.1.25 by Monty Taylor
Removed the split.
578
  assert(field_type <= DRIZZLE_TYPE_MAX);
579
  return field_types_result_type[field_type];
1 by brian
clean slate
580
}
581
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
582
583
bool Field::eq(Field *field)
584
{
585
  return (ptr == field->ptr && null_ptr == field->null_ptr &&
586
          null_bit == field->null_bit);
587
}
588
589
590
uint32_t Field::pack_length() const
591
{
592
  return field_length;
593
}
594
595
596
uint32_t Field::pack_length_in_rec() const
597
{
598
  return pack_length();
599
}
600
601
602
uint32_t Field::pack_length_from_metadata(uint32_t field_metadata)
603
{
604
  return field_metadata;
605
}
606
607
608
uint32_t Field::row_pack_length()
609
{
610
  return 0;
611
}
612
613
614
int Field::save_field_metadata(unsigned char *first_byte)
615
{
616
  return do_save_field_metadata(first_byte);
617
}
618
619
620
uint32_t Field::data_length()
621
{
622
  return pack_length();
623
}
624
625
626
uint32_t Field::used_length()
627
{
628
  return pack_length();
629
}
630
631
632
uint32_t Field::sort_length() const
633
{
634
  return pack_length();
635
}
636
637
638
uint32_t Field::max_data_length() const
639
{
640
  return pack_length();
641
}
642
643
644
int Field::reset(void)
645
{
646
  memset(ptr, 0, pack_length());
647
  return 0;
648
}
649
650
651
void Field::reset_fields()
652
{}
653
654
655
void Field::set_default()
656
{
657
  my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
658
  memcpy(ptr, ptr + l_offset, pack_length());
659
  if (null_ptr)
660
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
661
}
662
663
664
bool Field::binary() const
665
{
666
  return 1;
667
}
668
669
670
bool Field::zero_pack() const
671
{
672
  return 1;
673
}
674
675
676
enum ha_base_keytype Field::key_type() const
677
{
678
  return HA_KEYTYPE_BINARY;
679
}
680
681
682
uint32_t Field::key_length() const
683
{
684
  return pack_length();
685
}
686
687
688
enum_field_types Field::real_type() const
689
{
690
  return type();
691
}
692
693
694
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
695
{
696
  return cmp(a, b);
697
}
698
699
700
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
701
{
702
  return memcmp(a,b,pack_length());
703
}
704
705
706
int Field::cmp_offset(uint32_t row_offset)
707
{
708
  return cmp(ptr,ptr+row_offset);
709
}
710
711
712
int Field::cmp_binary_offset(uint32_t row_offset)
713
{
714
  return cmp_binary(ptr, ptr+row_offset);
715
}
716
717
718
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
719
{
720
  return cmp(a, b);
721
}
722
723
724
int Field::key_cmp(const unsigned char *str, uint32_t)
725
{
726
  return cmp(ptr,str);
727
}
728
729
730
uint32_t Field::decimals() const
731
{
732
  return 0;
733
}
734
735
736
bool Field::is_null(my_ptrdiff_t row_offset)
737
{
738
  return null_ptr ?
739
    (null_ptr[row_offset] & null_bit ? true : false) :
740
    table->null_row;
741
}
742
743
744
bool Field::is_real_null(my_ptrdiff_t row_offset)
745
{
746
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
747
}
748
749
750
bool Field::is_null_in_record(const unsigned char *record)
751
{
752
  if (!null_ptr)
753
    return false;
754
  return test(record[(uint32_t) (null_ptr -table->record[0])] &
755
              null_bit);
756
}
757
758
759
bool Field::is_null_in_record_with_offset(my_ptrdiff_t offset)
760
{
761
  if (!null_ptr)
762
    return false;
763
  return test(null_ptr[offset] & null_bit);
764
}
765
766
767
void Field::set_null(my_ptrdiff_t row_offset)
768
{
769
  if (null_ptr)
770
    null_ptr[row_offset]|= null_bit;
771
}
772
773
774
void Field::set_notnull(my_ptrdiff_t row_offset)
775
{
776
  if (null_ptr)
777
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
778
}
779
780
781
bool Field::maybe_null(void)
782
{
783
  return null_ptr != 0 || table->maybe_null;
784
}
785
786
787
bool Field::real_maybe_null(void)
788
{
789
  return null_ptr != 0;
790
}
791
792
793
size_t Field::last_null_byte() const
794
{
795
  size_t bytes= do_last_null_byte();
796
  assert(bytes <= table->getNullBytes());
797
  return bytes;
798
}
799
800
1 by brian
clean slate
801
/*****************************************************************************
802
  Static help functions
803
*****************************************************************************/
804
805
bool Field::type_can_have_key_part(enum enum_field_types type)
806
{
807
  switch (type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
808
  case DRIZZLE_TYPE_VARCHAR:
809
  case DRIZZLE_TYPE_BLOB:
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
810
    return true;
1 by brian
clean slate
811
  default:
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
812
    return false;
1 by brian
clean slate
813
  }
814
}
815
816
817
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
818
  Process decimal library return codes and issue warnings for overflow and
819
  truncation.
820
821
  @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
822
823
  @retval
824
    1  there was overflow
825
  @retval
826
    0  no error or some other errors except overflow
1 by brian
clean slate
827
*/
828
829
int Field::warn_if_overflow(int op_result)
830
{
831
  if (op_result == E_DEC_OVERFLOW)
832
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
833
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1 by brian
clean slate
834
    return 1;
835
  }
836
  if (op_result == E_DEC_TRUNCATED)
837
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
838
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
839
    /* We return 0 here as this is not a critical issue */
840
  }
841
  return 0;
842
}
843
844
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
845
void Field::init(Table *table_arg)
846
{
847
  orig_table= table= table_arg;
848
  table_name= &table_arg->alias;
849
}
850
851
1 by brian
clean slate
852
#ifdef NOT_USED
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
853
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
1 by brian
clean slate
854
{
855
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
856
857
  while (length && my_isspace(cs,*str))
520.1.28 by Monty Taylor
Reverted too-big formatting change.
858
  {						// Allow start space
1 by brian
clean slate
859
    length--; str++;
860
  }
861
  if (!length)
862
    return 0;
863
  if (*str == '+' || *str == '-')
864
  {
865
    length--; str++;
866
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
867
      return 0;
868
  }
869
  while (length && my_isdigit(cs,*str))
870
  {
871
    length--; str++;
872
  }
873
  if (!length)
874
    return 1;
875
  if (*str == '.')
876
  {
877
    length--; str++;
878
    while (length && my_isdigit(cs,*str))
879
    {
880
      length--; str++;
881
    }
882
  }
883
  if (!length)
884
    return 1;
885
  if (*str == 'E' || *str == 'e')
886
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
887
    if (length < 3 || (str[1] != '+' && str[1] != '-') ||
1 by brian
clean slate
888
        !my_isdigit(cs,str[2]))
889
      return 0;
890
    length-=3;
891
    str+=3;
892
    while (length && my_isdigit(cs,*str))
893
    {
894
      length--; str++;
895
    }
896
  }
897
  for (; length ; length--, str++)
520.1.28 by Monty Taylor
Reverted too-big formatting change.
898
  {						// Allow end space
1 by brian
clean slate
899
    if (!my_isspace(cs,*str))
900
      return 0;
901
  }
902
  return 1;
903
}
904
#endif
905
906
907
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
908
  Interpret field value as an integer but return the result as a string.
1 by brian
clean slate
909
520.1.28 by Monty Taylor
Reverted too-big formatting change.
910
  This is used for printing bit_fields as numbers while debugging.
1 by brian
clean slate
911
*/
912
275 by Brian Aker
Full removal of my_bool from central server.
913
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
1 by brian
clean slate
914
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
915
  const CHARSET_INFO * const cs= &my_charset_bin;
438.1.13 by Brian Aker
uint cleanup.
916
  uint32_t length;
152 by Brian Aker
longlong replacement
917
  int64_t value= val_int();
1 by brian
clean slate
918
919
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
920
    return 0;
438.1.13 by Brian Aker
uint cleanup.
921
  length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
520.1.28 by Monty Taylor
Reverted too-big formatting change.
922
                                                MY_INT64_NUM_DECIMAL_DIGITS,
923
                                                unsigned_val ? 10 : -10,
924
                                                value);
1 by brian
clean slate
925
  val_buffer->length(length);
926
  return val_buffer;
927
}
928
929
930
/// This is used as a table name when the table structure is not set up
481 by Brian Aker
Remove all of uchar.
931
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
932
             unsigned char null_bit_arg,
933
             utype unireg_check_arg, const char *field_name_arg)
1 by brian
clean slate
934
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
935
   table(0), orig_table(0), table_name(0),
936
   field_name(field_name_arg),
937
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
938
   part_of_sortkey(0), unireg_check(unireg_check_arg),
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
939
   field_length(length_arg), null_bit(null_bit_arg),
383.7.1 by Andrey Zhakov
Initial submit of code and tests
940
   is_created_from_null_item(false),
941
   vcol_info(NULL), is_stored(true)
1 by brian
clean slate
942
{
943
  flags=null_ptr ? 0: NOT_NULL_FLAG;
944
  comment.str= (char*) "";
945
  comment.length=0;
946
  field_index= 0;
947
}
948
949
290 by Brian Aker
Update for ulong change over.
950
void Field::hash(uint32_t *nr, uint32_t *nr2)
1 by brian
clean slate
951
{
952
  if (is_null())
953
  {
954
    *nr^= (*nr << 1) | 1;
955
  }
956
  else
957
  {
438.1.13 by Brian Aker
uint cleanup.
958
    uint32_t len= pack_length();
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
959
    const CHARSET_INFO * const cs= charset();
1 by brian
clean slate
960
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
961
  }
962
}
963
964
size_t
965
Field::do_last_null_byte() const
966
{
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
967
  assert(null_ptr == NULL || null_ptr >= table->record[0]);
1 by brian
clean slate
968
  if (null_ptr)
969
    return (size_t) (null_ptr - table->record[0]) + 1;
970
  return LAST_NULL_BYTE_UNDEF;
971
}
972
973
974
void Field::copy_from_tmp(int row_offset)
975
{
976
  memcpy(ptr,ptr+row_offset,pack_length());
977
  if (null_ptr)
978
  {
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
979
    *null_ptr= (unsigned char) ((null_ptr[0] &
980
                                 (unsigned char) ~(uint32_t) null_bit) |
981
                                (null_ptr[row_offset] &
982
                                 (unsigned char) null_bit));
1 by brian
clean slate
983
  }
984
}
985
986
987
bool Field::send_binary(Protocol *protocol)
988
{
989
  char buff[MAX_FIELD_WIDTH];
990
  String tmp(buff,sizeof(buff),charset());
991
  val_str(&tmp);
992
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
993
}
994
995
996
/**
997
   Check to see if field size is compatible with destination.
998
999
   This method is used in row-based replication to verify that the slave's
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
1000
   field size is less than or equal to the master's field size. The
1 by brian
clean slate
1001
   encoded field metadata (from the master or source) is decoded and compared
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
1002
   to the size of this field (the slave or destination).
1 by brian
clean slate
1003
1004
   @param   field_metadata   Encoded size in field metadata
1005
1006
   @retval 0 if this field's size is < the source field's size
1007
   @retval 1 if this field's size is >= the source field's size
1008
*/
438.1.13 by Brian Aker
uint cleanup.
1009
int Field::compatible_field_size(uint32_t field_metadata)
1 by brian
clean slate
1010
{
438.1.13 by Brian Aker
uint cleanup.
1011
  uint32_t const source_size= pack_length_from_metadata(field_metadata);
1012
  uint32_t const destination_size= row_pack_length();
1 by brian
clean slate
1013
  return (source_size <= destination_size);
1014
}
1015
1016
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
1017
int Field::store(const char *to, uint32_t length,
1018
                 const CHARSET_INFO * const cs,
1 by brian
clean slate
1019
                 enum_check_fields check_level)
1020
{
1021
  int res;
1022
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
1023
  table->in_use->count_cuted_fields= check_level;
1024
  res= store(to, length, cs);
1025
  table->in_use->count_cuted_fields= old_check_level;
1026
  return res;
1027
}
1028
1029
1030
/**
1031
   Pack the field into a format suitable for storage and transfer.
1032
1033
   To implement packing functionality, only the virtual function
1034
   should be overridden. The other functions are just convenience
1035
   functions and hence should not be overridden.
1036
1037
   The value of <code>low_byte_first</code> is dependent on how the
1038
   packed data is going to be used: for local use, e.g., temporary
1039
   store on disk or in memory, use the native format since that is
1040
   faster. For data that is going to be transfered to other machines
1041
   (e.g., when writing data to the binary log), data should always be
1042
   stored in little-endian format.
1043
1044
   @note The default method for packing fields just copy the raw bytes
1045
   of the record into the destination, but never more than
1046
   <code>max_length</code> characters.
1047
1048
   @param to
1049
   Pointer to memory area where representation of field should be put.
1050
1051
   @param from
1052
   Pointer to memory area where record representation of field is
1053
   stored.
1054
1055
   @param max_length
1056
   Maximum length of the field, as given in the column definition. For
1057
   example, for <code>CHAR(1000)</code>, the <code>max_length</code>
1058
   is 1000. This information is sometimes needed to decide how to pack
1059
   the data.
1060
1061
   @param low_byte_first
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
1062
   @c true if integers should be stored little-endian, @c false if
1 by brian
clean slate
1063
   native format should be used. Note that for little-endian machines,
1064
   the value of this flag is a moot point since the native format is
1065
   little-endian.
1066
*/
481 by Brian Aker
Remove all of uchar.
1067
unsigned char *
1068
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1069
            bool)
1 by brian
clean slate
1070
{
205 by Brian Aker
uint32 -> uin32_t
1071
  uint32_t length= pack_length();
1 by brian
clean slate
1072
  set_if_smaller(length, max_length);
1073
  memcpy(to, from, length);
1074
  return to+length;
1075
}
1076
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
1077
1078
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
1079
{
1080
  unsigned char *result= this->pack(to, from, UINT32_MAX,
1081
                                    table->s->db_low_byte_first);
1082
  return(result);
1083
}
1084
1085
1 by brian
clean slate
1086
/**
1087
   Unpack a field from row data.
1088
1089
   This method is used to unpack a field from a master whose size of
1090
   the field is less than that of the slave.
1091
1092
   The <code>param_data</code> parameter is a two-byte integer (stored
1093
   in the least significant 16 bits of the unsigned integer) usually
1094
   consisting of two parts: the real type in the most significant byte
1095
   and a original pack length in the least significant byte.
1096
1097
   The exact layout of the <code>param_data</code> field is given by
1098
   the <code>Table_map_log_event::save_field_metadata()</code>.
1099
1100
   This is the default method for unpacking a field. It just copies
1101
   the memory block in byte order (of original pack length bytes or
1102
   length of field, whichever is smaller).
1103
1104
   @param   to         Destination of the data
1105
   @param   from       Source of the data
1106
   @param   param_data Real type and original pack length of the field
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1107
                       data
1 by brian
clean slate
1108
1109
   @param low_byte_first
1110
   If this flag is @c true, all composite entities (e.g., lengths)
1111
   should be unpacked in little-endian format; otherwise, the entities
1112
   are unpacked in native order.
1113
1114
   @return  New pointer into memory based on from + length of the data
1115
*/
481 by Brian Aker
Remove all of uchar.
1116
const unsigned char *
1117
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1118
              bool)
1 by brian
clean slate
1119
{
438.1.13 by Brian Aker
uint cleanup.
1120
  uint32_t length=pack_length();
1 by brian
clean slate
1121
  int from_type= 0;
1122
  /*
1123
    If from length is > 255, it has encoded data in the upper bits. Need
1124
    to mask it out.
1125
  */
1126
  if (param_data > 255)
1127
  {
1128
    from_type= (param_data & 0xff00) >> 8U;  // real_type.
1129
    param_data= param_data & 0x00ff;        // length.
1130
  }
1131
1132
  if ((param_data == 0) ||
1133
      (length == param_data) ||
1134
      (from_type != real_type()))
1135
  {
1136
    memcpy(to, from, length);
1137
    return from+length;
1138
  }
1139
438.1.13 by Brian Aker
uint cleanup.
1140
  uint32_t len= (param_data && (param_data < length)) ?
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1141
            param_data : length;
1 by brian
clean slate
1142
1143
  memcpy(to, from, param_data > length ? length : len);
1144
  return from+len;
1145
}
1146
1147
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
1148
const unsigned char *Field::unpack(unsigned char* to,
1149
                                   const unsigned char *from)
1150
{
1151
  const unsigned char *result= unpack(to, from, 0U,
1152
                                      table->s->db_low_byte_first);
1153
  return(result);
1154
}
1155
1156
1157
uint32_t Field::packed_col_length(const unsigned char *, uint32_t length)
1158
{
1159
  return length;
1160
}
1161
1162
1163
int Field::pack_cmp(const unsigned char *a, const unsigned char *b,
1164
                    uint32_t, bool)
1165
{
1166
  return cmp(a,b);
1167
}
1168
1169
1170
int Field::pack_cmp(const unsigned char *b, uint32_t, bool)
1171
{
1172
  return cmp(ptr,b);
1173
}
1174
1175
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1176
my_decimal *Field::val_decimal(my_decimal *)
1 by brian
clean slate
1177
{
1178
  /* This never have to be called */
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1179
  assert(0);
1 by brian
clean slate
1180
  return 0;
1181
}
1182
1183
1184
void Field::make_field(Send_field *field)
1185
{
1186
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1187
  {
1188
    field->db_name= orig_table->s->db.str;
1189
    field->org_table_name= orig_table->s->table_name.str;
1190
  }
1191
  else
1192
    field->org_table_name= field->db_name= "";
1193
  if (orig_table)
1194
  {
1195
    field->table_name= orig_table->alias;
1196
    field->org_col_name= field_name;
1197
  }
1198
  else
1199
  {
1200
    field->table_name= "";
1201
    field->org_col_name= "";
1202
  }
1203
  field->col_name= field_name;
1204
  field->charsetnr= charset()->number;
1205
  field->length=field_length;
1206
  field->type=type();
1207
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1208
  field->decimals= 0;
1209
}
1210
1211
1212
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1213
  Conversion from decimal to int64_t with checking overflow and
1214
  setting correct value (min/max) in case of overflow.
1215
1216
  @param val             value which have to be converted
1217
  @param unsigned_flag   type of integer in which we convert val
1218
  @param err             variable to pass error code
1219
1220
  @return
1221
    value converted from val
1 by brian
clean slate
1222
*/
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1223
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
1 by brian
clean slate
1224
{
152 by Brian Aker
longlong replacement
1225
  int64_t i;
508 by Brian Aker
Second pass on unsigned
1226
  if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1227
                                      ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1228
                                      val, false, &i)))
1 by brian
clean slate
1229
  {
163 by Brian Aker
Merge Monty's code.
1230
    i= (val->sign() ? INT64_MIN : INT64_MAX);
1 by brian
clean slate
1231
    *err= 1;
1232
  }
1233
  return i;
1234
}
1235
438.1.13 by Brian Aker
uint cleanup.
1236
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
1 by brian
clean slate
1237
{
438.1.13 by Brian Aker
uint cleanup.
1238
  uint32_t store_length;
1 by brian
clean slate
1239
  copy->str=ptr;
1240
  copy->length=pack_length();
1241
  copy->blob_field=0;
1242
  if (flags & BLOB_FLAG)
1243
  {
1244
    copy->blob_field=(Field_blob*) this;
1245
    copy->strip=0;
1246
    copy->length-= table->s->blob_ptr_size;
1247
    return copy->length;
1248
  }
1249
  else
1250
  {
1251
    copy->strip=0;
1252
    store_length= 0;
1253
  }
1254
  return copy->length+ store_length;
1255
}
1256
1257
438.1.13 by Brian Aker
uint cleanup.
1258
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1 by brian
clean slate
1259
{
1260
  char buff[40];
1261
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1262
  if (!(res=val_str(&tmp)) ||
1263
      str_to_datetime_with_warn(res->ptr(), res->length(),
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1264
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
1 by brian
clean slate
1265
    return 1;
1266
  return 0;
1267
}
1268
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1269
bool Field::get_time(DRIZZLE_TIME *ltime)
1 by brian
clean slate
1270
{
1271
  char buff[40];
1272
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1273
  if (!(res=val_str(&tmp)) ||
1274
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
1275
    return 1;
1276
  return 0;
1277
}
1278
1279
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1280
  This is called when storing a date in a string.
1 by brian
clean slate
1281
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1282
  @note
1283
    Needs to be changed if/when we want to support different time formats.
1 by brian
clean slate
1284
*/
1285
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1286
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
1 by brian
clean slate
1287
{
1288
  char buff[MAX_DATE_STRING_REP_LENGTH];
438.1.13 by Brian Aker
uint cleanup.
1289
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
1 by brian
clean slate
1290
  return store(buff, length, &my_charset_bin);
1291
}
1292
1293
438.1.13 by Brian Aker
uint cleanup.
1294
bool Field::optimize_range(uint32_t idx, uint32_t part)
1 by brian
clean slate
1295
{
1296
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1297
}
1298
1299
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1300
Field *Field::new_field(MEM_ROOT *root, Table *new_table, bool)
1 by brian
clean slate
1301
{
1302
  Field *tmp;
1303
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1304
    return 0;
1305
1306
  if (tmp->table->maybe_null)
1307
    tmp->flags&= ~NOT_NULL_FLAG;
1308
  tmp->table= new_table;
1309
  tmp->key_start.init(0);
1310
  tmp->part_of_key.init(0);
1311
  tmp->part_of_sortkey.init(0);
1312
  tmp->unireg_check= Field::NONE;
216 by Brian Aker
Remove completely ZEROFILL
1313
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1 by brian
clean slate
1314
  tmp->reset_fields();
1315
  return tmp;
1316
}
1317
1318
327.1.1 by Brian Aker
First pass in encapsulating table (it is now an object, no longer a structure).
1319
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
481 by Brian Aker
Remove all of uchar.
1320
                            unsigned char *new_ptr, unsigned char *new_null_ptr,
438.1.13 by Brian Aker
uint cleanup.
1321
                            uint32_t new_null_bit)
1 by brian
clean slate
1322
{
1323
  Field *tmp;
1324
  if ((tmp= new_field(root, new_table, table == new_table)))
1325
  {
1326
    tmp->ptr=      new_ptr;
1327
    tmp->null_ptr= new_null_ptr;
1328
    tmp->null_bit= new_null_bit;
1329
  }
1330
  return tmp;
1331
}
1332
1333
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1334
/* This is used to generate a field in Table from TABLE_SHARE */
1 by brian
clean slate
1335
327.1.1 by Brian Aker
First pass in encapsulating table (it is now an object, no longer a structure).
1336
Field *Field::clone(MEM_ROOT *root, Table *new_table)
1 by brian
clean slate
1337
{
1338
  Field *tmp;
1339
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1340
  {
1341
    tmp->init(new_table);
1342
    tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
1343
                                           new_table->s->default_values));
1344
  }
1345
  return tmp;
1346
}
1347
1348
438.1.13 by Brian Aker
uint cleanup.
1349
uint32_t Field::is_equal(Create_field *new_field)
1 by brian
clean slate
1350
{
1351
  return (new_field->sql_type == real_type());
1352
}
1353
1354
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1355
  @retval
1356
    1  if the fields are equally defined
1357
  @retval
1358
    0  if the fields are unequally defined
1 by brian
clean slate
1359
*/
1360
1361
bool Field::eq_def(Field *field)
1362
{
1363
  if (real_type() != field->real_type() || charset() != field->charset() ||
1364
      pack_length() != field->pack_length())
1365
    return 0;
1366
  return 1;
1367
}
1368
1369
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1370
  @return
1371
  returns 1 if the fields are equally defined
1 by brian
clean slate
1372
*/
1373
bool Field_enum::eq_def(Field *field)
1374
{
1375
  if (!Field::eq_def(field))
1376
    return 0;
1377
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
1378
1379
  if (typelib->count < from_lib->count)
1380
    return 0;
438.1.13 by Brian Aker
uint cleanup.
1381
  for (uint32_t i=0 ; i < from_lib->count ; i++)
1 by brian
clean slate
1382
    if (my_strnncoll(field_charset,
481 by Brian Aker
Remove all of uchar.
1383
                     (const unsigned char*)typelib->type_names[i],
1 by brian
clean slate
1384
                     strlen(typelib->type_names[i]),
481 by Brian Aker
Remove all of uchar.
1385
                     (const unsigned char*)from_lib->type_names[i],
1 by brian
clean slate
1386
                     strlen(from_lib->type_names[i])))
1387
      return 0;
1388
  return 1;
1389
}
1390
1391
/*****************************************************************************
1392
  Handling of field and Create_field
1393
*****************************************************************************/
1394
1395
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1396
  Convert create_field::length from number of characters to number of bytes.
1 by brian
clean slate
1397
*/
1398
1399
void Create_field::create_length_to_internal_length(void)
1400
{
1401
  switch (sql_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1402
  case DRIZZLE_TYPE_BLOB:
1403
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
1404
    length*= charset->mbmaxlen;
1405
    key_length= length;
1406
    pack_length= calc_pack_length(sql_type, length);
1407
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1408
  case DRIZZLE_TYPE_ENUM:
1 by brian
clean slate
1409
    /* Pack_length already calculated in sql_parse.cc */
1410
    length*= charset->mbmaxlen;
1411
    key_length= pack_length;
1412
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1413
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
1414
    key_length= pack_length=
1415
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1416
								decimals,
1417
								flags &
1418
								UNSIGNED_FLAG),
1419
				 decimals);
1 by brian
clean slate
1420
    break;
1421
  default:
1422
    key_length= pack_length= calc_pack_length(sql_type, length);
1423
    break;
1424
  }
1425
}
1426
1427
1428
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1429
  Init for a tmp table field. To be extended if need be.
1 by brian
clean slate
1430
*/
1431
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
205 by Brian Aker
uint32 -> uin32_t
1432
                                      uint32_t length_arg, uint32_t decimals_arg,
1 by brian
clean slate
1433
                                      bool maybe_null, bool is_unsigned)
1434
{
1435
  field_name= "";
1436
  sql_type= sql_type_arg;
1437
  char_length= length= length_arg;;
1438
  unireg_check= Field::NONE;
1439
  interval= 0;
1440
  charset= &my_charset_bin;
1441
  pack_flag= (FIELDFLAG_NUMBER |
1442
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1443
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1444
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1445
  vcol_info= NULL;
1446
  is_stored= true;
1 by brian
clean slate
1447
}
1448
1449
1450
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1451
  Initialize field definition for create.
1452
1453
  @param session                   Thread handle
1454
  @param fld_name              Field name
1455
  @param fld_type              Field type
1456
  @param fld_length            Field length
1457
  @param fld_decimals          Decimal (if any)
1458
  @param fld_type_modifier     Additional type information
1459
  @param fld_default_value     Field default value (if any)
1460
  @param fld_on_update_value   The value of ON UPDATE clause
1461
  @param fld_comment           Field comment
1462
  @param fld_change            Field change
1463
  @param fld_interval_list     Interval list (if any)
1464
  @param fld_charset           Field charset
1465
  @param fld_vcol_info         Virtual column data
1466
1467
  @retval
1468
    false on success
1469
  @retval
1470
    true  on error
1 by brian
clean slate
1471
*/
1472
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1473
bool Create_field::init(Session *, char *fld_name, enum_field_types fld_type,
1 by brian
clean slate
1474
                        char *fld_length, char *fld_decimals,
438.1.13 by Brian Aker
uint cleanup.
1475
                        uint32_t fld_type_modifier, Item *fld_default_value,
1 by brian
clean slate
1476
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
1477
                        char *fld_change, List<String> *fld_interval_list,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1478
                        const CHARSET_INFO * const fld_charset,
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1479
                        uint32_t, enum column_format_type column_format,
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1480
                        virtual_column_info *fld_vcol_info)
1 by brian
clean slate
1481
{
438.1.13 by Brian Aker
uint cleanup.
1482
  uint32_t sign_len, allowed_type_modifier= 0;
290 by Brian Aker
Update for ulong change over.
1483
  uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
1 by brian
clean slate
1484
1485
  field= 0;
1486
  field_name= fld_name;
1487
  def= fld_default_value;
1488
  flags= fld_type_modifier;
438.1.13 by Brian Aker
uint cleanup.
1489
  flags|= (((uint32_t)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1 by brian
clean slate
1490
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1491
                 Field::NEXT_NUMBER : Field::NONE);
438.1.13 by Brian Aker
uint cleanup.
1492
  decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
1 by brian
clean slate
1493
  if (decimals >= NOT_FIXED_DEC)
1494
  {
1495
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1496
             NOT_FIXED_DEC-1);
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1497
    return(true);
1 by brian
clean slate
1498
  }
1499
1500
  sql_type= fld_type;
1501
  length= 0;
1502
  change= fld_change;
1503
  interval= 0;
1504
  pack_length= key_length= 0;
1505
  charset= fld_charset;
1506
  interval_list.empty();
1507
1508
  comment= *fld_comment;
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1509
  vcol_info= fld_vcol_info;
1510
  is_stored= true;
1511
1512
  /* Initialize data for a virtual field */
1513
  if (fld_type == DRIZZLE_TYPE_VIRTUAL)
1514
  {
1515
    assert(vcol_info && vcol_info->expr_item);
1516
    is_stored= vcol_info->get_field_stored();
1517
    /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1518
      Perform per item-type checks to determine if the expression is
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1519
      allowed for a virtual column.
1520
      Note that validation of the specific function is done later in
1521
      procedures open_table_from_share and fix_fields_vcol_func
1522
    */
1523
    switch (vcol_info->expr_item->type()) {
1524
    case Item::FUNC_ITEM:
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1525
         if (((Item_func *)vcol_info->expr_item)->functype() == Item_func::FUNC_SP)
1526
         {
1527
           my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1528
           return(true);
1529
         }
1530
         break;
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1531
    case Item::COPY_STR_ITEM:
1532
    case Item::FIELD_AVG_ITEM:
1533
    case Item::PROC_ITEM:
1534
    case Item::REF_ITEM:
1535
    case Item::FIELD_STD_ITEM:
1536
    case Item::FIELD_VARIANCE_ITEM:
1537
    case Item::INSERT_VALUE_ITEM:
1538
    case Item::SUBSELECT_ITEM:
1539
    case Item::CACHE_ITEM:
1540
    case Item::TYPE_HOLDER:
1541
    case Item::PARAM_ITEM:
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1542
         my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1543
         return true;
1544
         break;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1545
    default:
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1546
      // Continue with the field creation
1547
      break;
1548
    }
1549
    /*
1550
      Make a field created for the real type.
1551
      Note that "real" and virtual fields differ from each other
1552
      only by Field::vcol_info, which is always 0 for normal columns.
1553
      vcol_info is updated for fields later in procedure open_binary_frm.
1554
    */
1555
    sql_type= fld_type= vcol_info->get_real_type();
1556
  }
1557
1 by brian
clean slate
1558
  /*
1559
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1560
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1561
  */
1562
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1563
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1 by brian
clean slate
1564
    flags|= NO_DEFAULT_VALUE_FLAG;
1565
438.1.13 by Brian Aker
uint cleanup.
1566
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
1 by brian
clean slate
1567
    fld_length= 0; /* purecov: inspected */
1568
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1569
1570
  switch (fld_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1571
  case DRIZZLE_TYPE_TINY:
1 by brian
clean slate
1572
    if (!fld_length)
1573
      length= MAX_TINYINT_WIDTH+sign_len;
1574
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1575
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1576
  case DRIZZLE_TYPE_LONG:
1 by brian
clean slate
1577
    if (!fld_length)
1578
      length= MAX_INT_WIDTH+sign_len;
1579
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1580
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1581
  case DRIZZLE_TYPE_LONGLONG:
1 by brian
clean slate
1582
    if (!fld_length)
1583
      length= MAX_BIGINT_WIDTH;
1584
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1585
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1586
  case DRIZZLE_TYPE_NULL:
1 by brian
clean slate
1587
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1588
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
1589
    my_decimal_trim(&length, &decimals);
1590
    if (length > DECIMAL_MAX_PRECISION)
1591
    {
1592
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1593
               DECIMAL_MAX_PRECISION);
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1594
      return(true);
1 by brian
clean slate
1595
    }
1596
    if (length < decimals)
1597
    {
1598
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1599
      return(true);
1 by brian
clean slate
1600
    }
1601
    length=
1602
      my_decimal_precision_to_length(length, decimals,
1603
                                     fld_type_modifier & UNSIGNED_FLAG);
1604
    pack_length=
1605
      my_decimal_get_binary_size(length, decimals);
1606
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1607
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
1608
    /*
1609
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1610
      if they don't have a default value
1611
    */
1612
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1613
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1614
  case DRIZZLE_TYPE_BLOB:
1 by brian
clean slate
1615
    if (fld_default_value)
1616
    {
1617
      /* Allow empty as default value. */
1618
      String str,*res;
1619
      res= fld_default_value->val_str(&str);
1620
    }
1621
    flags|= BLOB_FLAG;
1622
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1623
  case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
1624
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1625
    if (!fld_length && !fld_decimals)
1626
    {
1627
      length= DBL_DIG+7;
1628
      decimals= NOT_FIXED_DEC;
1629
    }
1630
    if (length < decimals &&
1631
        decimals != NOT_FIXED_DEC)
1632
    {
1633
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1634
      return(true);
1 by brian
clean slate
1635
    }
1636
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1637
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
1638
    if (!fld_length)
1639
    {
1640
      /* Compressed date YYYYMMDDHHMMSS */
1641
      length= MAX_DATETIME_COMPRESSED_WIDTH;
1642
    }
1643
    else if (length != MAX_DATETIME_WIDTH)
1644
    {
1645
      /*
1646
        We support only even TIMESTAMP lengths less or equal than 14
1647
        and 19 as length of 4.1 compatible representation.
1648
      */
1649
      length= ((length+1)/2)*2; /* purecov: inspected */
398.1.4 by Monty Taylor
Renamed max/min.
1650
      length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1 by brian
clean slate
1651
    }
216 by Brian Aker
Remove completely ZEROFILL
1652
    flags|= UNSIGNED_FLAG;
1 by brian
clean slate
1653
    if (fld_default_value)
1654
    {
1655
      /* Grammar allows only NOW() value for ON UPDATE clause */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1656
      if (fld_default_value->type() == Item::FUNC_ITEM &&
1 by brian
clean slate
1657
          ((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
1658
      {
1659
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1660
                                             Field::TIMESTAMP_DN_FIELD);
1 by brian
clean slate
1661
        /*
1662
          We don't need default value any longer moreover it is dangerous.
1663
          Everything handled by unireg_check further.
1664
        */
1665
        def= 0;
1666
      }
1667
      else
1668
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1669
                                             Field::NONE);
1 by brian
clean slate
1670
    }
1671
    else
1672
    {
1673
      /*
1674
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
1675
        or ON UPDATE values then for the sake of compatiblity we should treat
1676
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
1677
        have another TIMESTAMP column with auto-set option before this one)
1678
        or DEFAULT 0 (in other cases).
1679
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
1680
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
1681
        information about all TIMESTAMP fields in table will be availiable.
1682
1683
        If we have TIMESTAMP NULL column without explicit DEFAULT value
1684
        we treat it as having DEFAULT NULL attribute.
1685
      */
1686
      unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
1687
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1688
                                              Field::NONE));
1 by brian
clean slate
1689
    }
1690
    break;
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
1691
  case DRIZZLE_TYPE_DATE:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1692
    length= 10;
1693
    break;
1694
  case DRIZZLE_TYPE_TIME:
1695
    length= 10;
1696
    break;
1697
  case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
1698
    length= MAX_DATETIME_WIDTH;
1699
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1700
  case DRIZZLE_TYPE_ENUM:
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1701
    {
1702
      /* Should be safe. */
1703
      pack_length= get_enum_pack_length(fld_interval_list->elements);
1 by brian
clean slate
1704
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1705
      List_iterator<String> it(*fld_interval_list);
1706
      String *tmp;
1707
      while ((tmp= it++))
1708
        interval_list.push_back(tmp);
1709
      length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1710
      break;
1711
   }
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1712
  case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1713
    assert(0);
1 by brian
clean slate
1714
  }
1715
  /* Remember the value of length */
1716
  char_length= length;
1717
1718
  if (!(flags & BLOB_FLAG) &&
325 by Brian Aker
Remove SET
1719
      ((length > max_field_charlength &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1720
        fld_type != DRIZZLE_TYPE_ENUM &&
1721
        (fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
241 by Brian Aker
First pass of CHAR removal.
1722
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1 by brian
clean slate
1723
  {
241 by Brian Aker
First pass of CHAR removal.
1724
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1725
              MYF(0),
1726
              fld_name, max_field_charlength); /* purecov: inspected */
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1727
    return(true);
1 by brian
clean slate
1728
  }
1729
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
1730
  if ((~allowed_type_modifier) & fld_type_modifier)
1731
  {
1732
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1733
    return(true);
1 by brian
clean slate
1734
  }
1735
51.1.58 by Jay Pipes
Replace/remove DBUG and standardize TRUE/FALSE. Remove ASSERT_COLUMN_XXX macros, that work will be done on another
1736
  return(false); /* success */
1 by brian
clean slate
1737
}
1738
1739
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1740
enum_field_types get_blob_type_from_length(uint32_t)
1 by brian
clean slate
1741
{
1742
  enum_field_types type;
114 by Brian Aker
More cleanup around Drizzle types.
1743
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1744
  type= DRIZZLE_TYPE_BLOB;
114 by Brian Aker
More cleanup around Drizzle types.
1745
1 by brian
clean slate
1746
  return type;
1747
}
1748
1749
1750
/*
1751
  Make a field from the .frm file info
1752
*/
1753
205 by Brian Aker
uint32 -> uin32_t
1754
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1 by brian
clean slate
1755
{
1756
  switch (type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1757
  case DRIZZLE_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1758
  case DRIZZLE_TYPE_TINY	: return 1;
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
1759
  case DRIZZLE_TYPE_DATE:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1760
  case DRIZZLE_TYPE_TIME:   return 3;
1761
  case DRIZZLE_TYPE_TIMESTAMP:
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1762
  case DRIZZLE_TYPE_LONG	: return 4;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1763
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1764
  case DRIZZLE_TYPE_DATETIME:
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1765
  case DRIZZLE_TYPE_LONGLONG: return 8;	/* Don't crash if no int64_t */
1766
  case DRIZZLE_TYPE_NULL	: return 0;
1767
  case DRIZZLE_TYPE_BLOB:		return 4+portable_sizeof_char_ptr;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1768
  case DRIZZLE_TYPE_ENUM:
1769
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
1770
    abort(); return 0;                          // This shouldn't happen
1771
  default:
1772
    return 0;
1773
  }
1774
}
1775
1776
438.1.13 by Brian Aker
uint cleanup.
1777
uint32_t pack_length_to_packflag(uint32_t type)
1 by brian
clean slate
1778
{
1779
  switch (type) {
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1780
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1781
    case 2: assert(1);
1782
    case 3: assert(1);
1783
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
1784
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1 by brian
clean slate
1785
  }
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1786
  return 0;					// This shouldn't happen
1 by brian
clean slate
1787
}
1788
1789
481 by Brian Aker
Remove all of uchar.
1790
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1791
		  unsigned char *null_pos, unsigned char null_bit,
1792
		  uint32_t pack_flag,
1793
		  enum_field_types field_type,
1794
		  const CHARSET_INFO * field_charset,
1795
		  Field::utype unireg_check,
1796
		  TYPELIB *interval,
1797
		  const char *field_name)
1 by brian
clean slate
1798
{
1799
  if (!f_maybe_null(pack_flag))
1800
  {
1801
    null_pos=0;
1802
    null_bit=0;
1803
  }
1804
  else
1805
  {
481 by Brian Aker
Remove all of uchar.
1806
    null_bit= ((unsigned char) 1) << null_bit;
1 by brian
clean slate
1807
  }
1808
1809
  switch (field_type) {
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
1810
  case DRIZZLE_TYPE_DATE:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1811
  case DRIZZLE_TYPE_TIME:
1812
  case DRIZZLE_TYPE_DATETIME:
1813
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
1814
    field_charset= &my_charset_bin;
1815
  default: break;
1816
  }
1817
1818
  if (f_is_alpha(pack_flag))
1819
  {
1820
    if (!f_is_packed(pack_flag))
1821
    {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1822
      if (field_type == DRIZZLE_TYPE_VARCHAR)
1 by brian
clean slate
1823
        return new Field_varstring(ptr,field_length,
1824
                                   HA_VARCHAR_PACKLENGTH(field_length),
1825
                                   null_pos,null_bit,
1826
                                   unireg_check, field_name,
1827
                                   share,
1828
                                   field_charset);
1829
      return 0;                                 // Error
1830
    }
1831
438.1.13 by Brian Aker
uint cleanup.
1832
    uint32_t pack_length=calc_pack_length((enum_field_types)
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1833
				      f_packtype(pack_flag),
1834
				      field_length);
1 by brian
clean slate
1835
1836
    if (f_is_blob(pack_flag))
1837
      return new Field_blob(ptr,null_pos,null_bit,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1838
			    unireg_check, field_name, share,
1839
			    pack_length, field_charset);
1 by brian
clean slate
1840
    if (interval)
1841
    {
1842
      if (f_is_enum(pack_flag))
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1843
{
1844
	return new Field_enum(ptr,field_length,null_pos,null_bit,
1845
				  unireg_check, field_name,
1846
				  get_enum_pack_length(interval->count),
1847
                                  interval, field_charset);
1848
}
1 by brian
clean slate
1849
    }
1850
  }
1851
1852
  switch (field_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1853
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
1854
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
1855
                                 unireg_check, field_name,
1856
                                 f_decimals(pack_flag),
216 by Brian Aker
Remove completely ZEROFILL
1857
                                 f_is_decimal_precision(pack_flag) != 0,
1 by brian
clean slate
1858
                                 f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1859
  case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
1860
    return new Field_double(ptr,field_length,null_pos,null_bit,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1861
			    unireg_check, field_name,
1862
			    f_decimals(pack_flag),
1863
			    false,
1864
			    f_is_dec(pack_flag)== 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1865
  case DRIZZLE_TYPE_TINY:
513 by Brian Aker
Removed Field_tiny class
1866
    assert(0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1867
  case DRIZZLE_TYPE_LONG:
1 by brian
clean slate
1868
    return new Field_long(ptr,field_length,null_pos,null_bit,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1869
			   unireg_check, field_name,
1870
                           false,
1871
			   f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1872
  case DRIZZLE_TYPE_LONGLONG:
152 by Brian Aker
longlong replacement
1873
    return new Field_int64_t(ptr,field_length,null_pos,null_bit,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1874
			      unireg_check, field_name,
1875
                              false,
1876
			      f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1877
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
1878
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
1879
                               unireg_check, field_name, share,
1880
                               field_charset);
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
1881
  case DRIZZLE_TYPE_DATE:
1882
    return new Field_date(ptr,null_pos,null_bit,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1883
			     unireg_check, field_name, field_charset);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1884
  case DRIZZLE_TYPE_TIME:
1 by brian
clean slate
1885
    return new Field_time(ptr,null_pos,null_bit,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1886
			  unireg_check, field_name, field_charset);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1887
  case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
1888
    return new Field_datetime(ptr,null_pos,null_bit,
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1889
			      unireg_check, field_name, field_charset);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1890
  case DRIZZLE_TYPE_NULL:
1 by brian
clean slate
1891
    return new Field_null(ptr, field_length, unireg_check, field_name,
1892
                          field_charset);
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1893
  case DRIZZLE_TYPE_VIRTUAL:                    // Must not happen
1894
    assert(0);
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1895
  default:					// Impossible (Wrong version)
1 by brian
clean slate
1896
    break;
1897
  }
1898
  return 0;
1899
}
1900
1901
1902
/** Create a field suitable for create of table. */
1903
1904
Create_field::Create_field(Field *old_field,Field *orig_field)
1905
{
1906
  field=      old_field;
1907
  field_name=change=old_field->field_name;
1908
  length=     old_field->field_length;
1909
  flags=      old_field->flags;
1910
  unireg_check=old_field->unireg_check;
1911
  pack_length=old_field->pack_length();
1912
  key_length= old_field->key_length();
1913
  sql_type=   old_field->real_type();
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1914
  charset=    old_field->charset();		// May be NULL ptr
1 by brian
clean slate
1915
  comment=    old_field->comment;
1916
  decimals=   old_field->decimals();
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1917
  vcol_info=  old_field->vcol_info;
1918
  is_stored= old_field->is_stored;
1 by brian
clean slate
1919
1920
  /* Fix if the original table had 4 byte pointer blobs */
1921
  if (flags & BLOB_FLAG)
1922
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1923
		  portable_sizeof_char_ptr);
1 by brian
clean slate
1924
1925
  switch (sql_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1926
  case DRIZZLE_TYPE_BLOB:
1927
    sql_type= DRIZZLE_TYPE_BLOB;
1 by brian
clean slate
1928
    length/= charset->mbmaxlen;
1929
    key_length/= charset->mbmaxlen;
1930
    break;
1931
    /* Change CHAR -> VARCHAR if dynamic record length */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1932
  case DRIZZLE_TYPE_ENUM:
1933
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
1934
    /* This is corrected in create_length_to_internal_length */
1935
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1936
    break;
1937
  default:
1938
    break;
1939
  }
1940
1941
  if (flags & (ENUM_FLAG | SET_FLAG))
1942
    interval= ((Field_enum*) old_field)->typelib;
1943
  else
1944
    interval=0;
1945
  def=0;
1946
  char_length= length;
1947
1948
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
1949
      old_field->ptr && orig_field &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1950
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1951
       old_field->table->timestamp_field != old_field ||  /* timestamp field */
1 by brian
clean slate
1952
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
1953
  {
1954
    char buff[MAX_FIELD_WIDTH];
1955
    String tmp(buff,sizeof(buff), charset);
1956
    my_ptrdiff_t diff;
1957
1958
    /* Get the value from default_values */
1959
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
1960
                          orig_field->table->record[0]);
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1961
    orig_field->move_field_offset(diff);	// Points now at default_values
1 by brian
clean slate
1962
    if (!orig_field->is_real_null())
1963
    {
1964
      char buff[MAX_FIELD_WIDTH], *pos;
1965
      String tmp(buff, sizeof(buff), charset), *res;
1966
      res= orig_field->val_str(&tmp);
1967
      pos= (char*) sql_strmake(res->ptr(), res->length());
1968
      def= new Item_string(pos, res->length(), charset);
1969
    }
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1970
    orig_field->move_field_offset(-diff);	// Back to record[0]
1 by brian
clean slate
1971
  }
1972
}
1973
1974
1975
/*****************************************************************************
1976
 Warning handling
1977
*****************************************************************************/
1978
1979
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
1980
  Produce warning or note about data saved into field.
1981
1982
  @param level            - level of message (Note/Warning/Error)
1983
  @param code             - error code of message to be produced
1984
  @param cuted_increment  - whenever we should increase cut fields count or not
1985
1986
  @note
1987
    This function won't produce warning and increase cut fields counter
1988
    if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
1989
1990
    if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
1991
    This allows us to avoid notes in optimisation, like convert_constant_item().
1992
1993
  @retval
1994
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
1995
  @retval
1996
    0 otherwise
1 by brian
clean slate
1997
*/
1998
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1999
bool
438.1.13 by Brian Aker
uint cleanup.
2000
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1 by brian
clean slate
2001
                   int cuted_increment)
2002
{
2003
  /*
2004
    If this field was created only for type conversion purposes it
2005
    will have table == NULL.
2006
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
2007
  Session *session= table ? table->in_use : current_session;
2008
  if (session->count_cuted_fields)
1 by brian
clean slate
2009
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
2010
    session->cuted_fields+= cuted_increment;
2011
    push_warning_printf(session, level, code, ER(code), field_name,
2012
                        session->row_count);
1 by brian
clean slate
2013
    return 0;
2014
  }
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
2015
  return level >= DRIZZLE_ERROR::WARN_LEVEL_WARN;
1 by brian
clean slate
2016
}
2017
2018
2019
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
2020
  Produce warning or note about datetime string data saved into field.
2021
2022
  @param level            level of message (Note/Warning/Error)
2023
  @param code             error code of message to be produced
2024
  @param str              string value which we tried to save
2025
  @param str_length       length of string which we tried to save
2026
  @param ts_type          type of datetime value (datetime/date/time)
2027
  @param cuted_increment  whenever we should increase cut fields count or not
2028
2029
  @note
2030
    This function will always produce some warning but won't increase cut
2031
    fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
2032
    thread.
1 by brian
clean slate
2033
*/
2034
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2035
void
2036
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
2037
                            unsigned int code,
2038
                            const char *str, uint32_t str_length,
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
2039
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
1 by brian
clean slate
2040
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2041
  Session *session= table ? table->in_use : current_session;
2042
  if ((session->really_abort_on_warning() &&
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
2043
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1 by brian
clean slate
2044
      set_warning(level, code, cuted_increment))
520.1.22 by Brian Aker
Second pass of thd cleanup
2045
    make_truncated_value_warning(session, level, str, str_length, ts_type,
1 by brian
clean slate
2046
                                 field_name);
2047
}
2048
2049
2050
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
2051
  Produce warning or note about integer datetime value saved into field.
2052
2053
  @param level            level of message (Note/Warning/Error)
2054
  @param code             error code of message to be produced
2055
  @param nr               numeric value which we tried to save
2056
  @param ts_type          type of datetime value (datetime/date/time)
2057
  @param cuted_increment  whenever we should increase cut fields count or not
2058
2059
  @note
2060
    This function will always produce some warning but won't increase cut
2061
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2062
    thread.
1 by brian
clean slate
2063
*/
2064
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2065
void
2066
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
2067
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
1 by brian
clean slate
2068
                            int cuted_increment)
2069
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2070
  Session *session= table ? table->in_use : current_session;
2071
  if (session->really_abort_on_warning() ||
1 by brian
clean slate
2072
      set_warning(level, code, cuted_increment))
2073
  {
2074
    char str_nr[22];
152 by Brian Aker
longlong replacement
2075
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2076
    make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1 by brian
clean slate
2077
                                 ts_type, field_name);
2078
  }
2079
}
2080
2081
2082
/**
520.1.28 by Monty Taylor
Reverted too-big formatting change.
2083
  Produce warning or note about double datetime data saved into field.
2084
2085
  @param level            level of message (Note/Warning/Error)
2086
  @param code             error code of message to be produced
2087
  @param nr               double value which we tried to save
2088
  @param ts_type          type of datetime value (datetime/date/time)
2089
2090
  @note
2091
    This function will always produce some warning but won't increase cut
2092
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2093
    thread.
1 by brian
clean slate
2094
*/
2095
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2096
void
2097
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, const uint32_t code,
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
2098
                            double nr, enum enum_drizzle_timestamp_type ts_type)
1 by brian
clean slate
2099
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2100
  Session *session= table ? table->in_use : current_session;
2101
  if (session->really_abort_on_warning() ||
1 by brian
clean slate
2102
      set_warning(level, code, 1))
2103
  {
2104
    /* DBL_DIG is enough to print '-[digits].E+###' */
2105
    char str_nr[DBL_DIG + 8];
438.1.13 by Brian Aker
uint cleanup.
2106
    uint32_t str_len= sprintf(str_nr, "%g", nr);
520.1.22 by Brian Aker
Second pass of thd cleanup
2107
    make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1 by brian
clean slate
2108
                                 field_name);
2109
  }
2110
}
575.4.7 by Monty Taylor
More header cleanup.
2111