~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

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