~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/field.cc

MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

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