~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/**
18
  @file
19
20
  @brief
21
  This file implements classes defined in field.h
22
*/
23
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation				// gcc: Class implementation
26
#endif
27
28
#include "mysql_priv.h"
29
#include "sql_select.h"
30
#include <errno.h>
31
32
// Maximum allowed exponent value for converting string to decimal
33
#define MAX_EXPONENT 1024
34
35
/*****************************************************************************
36
  Instansiate templates and static variables
37
*****************************************************************************/
38
39
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
40
template class List<Create_field>;
41
template class List_iterator<Create_field>;
42
#endif
43
44
45
/*
46
  Rules for merging different types of fields in UNION
47
48
  NOTE: to avoid 256*256 table, gap in table types numeration is skiped
49
  following #defines describe that gap and how to canculate number of fields
50
  and index of field in thia array.
51
*/
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
52
#define FIELDTYPE_TEAR_FROM (DRIZZLE_TYPE_VARCHAR + 1)
53
#define FIELDTYPE_TEAR_TO   (DRIZZLE_TYPE_NEWDECIMAL - 1)
1 by brian
clean slate
54
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
55
inline int field_type2index (enum_field_types field_type)
56
{
57
  return (field_type < FIELDTYPE_TEAR_FROM ?
58
          field_type :
59
          ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
60
}
61
62
63
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
64
{
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
65
  /* DRIZZLE_TYPE_DECIMAL -> */
66
  {
67
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
68
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
69
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
70
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
71
  //DRIZZLE_TYPE_DOUBLE
72
    DRIZZLE_TYPE_DOUBLE,
73
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
74
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
75
  //DRIZZLE_TYPE_LONGLONG
76
    DRIZZLE_TYPE_NEWDECIMAL,
77
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
78
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
79
  //DRIZZLE_TYPE_DATETIME
80
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
81
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
82
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
83
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
84
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
85
  //DRIZZLE_TYPE_SET
86
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
87
  //DRIZZLE_TYPE_BLOB
88
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
89
  },
90
  /* DRIZZLE_TYPE_TINY -> */
91
  {
92
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
93
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
94
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
95
    DRIZZLE_TYPE_SHORT,       DRIZZLE_TYPE_LONG,
96
  //DRIZZLE_TYPE_DOUBLE
97
    DRIZZLE_TYPE_DOUBLE,
98
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
99
    DRIZZLE_TYPE_TINY,        DRIZZLE_TYPE_VARCHAR,
100
  //DRIZZLE_TYPE_LONGLONG
101
    DRIZZLE_TYPE_LONGLONG,
102
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
103
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
104
  //DRIZZLE_TYPE_DATETIME
105
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
106
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
107
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
108
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
109
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
110
  //DRIZZLE_TYPE_SET
111
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
112
  //DRIZZLE_TYPE_BLOB
113
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
114
  },
115
  /* DRIZZLE_TYPE_SHORT -> */
116
  {
117
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
118
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_SHORT,
119
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
120
    DRIZZLE_TYPE_SHORT,       DRIZZLE_TYPE_LONG,
121
  //DRIZZLE_TYPE_DOUBLE
122
    DRIZZLE_TYPE_DOUBLE,
123
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
124
    DRIZZLE_TYPE_SHORT,       DRIZZLE_TYPE_VARCHAR,
125
  //DRIZZLE_TYPE_LONGLONG
126
    DRIZZLE_TYPE_LONGLONG,
127
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
128
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
129
  //DRIZZLE_TYPE_DATETIME
130
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
131
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
132
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
133
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
134
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
135
  //DRIZZLE_TYPE_SET
136
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
137
  //DRIZZLE_TYPE_BLOB
138
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
139
  },
140
  /* DRIZZLE_TYPE_LONG -> */
141
  {
142
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
143
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONG,
144
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
145
    DRIZZLE_TYPE_LONG,        DRIZZLE_TYPE_LONG,
146
  //DRIZZLE_TYPE_DOUBLE
147
    DRIZZLE_TYPE_DOUBLE,
148
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
149
    DRIZZLE_TYPE_LONG,         DRIZZLE_TYPE_VARCHAR,
150
  //DRIZZLE_TYPE_LONGLONG
151
    DRIZZLE_TYPE_LONGLONG,
152
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
153
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
154
  //DRIZZLE_TYPE_DATETIME
155
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
156
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
157
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
158
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
159
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
160
  //DRIZZLE_TYPE_SET
161
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
162
  //DRIZZLE_TYPE_BLOB
163
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
164
  },
165
  /* DRIZZLE_TYPE_DOUBLE -> */
166
  {
167
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
168
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_DOUBLE,
169
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
170
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_DOUBLE,
171
  //DRIZZLE_TYPE_DOUBLE
172
    DRIZZLE_TYPE_DOUBLE,
173
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
174
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
175
  //DRIZZLE_TYPE_LONGLONG
176
    DRIZZLE_TYPE_DOUBLE,
177
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
178
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
179
  //DRIZZLE_TYPE_DATETIME
180
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
181
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
182
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
183
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
184
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
185
  //DRIZZLE_TYPE_SET
186
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
187
  //DRIZZLE_TYPE_BLOB
188
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
189
  },
190
  /* DRIZZLE_TYPE_NULL -> */
191
  {
192
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
193
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
194
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
195
    DRIZZLE_TYPE_SHORT,       DRIZZLE_TYPE_LONG,
196
  //DRIZZLE_TYPE_DOUBLE
197
    DRIZZLE_TYPE_DOUBLE,
198
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
199
    DRIZZLE_TYPE_NULL,        DRIZZLE_TYPE_TIMESTAMP,
200
  //DRIZZLE_TYPE_LONGLONG
201
    DRIZZLE_TYPE_LONGLONG,
202
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
203
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_TIME,
222 by Brian Aker
Remove YEAR field type
204
  //DRIZZLE_TYPE_DATETIME
205
    DRIZZLE_TYPE_DATETIME,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
206
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
207
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
208
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
209
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_ENUM,
210
  //DRIZZLE_TYPE_SET
211
    DRIZZLE_TYPE_SET,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
212
  //DRIZZLE_TYPE_BLOB
213
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
214
  },
215
  /* DRIZZLE_TYPE_TIMESTAMP -> */
216
  {
217
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
218
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
219
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
220
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
221
  //DRIZZLE_TYPE_DOUBLE
222
    DRIZZLE_TYPE_VARCHAR,
223
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
224
    DRIZZLE_TYPE_TIMESTAMP,   DRIZZLE_TYPE_TIMESTAMP,
225
  //DRIZZLE_TYPE_LONGLONG
226
    DRIZZLE_TYPE_VARCHAR,
227
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
228
    DRIZZLE_TYPE_DATETIME,    DRIZZLE_TYPE_DATETIME,
222 by Brian Aker
Remove YEAR field type
229
  //DRIZZLE_TYPE_DATETIME
230
    DRIZZLE_TYPE_DATETIME,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
231
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
232
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
233
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
234
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
235
  //DRIZZLE_TYPE_SET
236
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
237
  //DRIZZLE_TYPE_BLOB
238
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
239
  },
240
  /* DRIZZLE_TYPE_LONGLONG -> */
241
  {
242
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
243
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONGLONG,
244
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
245
    DRIZZLE_TYPE_LONGLONG,    DRIZZLE_TYPE_LONGLONG,
246
  //DRIZZLE_TYPE_DOUBLE
247
    DRIZZLE_TYPE_DOUBLE,
248
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
249
    DRIZZLE_TYPE_LONGLONG,    DRIZZLE_TYPE_VARCHAR,
250
  //DRIZZLE_TYPE_LONGLONG
251
    DRIZZLE_TYPE_LONGLONG,
252
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
253
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
254
  //DRIZZLE_TYPE_DATETIME
255
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
256
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
257
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
258
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
259
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
260
  //DRIZZLE_TYPE_SET
261
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
262
  //DRIZZLE_TYPE_BLOB
263
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
264
  },
265
  /* DRIZZLE_TYPE_DATE -> */
266
  {
267
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
268
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
269
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
270
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
271
  //DRIZZLE_TYPE_DOUBLE
272
    DRIZZLE_TYPE_VARCHAR,
273
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
274
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_DATETIME,
275
  //DRIZZLE_TYPE_LONGLONG
276
    DRIZZLE_TYPE_VARCHAR,
277
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
278
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_DATETIME,
222 by Brian Aker
Remove YEAR field type
279
  //DRIZZLE_TYPE_DATETIME
280
    DRIZZLE_TYPE_DATETIME,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
281
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
282
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
283
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
284
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
285
  //DRIZZLE_TYPE_SET
286
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
287
  //DRIZZLE_TYPE_BLOB
288
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
289
  },
290
  /* DRIZZLE_TYPE_TIME -> */
291
  {
292
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
293
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
294
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
295
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
296
  //DRIZZLE_TYPE_DOUBLE
297
    DRIZZLE_TYPE_VARCHAR,
298
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
299
    DRIZZLE_TYPE_TIME,        DRIZZLE_TYPE_DATETIME,
300
  //DRIZZLE_TYPE_LONGLONG
301
    DRIZZLE_TYPE_VARCHAR,
302
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
303
    DRIZZLE_TYPE_DATETIME,    DRIZZLE_TYPE_TIME,
222 by Brian Aker
Remove YEAR field type
304
  //DRIZZLE_TYPE_DATETIME
305
    DRIZZLE_TYPE_DATETIME,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
306
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
307
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
308
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
309
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
310
  //DRIZZLE_TYPE_SET
311
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
312
  //DRIZZLE_TYPE_BLOB
313
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
314
  },
315
  /* DRIZZLE_TYPE_DATETIME -> */
316
  {
317
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
318
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
319
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
320
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
321
  //DRIZZLE_TYPE_DOUBLE
322
    DRIZZLE_TYPE_VARCHAR,
323
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
324
    DRIZZLE_TYPE_DATETIME,    DRIZZLE_TYPE_DATETIME,
325
  //DRIZZLE_TYPE_LONGLONG
326
    DRIZZLE_TYPE_VARCHAR,
327
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
328
    DRIZZLE_TYPE_DATETIME,    DRIZZLE_TYPE_DATETIME,
222 by Brian Aker
Remove YEAR field type
329
  //DRIZZLE_TYPE_DATETIME
330
    DRIZZLE_TYPE_DATETIME,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
331
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
332
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
333
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
334
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
335
  //DRIZZLE_TYPE_SET
336
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
337
  //DRIZZLE_TYPE_BLOB
338
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
339
  },
340
  /* DRIZZLE_TYPE_NEWDATE -> */
341
  {
342
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
343
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
344
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
345
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
346
  //DRIZZLE_TYPE_DOUBLE
347
    DRIZZLE_TYPE_VARCHAR,
348
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
349
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_DATETIME,
350
  //DRIZZLE_TYPE_LONGLONG
351
    DRIZZLE_TYPE_VARCHAR,
352
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
353
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_DATETIME,
222 by Brian Aker
Remove YEAR field type
354
  //DRIZZLE_TYPE_DATETIME
355
    DRIZZLE_TYPE_DATETIME,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
356
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
357
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
358
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
359
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
360
  //DRIZZLE_TYPE_SET
361
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
362
  //DRIZZLE_TYPE_BLOB
363
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
364
  },
365
  /* DRIZZLE_TYPE_VARCHAR -> */
366
  {
367
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
368
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
369
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
370
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
371
  //DRIZZLE_TYPE_DOUBLE
372
    DRIZZLE_TYPE_VARCHAR,
373
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
374
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
375
  //DRIZZLE_TYPE_LONGLONG
376
    DRIZZLE_TYPE_VARCHAR,
377
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
378
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
379
  //DRIZZLE_TYPE_DATETIME
380
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
381
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
382
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
383
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
384
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
385
  //DRIZZLE_TYPE_SET
386
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
387
  //DRIZZLE_TYPE_BLOB
388
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
389
  },
390
  /* DRIZZLE_TYPE_NEWDECIMAL -> */
391
  {
392
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
393
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
394
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
395
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
396
  //DRIZZLE_TYPE_DOUBLE
397
    DRIZZLE_TYPE_DOUBLE,
398
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
399
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
400
  //DRIZZLE_TYPE_LONGLONG
401
    DRIZZLE_TYPE_NEWDECIMAL,
402
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
403
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
404
  //DRIZZLE_TYPE_DATETIME
405
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
406
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
407
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
408
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
409
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
410
  //DRIZZLE_TYPE_SET
411
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
412
  //DRIZZLE_TYPE_BLOB
413
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
414
  },
415
  /* DRIZZLE_TYPE_ENUM -> */
416
  {
417
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
418
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
419
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
420
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
421
  //DRIZZLE_TYPE_DOUBLE
422
    DRIZZLE_TYPE_VARCHAR,
423
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
424
    DRIZZLE_TYPE_ENUM,        DRIZZLE_TYPE_VARCHAR,
425
  //DRIZZLE_TYPE_LONGLONG
426
    DRIZZLE_TYPE_VARCHAR,
427
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
428
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
429
  //DRIZZLE_TYPE_DATETIME
430
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
431
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
432
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
433
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
434
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
435
  //DRIZZLE_TYPE_SET
436
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
437
  //DRIZZLE_TYPE_BLOB
438
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
439
  },
440
  /* DRIZZLE_TYPE_SET -> */
441
  {
442
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
443
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
444
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
445
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
446
  //DRIZZLE_TYPE_DOUBLE
447
    DRIZZLE_TYPE_VARCHAR,
448
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
449
    DRIZZLE_TYPE_SET,         DRIZZLE_TYPE_VARCHAR,
450
  //DRIZZLE_TYPE_LONGLONG
451
    DRIZZLE_TYPE_VARCHAR,
452
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
453
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
222 by Brian Aker
Remove YEAR field type
454
  //DRIZZLE_TYPE_DATETIME
455
    DRIZZLE_TYPE_VARCHAR,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
456
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
457
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
458
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
459
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
460
  //DRIZZLE_TYPE_SET
461
    DRIZZLE_TYPE_VARCHAR,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
462
  //DRIZZLE_TYPE_BLOB
463
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
464
  },
465
  /* DRIZZLE_TYPE_BLOB -> */
466
  {
467
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
468
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
469
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
470
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
471
  //DRIZZLE_TYPE_DOUBLE
472
    DRIZZLE_TYPE_BLOB,
473
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
474
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
475
  //DRIZZLE_TYPE_LONGLONG
476
    DRIZZLE_TYPE_BLOB,
477
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
478
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
222 by Brian Aker
Remove YEAR field type
479
  //DRIZZLE_TYPE_DATETIME
480
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
481
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
482
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
483
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
484
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
485
  //DRIZZLE_TYPE_SET
486
    DRIZZLE_TYPE_BLOB,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
487
  //DRIZZLE_TYPE_BLOB
488
    DRIZZLE_TYPE_BLOB,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
489
  },
1 by brian
clean slate
490
};
491
492
/**
493
  Return type of which can carry value of both given types in UNION result.
494
495
  @param a  type for merging
496
  @param b  type for merging
497
498
  @return
499
    type of field
500
*/
501
502
enum_field_types Field::field_type_merge(enum_field_types a,
503
                                         enum_field_types b)
504
{
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
505
  assert(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
506
  assert(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
1 by brian
clean slate
507
  return field_types_merge_rules[field_type2index(a)]
508
                                [field_type2index(b)];
509
}
510
511
512
static Item_result field_types_result_type [FIELDTYPE_NUM]=
513
{
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
514
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
1 by brian
clean slate
515
  DECIMAL_RESULT,           INT_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
516
  //DRIZZLE_TYPE_SHORT        DRIZZLE_TYPE_LONG
1 by brian
clean slate
517
  INT_RESULT,               INT_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
518
  //DRIZZLE_TYPE_DOUBLE
166 by Brian Aker
Removal of FLOAT type
519
  REAL_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
520
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
1 by brian
clean slate
521
  STRING_RESULT,            STRING_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
522
  //DRIZZLE_TYPE_LONGLONG
167 by Brian Aker
Remove old 3byte int
523
  INT_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
524
  //DRIZZLE_TYPE_DATE         DRIZZLE_TYPE_TIME
1 by brian
clean slate
525
  STRING_RESULT,            STRING_RESULT,
222 by Brian Aker
Remove YEAR field type
526
  //DRIZZLE_TYPE_DATETIME
527
  STRING_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
528
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
1 by brian
clean slate
529
  STRING_RESULT,            STRING_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
530
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
1 by brian
clean slate
531
  DECIMAL_RESULT,           STRING_RESULT,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
532
  //DRIZZLE_TYPE_SET
120 by Brian Aker
Removed TINY BLOB.
533
  STRING_RESULT,
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
534
  //DRIZZLE_TYPE_BLOB
535
  STRING_RESULT,
1 by brian
clean slate
536
};
537
538
539
/*
540
  Test if the given string contains important data:
541
  not spaces for character string,
542
  or any data for binary string.
543
544
  SYNOPSIS
545
    test_if_important_data()
546
    cs          Character set
547
    str         String to test
548
    strend      String end
549
550
  RETURN
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
551
    false - If string does not have important data
552
    true  - If string has some important data
1 by brian
clean slate
553
*/
554
555
static bool
556
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
557
{
558
  if (cs != &my_charset_bin)
559
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
560
  return (str < strend);
561
}
562
563
564
/**
565
  Detect Item_result by given field type of UNION merge result.
566
567
  @param field_type  given field type
568
569
  @return
570
    Item_result (type of internal MySQL expression result)
571
*/
572
573
Item_result Field::result_merge_type(enum_field_types field_type)
574
{
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
575
  assert(field_type < FIELDTYPE_TEAR_FROM || field_type
1 by brian
clean slate
576
              > FIELDTYPE_TEAR_TO);
577
  return field_types_result_type[field_type2index(field_type)];
578
}
579
580
/*****************************************************************************
581
  Static help functions
582
*****************************************************************************/
583
584
585
/**
586
  Check whether a field type can be partially indexed by a key.
587
588
  This is a static method, rather than a virtual function, because we need
589
  to check the type of a non-Field in mysql_alter_table().
590
591
  @param type  field type
592
593
  @retval
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
594
    true  Type can have a prefixed key
1 by brian
clean slate
595
  @retval
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
596
    false Type can not have a prefixed key
1 by brian
clean slate
597
*/
598
599
bool Field::type_can_have_key_part(enum enum_field_types type)
600
{
601
  switch (type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
602
  case DRIZZLE_TYPE_VARCHAR:
603
  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
604
    return true;
1 by brian
clean slate
605
  default:
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
606
    return false;
1 by brian
clean slate
607
  }
608
}
609
610
611
/**
612
  Numeric fields base class constructor.
613
*/
205 by Brian Aker
uint32 -> uin32_t
614
Field_num::Field_num(uchar *ptr_arg,uint32_t len_arg, uchar *null_ptr_arg,
1 by brian
clean slate
615
                     uchar null_bit_arg, utype unireg_check_arg,
616
                     const char *field_name_arg,
206 by Brian Aker
Removed final uint dead types.
617
                     uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
1 by brian
clean slate
618
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
619
         unireg_check_arg, field_name_arg),
216 by Brian Aker
Remove completely ZEROFILL
620
  dec(dec_arg),decimal_precision(zero_arg),unsigned_flag(unsigned_arg)
1 by brian
clean slate
621
{
622
  if (unsigned_flag)
623
    flags|=UNSIGNED_FLAG;
624
}
625
626
627
/**
628
  Test if given number is a int.
629
630
  @todo
631
    Make this multi-byte-character safe
632
633
  @param str		String to test
634
  @param length        Length of 'str'
635
  @param int_end	Pointer to char after last used digit
636
  @param cs		Character set
637
638
  @note
639
    This is called after one has called strntoull10rnd() function.
640
641
  @retval
642
    0	OK
643
  @retval
644
    1	error: empty string or wrong integer.
645
  @retval
646
    2   error: garbage at the end of string.
647
*/
648
649
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
650
                         const char *int_end, int error)
651
{
652
  /* Test if we get an empty string or wrong integer */
653
  if (str == int_end || error == MY_ERRNO_EDOM)
654
  {
655
    char buff[128];
205 by Brian Aker
uint32 -> uin32_t
656
    String tmp(buff, (uint32_t) sizeof(buff), system_charset_info);
1 by brian
clean slate
657
    tmp.copy(str, length, system_charset_info);
658
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
659
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
660
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
661
                        "integer", tmp.c_ptr(), field_name,
662
                        (ulong) table->in_use->row_count);
663
    return 1;
664
  }
665
  /* Test if we have garbage at the end of the given string. */
666
  if (test_if_important_data(cs, int_end, str + length))
667
  {
212.5.42 by Monty Taylor
Ding dong include is dead.
668
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
669
    return 2;
670
  }
671
  return 0;
672
}
673
674
675
/*
676
  Conver a string to an integer then check bounds.
677
  
678
  SYNOPSIS
679
    Field_num::get_int
680
    cs            Character set
681
    from          String to convert
682
    len           Length of the string
152 by Brian Aker
longlong replacement
683
    rnd           OUT int64_t value
1 by brian
clean slate
684
    unsigned_max  max unsigned value
685
    signed_min    min signed value
686
    signed_max    max signed value
687
688
  DESCRIPTION
689
    The function calls strntoull10rnd() to get an integer value then
690
    check bounds and errors returned. In case of any error a warning
691
    is raised.
692
693
  RETURN
694
    0   ok
695
    1   error
696
*/
697
698
bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len,
152 by Brian Aker
longlong replacement
699
                        int64_t *rnd, uint64_t unsigned_max, 
700
                        int64_t signed_min, int64_t signed_max)
1 by brian
clean slate
701
{
702
  char *end;
703
  int error;
704
  
152 by Brian Aker
longlong replacement
705
  *rnd= (int64_t) cs->cset->strntoull10rnd(cs, from, len,
1 by brian
clean slate
706
                                            unsigned_flag, &end,
707
                                            &error);
708
  if (unsigned_flag)
709
  {
710
152 by Brian Aker
longlong replacement
711
    if ((((uint64_t) *rnd > unsigned_max) && (*rnd= (int64_t) unsigned_max)) ||
1 by brian
clean slate
712
        error == MY_ERRNO_ERANGE)
713
    {
714
      goto out_of_range;
715
    }
716
  }
717
  else
718
  {
719
    if (*rnd < signed_min)
720
    {
721
      *rnd= signed_min;
722
      goto out_of_range;
723
    }
724
    else if (*rnd > signed_max)
725
    {
726
      *rnd= signed_max;
727
      goto out_of_range;
728
    }
729
  }
730
  if (table->in_use->count_cuted_fields &&
731
      check_int(cs, from, len, end, error))
732
    return 1;
733
  return 0;
734
735
out_of_range:
736
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
737
  return 1;
738
}
739
740
/**
741
  Process decimal library return codes and issue warnings for overflow and
742
  truncation.
743
744
  @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
745
746
  @retval
747
    1  there was overflow
748
  @retval
749
    0  no error or some other errors except overflow
750
*/
751
752
int Field::warn_if_overflow(int op_result)
753
{
754
  if (op_result == E_DEC_OVERFLOW)
755
  {
756
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
757
    return 1;
758
  }
759
  if (op_result == E_DEC_TRUNCATED)
760
  {
212.5.42 by Monty Taylor
Ding dong include is dead.
761
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
762
    /* We return 0 here as this is not a critical issue */
763
  }
764
  return 0;
765
}
766
767
768
#ifdef NOT_USED
769
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
770
{
771
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
772
773
  while (length && my_isspace(cs,*str))
774
  {						// Allow start space
775
    length--; str++;
776
  }
777
  if (!length)
778
    return 0;
779
  if (*str == '+' || *str == '-')
780
  {
781
    length--; str++;
782
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
783
      return 0;
784
  }
785
  while (length && my_isdigit(cs,*str))
786
  {
787
    length--; str++;
788
  }
789
  if (!length)
790
    return 1;
791
  if (*str == '.')
792
  {
793
    length--; str++;
794
    while (length && my_isdigit(cs,*str))
795
    {
796
      length--; str++;
797
    }
798
  }
799
  if (!length)
800
    return 1;
801
  if (*str == 'E' || *str == 'e')
802
  {
803
    if (length < 3 || (str[1] != '+' && str[1] != '-') || 
804
        !my_isdigit(cs,str[2]))
805
      return 0;
806
    length-=3;
807
    str+=3;
808
    while (length && my_isdigit(cs,*str))
809
    {
810
      length--; str++;
811
    }
812
  }
813
  for (; length ; length--, str++)
814
  {						// Allow end space
815
    if (!my_isspace(cs,*str))
816
      return 0;
817
  }
818
  return 1;
819
}
820
#endif
821
822
823
/**
824
  Interpret field value as an integer but return the result as a string.
825
826
  This is used for printing bit_fields as numbers while debugging.
827
*/
828
829
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
830
{
831
  CHARSET_INFO *cs= &my_charset_bin;
832
  uint length;
152 by Brian Aker
longlong replacement
833
  int64_t value= val_int();
1 by brian
clean slate
834
835
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
836
    return 0;
152 by Brian Aker
longlong replacement
837
  length= (uint) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
1 by brian
clean slate
838
                                                MY_INT64_NUM_DECIMAL_DIGITS,
839
                                                unsigned_val ? 10 : -10,
840
                                                value);
841
  val_buffer->length(length);
842
  return val_buffer;
843
}
844
845
846
/// This is used as a table name when the table structure is not set up
205 by Brian Aker
uint32 -> uin32_t
847
Field::Field(uchar *ptr_arg,uint32_t length_arg,uchar *null_ptr_arg,
1 by brian
clean slate
848
	     uchar null_bit_arg,
849
	     utype unireg_check_arg, const char *field_name_arg)
850
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
851
   table(0), orig_table(0), table_name(0),
852
   field_name(field_name_arg),
853
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
854
   part_of_sortkey(0), unireg_check(unireg_check_arg),
855
   field_length(length_arg), null_bit(null_bit_arg), 
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
856
   is_created_from_null_item(false)
1 by brian
clean slate
857
{
858
  flags=null_ptr ? 0: NOT_NULL_FLAG;
859
  comment.str= (char*) "";
860
  comment.length=0;
861
  field_index= 0;
862
}
863
864
865
void Field::hash(ulong *nr, ulong *nr2)
866
{
867
  if (is_null())
868
  {
869
    *nr^= (*nr << 1) | 1;
870
  }
871
  else
872
  {
873
    uint len= pack_length();
874
    CHARSET_INFO *cs= charset();
875
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
876
  }
877
}
878
879
size_t
880
Field::do_last_null_byte() const
881
{
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
882
  assert(null_ptr == NULL || null_ptr >= table->record[0]);
1 by brian
clean slate
883
  if (null_ptr)
884
    return (size_t) (null_ptr - table->record[0]) + 1;
885
  return LAST_NULL_BYTE_UNDEF;
886
}
887
888
889
void Field::copy_from_tmp(int row_offset)
890
{
891
  memcpy(ptr,ptr+row_offset,pack_length());
892
  if (null_ptr)
893
  {
894
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | (null_ptr[row_offset] & (uchar) null_bit));
895
  }
896
}
897
898
899
bool Field::send_binary(Protocol *protocol)
900
{
901
  char buff[MAX_FIELD_WIDTH];
902
  String tmp(buff,sizeof(buff),charset());
903
  val_str(&tmp);
904
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
905
}
906
907
908
/**
909
   Check to see if field size is compatible with destination.
910
911
   This method is used in row-based replication to verify that the slave's
912
   field size is less than or equal to the master's field size. The 
913
   encoded field metadata (from the master or source) is decoded and compared
914
   to the size of this field (the slave or destination). 
915
916
   @param   field_metadata   Encoded size in field metadata
917
918
   @retval 0 if this field's size is < the source field's size
919
   @retval 1 if this field's size is >= the source field's size
920
*/
921
int Field::compatible_field_size(uint field_metadata)
922
{
923
  uint const source_size= pack_length_from_metadata(field_metadata);
924
  uint const destination_size= row_pack_length();
925
  return (source_size <= destination_size);
926
}
927
928
929
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
930
                 enum_check_fields check_level)
931
{
932
  int res;
933
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
934
  table->in_use->count_cuted_fields= check_level;
935
  res= store(to, length, cs);
936
  table->in_use->count_cuted_fields= old_check_level;
937
  return res;
938
}
939
940
941
/**
942
   Pack the field into a format suitable for storage and transfer.
943
944
   To implement packing functionality, only the virtual function
945
   should be overridden. The other functions are just convenience
946
   functions and hence should not be overridden.
947
948
   The value of <code>low_byte_first</code> is dependent on how the
949
   packed data is going to be used: for local use, e.g., temporary
950
   store on disk or in memory, use the native format since that is
951
   faster. For data that is going to be transfered to other machines
952
   (e.g., when writing data to the binary log), data should always be
953
   stored in little-endian format.
954
955
   @note The default method for packing fields just copy the raw bytes
956
   of the record into the destination, but never more than
957
   <code>max_length</code> characters.
958
959
   @param to
960
   Pointer to memory area where representation of field should be put.
961
962
   @param from
963
   Pointer to memory area where record representation of field is
964
   stored.
965
966
   @param max_length
967
   Maximum length of the field, as given in the column definition. For
968
   example, for <code>CHAR(1000)</code>, the <code>max_length</code>
969
   is 1000. This information is sometimes needed to decide how to pack
970
   the data.
971
972
   @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
973
   @c true if integers should be stored little-endian, @c false if
1 by brian
clean slate
974
   native format should be used. Note that for little-endian machines,
975
   the value of this flag is a moot point since the native format is
976
   little-endian.
977
*/
978
uchar *
979
Field::pack(uchar *to, const uchar *from, uint max_length,
980
            bool low_byte_first __attribute__((unused)))
981
{
205 by Brian Aker
uint32 -> uin32_t
982
  uint32_t length= pack_length();
1 by brian
clean slate
983
  set_if_smaller(length, max_length);
984
  memcpy(to, from, length);
985
  return to+length;
986
}
987
988
/**
989
   Unpack a field from row data.
990
991
   This method is used to unpack a field from a master whose size of
992
   the field is less than that of the slave.
993
994
   The <code>param_data</code> parameter is a two-byte integer (stored
995
   in the least significant 16 bits of the unsigned integer) usually
996
   consisting of two parts: the real type in the most significant byte
997
   and a original pack length in the least significant byte.
998
999
   The exact layout of the <code>param_data</code> field is given by
1000
   the <code>Table_map_log_event::save_field_metadata()</code>.
1001
1002
   This is the default method for unpacking a field. It just copies
1003
   the memory block in byte order (of original pack length bytes or
1004
   length of field, whichever is smaller).
1005
1006
   @param   to         Destination of the data
1007
   @param   from       Source of the data
1008
   @param   param_data Real type and original pack length of the field
1009
                       data
1010
1011
   @param low_byte_first
1012
   If this flag is @c true, all composite entities (e.g., lengths)
1013
   should be unpacked in little-endian format; otherwise, the entities
1014
   are unpacked in native order.
1015
1016
   @return  New pointer into memory based on from + length of the data
1017
*/
1018
const uchar *
1019
Field::unpack(uchar* to, const uchar *from, uint param_data,
1020
              bool low_byte_first __attribute__((unused)))
1021
{
1022
  uint length=pack_length();
1023
  int from_type= 0;
1024
  /*
1025
    If from length is > 255, it has encoded data in the upper bits. Need
1026
    to mask it out.
1027
  */
1028
  if (param_data > 255)
1029
  {
1030
    from_type= (param_data & 0xff00) >> 8U;  // real_type.
1031
    param_data= param_data & 0x00ff;        // length.
1032
  }
1033
1034
  if ((param_data == 0) ||
1035
      (length == param_data) ||
1036
      (from_type != real_type()))
1037
  {
1038
    memcpy(to, from, length);
1039
    return from+length;
1040
  }
1041
1042
  uint len= (param_data && (param_data < length)) ?
1043
            param_data : length;
1044
1045
  memcpy(to, from, param_data > length ? length : len);
1046
  return from+len;
1047
}
1048
1049
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1050
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((unused)))
1 by brian
clean slate
1051
{
1052
  /* 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
1053
  assert(0);
1 by brian
clean slate
1054
  return 0;
1055
}
1056
1057
216 by Brian Aker
Remove completely ZEROFILL
1058
void Field_num::add_unsigned(String &res) const
1 by brian
clean slate
1059
{
1060
  if (unsigned_flag)
1061
    res.append(STRING_WITH_LEN(" unsigned"));
1062
}
1063
1064
1065
void Field::make_field(Send_field *field)
1066
{
1067
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1068
  {
1069
    field->db_name= orig_table->s->db.str;
1070
    field->org_table_name= orig_table->s->table_name.str;
1071
  }
1072
  else
1073
    field->org_table_name= field->db_name= "";
1074
  if (orig_table)
1075
  {
1076
    field->table_name= orig_table->alias;
1077
    field->org_col_name= field_name;
1078
  }
1079
  else
1080
  {
1081
    field->table_name= "";
1082
    field->org_col_name= "";
1083
  }
1084
  field->col_name= field_name;
1085
  field->charsetnr= charset()->number;
1086
  field->length=field_length;
1087
  field->type=type();
1088
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1089
  field->decimals= 0;
1090
}
1091
1092
1093
/**
152 by Brian Aker
longlong replacement
1094
  Conversion from decimal to int64_t with checking overflow and
1 by brian
clean slate
1095
  setting correct value (min/max) in case of overflow.
1096
1097
  @param val             value which have to be converted
1098
  @param unsigned_flag   type of integer in which we convert val
1099
  @param err             variable to pass error code
1100
1101
  @return
1102
    value converted from val
1103
*/
152 by Brian Aker
longlong replacement
1104
int64_t Field::convert_decimal2int64_t(const my_decimal *val,
1 by brian
clean slate
1105
                                         bool unsigned_flag, int *err)
1106
{
152 by Brian Aker
longlong replacement
1107
  int64_t i;
1 by brian
clean slate
1108
  if (unsigned_flag)
1109
  {
1110
    if (val->sign())
1111
    {
1112
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1113
      i= 0;
1114
      *err= 1;
1115
    }
1116
    else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1117
                                           ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
1118
                                           val, true, &i)))
1 by brian
clean slate
1119
    {
152 by Brian Aker
longlong replacement
1120
      i= ~(int64_t) 0;
1 by brian
clean slate
1121
      *err= 1;
1122
    }
1123
  }
1124
  else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1125
                                         ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
1126
                                         val, false, &i)))
1 by brian
clean slate
1127
  {
163 by Brian Aker
Merge Monty's code.
1128
    i= (val->sign() ? INT64_MIN : INT64_MAX);
1 by brian
clean slate
1129
    *err= 1;
1130
  }
1131
  return i;
1132
}
1133
1134
1135
/**
1136
  Storing decimal in integer fields.
1137
1138
  @param val       value for storing
1139
1140
  @note
1141
    This method is used by all integer fields, real/decimal redefine it
1142
1143
  @retval
1144
    0     OK
1145
  @retval
1146
    !=0  error
1147
*/
1148
1149
int Field_num::store_decimal(const my_decimal *val)
1150
{
1151
  int err= 0;
152 by Brian Aker
longlong replacement
1152
  int64_t i= convert_decimal2int64_t(val, unsigned_flag, &err);
1 by brian
clean slate
1153
  return test(err | store(i, unsigned_flag));
1154
}
1155
1156
1157
/**
1158
  Return decimal value of integer field.
1159
1160
  @param decimal_value     buffer for storing decimal value
1161
1162
  @note
1163
    This method is used by all integer fields, real/decimal redefine it.
152 by Brian Aker
longlong replacement
1164
    All int64_t values fit in our decimal buffer which cal store 8*9=72
1 by brian
clean slate
1165
    digits of integer number
1166
1167
  @return
1168
    pointer to decimal buffer with value of field
1169
*/
1170
1171
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
1172
{
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
1173
  assert(result_type() == INT_RESULT);
152 by Brian Aker
longlong replacement
1174
  int64_t nr= val_int();
1 by brian
clean slate
1175
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1176
  return decimal_value;
1177
}
1178
1179
205 by Brian Aker
uint32 -> uin32_t
1180
Field_str::Field_str(uchar *ptr_arg,uint32_t len_arg, uchar *null_ptr_arg,
1 by brian
clean slate
1181
                     uchar null_bit_arg, utype unireg_check_arg,
1182
                     const char *field_name_arg, CHARSET_INFO *charset_arg)
1183
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1184
         unireg_check_arg, field_name_arg)
1185
{
1186
  field_charset= charset_arg;
1187
  if (charset_arg->state & MY_CS_BINSORT)
1188
    flags|=BINARY_FLAG;
1189
  field_derivation= DERIVATION_IMPLICIT;
1190
}
1191
1192
1193
void Field_num::make_field(Send_field *field)
1194
{
1195
  Field::make_field(field);
1196
  field->decimals= dec;
1197
}
1198
1199
/**
1200
  Decimal representation of Field_str.
1201
1202
  @param d         value for storing
1203
1204
  @note
1205
    Field_str is the base class for fields like Field_enum,
1206
    Field_date and some similar. Some dates use fraction and also
1207
    string value should be converted to floating point value according
1208
    our rules, so we use double to store value of decimal in string.
1209
1210
  @todo
1211
    use decimal2string?
1212
1213
  @retval
1214
    0     OK
1215
  @retval
1216
    !=0  error
1217
*/
1218
1219
int Field_str::store_decimal(const my_decimal *d)
1220
{
1221
  double val;
1222
  /* TODO: use decimal2string? */
1223
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
1224
                                            ~E_DEC_OVERFLOW, d, &val));
1225
  return err | store(val);
1226
}
1227
1228
1229
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
1230
{
152 by Brian Aker
longlong replacement
1231
  int64_t nr= val_int();
1 by brian
clean slate
1232
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
1233
  return decimal_value;
1234
}
1235
1236
1237
uint Field::fill_cache_field(CACHE_FIELD *copy)
1238
{
1239
  uint store_length;
1240
  copy->str=ptr;
1241
  copy->length=pack_length();
1242
  copy->blob_field=0;
1243
  if (flags & BLOB_FLAG)
1244
  {
1245
    copy->blob_field=(Field_blob*) this;
1246
    copy->strip=0;
1247
    copy->length-= table->s->blob_ptr_size;
1248
    return copy->length;
1249
  }
1250
  else
1251
  {
1252
    copy->strip=0;
1253
    store_length= 0;
1254
  }
1255
  return copy->length+ store_length;
1256
}
1257
1258
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1259
bool Field::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
1 by brian
clean slate
1260
{
1261
  char buff[40];
1262
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1263
  if (!(res=val_str(&tmp)) ||
1264
      str_to_datetime_with_warn(res->ptr(), res->length(),
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1265
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
1 by brian
clean slate
1266
    return 1;
1267
  return 0;
1268
}
1269
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1270
bool Field::get_time(DRIZZLE_TIME *ltime)
1 by brian
clean slate
1271
{
1272
  char buff[40];
1273
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1274
  if (!(res=val_str(&tmp)) ||
1275
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
1276
    return 1;
1277
  return 0;
1278
}
1279
1280
/**
1281
  This is called when storing a date in a string.
1282
1283
  @note
1284
    Needs to be changed if/when we want to support different time formats.
1285
*/
1286
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1287
int Field::store_time(DRIZZLE_TIME *ltime,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1288
                      timestamp_type type_arg __attribute__((unused)))
1 by brian
clean slate
1289
{
1290
  char buff[MAX_DATE_STRING_REP_LENGTH];
1291
  uint length= (uint) my_TIME_to_str(ltime, buff);
1292
  return store(buff, length, &my_charset_bin);
1293
}
1294
1295
1296
bool Field::optimize_range(uint idx, uint part)
1297
{
1298
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1299
}
1300
1301
1302
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
1303
                        bool keep_type __attribute__((unused)))
1304
{
1305
  Field *tmp;
1306
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1307
    return 0;
1308
1309
  if (tmp->table->maybe_null)
1310
    tmp->flags&= ~NOT_NULL_FLAG;
1311
  tmp->table= new_table;
1312
  tmp->key_start.init(0);
1313
  tmp->part_of_key.init(0);
1314
  tmp->part_of_sortkey.init(0);
1315
  tmp->unireg_check= Field::NONE;
216 by Brian Aker
Remove completely ZEROFILL
1316
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1 by brian
clean slate
1317
  tmp->reset_fields();
1318
  return tmp;
1319
}
1320
1321
1322
Field *Field::new_key_field(MEM_ROOT *root, struct st_table *new_table,
1323
                            uchar *new_ptr, uchar *new_null_ptr,
1324
                            uint new_null_bit)
1325
{
1326
  Field *tmp;
1327
  if ((tmp= new_field(root, new_table, table == new_table)))
1328
  {
1329
    tmp->ptr=      new_ptr;
1330
    tmp->null_ptr= new_null_ptr;
1331
    tmp->null_bit= new_null_bit;
1332
  }
1333
  return tmp;
1334
}
1335
1336
1337
/* This is used to generate a field in TABLE from TABLE_SHARE */
1338
1339
Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
1340
{
1341
  Field *tmp;
1342
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1343
  {
1344
    tmp->init(new_table);
1345
    tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
1346
                                           new_table->s->default_values));
1347
  }
1348
  return tmp;
1349
}
1350
1351
1352
/****************************************************************************
1353
** tiny int
1354
****************************************************************************/
1355
1356
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
1357
{
1358
  int error;
152 by Brian Aker
longlong replacement
1359
  int64_t rnd;
1 by brian
clean slate
1360
  
1361
  error= get_int(cs, from, len, &rnd, 255, -128, 127);
1362
  ptr[0]= unsigned_flag ? (char) (uint64_t) rnd : (char) rnd;
1363
  return error;
1364
}
1365
1366
1367
int Field_tiny::store(double nr)
1368
{
1369
  int error= 0;
1370
  nr=rint(nr);
1371
  if (unsigned_flag)
1372
  {
1373
    if (nr < 0.0)
1374
    {
1375
      *ptr=0;
1376
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1377
      error= 1;
1378
    }
1379
    else if (nr > 255.0)
1380
    {
1381
      *ptr=(char) 255;
1382
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1383
      error= 1;
1384
    }
1385
    else
1386
      *ptr=(char) nr;
1387
  }
1388
  else
1389
  {
1390
    if (nr < -128.0)
1391
    {
1392
      *ptr= (char) -128;
1393
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1394
      error= 1;
1395
    }
1396
    else if (nr > 127.0)
1397
    {
1398
      *ptr=127;
1399
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1400
      error= 1;
1401
    }
1402
    else
1403
      *ptr=(char) (int) nr;
1404
  }
1405
  return error;
1406
}
1407
1408
152 by Brian Aker
longlong replacement
1409
int Field_tiny::store(int64_t nr, bool unsigned_val)
1 by brian
clean slate
1410
{
1411
  int error= 0;
1412
1413
  if (unsigned_flag)
1414
  {
1415
    if (nr < 0 && !unsigned_val)
1416
    {
1417
      *ptr= 0;
1418
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1419
      error= 1;
1420
    }
1421
    else if ((uint64_t) nr > (uint64_t) 255)
1422
    {
1423
      *ptr= (char) 255;
1424
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1425
      error= 1;
1426
    }
1427
    else
1428
      *ptr=(char) nr;
1429
  }
1430
  else
1431
  {
1432
    if (nr < 0 && unsigned_val)
1433
      nr= 256;                                    // Generate overflow
1434
    if (nr < -128)
1435
    {
1436
      *ptr= (char) -128;
1437
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1438
      error= 1;
1439
    }
1440
    else if (nr > 127)
1441
    {
1442
      *ptr=127;
1443
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1444
      error= 1;
1445
    }
1446
    else
1447
      *ptr=(char) nr;
1448
  }
1449
  return error;
1450
}
1451
1452
1453
double Field_tiny::val_real(void)
1454
{
1455
  int tmp= unsigned_flag ? (int) ptr[0] :
1456
    (int) ((signed char*) ptr)[0];
1457
  return (double) tmp;
1458
}
1459
1460
152 by Brian Aker
longlong replacement
1461
int64_t Field_tiny::val_int(void)
1 by brian
clean slate
1462
{
1463
  int tmp= unsigned_flag ? (int) ptr[0] :
1464
    (int) ((signed char*) ptr)[0];
152 by Brian Aker
longlong replacement
1465
  return (int64_t) tmp;
1 by brian
clean slate
1466
}
1467
1468
1469
String *Field_tiny::val_str(String *val_buffer,
1470
			    String *val_ptr __attribute__((unused)))
1471
{
1472
  CHARSET_INFO *cs= &my_charset_bin;
1473
  uint length;
1474
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
1475
  val_buffer->alloc(mlength);
1476
  char *to=(char*) val_buffer->ptr();
1477
1478
  if (unsigned_flag)
1479
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
1480
					   (long) *ptr);
1481
  else
1482
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
1483
					   (long) *((signed char*) ptr));
1484
  
1485
  val_buffer->length(length);
216 by Brian Aker
Remove completely ZEROFILL
1486
1 by brian
clean slate
1487
  return val_buffer;
1488
}
1489
1490
bool Field_tiny::send_binary(Protocol *protocol)
1491
{
206 by Brian Aker
Removed final uint dead types.
1492
  return protocol->store_tiny((int64_t) (int8_t) ptr[0]);
1 by brian
clean slate
1493
}
1494
1495
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
1496
{
1497
  signed char a,b;
1498
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
1499
  if (unsigned_flag)
1500
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
1501
  return (a < b) ? -1 : (a > b) ? 1 : 0;
1502
}
1503
1504
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
1505
{
1506
  if (unsigned_flag)
1507
    *to= *ptr;
1508
  else
1509
    to[0] = (char) (ptr[0] ^ (uchar) 128);	/* Revers signbit */
1510
}
1511
1512
void Field_tiny::sql_type(String &res) const
1513
{
1514
  CHARSET_INFO *cs=res.charset();
1515
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1516
			  "tinyint(%d)",(int) field_length));
216 by Brian Aker
Remove completely ZEROFILL
1517
  add_unsigned(res);
1 by brian
clean slate
1518
}
1519
1520
1521
/*
1522
  Report "not well formed" or "cannot convert" error
1523
  after storing a character string info a field.
1524
1525
  SYNOPSIS
1526
    check_string_copy_error()
1527
    field                    - Field
1528
    well_formed_error_pos    - where not well formed data was first met
1529
    cannot_convert_error_pos - where a not-convertable character was first met
1530
    end                      - end of the string
1531
    cs                       - character set of the string
1532
1533
  NOTES
1534
    As of version 5.0 both cases return the same error:
1535
  
1536
      "Invalid string value: 'xxx' for column 't' at row 1"
1537
  
1538
  Future versions will possibly introduce a new error message:
1539
1540
      "Cannot convert character string: 'xxx' for column 't' at row 1"
1541
1542
  RETURN
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
1543
    false - If errors didn't happen
1544
    true  - If an error happened
1 by brian
clean slate
1545
*/
1546
173.1.1 by Toru Maesaka
ripped out blob from field.cc/.h and placed in field/
1547
bool
1 by brian
clean slate
1548
check_string_copy_error(Field_str *field,
1549
                        const char *well_formed_error_pos,
1550
                        const char *cannot_convert_error_pos,
1551
                        const char *end,
1552
                        CHARSET_INFO *cs)
1553
{
1554
  const char *pos, *end_orig;
1555
  char tmp[64], *t;
1556
  
1557
  if (!(pos= well_formed_error_pos) &&
1558
      !(pos= cannot_convert_error_pos))
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
1559
    return false;
1 by brian
clean slate
1560
1561
  end_orig= end;
1562
  set_if_smaller(end, pos + 6);
1563
1564
  for (t= tmp; pos < end; pos++)
1565
  {
1566
    /*
1567
      If the source string is ASCII compatible (mbminlen==1)
1568
      and the source character is in ASCII printable range (0x20..0x7F),
1569
      then display the character as is.
1570
      
1571
      Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
1572
      or the source character is not in the printable range,
1573
      then print the character using HEX notation.
1574
    */
1575
    if (((unsigned char) *pos) >= 0x20 &&
1576
        ((unsigned char) *pos) <= 0x7F &&
1577
        cs->mbminlen == 1)
1578
    {
1579
      *t++= *pos;
1580
    }
1581
    else
1582
    {
1583
      *t++= '\\';
1584
      *t++= 'x';
1585
      *t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
1586
      *t++= _dig_vec_upper[((unsigned char) *pos) & 15];
1587
    }
1588
  }
1589
  if (end_orig > end)
1590
  {
1591
    *t++= '.';
1592
    *t++= '.';
1593
    *t++= '.';
1594
  }
1595
  *t= '\0';
1596
  push_warning_printf(field->table->in_use, 
1597
                      field->table->in_use->abort_on_warning ?
1598
                      MYSQL_ERROR::WARN_LEVEL_ERROR :
1599
                      MYSQL_ERROR::WARN_LEVEL_WARN,
1600
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
1601
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1602
                      "string", tmp, field->field_name,
1603
                      (ulong) field->table->in_use->row_count);
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
1604
  return true;
1 by brian
clean slate
1605
}
1606
1607
1608
/*
1609
  Check if we lost any important data and send a truncation error/warning
1610
1611
  SYNOPSIS
1612
    Field_longstr::report_if_important_data()
1613
    ptr                      - Truncated rest of string
1614
    end                      - End of truncated string
1615
1616
  RETURN VALUES
1617
    0   - None was truncated (or we don't count cut fields)
1618
    2   - Some bytes was truncated
1619
1620
  NOTE
1621
    Check if we lost any important data (anything in a binary string,
1622
    or any non-space in others). If only trailing spaces was lost,
1623
    send a truncation note, otherwise send a truncation error.
1624
*/
1625
1626
int
1627
Field_longstr::report_if_important_data(const char *ptr, const char *end)
1628
{
1629
  if ((ptr < end) && table->in_use->count_cuted_fields)
1630
  {
1631
    if (test_if_important_data(field_charset, ptr, end))
1632
    {
1633
      if (table->in_use->abort_on_warning)
1634
        set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
1635
      else
212.5.42 by Monty Taylor
Ding dong include is dead.
1636
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
1637
    }
1638
    else /* If we lost only spaces then produce a NOTE, not a WARNING */
212.5.42 by Monty Taylor
Ding dong include is dead.
1639
      set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
1640
    return 2;
1641
  }
1642
  return 0;
1643
}
1644
1645
1646
/**
241 by Brian Aker
First pass of CHAR removal.
1647
  Store double value in Field_varstring.
1 by brian
clean slate
1648
1649
  Pretty prints double number into field_length characters buffer.
1650
1651
  @param nr            number
1652
*/
1653
1654
int Field_str::store(double nr)
1655
{
1656
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
1657
  uint local_char_length= field_length / charset()->mbmaxlen;
1658
  size_t length;
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
1659
  bool error;
1 by brian
clean slate
1660
1661
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
1662
  if (error)
1663
  {
1664
    if (table->in_use->abort_on_warning)
1665
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
1666
    else
212.5.42 by Monty Taylor
Ding dong include is dead.
1667
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
1668
  }
1669
  return store(buff, length, charset());
1670
}
1671
1672
1673
uint Field::is_equal(Create_field *new_field)
1674
{
1675
  return (new_field->sql_type == real_type());
1676
}
1677
1678
1679
/* If one of the fields is binary and the other one isn't return 1 else 0 */
1680
205 by Brian Aker
uint32 -> uin32_t
1681
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32_t flag_arg)
1 by brian
clean slate
1682
{
1683
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
1684
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
1685
         (!(new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
1686
          (flag_arg & (BINCMP_FLAG | BINARY_FLAG))));
1687
}
1688
1689
1690
uint Field_str::is_equal(Create_field *new_field)
1691
{
1692
  if (compare_str_field_flags(new_field, flags))
1693
    return 0;
1694
1695
  return ((new_field->sql_type == real_type()) &&
1696
	  new_field->charset == field_charset &&
1697
	  new_field->length == max_display_length());
1698
}
1699
1700
1701
int Field_longstr::store_decimal(const my_decimal *d)
1702
{
1703
  char buff[DECIMAL_MAX_STR_LENGTH+1];
1704
  String str(buff, sizeof(buff), &my_charset_bin);
1705
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
1706
  return store(str.ptr(), str.length(), str.charset());
1707
}
1708
205 by Brian Aker
uint32 -> uin32_t
1709
uint32_t Field_longstr::max_data_length() const
1 by brian
clean slate
1710
{
1711
  return field_length + (field_length > 255 ? 2 : 1);
1712
}
1713
1714
1715
/****************************************************************************
1716
** enum type.
1717
** This is a string which only can have a selection of different values.
1718
** If one uses this string in a number context one gets the type number.
1719
****************************************************************************/
1720
1721
enum ha_base_keytype Field_enum::key_type() const
1722
{
1723
  switch (packlength) {
1724
  default: return HA_KEYTYPE_BINARY;
1725
  case 2: return HA_KEYTYPE_USHORT_INT;
1726
  case 3: return HA_KEYTYPE_UINT24;
1727
  case 4: return HA_KEYTYPE_ULONG_INT;
1728
  case 8: return HA_KEYTYPE_ULONGLONG;
1729
  }
1730
}
1731
1732
void Field_enum::store_type(uint64_t value)
1733
{
1734
  switch (packlength) {
1735
  case 1: ptr[0]= (uchar) value;  break;
1736
  case 2:
1737
#ifdef WORDS_BIGENDIAN
1738
  if (table->s->db_low_byte_first)
1739
  {
1740
    int2store(ptr,(unsigned short) value);
1741
  }
1742
  else
1743
#endif
1744
    shortstore(ptr,(unsigned short) value);
1745
  break;
1746
  case 3: int3store(ptr,(long) value); break;
1747
  case 4:
1748
#ifdef WORDS_BIGENDIAN
1749
  if (table->s->db_low_byte_first)
1750
  {
1751
    int4store(ptr,value);
1752
  }
1753
  else
1754
#endif
1755
    longstore(ptr,(long) value);
1756
  break;
1757
  case 8:
1758
#ifdef WORDS_BIGENDIAN
1759
  if (table->s->db_low_byte_first)
1760
  {
1761
    int8store(ptr,value);
1762
  }
1763
  else
1764
#endif
152 by Brian Aker
longlong replacement
1765
    int64_tstore(ptr,value); break;
1 by brian
clean slate
1766
  }
1767
}
1768
1769
1770
/**
1771
  @note
1772
    Storing a empty string in a enum field gives a warning
1773
    (if there isn't a empty value in the enum)
1774
*/
1775
1776
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
1777
{
1778
  int err= 0;
205 by Brian Aker
uint32 -> uin32_t
1779
  uint32_t not_used;
1 by brian
clean slate
1780
  char buff[STRING_BUFFER_USUAL_SIZE];
1781
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
1782
1783
  /* Convert character set if necessary */
1784
  if (String::needs_conversion(length, cs, field_charset, &not_used))
1785
  { 
1786
    uint dummy_errors;
1787
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
1788
    from= tmpstr.ptr();
1789
    length=  tmpstr.length();
1790
  }
1791
1792
  /* Remove end space */
1793
  length= field_charset->cset->lengthsp(field_charset, from, length);
1794
  uint tmp=find_type2(typelib, from, length, field_charset);
1795
  if (!tmp)
1796
  {
1797
    if (length < 6) // Can't be more than 99999 enums
1798
    {
1799
      /* This is for reading numbers with LOAD DATA INFILE */
1800
      char *end;
1801
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
1802
      if (err || end != from+length || tmp > typelib->count)
1803
      {
1804
	tmp=0;
212.5.42 by Monty Taylor
Ding dong include is dead.
1805
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
1806
      }
1807
      if (!table->in_use->count_cuted_fields)
1808
        err= 0;
1809
    }
1810
    else
212.5.42 by Monty Taylor
Ding dong include is dead.
1811
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
1812
  }
1813
  store_type((uint64_t) tmp);
1814
  return err;
1815
}
1816
1817
1818
int Field_enum::store(double nr)
1819
{
152 by Brian Aker
longlong replacement
1820
  return Field_enum::store((int64_t) nr, false);
1 by brian
clean slate
1821
}
1822
1823
152 by Brian Aker
longlong replacement
1824
int Field_enum::store(int64_t nr,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1825
                      bool unsigned_val __attribute__((unused)))
1 by brian
clean slate
1826
{
1827
  int error= 0;
1828
  if ((uint64_t) nr > typelib->count || nr == 0)
1829
  {
212.5.42 by Monty Taylor
Ding dong include is dead.
1830
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
1831
    if (nr != 0 || table->in_use->count_cuted_fields)
1832
    {
1833
      nr= 0;
1834
      error= 1;
1835
    }
1836
  }
1837
  store_type((uint64_t) (uint) nr);
1838
  return error;
1839
}
1840
1841
1842
double Field_enum::val_real(void)
1843
{
1844
  return (double) Field_enum::val_int();
1845
}
1846
1847
152 by Brian Aker
longlong replacement
1848
int64_t Field_enum::val_int(void)
1 by brian
clean slate
1849
{
1850
  switch (packlength) {
1851
  case 1:
152 by Brian Aker
longlong replacement
1852
    return (int64_t) ptr[0];
1 by brian
clean slate
1853
  case 2:
1854
  {
206 by Brian Aker
Removed final uint dead types.
1855
    uint16_t tmp;
1 by brian
clean slate
1856
#ifdef WORDS_BIGENDIAN
1857
    if (table->s->db_low_byte_first)
1858
      tmp=sint2korr(ptr);
1859
    else
1860
#endif
1861
      shortget(tmp,ptr);
152 by Brian Aker
longlong replacement
1862
    return (int64_t) tmp;
1 by brian
clean slate
1863
  }
1864
  case 3:
152 by Brian Aker
longlong replacement
1865
    return (int64_t) uint3korr(ptr);
1 by brian
clean slate
1866
  case 4:
1867
  {
205 by Brian Aker
uint32 -> uin32_t
1868
    uint32_t tmp;
1 by brian
clean slate
1869
#ifdef WORDS_BIGENDIAN
1870
    if (table->s->db_low_byte_first)
1871
      tmp=uint4korr(ptr);
1872
    else
1873
#endif
1874
      longget(tmp,ptr);
152 by Brian Aker
longlong replacement
1875
    return (int64_t) tmp;
1 by brian
clean slate
1876
  }
1877
  case 8:
1878
  {
152 by Brian Aker
longlong replacement
1879
    int64_t tmp;
1 by brian
clean slate
1880
#ifdef WORDS_BIGENDIAN
1881
    if (table->s->db_low_byte_first)
1882
      tmp=sint8korr(ptr);
1883
    else
1884
#endif
152 by Brian Aker
longlong replacement
1885
      int64_tget(tmp,ptr);
1 by brian
clean slate
1886
    return tmp;
1887
  }
1888
  }
1889
  return 0;					// impossible
1890
}
1891
1892
1893
/**
1894
   Save the field metadata for enum fields.
1895
1896
   Saves the real type in the first byte and the pack length in the 
1897
   second byte of the field metadata array at index of *metadata_ptr and
1898
   *(metadata_ptr + 1).
1899
1900
   @param   metadata_ptr   First byte of field metadata
1901
1902
   @returns number of bytes written to metadata_ptr
1903
*/
1904
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
1905
{
1906
  *metadata_ptr= real_type();
1907
  *(metadata_ptr + 1)= pack_length();
1908
  return 2;
1909
}
1910
1911
1912
String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
1913
			    String *val_ptr)
1914
{
1915
  uint tmp=(uint) Field_enum::val_int();
1916
  if (!tmp || tmp > typelib->count)
1917
    val_ptr->set("", 0, field_charset);
1918
  else
1919
    val_ptr->set((const char*) typelib->type_names[tmp-1],
1920
		 typelib->type_lengths[tmp-1],
1921
		 field_charset);
1922
  return val_ptr;
1923
}
1924
1925
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
1926
{
1927
  uchar *old= ptr;
1928
  ptr= (uchar*) a_ptr;
1929
  uint64_t a=Field_enum::val_int();
1930
  ptr= (uchar*) b_ptr;
1931
  uint64_t b=Field_enum::val_int();
1932
  ptr= old;
1933
  return (a < b) ? -1 : (a > b) ? 1 : 0;
1934
}
1935
1936
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
1937
{
1938
  uint64_t value=Field_enum::val_int();
1939
  to+=packlength-1;
1940
  for (uint i=0 ; i < packlength ; i++)
1941
  {
1942
    *to-- = (uchar) (value & 255);
1943
    value>>=8;
1944
  }
1945
}
1946
1947
1948
void Field_enum::sql_type(String &res) const
1949
{
1950
  char buffer[255];
1951
  String enum_item(buffer, sizeof(buffer), res.charset());
1952
1953
  res.length(0);
1954
  res.append(STRING_WITH_LEN("enum("));
1955
1956
  bool flag=0;
1957
  uint *len= typelib->type_lengths;
1958
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
1959
  {
1960
    uint dummy_errors;
1961
    if (flag)
1962
      res.append(',');
1963
    /* convert to res.charset() == utf8, then quote */
1964
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
1965
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
1966
    flag= 1;
1967
  }
1968
  res.append(')');
1969
}
1970
1971
1972
Field *Field_enum::new_field(MEM_ROOT *root, struct st_table *new_table,
1973
                             bool keep_type)
1974
{
1975
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
1976
  if (res)
1977
    res->typelib= copy_typelib(root, typelib);
1978
  return res;
1979
}
1980
1981
1982
/**
1983
  @retval
1984
    1  if the fields are equally defined
1985
  @retval
1986
    0  if the fields are unequally defined
1987
*/
1988
1989
bool Field::eq_def(Field *field)
1990
{
1991
  if (real_type() != field->real_type() || charset() != field->charset() ||
1992
      pack_length() != field->pack_length())
1993
    return 0;
1994
  return 1;
1995
}
1996
1997
/**
1998
  @return
1999
  returns 1 if the fields are equally defined
2000
*/
2001
bool Field_enum::eq_def(Field *field)
2002
{
2003
  if (!Field::eq_def(field))
2004
    return 0;
2005
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
2006
2007
  if (typelib->count < from_lib->count)
2008
    return 0;
2009
  for (uint i=0 ; i < from_lib->count ; i++)
2010
    if (my_strnncoll(field_charset,
2011
                     (const uchar*)typelib->type_names[i],
2012
                     strlen(typelib->type_names[i]),
2013
                     (const uchar*)from_lib->type_names[i],
2014
                     strlen(from_lib->type_names[i])))
2015
      return 0;
2016
  return 1;
2017
}
2018
2019
/**
2020
  @return
2021
  returns 1 if the fields are equally defined
2022
*/
2023
bool Field_num::eq_def(Field *field)
2024
{
2025
  if (!Field::eq_def(field))
2026
    return 0;
2027
  Field_num *from_num= (Field_num*) field;
2028
2029
  if (unsigned_flag != from_num->unsigned_flag ||
2030
      dec != from_num->dec)
2031
    return 0;
2032
  return 1;
2033
}
2034
2035
2036
uint Field_num::is_equal(Create_field *new_field)
2037
{
2038
  return ((new_field->sql_type == real_type()) &&
2039
	  ((new_field->flags & UNSIGNED_FLAG) == (uint) (flags &
2040
							 UNSIGNED_FLAG)) &&
2041
	  ((new_field->flags & AUTO_INCREMENT_FLAG) ==
2042
	   (uint) (flags & AUTO_INCREMENT_FLAG)) &&
2043
	  (new_field->length <= max_display_length()));
2044
}
2045
2046
2047
/*****************************************************************************
2048
  Handling of field and Create_field
2049
*****************************************************************************/
2050
2051
/**
2052
  Convert create_field::length from number of characters to number of bytes.
2053
*/
2054
2055
void Create_field::create_length_to_internal_length(void)
2056
{
2057
  switch (sql_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2058
  case DRIZZLE_TYPE_BLOB:
2059
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
2060
    length*= charset->mbmaxlen;
2061
    key_length= length;
2062
    pack_length= calc_pack_length(sql_type, length);
2063
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2064
  case DRIZZLE_TYPE_ENUM:
2065
  case DRIZZLE_TYPE_SET:
1 by brian
clean slate
2066
    /* Pack_length already calculated in sql_parse.cc */
2067
    length*= charset->mbmaxlen;
2068
    key_length= pack_length;
2069
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2070
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
2071
    key_length= pack_length=
2072
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
2073
								decimals,
2074
								flags &
2075
								UNSIGNED_FLAG),
2076
				 decimals);
2077
    break;
2078
  default:
2079
    key_length= pack_length= calc_pack_length(sql_type, length);
2080
    break;
2081
  }
2082
}
2083
2084
2085
/**
2086
  Init for a tmp table field. To be extended if need be.
2087
*/
2088
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
205 by Brian Aker
uint32 -> uin32_t
2089
                                      uint32_t length_arg, uint32_t decimals_arg,
1 by brian
clean slate
2090
                                      bool maybe_null, bool is_unsigned)
2091
{
2092
  field_name= "";
2093
  sql_type= sql_type_arg;
2094
  char_length= length= length_arg;;
2095
  unireg_check= Field::NONE;
2096
  interval= 0;
2097
  charset= &my_charset_bin;
2098
  pack_flag= (FIELDFLAG_NUMBER |
2099
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
2100
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
2101
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
2102
}
2103
2104
2105
/**
2106
  Initialize field definition for create.
2107
2108
  @param thd                   Thread handle
2109
  @param fld_name              Field name
2110
  @param fld_type              Field type
2111
  @param fld_length            Field length
2112
  @param fld_decimals          Decimal (if any)
2113
  @param fld_type_modifier     Additional type information
2114
  @param fld_default_value     Field default value (if any)
2115
  @param fld_on_update_value   The value of ON UPDATE clause
2116
  @param fld_comment           Field comment
2117
  @param fld_change            Field change
2118
  @param fld_interval_list     Interval list (if any)
2119
  @param fld_charset           Field charset
2120
2121
  @retval
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
2122
    false on success
1 by brian
clean slate
2123
  @retval
51.1.56 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on field.h/cc
2124
    true  on error
1 by brian
clean slate
2125
*/
2126
2127
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
2128
                        char *fld_length, char *fld_decimals,
2129
                        uint fld_type_modifier, Item *fld_default_value,
2130
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
2131
                        char *fld_change, List<String> *fld_interval_list,
77.1.45 by Monty Taylor
Warning fixes.
2132
                        CHARSET_INFO *fld_charset,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2133
                        uint fld_geom_type __attribute__((unused)),
1 by brian
clean slate
2134
                        enum column_format_type column_format)
2135
{
2136
  uint sign_len, allowed_type_modifier= 0;
2137
  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
2138
2139
  field= 0;
2140
  field_name= fld_name;
2141
  def= fld_default_value;
2142
  flags= fld_type_modifier;
2143
  flags|= (((uint)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
2144
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
2145
                 Field::NEXT_NUMBER : Field::NONE);
2146
  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
2147
  if (decimals >= NOT_FIXED_DEC)
2148
  {
2149
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
2150
             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
2151
    return(true);
1 by brian
clean slate
2152
  }
2153
2154
  sql_type= fld_type;
2155
  length= 0;
2156
  change= fld_change;
2157
  interval= 0;
2158
  pack_length= key_length= 0;
2159
  charset= fld_charset;
2160
  interval_list.empty();
2161
2162
  comment= *fld_comment;
2163
  /*
2164
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
2165
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
2166
  */
2167
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2168
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1 by brian
clean slate
2169
    flags|= NO_DEFAULT_VALUE_FLAG;
2170
2171
  if (fld_length && !(length= (uint) atoi(fld_length)))
2172
    fld_length= 0; /* purecov: inspected */
2173
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
2174
2175
  switch (fld_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2176
  case DRIZZLE_TYPE_TINY:
1 by brian
clean slate
2177
    if (!fld_length)
2178
      length= MAX_TINYINT_WIDTH+sign_len;
2179
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
2180
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2181
  case DRIZZLE_TYPE_SHORT:
1 by brian
clean slate
2182
    if (!fld_length)
2183
      length= MAX_SMALLINT_WIDTH+sign_len;
2184
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
2185
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2186
  case DRIZZLE_TYPE_LONG:
1 by brian
clean slate
2187
    if (!fld_length)
2188
      length= MAX_INT_WIDTH+sign_len;
2189
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
2190
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2191
  case DRIZZLE_TYPE_LONGLONG:
1 by brian
clean slate
2192
    if (!fld_length)
2193
      length= MAX_BIGINT_WIDTH;
2194
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
2195
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2196
  case DRIZZLE_TYPE_NULL:
1 by brian
clean slate
2197
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2198
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
2199
    my_decimal_trim(&length, &decimals);
2200
    if (length > DECIMAL_MAX_PRECISION)
2201
    {
2202
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
2203
               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
2204
      return(true);
1 by brian
clean slate
2205
    }
2206
    if (length < decimals)
2207
    {
2208
      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
2209
      return(true);
1 by brian
clean slate
2210
    }
2211
    length=
2212
      my_decimal_precision_to_length(length, decimals,
2213
                                     fld_type_modifier & UNSIGNED_FLAG);
2214
    pack_length=
2215
      my_decimal_get_binary_size(length, decimals);
2216
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2217
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
2218
    /*
2219
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
2220
      if they don't have a default value
2221
    */
2222
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
2223
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2224
  case DRIZZLE_TYPE_BLOB:
1 by brian
clean slate
2225
    if (fld_default_value)
2226
    {
2227
      /* Allow empty as default value. */
2228
      String str,*res;
2229
      res= fld_default_value->val_str(&str);
2230
      /*
2231
        A default other than '' is always an error, and any non-NULL
2232
        specified default is an error in strict mode.
2233
      */
2234
      if (res->length() || (thd->variables.sql_mode &
2235
                            (MODE_STRICT_TRANS_TABLES |
2236
                             MODE_STRICT_ALL_TABLES)))
2237
      {
2238
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
2239
                 fld_name); /* 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
2240
        return(true);
1 by brian
clean slate
2241
      }
2242
      else
2243
      {
2244
        /*
2245
          Otherwise a default of '' is just a warning.
2246
        */
2247
        push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2248
                            ER_BLOB_CANT_HAVE_DEFAULT,
2249
                            ER(ER_BLOB_CANT_HAVE_DEFAULT),
2250
                            fld_name);
2251
      }
2252
      def= 0;
2253
    }
2254
    flags|= BLOB_FLAG;
2255
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2256
  case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
2257
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
2258
    if (!fld_length && !fld_decimals)
2259
    {
2260
      length= DBL_DIG+7;
2261
      decimals= NOT_FIXED_DEC;
2262
    }
2263
    if (length < decimals &&
2264
        decimals != NOT_FIXED_DEC)
2265
    {
2266
      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
2267
      return(true);
1 by brian
clean slate
2268
    }
2269
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2270
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
2271
    if (!fld_length)
2272
    {
2273
      /* Compressed date YYYYMMDDHHMMSS */
2274
      length= MAX_DATETIME_COMPRESSED_WIDTH;
2275
    }
2276
    else if (length != MAX_DATETIME_WIDTH)
2277
    {
2278
      /*
2279
        We support only even TIMESTAMP lengths less or equal than 14
2280
        and 19 as length of 4.1 compatible representation.
2281
      */
2282
      length= ((length+1)/2)*2; /* purecov: inspected */
2283
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
2284
    }
216 by Brian Aker
Remove completely ZEROFILL
2285
    flags|= UNSIGNED_FLAG;
1 by brian
clean slate
2286
    if (fld_default_value)
2287
    {
2288
      /* Grammar allows only NOW() value for ON UPDATE clause */
2289
      if (fld_default_value->type() == Item::FUNC_ITEM && 
2290
          ((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
2291
      {
2292
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
2293
                                             Field::TIMESTAMP_DN_FIELD);
2294
        /*
2295
          We don't need default value any longer moreover it is dangerous.
2296
          Everything handled by unireg_check further.
2297
        */
2298
        def= 0;
2299
      }
2300
      else
2301
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
2302
                                             Field::NONE);
2303
    }
2304
    else
2305
    {
2306
      /*
2307
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
2308
        or ON UPDATE values then for the sake of compatiblity we should treat
2309
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
2310
        have another TIMESTAMP column with auto-set option before this one)
2311
        or DEFAULT 0 (in other cases).
2312
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
2313
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
2314
        information about all TIMESTAMP fields in table will be availiable.
2315
2316
        If we have TIMESTAMP NULL column without explicit DEFAULT value
2317
        we treat it as having DEFAULT NULL attribute.
2318
      */
2319
      unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
2320
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
2321
                                              Field::NONE));
2322
    }
2323
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2324
  case DRIZZLE_TYPE_DATE:
1 by brian
clean slate
2325
    /* Old date type. */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2326
    sql_type= DRIZZLE_TYPE_NEWDATE;
1 by brian
clean slate
2327
    /* fall trough */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2328
  case DRIZZLE_TYPE_NEWDATE:
2329
    length= 10;
2330
    break;
2331
  case DRIZZLE_TYPE_TIME:
2332
    length= 10;
2333
    break;
2334
  case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
2335
    length= MAX_DATETIME_WIDTH;
2336
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2337
  case DRIZZLE_TYPE_SET:
1 by brian
clean slate
2338
    {
2339
      pack_length= get_set_pack_length(fld_interval_list->elements);
2340
2341
      List_iterator<String> it(*fld_interval_list);
2342
      String *tmp;
2343
      while ((tmp= it++))
2344
        interval_list.push_back(tmp);
2345
      /*
2346
        Set fake length to 1 to pass the below conditions.
2347
        Real length will be set in mysql_prepare_table()
2348
        when we know the character set of the column
2349
      */
2350
      length= 1;
2351
      break;
2352
    }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2353
  case DRIZZLE_TYPE_ENUM:
1 by brian
clean slate
2354
    {
2355
      /* Should be safe. */
2356
      pack_length= get_enum_pack_length(fld_interval_list->elements);
2357
2358
      List_iterator<String> it(*fld_interval_list);
2359
      String *tmp;
2360
      while ((tmp= it++))
2361
        interval_list.push_back(tmp);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2362
      length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1 by brian
clean slate
2363
      break;
2364
   }
2365
  }
2366
  /* Remember the value of length */
2367
  char_length= length;
2368
2369
  if (!(flags & BLOB_FLAG) &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2370
      ((length > max_field_charlength && fld_type != DRIZZLE_TYPE_SET &&
2371
        fld_type != DRIZZLE_TYPE_ENUM &&
2372
        (fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
241 by Brian Aker
First pass of CHAR removal.
2373
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1 by brian
clean slate
2374
  {
241 by Brian Aker
First pass of CHAR removal.
2375
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
1 by brian
clean slate
2376
              MYF(0),
2377
              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
2378
    return(true);
1 by brian
clean slate
2379
  }
2380
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
2381
  if ((~allowed_type_modifier) & fld_type_modifier)
2382
  {
2383
    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
2384
    return(true);
1 by brian
clean slate
2385
  }
2386
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
2387
  return(false); /* success */
1 by brian
clean slate
2388
}
2389
2390
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2391
enum_field_types get_blob_type_from_length(ulong length __attribute__((unused)))
1 by brian
clean slate
2392
{
2393
  enum_field_types type;
114 by Brian Aker
More cleanup around Drizzle types.
2394
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2395
  type= DRIZZLE_TYPE_BLOB;
114 by Brian Aker
More cleanup around Drizzle types.
2396
1 by brian
clean slate
2397
  return type;
2398
}
2399
2400
2401
/*
2402
  Make a field from the .frm file info
2403
*/
2404
205 by Brian Aker
uint32 -> uin32_t
2405
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1 by brian
clean slate
2406
{
2407
  switch (type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2408
  case DRIZZLE_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
2409
  case DRIZZLE_TYPE_TINY	: return 1;
2410
  case DRIZZLE_TYPE_SHORT : return 2;
2411
  case DRIZZLE_TYPE_DATE:
2412
  case DRIZZLE_TYPE_NEWDATE:
2413
  case DRIZZLE_TYPE_TIME:   return 3;
2414
  case DRIZZLE_TYPE_TIMESTAMP:
2415
  case DRIZZLE_TYPE_LONG	: return 4;
2416
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
2417
  case DRIZZLE_TYPE_DATETIME:
2418
  case DRIZZLE_TYPE_LONGLONG: return 8;	/* Don't crash if no int64_t */
2419
  case DRIZZLE_TYPE_NULL	: return 0;
2420
  case DRIZZLE_TYPE_BLOB:		return 4+portable_sizeof_char_ptr;
2421
  case DRIZZLE_TYPE_SET:
2422
  case DRIZZLE_TYPE_ENUM:
2423
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
2424
    abort(); return 0;                          // This shouldn't happen
2425
  default:
2426
    return 0;
2427
  }
2428
}
2429
2430
2431
uint pack_length_to_packflag(uint type)
2432
{
2433
  switch (type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2434
    case 1: return f_settype((uint) DRIZZLE_TYPE_TINY);
2435
    case 2: return f_settype((uint) DRIZZLE_TYPE_SHORT);
121 by Brian Aker
More INT24 removal
2436
    case 3: assert(1);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2437
    case 4: return f_settype((uint) DRIZZLE_TYPE_LONG);
2438
    case 8: return f_settype((uint) DRIZZLE_TYPE_LONGLONG);
1 by brian
clean slate
2439
  }
2440
  return 0;					// This shouldn't happen
2441
}
2442
2443
205 by Brian Aker
uint32 -> uin32_t
2444
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32_t field_length,
1 by brian
clean slate
2445
		  uchar *null_pos, uchar null_bit,
2446
		  uint pack_flag,
2447
		  enum_field_types field_type,
2448
		  CHARSET_INFO *field_charset,
2449
		  Field::utype unireg_check,
2450
		  TYPELIB *interval,
2451
		  const char *field_name)
2452
{
2453
  if (!f_maybe_null(pack_flag))
2454
  {
2455
    null_pos=0;
2456
    null_bit=0;
2457
  }
2458
  else
2459
  {
2460
    null_bit= ((uchar) 1) << null_bit;
2461
  }
2462
2463
  switch (field_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2464
  case DRIZZLE_TYPE_DATE:
2465
  case DRIZZLE_TYPE_NEWDATE:
2466
  case DRIZZLE_TYPE_TIME:
2467
  case DRIZZLE_TYPE_DATETIME:
2468
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
2469
    field_charset= &my_charset_bin;
2470
  default: break;
2471
  }
2472
2473
  if (f_is_alpha(pack_flag))
2474
  {
2475
    if (!f_is_packed(pack_flag))
2476
    {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2477
      if (field_type == DRIZZLE_TYPE_VARCHAR)
1 by brian
clean slate
2478
        return new Field_varstring(ptr,field_length,
2479
                                   HA_VARCHAR_PACKLENGTH(field_length),
2480
                                   null_pos,null_bit,
2481
                                   unireg_check, field_name,
2482
                                   share,
2483
                                   field_charset);
2484
      return 0;                                 // Error
2485
    }
2486
2487
    uint pack_length=calc_pack_length((enum_field_types)
2488
				      f_packtype(pack_flag),
2489
				      field_length);
2490
2491
    if (f_is_blob(pack_flag))
2492
      return new Field_blob(ptr,null_pos,null_bit,
2493
			    unireg_check, field_name, share,
2494
			    pack_length, field_charset);
2495
    if (interval)
2496
    {
2497
      if (f_is_enum(pack_flag))
2498
	return new Field_enum(ptr,field_length,null_pos,null_bit,
2499
				  unireg_check, field_name,
2500
				  pack_length, interval, field_charset);
2501
      else
2502
	return new Field_set(ptr,field_length,null_pos,null_bit,
2503
			     unireg_check, field_name,
2504
			     pack_length, interval, field_charset);
2505
    }
2506
  }
2507
2508
  switch (field_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2509
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
2510
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
2511
                                 unireg_check, field_name,
2512
                                 f_decimals(pack_flag),
216 by Brian Aker
Remove completely ZEROFILL
2513
                                 f_is_decimal_precision(pack_flag) != 0,
1 by brian
clean slate
2514
                                 f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2515
  case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
2516
    return new Field_double(ptr,field_length,null_pos,null_bit,
2517
			    unireg_check, field_name,
2518
			    f_decimals(pack_flag),
216 by Brian Aker
Remove completely ZEROFILL
2519
			    false,
1 by brian
clean slate
2520
			    f_is_dec(pack_flag)== 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2521
  case DRIZZLE_TYPE_TINY:
1 by brian
clean slate
2522
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
2523
			  unireg_check, field_name,
216 by Brian Aker
Remove completely ZEROFILL
2524
                          false,
1 by brian
clean slate
2525
			  f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2526
  case DRIZZLE_TYPE_SHORT:
1 by brian
clean slate
2527
    return new Field_short(ptr,field_length,null_pos,null_bit,
2528
			   unireg_check, field_name,
216 by Brian Aker
Remove completely ZEROFILL
2529
                           false,
1 by brian
clean slate
2530
			   f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2531
  case DRIZZLE_TYPE_LONG:
1 by brian
clean slate
2532
    return new Field_long(ptr,field_length,null_pos,null_bit,
2533
			   unireg_check, field_name,
216 by Brian Aker
Remove completely ZEROFILL
2534
                           false,
1 by brian
clean slate
2535
			   f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2536
  case DRIZZLE_TYPE_LONGLONG:
152 by Brian Aker
longlong replacement
2537
    return new Field_int64_t(ptr,field_length,null_pos,null_bit,
1 by brian
clean slate
2538
			      unireg_check, field_name,
216 by Brian Aker
Remove completely ZEROFILL
2539
                              false,
1 by brian
clean slate
2540
			      f_is_dec(pack_flag) == 0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2541
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
2542
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
2543
                               unireg_check, field_name, share,
2544
                               field_charset);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2545
  case DRIZZLE_TYPE_DATE:
2546
  case DRIZZLE_TYPE_NEWDATE:
1 by brian
clean slate
2547
    return new Field_newdate(ptr,null_pos,null_bit,
2548
			     unireg_check, field_name, field_charset);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2549
  case DRIZZLE_TYPE_TIME:
1 by brian
clean slate
2550
    return new Field_time(ptr,null_pos,null_bit,
2551
			  unireg_check, field_name, field_charset);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2552
  case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
2553
    return new Field_datetime(ptr,null_pos,null_bit,
2554
			      unireg_check, field_name, field_charset);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2555
  case DRIZZLE_TYPE_NULL:
1 by brian
clean slate
2556
    return new Field_null(ptr, field_length, unireg_check, field_name,
2557
                          field_charset);
2558
  default:					// Impossible (Wrong version)
2559
    break;
2560
  }
2561
  return 0;
2562
}
2563
2564
2565
/** Create a field suitable for create of table. */
2566
2567
Create_field::Create_field(Field *old_field,Field *orig_field)
2568
{
2569
  field=      old_field;
2570
  field_name=change=old_field->field_name;
2571
  length=     old_field->field_length;
2572
  flags=      old_field->flags;
2573
  unireg_check=old_field->unireg_check;
2574
  pack_length=old_field->pack_length();
2575
  key_length= old_field->key_length();
2576
  sql_type=   old_field->real_type();
2577
  charset=    old_field->charset();		// May be NULL ptr
2578
  comment=    old_field->comment;
2579
  decimals=   old_field->decimals();
2580
2581
  /* Fix if the original table had 4 byte pointer blobs */
2582
  if (flags & BLOB_FLAG)
2583
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
2584
		  portable_sizeof_char_ptr);
2585
2586
  switch (sql_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2587
  case DRIZZLE_TYPE_BLOB:
2588
    sql_type= DRIZZLE_TYPE_BLOB;
1 by brian
clean slate
2589
    length/= charset->mbmaxlen;
2590
    key_length/= charset->mbmaxlen;
2591
    break;
2592
    /* Change CHAR -> VARCHAR if dynamic record length */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2593
  case DRIZZLE_TYPE_ENUM:
2594
  case DRIZZLE_TYPE_SET:
2595
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
2596
    /* This is corrected in create_length_to_internal_length */
2597
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
2598
    break;
2599
  default:
2600
    break;
2601
  }
2602
2603
  if (flags & (ENUM_FLAG | SET_FLAG))
2604
    interval= ((Field_enum*) old_field)->typelib;
2605
  else
2606
    interval=0;
2607
  def=0;
2608
  char_length= length;
2609
2610
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
2611
      old_field->ptr && orig_field &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2612
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
1 by brian
clean slate
2613
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
2614
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
2615
  {
2616
    char buff[MAX_FIELD_WIDTH];
2617
    String tmp(buff,sizeof(buff), charset);
2618
    my_ptrdiff_t diff;
2619
2620
    /* Get the value from default_values */
2621
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
2622
                          orig_field->table->record[0]);
2623
    orig_field->move_field_offset(diff);	// Points now at default_values
2624
    if (!orig_field->is_real_null())
2625
    {
2626
      char buff[MAX_FIELD_WIDTH], *pos;
2627
      String tmp(buff, sizeof(buff), charset), *res;
2628
      res= orig_field->val_str(&tmp);
2629
      pos= (char*) sql_strmake(res->ptr(), res->length());
2630
      def= new Item_string(pos, res->length(), charset);
2631
    }
2632
    orig_field->move_field_offset(-diff);	// Back to record[0]
2633
  }
2634
}
2635
2636
2637
/*****************************************************************************
2638
 Warning handling
2639
*****************************************************************************/
2640
2641
/**
2642
  Produce warning or note about data saved into field.
2643
2644
  @param level            - level of message (Note/Warning/Error)
2645
  @param code             - error code of message to be produced
2646
  @param cuted_increment  - whenever we should increase cut fields count or not
2647
2648
  @note
2649
    This function won't produce warning and increase cut fields counter
2650
    if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
2651
2652
    if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
2653
    This allows us to avoid notes in optimisation, like convert_constant_item().
2654
2655
  @retval
2656
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
2657
  @retval
2658
    0 otherwise
2659
*/
2660
2661
bool 
2662
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
2663
                   int cuted_increment)
2664
{
2665
  /*
2666
    If this field was created only for type conversion purposes it
2667
    will have table == NULL.
2668
  */
2669
  THD *thd= table ? table->in_use : current_thd;
2670
  if (thd->count_cuted_fields)
2671
  {
2672
    thd->cuted_fields+= cuted_increment;
2673
    push_warning_printf(thd, level, code, ER(code), field_name,
2674
                        thd->row_count);
2675
    return 0;
2676
  }
2677
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
2678
}
2679
2680
2681
/**
2682
  Produce warning or note about datetime string data saved into field.
2683
2684
  @param level            level of message (Note/Warning/Error)
2685
  @param code             error code of message to be produced
2686
  @param str              string value which we tried to save
2687
  @param str_length       length of string which we tried to save
2688
  @param ts_type          type of datetime value (datetime/date/time)
2689
  @param cuted_increment  whenever we should increase cut fields count or not
2690
2691
  @note
2692
    This function will always produce some warning but won't increase cut
2693
    fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
2694
    thread.
2695
*/
2696
2697
void 
212.5.42 by Monty Taylor
Ding dong include is dead.
2698
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, 
2699
                            unsigned int code, 
1 by brian
clean slate
2700
                            const char *str, uint str_length, 
2701
                            timestamp_type ts_type, int cuted_increment)
2702
{
2703
  THD *thd= table ? table->in_use : current_thd;
2704
  if ((thd->really_abort_on_warning() &&
2705
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
2706
      set_warning(level, code, cuted_increment))
2707
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
2708
                                 field_name);
2709
}
2710
2711
2712
/**
2713
  Produce warning or note about integer datetime value saved into field.
2714
2715
  @param level            level of message (Note/Warning/Error)
2716
  @param code             error code of message to be produced
2717
  @param nr               numeric value which we tried to save
2718
  @param ts_type          type of datetime value (datetime/date/time)
2719
  @param cuted_increment  whenever we should increase cut fields count or not
2720
2721
  @note
2722
    This function will always produce some warning but won't increase cut
2723
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2724
    thread.
2725
*/
2726
2727
void 
2728
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
152 by Brian Aker
longlong replacement
2729
                            int64_t nr, timestamp_type ts_type,
1 by brian
clean slate
2730
                            int cuted_increment)
2731
{
2732
  THD *thd= table ? table->in_use : current_thd;
2733
  if (thd->really_abort_on_warning() ||
2734
      set_warning(level, code, cuted_increment))
2735
  {
2736
    char str_nr[22];
152 by Brian Aker
longlong replacement
2737
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
1 by brian
clean slate
2738
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
2739
                                 ts_type, field_name);
2740
  }
2741
}
2742
2743
2744
/**
2745
  Produce warning or note about double datetime data saved into field.
2746
2747
  @param level            level of message (Note/Warning/Error)
2748
  @param code             error code of message to be produced
2749
  @param nr               double value which we tried to save
2750
  @param ts_type          type of datetime value (datetime/date/time)
2751
2752
  @note
2753
    This function will always produce some warning but won't increase cut
2754
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2755
    thread.
2756
*/
2757
2758
void 
2759
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
2760
                            double nr, timestamp_type ts_type)
2761
{
2762
  THD *thd= table ? table->in_use : current_thd;
2763
  if (thd->really_abort_on_warning() ||
2764
      set_warning(level, code, 1))
2765
  {
2766
    /* DBL_DIG is enough to print '-[digits].E+###' */
2767
    char str_nr[DBL_DIG + 8];
171.1.1 by Patrick Galbraith
Dar, I forgot to commit this earlier.
2768
    uint str_len= sprintf(str_nr, "%g", nr);
1 by brian
clean slate
2769
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
2770
                                 field_name);
2771
  }
2772
}