~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Brian Aker
  • Date: 2009-09-26 04:00:11 UTC
  • mfrom: (1126.12.1 trunk-nodebug)
  • Revision ID: brian@gaz-20090926040011-2qzxdcbpm1ibpkhl
Merge Lee

Show diffs side-by-side

added added

removed removed

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