~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
17
/**
18
  @file
19
20
  @brief
21
  Functions to copy data to or from fields
22
23
    This could be done with a single short function but opencoding this
24
    gives much more speed.
25
*/
26
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
27
#include "config.h"
550 by Monty Taylor
Moved error.h into just the files that need it.
28
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
29
#include <drizzled/table.h>
30
#include <drizzled/session.h>
1 by brian
clean slate
31
584.5.1 by Monty Taylor
Removed field includes from field.h.
32
#include <drizzled/field/str.h>
33
#include <drizzled/field/num.h>
34
#include <drizzled/field/blob.h>
35
#include <drizzled/field/enum.h>
36
#include <drizzled/field/null.h>
37
#include <drizzled/field/date.h>
670.1.1 by Monty Taylor
Renamed fdecimal.* to decimal.*. Let's see how many things we can break!
38
#include <drizzled/field/decimal.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
39
#include <drizzled/field/real.h>
40
#include <drizzled/field/double.h>
2007 by Brian Aker
Refactor naming for integers.
41
#include <drizzled/field/int32.h>
42
#include <drizzled/field/int64.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
43
#include <drizzled/field/num.h>
1999.4.9 by Brian Aker
Created EPOCH
44
#include <drizzled/field/epoch.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
45
#include <drizzled/field/datetime.h>
46
#include <drizzled/field/varstring.h>
47
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
48
namespace drizzled
49
{
584.5.1 by Monty Taylor
Removed field includes from field.h.
50
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
51
static void do_field_eq(CopyField *copy)
1 by brian
clean slate
52
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
53
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
54
}
55
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
56
static void do_field_1(CopyField *copy)
1 by brian
clean slate
57
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
58
  copy->to_ptr[0]= copy->from_ptr[0];
1 by brian
clean slate
59
}
60
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
61
static void do_field_2(CopyField *copy)
1 by brian
clean slate
62
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
63
  copy->to_ptr[0]= copy->from_ptr[0];
64
  copy->to_ptr[1]= copy->from_ptr[1];
1 by brian
clean slate
65
}
66
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
67
static void do_field_3(CopyField *copy)
1 by brian
clean slate
68
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
69
  copy->to_ptr[0]= copy->from_ptr[0];
70
  copy->to_ptr[1]= copy->from_ptr[1];
71
  copy->to_ptr[2]= copy->from_ptr[2];
1 by brian
clean slate
72
}
73
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
74
static void do_field_4(CopyField *copy)
1 by brian
clean slate
75
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
76
  copy->to_ptr[0]= copy->from_ptr[0];
77
  copy->to_ptr[1]= copy->from_ptr[1];
78
  copy->to_ptr[2]= copy->from_ptr[2];
79
  copy->to_ptr[3]= copy->from_ptr[3];
1 by brian
clean slate
80
}
81
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
82
static void do_field_6(CopyField *copy)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
83
{                  // For blob field
84
  copy->to_ptr[0]= copy->from_ptr[0];
85
  copy->to_ptr[1]= copy->from_ptr[1];
86
  copy->to_ptr[2]= copy->from_ptr[2];
87
  copy->to_ptr[3]= copy->from_ptr[3];
88
  copy->to_ptr[4]= copy->from_ptr[4];
89
  copy->to_ptr[5]= copy->from_ptr[5];
1 by brian
clean slate
90
}
91
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
92
static void do_field_8(CopyField *copy)
1 by brian
clean slate
93
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
94
  copy->to_ptr[0]= copy->from_ptr[0];
95
  copy->to_ptr[1]= copy->from_ptr[1];
96
  copy->to_ptr[2]= copy->from_ptr[2];
97
  copy->to_ptr[3]= copy->from_ptr[3];
98
  copy->to_ptr[4]= copy->from_ptr[4];
99
  copy->to_ptr[5]= copy->from_ptr[5];
100
  copy->to_ptr[6]= copy->from_ptr[6];
101
  copy->to_ptr[7]= copy->from_ptr[7];
1 by brian
clean slate
102
}
103
104
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
105
static void do_field_to_null_str(CopyField *copy)
1 by brian
clean slate
106
{
107
  if (*copy->from_null_ptr & copy->from_bit)
108
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
109
    memset(copy->to_ptr, 0, copy->from_length);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
110
    copy->to_null_ptr[0]= 1;  // Always bit 1
1 by brian
clean slate
111
  }
112
  else
113
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
114
    copy->to_null_ptr[0]= 0;
115
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
116
  }
117
}
118
119
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
120
static void do_outer_field_to_null_str(CopyField *copy)
1 by brian
clean slate
121
{
122
  if (*copy->null_row ||
123
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
124
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
125
    memset(copy->to_ptr, 0, copy->from_length);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
126
    copy->to_null_ptr[0]= 1;  // Always bit 1
1 by brian
clean slate
127
  }
128
  else
129
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
130
    copy->to_null_ptr[0]= 0;
131
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
132
  }
133
}
134
135
136
int
137
set_field_to_null(Field *field)
138
{
139
  if (field->real_maybe_null())
140
  {
141
    field->set_null();
142
    field->reset();
143
    return 0;
144
  }
145
  field->reset();
1660.1.3 by Brian Aker
Encapsulate Table in field
146
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
685.4.2 by Jay Pipes
Added back truncation warnings. The NULL error is triggered from: session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL, not CHECK_FIELD_WARN
147
  {
148
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
149
    return 0;
150
  }
1660.1.3 by Brian Aker
Encapsulate Table in field
151
  if (!field->getTable()->in_use->no_errors)
1 by brian
clean slate
152
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
153
  return -1;
154
}
155
156
157
/**
158
  Set field to NULL or TIMESTAMP or to next auto_increment number.
159
160
  @param field           Field to update
161
  @param no_conversions  Set to 1 if we should return 1 if field can't
162
                         take null values.
163
                         If set to 0 we will do store the 'default value'
164
                         if the field is a special field. If not we will
165
                         give an error.
166
167
  @retval
168
    0    Field could take 0 or an automatic conversion was used
169
  @retval
170
    -1   Field could not take NULL and no conversion was used.
171
    If no_conversion was not set, an error message is printed
172
*/
173
174
int
175
set_field_to_null_with_conversions(Field *field, bool no_conversions)
176
{
177
  if (field->real_maybe_null())
178
  {
179
    field->set_null();
180
    field->reset();
181
    return 0;
182
  }
1999.4.9 by Brian Aker
Created EPOCH
183
1 by brian
clean slate
184
  if (no_conversions)
185
    return -1;
186
187
  /*
188
    Check if this is a special type, which will get a special walue
189
    when set to NULL (TIMESTAMP fields which allow setting to NULL
190
    are handled by first check).
191
  */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
192
  if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
1 by brian
clean slate
193
  {
1999.4.9 by Brian Aker
Created EPOCH
194
    ((field::Epoch::pointer) field)->set_time();
1 by brian
clean slate
195
    return 0;					// Ok to set time to NULL
196
  }
1999.4.9 by Brian Aker
Created EPOCH
197
1 by brian
clean slate
198
  field->reset();
1660.1.3 by Brian Aker
Encapsulate Table in field
199
  if (field == field->getTable()->next_number_field)
1 by brian
clean slate
200
  {
1660.1.3 by Brian Aker
Encapsulate Table in field
201
    field->getTable()->auto_increment_field_not_null= false;
1 by brian
clean slate
202
    return 0;				  // field is set in fill_record()
203
  }
1999.4.9 by Brian Aker
Created EPOCH
204
1660.1.3 by Brian Aker
Encapsulate Table in field
205
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
685.4.2 by Jay Pipes
Added back truncation warnings. The NULL error is triggered from: session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL, not CHECK_FIELD_WARN
206
  {
207
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
208
    return 0;
209
  }
1999.4.9 by Brian Aker
Created EPOCH
210
1660.1.3 by Brian Aker
Encapsulate Table in field
211
  if (!field->getTable()->in_use->no_errors)
1 by brian
clean slate
212
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
1999.4.9 by Brian Aker
Created EPOCH
213
1 by brian
clean slate
214
  return -1;
215
}
216
217
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
218
static void do_skip(CopyField *)
1 by brian
clean slate
219
{
220
}
221
222
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
223
static void do_copy_null(CopyField *copy)
1 by brian
clean slate
224
{
225
  if (*copy->from_null_ptr & copy->from_bit)
226
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
227
    *copy->to_null_ptr|= copy->to_bit;
1 by brian
clean slate
228
    copy->to_field->reset();
229
  }
230
  else
231
  {
232
    *copy->to_null_ptr&= ~copy->to_bit;
233
    (copy->do_copy2)(copy);
234
  }
235
}
236
237
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
238
static void do_outer_field_null(CopyField *copy)
1 by brian
clean slate
239
{
240
  if (*copy->null_row ||
241
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
242
  {
243
    *copy->to_null_ptr|=copy->to_bit;
244
    copy->to_field->reset();
245
  }
246
  else
247
  {
248
    *copy->to_null_ptr&= ~copy->to_bit;
249
    (copy->do_copy2)(copy);
250
  }
251
}
252
253
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
254
static void do_copy_not_null(CopyField *copy)
1 by brian
clean slate
255
{
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
256
  if (copy->to_field->hasDefault() and *copy->from_null_ptr & copy->from_bit)
257
  {
258
    copy->to_field->set_default();
259
  }
260
  else if (*copy->from_null_ptr & copy->from_bit)
1 by brian
clean slate
261
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
262
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
263
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
264
    copy->to_field->reset();
265
  }
266
  else
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
267
  {
1 by brian
clean slate
268
    (copy->do_copy2)(copy);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
269
  }
1 by brian
clean slate
270
}
271
272
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
273
static void do_copy_maybe_null(CopyField *copy)
1 by brian
clean slate
274
{
275
  *copy->to_null_ptr&= ~copy->to_bit;
276
  (copy->do_copy2)(copy);
277
}
278
279
/* timestamp and next_number has special handling in case of NULL values */
280
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
281
static void do_copy_timestamp(CopyField *copy)
1 by brian
clean slate
282
{
283
  if (*copy->from_null_ptr & copy->from_bit)
284
  {
285
    /* Same as in set_field_to_null_with_conversions() */
1999.4.9 by Brian Aker
Created EPOCH
286
    ((field::Epoch::pointer) copy->to_field)->set_time();
1 by brian
clean slate
287
  }
288
  else
1999.4.9 by Brian Aker
Created EPOCH
289
  {
1 by brian
clean slate
290
    (copy->do_copy2)(copy);
1999.4.9 by Brian Aker
Created EPOCH
291
  }
1 by brian
clean slate
292
}
293
294
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
295
static void do_copy_next_number(CopyField *copy)
1 by brian
clean slate
296
{
297
  if (*copy->from_null_ptr & copy->from_bit)
298
  {
299
    /* Same as in set_field_to_null_with_conversions() */
1660.1.3 by Brian Aker
Encapsulate Table in field
300
    copy->to_field->getTable()->auto_increment_field_not_null= false;
1 by brian
clean slate
301
    copy->to_field->reset();
302
  }
303
  else
1999.4.9 by Brian Aker
Created EPOCH
304
  {
1 by brian
clean slate
305
    (copy->do_copy2)(copy);
1999.4.9 by Brian Aker
Created EPOCH
306
  }
1 by brian
clean slate
307
}
308
309
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
310
static void do_copy_blob(CopyField *copy)
1 by brian
clean slate
311
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
312
  ulong length= ((Field_blob*) copy->from_field)->get_length();
1 by brian
clean slate
313
  ((Field_blob*) copy->to_field)->store_length(length);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
314
  memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
1 by brian
clean slate
315
}
316
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
317
static void do_conv_blob(CopyField *copy)
1 by brian
clean slate
318
{
1996.2.1 by Brian Aker
uuid type code.
319
  copy->from_field->val_str_internal(&copy->tmp);
1 by brian
clean slate
320
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
321
                                         copy->tmp.length(),
322
                                         copy->tmp.charset());
1 by brian
clean slate
323
}
324
325
/** Save blob in copy->tmp for GROUP BY. */
326
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
327
static void do_save_blob(CopyField *copy)
1 by brian
clean slate
328
{
329
  char buff[MAX_FIELD_WIDTH];
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
330
  String res(buff, sizeof(buff), copy->tmp.charset());
1996.2.1 by Brian Aker
uuid type code.
331
  copy->from_field->val_str_internal(&res);
1 by brian
clean slate
332
  copy->tmp.copy(res);
333
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
334
                                         copy->tmp.length(),
335
                                         copy->tmp.charset());
1 by brian
clean slate
336
}
337
338
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
339
static void do_field_string(CopyField *copy)
1 by brian
clean slate
340
{
341
  char buff[MAX_FIELD_WIDTH];
342
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
1996.2.1 by Brian Aker
uuid type code.
343
  copy->from_field->val_str_internal(&copy->tmp);
1 by brian
clean slate
344
  copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
345
                        copy->tmp.charset());
346
}
347
348
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
349
static void do_field_enum(CopyField *copy)
1 by brian
clean slate
350
{
351
  if (copy->from_field->val_int() == 0)
151 by Brian Aker
Ulonglong to uint64_t
352
    ((Field_enum *) copy->to_field)->store_type((uint64_t) 0);
1 by brian
clean slate
353
  else
354
    do_field_string(copy);
355
}
356
357
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
358
static void do_field_int(CopyField *copy)
1 by brian
clean slate
359
{
152 by Brian Aker
longlong replacement
360
  int64_t value= copy->from_field->val_int();
1 by brian
clean slate
361
  copy->to_field->store(value,
362
                        test(copy->from_field->flags & UNSIGNED_FLAG));
363
}
364
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
365
static void do_field_real(CopyField *copy)
1 by brian
clean slate
366
{
367
  double value=copy->from_field->val_real();
368
  copy->to_field->store(value);
369
}
370
371
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
372
static void do_field_decimal(CopyField *copy)
1 by brian
clean slate
373
{
2030.1.4 by Brian Aker
Change my_decimal to Decimal
374
  type::Decimal value;
1 by brian
clean slate
375
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
376
}
377
378
379
/**
380
  string copy for single byte characters set when to string is shorter than
381
  from string.
382
*/
383
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
384
static void do_cut_string(CopyField *copy)
1 by brian
clean slate
385
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
386
  const CHARSET_INFO * const cs= copy->from_field->charset();
1 by brian
clean slate
387
  memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
388
389
  /* Check if we loosed any important characters */
390
  if (cs->cset->scan(cs,
391
                     (char*) copy->from_ptr + copy->to_length,
392
                     (char*) copy->from_ptr + copy->from_length,
393
                     MY_SEQ_SPACES) < copy->from_length - copy->to_length)
394
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
395
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
396
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
397
  }
398
}
399
400
401
/**
402
  string copy for multi byte characters set when to string is shorter than
403
  from string.
404
*/
405
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
406
static void do_cut_string_complex(CopyField *copy)
1 by brian
clean slate
407
{						// Shorter string field
408
  int well_formed_error;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
409
  const CHARSET_INFO * const cs= copy->from_field->charset();
481 by Brian Aker
Remove all of uchar.
410
  const unsigned char *from_end= copy->from_ptr + copy->from_length;
482 by Brian Aker
Remove uint.
411
  uint32_t copy_length= cs->cset->well_formed_len(cs,
1 by brian
clean slate
412
                                              (char*) copy->from_ptr,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
413
                                              (char*) from_end,
1 by brian
clean slate
414
                                              copy->to_length / cs->mbmaxlen,
415
                                              &well_formed_error);
416
  if (copy->to_length < copy_length)
417
    copy_length= copy->to_length;
418
  memcpy(copy->to_ptr, copy->from_ptr, copy_length);
419
420
  /* Check if we lost any important characters */
421
  if (well_formed_error ||
422
      cs->cset->scan(cs, (char*) copy->from_ptr + copy_length,
423
                     (char*) from_end,
424
                     MY_SEQ_SPACES) < (copy->from_length - copy_length))
425
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
426
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
427
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
428
  }
429
430
  if (copy_length < copy->to_length)
431
    cs->cset->fill(cs, (char*) copy->to_ptr + copy_length,
432
                   copy->to_length - copy_length, ' ');
433
}
434
435
436
437
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
438
static void do_expand_binary(CopyField *copy)
1 by brian
clean slate
439
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
440
  const CHARSET_INFO * const cs= copy->from_field->charset();
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
441
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
442
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
443
                 copy->to_length-copy->from_length, '\0');
1 by brian
clean slate
444
}
445
446
447
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
448
static void do_expand_string(CopyField *copy)
1 by brian
clean slate
449
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
450
  const CHARSET_INFO * const cs= copy->from_field->charset();
1 by brian
clean slate
451
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
452
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
453
                 copy->to_length-copy->from_length, ' ');
1 by brian
clean slate
454
}
455
456
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
457
static void do_varstring1(CopyField *copy)
1 by brian
clean slate
458
{
895 by Brian Aker
Completion (?) of uint conversion.
459
  uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
1 by brian
clean slate
460
  if (length > copy->to_length- 1)
461
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
462
    length= copy->to_length - 1;
1660.1.3 by Brian Aker
Encapsulate Table in field
463
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
464
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
465
                                  ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
466
  }
481 by Brian Aker
Remove all of uchar.
467
  *(unsigned char*) copy->to_ptr= (unsigned char) length;
1 by brian
clean slate
468
  memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
469
}
470
471
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
472
static void do_varstring1_mb(CopyField *copy)
1 by brian
clean slate
473
{
474
  int well_formed_error;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
475
  const CHARSET_INFO * const cs= copy->from_field->charset();
895 by Brian Aker
Completion (?) of uint conversion.
476
  uint32_t from_length= (uint32_t) *(unsigned char*) copy->from_ptr;
481 by Brian Aker
Remove all of uchar.
477
  const unsigned char *from_ptr= copy->from_ptr + 1;
482 by Brian Aker
Remove uint.
478
  uint32_t to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
479
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_ptr,
1 by brian
clean slate
480
                                         (char*) from_ptr + from_length,
481
                                         to_char_length, &well_formed_error);
482
  if (length < from_length)
483
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
484
    if (current_session->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
485
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
486
                                  ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
487
  }
481 by Brian Aker
Remove all of uchar.
488
  *copy->to_ptr= (unsigned char) length;
1 by brian
clean slate
489
  memcpy(copy->to_ptr + 1, from_ptr, length);
490
}
491
492
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
493
static void do_varstring2(CopyField *copy)
1 by brian
clean slate
494
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
495
  uint32_t length= uint2korr(copy->from_ptr);
1 by brian
clean slate
496
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
497
  {
498
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
1660.1.3 by Brian Aker
Encapsulate Table in field
499
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
500
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
501
                                  ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
502
  }
503
  int2store(copy->to_ptr,length);
504
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, copy->from_ptr + HA_KEY_BLOB_LENGTH,
505
         length);
506
}
507
508
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
509
static void do_varstring2_mb(CopyField *copy)
1 by brian
clean slate
510
{
511
  int well_formed_error;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
512
  const CHARSET_INFO * const cs= copy->from_field->charset();
482 by Brian Aker
Remove uint.
513
  uint32_t char_length= (copy->to_length - HA_KEY_BLOB_LENGTH) / cs->mbmaxlen;
514
  uint32_t from_length= uint2korr(copy->from_ptr);
481 by Brian Aker
Remove all of uchar.
515
  const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
482 by Brian Aker
Remove uint.
516
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_beg,
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
517
                                             (char*) from_beg + from_length,
518
                                             char_length, &well_formed_error);
1 by brian
clean slate
519
  if (length < from_length)
520
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
521
    if (current_session->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
522
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
523
                                  ER_WARN_DATA_TRUNCATED, 1);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
524
  }
1 by brian
clean slate
525
  int2store(copy->to_ptr, length);
526
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
527
}
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
528
1 by brian
clean slate
529
530
/***************************************************************************
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
531
** The different functions that fills in a CopyField class
1 by brian
clean slate
532
***************************************************************************/
533
534
/**
535
  copy of field to maybe null string.
536
  If field is null then the all bytes are set to 0.
537
  if field is not null then the first byte is set to 1 and the rest of the
538
  string is the field value.
539
  The 'to' buffer should have a size of field->pack_length()+1
540
*/
541
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
542
void CopyField::set(unsigned char *to,Field *from)
1 by brian
clean slate
543
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
544
  from_ptr= from->ptr;
545
  to_ptr= to;
546
  from_length= from->pack_length();
1 by brian
clean slate
547
  if (from->maybe_null())
548
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
549
    from_null_ptr= from->null_ptr;
550
    from_bit= from->null_bit;
551
    to_ptr[0]= 1;                             // Null as default value
552
    to_null_ptr= (unsigned char*) to_ptr++;
553
    to_bit= 1;
1660.1.3 by Brian Aker
Encapsulate Table in field
554
    if (from->getTable()->maybe_null)
1 by brian
clean slate
555
    {
1660.1.3 by Brian Aker
Encapsulate Table in field
556
      null_row= &from->getTable()->null_row;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
557
      do_copy= do_outer_field_to_null_str;
1 by brian
clean slate
558
    }
559
    else
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
560
      do_copy= do_field_to_null_str;
1 by brian
clean slate
561
  }
562
  else
563
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
564
    to_null_ptr= 0;                           // For easy debugging
565
    do_copy= do_field_eq;
1 by brian
clean slate
566
  }
567
}
568
569
570
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
571
  To do:
1 by brian
clean slate
572
573
  If 'save\ is set to true and the 'from' is a blob field, do_copy is set to
574
  do_save_blob rather than do_conv_blob.  The only differences between them
575
  appears to be:
576
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
577
  - do_save_blob allocates and uses an intermediate buffer before calling
578
    Field_blob::store. Is this in order to trigger the call to
1 by brian
clean slate
579
    well_formed_copy_nchars, by changing the pointer copy->tmp.ptr()?
580
    That call will take place anyway in all known cases.
581
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
582
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
1 by brian
clean slate
583
    effect? Truncation is handled by well_formed_copy_nchars anyway.
584
 */
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
585
void CopyField::set(Field *to,Field *from,bool save)
1 by brian
clean slate
586
{
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
587
  if (to->type() == DRIZZLE_TYPE_NULL)
1 by brian
clean slate
588
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
589
    to_null_ptr= 0;           // For easy debugging
590
    to_ptr= 0;
591
    do_copy= do_skip;
1 by brian
clean slate
592
    return;
593
  }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
594
  from_field= from;
595
  to_field= to;
596
  from_ptr= from->ptr;
597
  from_length= from->pack_length();
598
  to_ptr= to->ptr;
599
  to_length= to_field->pack_length();
1 by brian
clean slate
600
601
  // set up null handling
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
602
  from_null_ptr= to_null_ptr= 0;
1 by brian
clean slate
603
  if (from->maybe_null())
604
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
605
    from_null_ptr= from->null_ptr;
606
    from_bit= from->null_bit;
1 by brian
clean slate
607
    if (to_field->real_maybe_null())
608
    {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
609
      to_null_ptr= to->null_ptr;
610
      to_bit= to->null_bit;
1 by brian
clean slate
611
      if (from_null_ptr)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
612
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
613
        do_copy= do_copy_null;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
614
      }
1 by brian
clean slate
615
      else
616
      {
1660.1.3 by Brian Aker
Encapsulate Table in field
617
        null_row= &from->getTable()->null_row;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
618
        do_copy= do_outer_field_null;
1 by brian
clean slate
619
      }
620
    }
621
    else
622
    {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
623
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
624
      {
1 by brian
clean slate
625
        do_copy= do_copy_timestamp;               // Automatic timestamp
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
626
      }
1660.1.3 by Brian Aker
Encapsulate Table in field
627
      else if (to_field == to_field->getTable()->next_number_field)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
628
      {
1 by brian
clean slate
629
        do_copy= do_copy_next_number;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
630
      }
1 by brian
clean slate
631
      else
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
632
      {
1 by brian
clean slate
633
        do_copy= do_copy_not_null;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
634
      }
1 by brian
clean slate
635
    }
636
  }
637
  else if (to_field->real_maybe_null())
638
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
639
    to_null_ptr= to->null_ptr;
640
    to_bit= to->null_bit;
1 by brian
clean slate
641
    do_copy= do_copy_maybe_null;
642
  }
643
  else
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
644
   do_copy= 0;
1 by brian
clean slate
645
646
  if ((to->flags & BLOB_FLAG) && save)
647
    do_copy2= do_save_blob;
648
  else
649
    do_copy2= get_copy_func(to,from);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
650
  if (!do_copy)         // Not null
651
    do_copy= do_copy2;
1 by brian
clean slate
652
}
653
654
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
655
CopyField::Copy_func *
656
CopyField::get_copy_func(Field *to,Field *from)
1 by brian
clean slate
657
{
1660.1.3 by Brian Aker
Encapsulate Table in field
658
  bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
659
                                     from->getTable()->getShare()->db_low_byte_first);
1 by brian
clean slate
660
  if (to->flags & BLOB_FLAG)
661
  {
662
    if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
663
      return do_conv_blob;
664
    if (from_length != to_length || !compatible_db_low_byte_first)
665
    {
666
      // Correct pointer to point at char pointer
1660.1.3 by Brian Aker
Encapsulate Table in field
667
      to_ptr+= to_length - to->getTable()->getShare()->blob_ptr_size;
668
      from_ptr+= from_length- from->getTable()->getShare()->blob_ptr_size;
1 by brian
clean slate
669
      return do_copy_blob;
670
    }
671
  }
672
  else
673
  {
674
    if (to->result_type() == DECIMAL_RESULT)
675
      return do_field_decimal;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
676
      
1 by brian
clean slate
677
    // Check if identical fields
678
    if (from->result_type() == STRING_RESULT)
679
    {
680
      /*
681
        If we are copying date or datetime's we have to check the dates
682
        if we don't allow 'all' dates.
683
      */
684
      if (to->real_type() != from->real_type() ||
685
          !compatible_db_low_byte_first ||
1660.1.3 by Brian Aker
Encapsulate Table in field
686
          (((to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_DATE) || to->type() == DRIZZLE_TYPE_DATETIME))
1 by brian
clean slate
687
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
688
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
689
        {
690
          if (to->result_type() != STRING_RESULT)
691
          {
692
            return do_field_int;  // Convert SET to number
693
          }
2008 by Brian Aker
Formatting + remove default from switch/case.
694
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
695
          return do_field_string;
696
        }
1 by brian
clean slate
697
      }
2008 by Brian Aker
Formatting + remove default from switch/case.
698
325 by Brian Aker
Remove SET
699
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
1 by brian
clean slate
700
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
701
        if (!to->eq_def(from))
1 by brian
clean slate
702
        {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
703
          if (from->real_type() == DRIZZLE_TYPE_ENUM &&
704
              to->real_type() == DRIZZLE_TYPE_ENUM)
1 by brian
clean slate
705
            return do_field_enum;
706
          else
707
            return do_field_string;
708
        }
709
      }
710
      else if (to->charset() != from->charset())
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
711
        return do_field_string;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
712
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
1 by brian
clean slate
713
      {
1267.1.7 by Stewart Smith
in field_conv.cc, get_copy_func should not try to cast Field_blob to Field_varstring as Field_ blob does not inherit from field_varstring. Previously this kinda worked out due to the first member of field_blob kinda making sense in this comparison. Instead, we just shortcut to that decision if we are converting from BLOB.
714
        /* Field_blob is not part of the Field_varstring hierarchy,
2008 by Brian Aker
Formatting + remove default from switch/case.
715
          and casting to varstring for calling pack_length_no_ptr()
716
          is always incorrect. Previously the below comparison has
717
          always evaluated to false as pack_length_no_ptr() for BLOB
718
          will return 4 and varstring can only be <= 2.
719
          If your brain slightly bleeds as to why this worked for
720
          so many years, you are in no way alone.
1267.1.7 by Stewart Smith
in field_conv.cc, get_copy_func should not try to cast Field_blob to Field_varstring as Field_ blob does not inherit from field_varstring. Previously this kinda worked out due to the first member of field_blob kinda making sense in this comparison. Instead, we just shortcut to that decision if we are converting from BLOB.
721
        */
722
        if (from->flags & BLOB_FLAG)
723
          return do_field_string;
724
1267.1.8 by Stewart Smith
change dynamic_cast in field_conv.cc back to static_cast for runtime performance
725
        if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
726
            (static_cast<Field_varstring*>(from))->pack_length_no_ptr())
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
727
        {
1 by brian
clean slate
728
          return do_field_string;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
729
        }
2008 by Brian Aker
Formatting + remove default from switch/case.
730
1 by brian
clean slate
731
        if (to_length != from_length)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
732
        {
1782.4.5 by Brian Aker
Encapsulate the length of the bytes in the varchar header.
733
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
1 by brian
clean slate
734
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
2008 by Brian Aker
Formatting + remove default from switch/case.
735
                   do_varstring1_mb) :
1 by brian
clean slate
736
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
2008 by Brian Aker
Formatting + remove default from switch/case.
737
                   do_varstring2_mb));
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
738
        }
1 by brian
clean slate
739
      }
740
      else if (to_length < from_length)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
741
      {
742
        return (from->charset()->mbmaxlen == 1 ?
1 by brian
clean slate
743
                do_cut_string : do_cut_string_complex);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
744
      }
1 by brian
clean slate
745
      else if (to_length > from_length)
746
      {
747
        if (to->charset() == &my_charset_bin)
748
          return do_expand_binary;
749
        else
750
          return do_expand_string;
751
      }
752
    }
753
    else if (to->real_type() != from->real_type() ||
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
754
             to_length != from_length ||
1 by brian
clean slate
755
             !compatible_db_low_byte_first)
756
    {
80 by Brian Aker
Most of the removal of dead DECIMAL type
757
      if (to->result_type() == STRING_RESULT)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
758
        return do_field_string;
1 by brian
clean slate
759
      if (to->result_type() == INT_RESULT)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
760
        return do_field_int;
761
1 by brian
clean slate
762
      return do_field_real;
763
    }
764
    else
765
    {
766
      if (!to->eq_def(from) || !compatible_db_low_byte_first)
767
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
768
        if (to->result_type() == INT_RESULT)
769
          return do_field_int;
770
        else
771
          return do_field_real;
1 by brian
clean slate
772
      }
773
    }
774
  }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
775
  
1 by brian
clean slate
776
    /* Eq fields */
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
777
  switch (to_length)
778
  {
1 by brian
clean slate
779
  case 1: return do_field_1;
780
  case 2: return do_field_2;
781
  case 3: return do_field_3;
782
  case 4: return do_field_4;
783
  case 6: return do_field_6;
784
  case 8: return do_field_8;
785
  }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
786
  
1 by brian
clean slate
787
  return do_field_eq;
788
}
789
790
791
/** Simple quick field convert that is called on insert. */
792
793
int field_conv(Field *to,Field *from)
794
{
795
  if (to->real_type() == from->real_type() &&
1660.1.3 by Brian Aker
Encapsulate Table in field
796
      !(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
1 by brian
clean slate
797
  {
798
    /* Please god, will someone rewrite this to be readable :( */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
799
    if (to->pack_length() == from->pack_length() &&
800
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
801
        to->real_type() != DRIZZLE_TYPE_ENUM &&
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
802
        (to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
1 by brian
clean slate
803
        from->charset() == to->charset() &&
1660.1.3 by Brian Aker
Encapsulate Table in field
804
	to->getTable()->getShare()->db_low_byte_first == from->getTable()->getShare()->db_low_byte_first &&
805
        (!(to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
1782.4.5 by Brian Aker
Encapsulate the length of the bytes in the varchar header.
806
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->pack_length_no_ptr() == ((Field_varstring*)to)->pack_length_no_ptr()))
1 by brian
clean slate
807
    {						// Identical fields
808
      /* This may happen if one does 'UPDATE ... SET x=x' */
809
      if (to->ptr != from->ptr)
810
        memcpy(to->ptr,from->ptr,to->pack_length());
811
      return 0;
812
    }
813
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
814
  if (to->type() == DRIZZLE_TYPE_BLOB)
1 by brian
clean slate
815
  {						// Be sure the value is stored
816
    Field_blob *blob=(Field_blob*) to;
1996.2.1 by Brian Aker
uuid type code.
817
    from->val_str_internal(&blob->value);
1 by brian
clean slate
818
    /*
819
      Copy value if copy_blobs is set, or source is not a string and
820
      we have a pointer to its internal string conversion buffer.
821
    */
1660.1.3 by Brian Aker
Encapsulate Table in field
822
    if (to->getTable()->copy_blobs ||
1 by brian
clean slate
823
        (!blob->value.is_alloced() &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
824
         from->real_type() != DRIZZLE_TYPE_VARCHAR))
1 by brian
clean slate
825
      blob->value.copy();
826
    return blob->store(blob->value.ptr(),blob->value.length(),from->charset());
827
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
828
  if (from->real_type() == DRIZZLE_TYPE_ENUM &&
829
      to->real_type() == DRIZZLE_TYPE_ENUM &&
1 by brian
clean slate
830
      from->val_int() == 0)
831
  {
832
    ((Field_enum *)(to))->store_type(0);
833
    return 0;
834
  }
835
  else if ((from->result_type() == STRING_RESULT &&
836
            (to->result_type() == STRING_RESULT ||
325 by Brian Aker
Remove SET
837
             (from->real_type() != DRIZZLE_TYPE_ENUM))))
1 by brian
clean slate
838
  {
839
    char buff[MAX_FIELD_WIDTH];
840
    String result(buff,sizeof(buff),from->charset());
1996.2.1 by Brian Aker
uuid type code.
841
    from->val_str_internal(&result);
1 by brian
clean slate
842
    /*
843
      We use c_ptr_quick() here to make it easier if to is a float/double
844
      as the conversion routines will do a copy of the result doesn't
845
      end with \0. Can be replaced with .ptr() when we have our own
846
      string->double conversion.
847
    */
848
    return to->store(result.c_ptr_quick(),result.length(),from->charset());
849
  }
850
  else if (from->result_type() == REAL_RESULT)
851
    return to->store(from->val_real());
852
  else if (from->result_type() == DECIMAL_RESULT)
853
  {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
854
    type::Decimal buff;
1 by brian
clean slate
855
    return to->store_decimal(from->val_decimal(&buff));
856
  }
857
  else
858
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
859
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
860
861
} /* namespace drizzled */