~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field_conv.cc

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
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 */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
 
17
17
/**
24
24
    gives much more speed.
25
25
*/
26
26
 
27
 
#include <drizzled/server_includes.h>
 
27
#include <config.h>
 
28
 
28
29
#include <drizzled/error.h>
29
30
#include <drizzled/table.h>
30
31
#include <drizzled/session.h>
 
32
#include <drizzled/current_session.h>
31
33
 
32
 
#include <drizzled/field/str.h>
33
 
#include <drizzled/field/longstr.h>
34
 
#include <drizzled/field/num.h>
 
34
#include <drizzled/copy_field.h>
35
35
#include <drizzled/field/blob.h>
 
36
#include <drizzled/field/date.h>
 
37
#include <drizzled/field/datetime.h>
 
38
#include <drizzled/field/decimal.h>
 
39
#include <drizzled/field/double.h>
36
40
#include <drizzled/field/enum.h>
 
41
#include <drizzled/field/epoch.h>
 
42
#include <drizzled/field/int32.h>
 
43
#include <drizzled/field/int64.h>
37
44
#include <drizzled/field/null.h>
38
 
#include <drizzled/field/date.h>
39
 
#include <drizzled/field/fdecimal.h>
 
45
#include <drizzled/field/num.h>
 
46
#include <drizzled/field/num.h>
40
47
#include <drizzled/field/real.h>
41
 
#include <drizzled/field/double.h>
42
 
#include <drizzled/field/long.h>
43
 
#include <drizzled/field/int64_t.h>
44
 
#include <drizzled/field/num.h>
45
 
#include <drizzled/field/timetype.h>
46
 
#include <drizzled/field/timestamp.h>
47
 
#include <drizzled/field/datetime.h>
48
 
#include <drizzled/field/fstring.h>
 
48
#include <drizzled/field/str.h>
49
49
#include <drizzled/field/varstring.h>
50
 
 
51
 
 
52
 
static void do_field_eq(Copy_field *copy)
53
 
{
54
 
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
55
 
}
56
 
 
57
 
static void do_field_1(Copy_field *copy)
58
 
{
59
 
  copy->to_ptr[0]=copy->from_ptr[0];
60
 
}
61
 
 
62
 
static void do_field_2(Copy_field *copy)
63
 
{
64
 
  copy->to_ptr[0]=copy->from_ptr[0];
65
 
  copy->to_ptr[1]=copy->from_ptr[1];
66
 
}
67
 
 
68
 
static void do_field_3(Copy_field *copy)
69
 
{
70
 
  copy->to_ptr[0]=copy->from_ptr[0];
71
 
  copy->to_ptr[1]=copy->from_ptr[1];
72
 
  copy->to_ptr[2]=copy->from_ptr[2];
73
 
}
74
 
 
75
 
static void do_field_4(Copy_field *copy)
76
 
{
77
 
  copy->to_ptr[0]=copy->from_ptr[0];
78
 
  copy->to_ptr[1]=copy->from_ptr[1];
79
 
  copy->to_ptr[2]=copy->from_ptr[2];
80
 
  copy->to_ptr[3]=copy->from_ptr[3];
81
 
}
82
 
 
83
 
static void do_field_6(Copy_field *copy)
84
 
{                                               // For blob field
85
 
  copy->to_ptr[0]=copy->from_ptr[0];
86
 
  copy->to_ptr[1]=copy->from_ptr[1];
87
 
  copy->to_ptr[2]=copy->from_ptr[2];
88
 
  copy->to_ptr[3]=copy->from_ptr[3];
89
 
  copy->to_ptr[4]=copy->from_ptr[4];
90
 
  copy->to_ptr[5]=copy->from_ptr[5];
91
 
}
92
 
 
93
 
static void do_field_8(Copy_field *copy)
94
 
{
95
 
  copy->to_ptr[0]=copy->from_ptr[0];
96
 
  copy->to_ptr[1]=copy->from_ptr[1];
97
 
  copy->to_ptr[2]=copy->from_ptr[2];
98
 
  copy->to_ptr[3]=copy->from_ptr[3];
99
 
  copy->to_ptr[4]=copy->from_ptr[4];
100
 
  copy->to_ptr[5]=copy->from_ptr[5];
101
 
  copy->to_ptr[6]=copy->from_ptr[6];
102
 
  copy->to_ptr[7]=copy->from_ptr[7];
103
 
}
104
 
 
105
 
 
106
 
static void do_field_to_null_str(Copy_field *copy)
 
50
#include <drizzled/util/test.h>
 
51
#include <drizzled/system_variables.h>
 
52
 
 
53
namespace drizzled {
 
54
 
 
55
static void do_field_eq(CopyField *copy)
 
56
{
 
57
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
 
58
}
 
59
 
 
60
static void do_field_1(CopyField *copy)
 
61
{
 
62
  copy->to_ptr[0]= copy->from_ptr[0];
 
63
}
 
64
 
 
65
static void do_field_2(CopyField *copy)
 
66
{
 
67
  copy->to_ptr[0]= copy->from_ptr[0];
 
68
  copy->to_ptr[1]= copy->from_ptr[1];
 
69
}
 
70
 
 
71
static void do_field_3(CopyField *copy)
 
72
{
 
73
  copy->to_ptr[0]= copy->from_ptr[0];
 
74
  copy->to_ptr[1]= copy->from_ptr[1];
 
75
  copy->to_ptr[2]= copy->from_ptr[2];
 
76
}
 
77
 
 
78
static void do_field_4(CopyField *copy)
 
79
{
 
80
  copy->to_ptr[0]= copy->from_ptr[0];
 
81
  copy->to_ptr[1]= copy->from_ptr[1];
 
82
  copy->to_ptr[2]= copy->from_ptr[2];
 
83
  copy->to_ptr[3]= copy->from_ptr[3];
 
84
}
 
85
 
 
86
static void do_field_6(CopyField *copy)
 
87
{                  // For blob field
 
88
  copy->to_ptr[0]= copy->from_ptr[0];
 
89
  copy->to_ptr[1]= copy->from_ptr[1];
 
90
  copy->to_ptr[2]= copy->from_ptr[2];
 
91
  copy->to_ptr[3]= copy->from_ptr[3];
 
92
  copy->to_ptr[4]= copy->from_ptr[4];
 
93
  copy->to_ptr[5]= copy->from_ptr[5];
 
94
}
 
95
 
 
96
static void do_field_8(CopyField *copy)
 
97
{
 
98
  copy->to_ptr[0]= copy->from_ptr[0];
 
99
  copy->to_ptr[1]= copy->from_ptr[1];
 
100
  copy->to_ptr[2]= copy->from_ptr[2];
 
101
  copy->to_ptr[3]= copy->from_ptr[3];
 
102
  copy->to_ptr[4]= copy->from_ptr[4];
 
103
  copy->to_ptr[5]= copy->from_ptr[5];
 
104
  copy->to_ptr[6]= copy->from_ptr[6];
 
105
  copy->to_ptr[7]= copy->from_ptr[7];
 
106
}
 
107
 
 
108
 
 
109
static void do_field_to_null_str(CopyField *copy)
107
110
{
108
111
  if (*copy->from_null_ptr & copy->from_bit)
109
112
  {
110
113
    memset(copy->to_ptr, 0, copy->from_length);
111
 
    copy->to_null_ptr[0]=1;                     // Always bit 1
 
114
    copy->to_null_ptr[0]= 1;  // Always bit 1
112
115
  }
113
116
  else
114
117
  {
115
 
    copy->to_null_ptr[0]=0;
116
 
    memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
118
    copy->to_null_ptr[0]= 0;
 
119
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
117
120
  }
118
121
}
119
122
 
120
123
 
121
 
static void do_outer_field_to_null_str(Copy_field *copy)
 
124
static void do_outer_field_to_null_str(CopyField *copy)
122
125
{
123
126
  if (*copy->null_row ||
124
127
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
125
128
  {
126
129
    memset(copy->to_ptr, 0, copy->from_length);
127
 
    copy->to_null_ptr[0]=1;                     // Always bit 1
 
130
    copy->to_null_ptr[0]= 1;  // Always bit 1
128
131
  }
129
132
  else
130
133
  {
131
 
    copy->to_null_ptr[0]=0;
132
 
    memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
134
    copy->to_null_ptr[0]= 0;
 
135
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
133
136
  }
134
137
}
135
138
 
144
147
    return 0;
145
148
  }
146
149
  field->reset();
147
 
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
150
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
148
151
  {
149
152
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
150
153
    return 0;
151
154
  }
152
 
  if (!field->table->in_use->no_errors)
 
155
  if (!field->getTable()->in_use->no_errors)
153
156
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
154
157
  return -1;
155
158
}
181
184
    field->reset();
182
185
    return 0;
183
186
  }
 
187
 
184
188
  if (no_conversions)
185
189
    return -1;
186
190
 
189
193
    when set to NULL (TIMESTAMP fields which allow setting to NULL
190
194
    are handled by first check).
191
195
  */
192
 
  if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
 
196
  if (field->is_timestamp())
193
197
  {
194
 
    ((Field_timestamp*) field)->set_time();
 
198
    ((field::Epoch::pointer) field)->set_time();
195
199
    return 0;                                   // Ok to set time to NULL
196
200
  }
 
201
 
197
202
  field->reset();
198
 
  if (field == field->table->next_number_field)
 
203
  if (field == field->getTable()->next_number_field)
199
204
  {
200
 
    field->table->auto_increment_field_not_null= false;
 
205
    field->getTable()->auto_increment_field_not_null= false;
201
206
    return 0;                             // field is set in fill_record()
202
207
  }
203
 
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
208
 
 
209
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
204
210
  {
205
211
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
206
212
    return 0;
207
213
  }
208
 
  if (!field->table->in_use->no_errors)
 
214
 
 
215
  if (!field->getTable()->in_use->no_errors)
209
216
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
 
217
 
210
218
  return -1;
211
219
}
212
220
 
213
221
 
214
 
static void do_skip(Copy_field *copy __attribute__((unused)))
 
222
static void do_skip(CopyField *)
215
223
{
216
224
}
217
225
 
218
226
 
219
 
static void do_copy_null(Copy_field *copy)
 
227
static void do_copy_null(CopyField *copy)
220
228
{
221
229
  if (*copy->from_null_ptr & copy->from_bit)
222
230
  {
223
 
    *copy->to_null_ptr|=copy->to_bit;
 
231
    *copy->to_null_ptr|= copy->to_bit;
224
232
    copy->to_field->reset();
225
233
  }
226
234
  else
231
239
}
232
240
 
233
241
 
234
 
static void do_outer_field_null(Copy_field *copy)
 
242
static void do_outer_field_null(CopyField *copy)
235
243
{
236
244
  if (*copy->null_row ||
237
245
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
247
255
}
248
256
 
249
257
 
250
 
static void do_copy_not_null(Copy_field *copy)
 
258
static void do_copy_not_null(CopyField *copy)
251
259
{
252
 
  if (*copy->from_null_ptr & copy->from_bit)
 
260
  if (copy->to_field->hasDefault() and *copy->from_null_ptr & copy->from_bit)
 
261
  {
 
262
    copy->to_field->set_default();
 
263
  }
 
264
  else if (*copy->from_null_ptr & copy->from_bit)
253
265
  {
254
266
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
255
267
                                ER_WARN_DATA_TRUNCATED, 1);
256
268
    copy->to_field->reset();
257
269
  }
258
270
  else
 
271
  {
259
272
    (copy->do_copy2)(copy);
 
273
  }
260
274
}
261
275
 
262
276
 
263
 
static void do_copy_maybe_null(Copy_field *copy)
 
277
static void do_copy_maybe_null(CopyField *copy)
264
278
{
265
279
  *copy->to_null_ptr&= ~copy->to_bit;
266
280
  (copy->do_copy2)(copy);
268
282
 
269
283
/* timestamp and next_number has special handling in case of NULL values */
270
284
 
271
 
static void do_copy_timestamp(Copy_field *copy)
 
285
static void do_copy_timestamp(CopyField *copy)
272
286
{
273
287
  if (*copy->from_null_ptr & copy->from_bit)
274
288
  {
275
289
    /* Same as in set_field_to_null_with_conversions() */
276
 
    ((Field_timestamp*) copy->to_field)->set_time();
 
290
    ((field::Epoch::pointer) copy->to_field)->set_time();
277
291
  }
278
292
  else
 
293
  {
279
294
    (copy->do_copy2)(copy);
 
295
  }
280
296
}
281
297
 
282
298
 
283
 
static void do_copy_next_number(Copy_field *copy)
 
299
static void do_copy_next_number(CopyField *copy)
284
300
{
285
301
  if (*copy->from_null_ptr & copy->from_bit)
286
302
  {
287
303
    /* Same as in set_field_to_null_with_conversions() */
288
 
    copy->to_field->table->auto_increment_field_not_null= false;
 
304
    copy->to_field->getTable()->auto_increment_field_not_null= false;
289
305
    copy->to_field->reset();
290
306
  }
291
307
  else
 
308
  {
292
309
    (copy->do_copy2)(copy);
 
310
  }
293
311
}
294
312
 
295
313
 
296
 
static void do_copy_blob(Copy_field *copy)
 
314
static void do_copy_blob(CopyField *copy)
297
315
{
298
 
  ulong length=((Field_blob*) copy->from_field)->get_length();
 
316
  ulong length= ((Field_blob*) copy->from_field)->get_length();
299
317
  ((Field_blob*) copy->to_field)->store_length(length);
300
 
  memcpy(copy->to_ptr,copy->from_ptr,sizeof(char*));
 
318
  memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
301
319
}
302
320
 
303
 
static void do_conv_blob(Copy_field *copy)
 
321
static void do_conv_blob(CopyField *copy)
304
322
{
305
 
  copy->from_field->val_str(&copy->tmp);
 
323
  copy->from_field->val_str_internal(&copy->tmp);
306
324
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
307
 
                                         copy->tmp.length(),
308
 
                                         copy->tmp.charset());
 
325
                                         copy->tmp.length(),
 
326
                                         copy->tmp.charset());
309
327
}
310
328
 
311
329
/** Save blob in copy->tmp for GROUP BY. */
312
330
 
313
 
static void do_save_blob(Copy_field *copy)
 
331
static void do_save_blob(CopyField *copy)
314
332
{
315
333
  char buff[MAX_FIELD_WIDTH];
316
 
  String res(buff,sizeof(buff),copy->tmp.charset());
317
 
  copy->from_field->val_str(&res);
 
334
  String res(buff, sizeof(buff), copy->tmp.charset());
 
335
  copy->from_field->val_str_internal(&res);
318
336
  copy->tmp.copy(res);
319
337
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
320
 
                                         copy->tmp.length(),
321
 
                                         copy->tmp.charset());
 
338
                                         copy->tmp.length(),
 
339
                                         copy->tmp.charset());
322
340
}
323
341
 
324
342
 
325
 
static void do_field_string(Copy_field *copy)
 
343
static void do_field_string(CopyField *copy)
326
344
{
327
345
  char buff[MAX_FIELD_WIDTH];
328
346
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
329
 
  copy->from_field->val_str(&copy->tmp);
330
 
  copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
331
 
                        copy->tmp.charset());
 
347
  copy->from_field->val_str_internal(&copy->tmp);
 
348
  copy->to_field->store(copy->tmp.data(),copy->tmp.length(), copy->tmp.charset());
332
349
}
333
350
 
334
351
 
335
 
static void do_field_enum(Copy_field *copy)
 
352
static void do_field_enum(CopyField *copy)
336
353
{
337
354
  if (copy->from_field->val_int() == 0)
338
355
    ((Field_enum *) copy->to_field)->store_type((uint64_t) 0);
341
358
}
342
359
 
343
360
 
344
 
static void do_field_int(Copy_field *copy)
 
361
static void do_field_int(CopyField *copy)
345
362
{
346
363
  int64_t value= copy->from_field->val_int();
347
364
  copy->to_field->store(value,
348
365
                        test(copy->from_field->flags & UNSIGNED_FLAG));
349
366
}
350
367
 
351
 
static void do_field_real(Copy_field *copy)
 
368
static void do_field_real(CopyField *copy)
352
369
{
353
370
  double value=copy->from_field->val_real();
354
371
  copy->to_field->store(value);
355
372
}
356
373
 
357
374
 
358
 
static void do_field_decimal(Copy_field *copy)
 
375
static void do_field_decimal(CopyField *copy)
359
376
{
360
 
  my_decimal value;
 
377
  type::Decimal value;
361
378
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
362
379
}
363
380
 
367
384
  from string.
368
385
*/
369
386
 
370
 
static void do_cut_string(Copy_field *copy)
 
387
static void do_cut_string(CopyField *copy)
371
388
{
372
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
373
 
  memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
 
389
  const charset_info_st * const cs= copy->from_field->charset();
 
390
  memcpy(copy->to_ptr, copy->from_ptr, copy->to_length);
374
391
 
375
392
  /* Check if we loosed any important characters */
376
393
  if (cs->cset->scan(cs,
389
406
  from string.
390
407
*/
391
408
 
392
 
static void do_cut_string_complex(Copy_field *copy)
 
409
static void do_cut_string_complex(CopyField *copy)
393
410
{                                               // Shorter string field
394
411
  int well_formed_error;
395
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
412
  const charset_info_st * const cs= copy->from_field->charset();
396
413
  const unsigned char *from_end= copy->from_ptr + copy->from_length;
397
414
  uint32_t copy_length= cs->cset->well_formed_len(cs,
398
415
                                              (char*) copy->from_ptr,
399
 
                                              (char*) from_end, 
 
416
                                              (char*) from_end,
400
417
                                              copy->to_length / cs->mbmaxlen,
401
418
                                              &well_formed_error);
402
419
  if (copy->to_length < copy_length)
421
438
 
422
439
 
423
440
 
424
 
static void do_expand_binary(Copy_field *copy)
425
 
{
426
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
427
 
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
428
 
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
429
 
                     copy->to_length-copy->from_length, '\0');
430
 
}
431
 
 
432
 
 
433
 
 
434
 
static void do_expand_string(Copy_field *copy)
435
 
{
436
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
437
 
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
438
 
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
439
 
                     copy->to_length-copy->from_length, ' ');
440
 
}
441
 
 
442
 
 
443
 
static void do_varstring1(Copy_field *copy)
444
 
{
445
 
  uint32_t length= (uint) *(unsigned char*) copy->from_ptr;
 
441
static void do_expand_binary(CopyField *copy)
 
442
{
 
443
  const charset_info_st * const cs= copy->from_field->charset();
 
444
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
 
445
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
 
446
                 copy->to_length-copy->from_length, '\0');
 
447
}
 
448
 
 
449
 
 
450
 
 
451
static void do_expand_string(CopyField *copy)
 
452
{
 
453
  const charset_info_st * const cs= copy->from_field->charset();
 
454
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
455
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
 
456
                 copy->to_length-copy->from_length, ' ');
 
457
}
 
458
 
 
459
 
 
460
static void do_varstring1(CopyField *copy)
 
461
{
 
462
  uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
446
463
  if (length > copy->to_length- 1)
447
464
  {
448
 
    length=copy->to_length - 1;
449
 
    if (copy->from_field->table->in_use->count_cuted_fields)
 
465
    length= copy->to_length - 1;
 
466
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
467
    {
450
468
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
451
469
                                  ER_WARN_DATA_TRUNCATED, 1);
 
470
    }
452
471
  }
453
472
  *(unsigned char*) copy->to_ptr= (unsigned char) length;
454
473
  memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
455
474
}
456
475
 
457
476
 
458
 
static void do_varstring1_mb(Copy_field *copy)
 
477
static void do_varstring1_mb(CopyField *copy)
459
478
{
460
479
  int well_formed_error;
461
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
462
 
  uint32_t from_length= (uint) *(unsigned char*) copy->from_ptr;
 
480
  const charset_info_st * const cs= copy->from_field->charset();
 
481
  uint32_t from_length= (uint32_t) *(unsigned char*) copy->from_ptr;
463
482
  const unsigned char *from_ptr= copy->from_ptr + 1;
464
483
  uint32_t to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
465
484
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_ptr,
468
487
  if (length < from_length)
469
488
  {
470
489
    if (current_session->count_cuted_fields)
 
490
    {
471
491
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
472
492
                                  ER_WARN_DATA_TRUNCATED, 1);
 
493
    }
473
494
  }
474
495
  *copy->to_ptr= (unsigned char) length;
475
496
  memcpy(copy->to_ptr + 1, from_ptr, length);
476
497
}
477
498
 
478
499
 
479
 
static void do_varstring2(Copy_field *copy)
 
500
static void do_varstring2(CopyField *copy)
480
501
{
481
 
  uint32_t length=uint2korr(copy->from_ptr);
 
502
  uint32_t length= uint2korr(copy->from_ptr);
482
503
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
483
504
  {
484
505
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
485
 
    if (copy->from_field->table->in_use->count_cuted_fields)
 
506
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
507
    {
486
508
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
487
509
                                  ER_WARN_DATA_TRUNCATED, 1);
 
510
    }
488
511
  }
489
512
  int2store(copy->to_ptr,length);
490
513
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, copy->from_ptr + HA_KEY_BLOB_LENGTH,
492
515
}
493
516
 
494
517
 
495
 
static void do_varstring2_mb(Copy_field *copy)
 
518
static void do_varstring2_mb(CopyField *copy)
496
519
{
497
520
  int well_formed_error;
498
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
521
  const charset_info_st * const cs= copy->from_field->charset();
499
522
  uint32_t char_length= (copy->to_length - HA_KEY_BLOB_LENGTH) / cs->mbmaxlen;
500
523
  uint32_t from_length= uint2korr(copy->from_ptr);
501
524
  const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
502
525
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_beg,
503
 
                                         (char*) from_beg + from_length,
504
 
                                         char_length, &well_formed_error);
 
526
                                             (char*) from_beg + from_length,
 
527
                                             char_length, &well_formed_error);
505
528
  if (length < from_length)
506
529
  {
507
530
    if (current_session->count_cuted_fields)
 
531
    {
508
532
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
509
533
                                  ER_WARN_DATA_TRUNCATED, 1);
510
 
  }  
 
534
    }
 
535
  }
511
536
  int2store(copy->to_ptr, length);
512
537
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
513
538
}
514
 
 
 
539
 
515
540
 
516
541
/***************************************************************************
517
 
** The different functions that fills in a Copy_field class
 
542
** The different functions that fills in a CopyField class
518
543
***************************************************************************/
519
544
 
520
545
/**
525
550
  The 'to' buffer should have a size of field->pack_length()+1
526
551
*/
527
552
 
528
 
void Copy_field::set(unsigned char *to,Field *from)
 
553
void CopyField::set(unsigned char *to,Field *from)
529
554
{
530
 
  from_ptr=from->ptr;
531
 
  to_ptr=to;
532
 
  from_length=from->pack_length();
 
555
  from_ptr= from->ptr;
 
556
  to_ptr= to;
 
557
  from_length= from->pack_length();
533
558
  if (from->maybe_null())
534
559
  {
535
 
    from_null_ptr=from->null_ptr;
536
 
    from_bit=     from->null_bit;
537
 
    to_ptr[0]=    1;                            // Null as default value
538
 
    to_null_ptr=  (unsigned char*) to_ptr++;
539
 
    to_bit=       1;
540
 
    if (from->table->maybe_null)
 
560
    from_null_ptr= from->null_ptr;
 
561
    from_bit= from->null_bit;
 
562
    to_ptr[0]= 1;                             // Null as default value
 
563
    to_null_ptr= (unsigned char*) to_ptr++;
 
564
    to_bit= 1;
 
565
    if (from->getTable()->maybe_null)
541
566
    {
542
 
      null_row=   &from->table->null_row;
543
 
      do_copy=    do_outer_field_to_null_str;
 
567
      null_row= &from->getTable()->null_row;
 
568
      do_copy= do_outer_field_to_null_str;
544
569
    }
545
570
    else
546
 
      do_copy=    do_field_to_null_str;
 
571
      do_copy= do_field_to_null_str;
547
572
  }
548
573
  else
549
574
  {
550
 
    to_null_ptr=  0;                            // For easy debugging
551
 
    do_copy=      do_field_eq;
 
575
    to_null_ptr= 0;                           // For easy debugging
 
576
    do_copy= do_field_eq;
552
577
  }
553
578
}
554
579
 
555
580
 
556
581
/*
557
 
  To do: 
 
582
  To do:
558
583
 
559
584
  If 'save\ is set to true and the 'from' is a blob field, do_copy is set to
560
585
  do_save_blob rather than do_conv_blob.  The only differences between them
561
586
  appears to be:
562
587
 
563
 
  - do_save_blob allocates and uses an intermediate buffer before calling 
564
 
    Field_blob::store. Is this in order to trigger the call to 
 
588
  - do_save_blob allocates and uses an intermediate buffer before calling
 
589
    Field_blob::store. Is this in order to trigger the call to
565
590
    well_formed_copy_nchars, by changing the pointer copy->tmp.ptr()?
566
591
    That call will take place anyway in all known cases.
567
592
 
568
 
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended 
 
593
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
569
594
    effect? Truncation is handled by well_formed_copy_nchars anyway.
570
595
 */
571
 
void Copy_field::set(Field *to,Field *from,bool save)
 
596
void CopyField::set(Field *to,Field *from,bool save)
572
597
{
573
598
  if (to->type() == DRIZZLE_TYPE_NULL)
574
599
  {
575
 
    to_null_ptr=0;                              // For easy debugging
576
 
    to_ptr=0;
577
 
    do_copy=do_skip;
 
600
    to_null_ptr= 0;           // For easy debugging
 
601
    to_ptr= 0;
 
602
    do_copy= do_skip;
578
603
    return;
579
604
  }
580
 
  from_field=from;
581
 
  to_field=to;
582
 
  from_ptr=from->ptr;
583
 
  from_length=from->pack_length();
584
 
  to_ptr=  to->ptr;
585
 
  to_length=to_field->pack_length();
 
605
  from_field= from;
 
606
  to_field= to;
 
607
  from_ptr= from->ptr;
 
608
  from_length= from->pack_length();
 
609
  to_ptr= to->ptr;
 
610
  to_length= to_field->pack_length();
586
611
 
587
612
  // set up null handling
588
 
  from_null_ptr=to_null_ptr=0;
 
613
  from_null_ptr= to_null_ptr= 0;
589
614
  if (from->maybe_null())
590
615
  {
591
 
    from_null_ptr=      from->null_ptr;
592
 
    from_bit=           from->null_bit;
 
616
    from_null_ptr= from->null_ptr;
 
617
    from_bit= from->null_bit;
593
618
    if (to_field->real_maybe_null())
594
619
    {
595
 
      to_null_ptr=      to->null_ptr;
596
 
      to_bit=           to->null_bit;
 
620
      to_null_ptr= to->null_ptr;
 
621
      to_bit= to->null_bit;
597
622
      if (from_null_ptr)
598
 
        do_copy=        do_copy_null;
 
623
      {
 
624
        do_copy= do_copy_null;
 
625
      }
599
626
      else
600
627
      {
601
 
        null_row=       &from->table->null_row;
602
 
        do_copy=        do_outer_field_null;
 
628
        null_row= &from->getTable()->null_row;
 
629
        do_copy= do_outer_field_null;
603
630
      }
604
631
    }
605
632
    else
606
633
    {
607
 
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
 
634
      if (to_field->is_timestamp())
 
635
      {
608
636
        do_copy= do_copy_timestamp;               // Automatic timestamp
609
 
      else if (to_field == to_field->table->next_number_field)
 
637
      }
 
638
      else if (to_field == to_field->getTable()->next_number_field)
 
639
      {
610
640
        do_copy= do_copy_next_number;
 
641
      }
611
642
      else
 
643
      {
612
644
        do_copy= do_copy_not_null;
 
645
      }
613
646
    }
614
647
  }
615
648
  else if (to_field->real_maybe_null())
616
649
  {
617
 
    to_null_ptr=        to->null_ptr;
618
 
    to_bit=             to->null_bit;
 
650
    to_null_ptr= to->null_ptr;
 
651
    to_bit= to->null_bit;
619
652
    do_copy= do_copy_maybe_null;
620
653
  }
621
654
  else
622
 
   do_copy=0;
 
655
   do_copy= 0;
623
656
 
624
657
  if ((to->flags & BLOB_FLAG) && save)
625
658
    do_copy2= do_save_blob;
626
659
  else
627
660
    do_copy2= get_copy_func(to,from);
628
 
  if (!do_copy)                                 // Not null
629
 
    do_copy=do_copy2;
 
661
  if (!do_copy)         // Not null
 
662
    do_copy= do_copy2;
630
663
}
631
664
 
632
665
 
633
 
Copy_field::Copy_func *
634
 
Copy_field::get_copy_func(Field *to,Field *from)
 
666
CopyField::Copy_func *
 
667
CopyField::get_copy_func(Field *to,Field *from)
635
668
{
636
 
  bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
637
 
                                     from->table->s->db_low_byte_first);
 
669
  bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
 
670
                                     from->getTable()->getShare()->db_low_byte_first);
638
671
  if (to->flags & BLOB_FLAG)
639
672
  {
640
673
    if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
642
675
    if (from_length != to_length || !compatible_db_low_byte_first)
643
676
    {
644
677
      // Correct pointer to point at char pointer
645
 
      to_ptr+=   to_length - to->table->s->blob_ptr_size;
646
 
      from_ptr+= from_length- from->table->s->blob_ptr_size;
 
678
      to_ptr+= to_length - to->getTable()->getShare()->sizeBlobPtr();
 
679
      from_ptr+= from_length- from->getTable()->getShare()->sizeBlobPtr();
647
680
      return do_copy_blob;
648
681
    }
649
682
  }
651
684
  {
652
685
    if (to->result_type() == DECIMAL_RESULT)
653
686
      return do_field_decimal;
 
687
      
654
688
    // Check if identical fields
655
689
    if (from->result_type() == STRING_RESULT)
656
690
    {
660
694
      */
661
695
      if (to->real_type() != from->real_type() ||
662
696
          !compatible_db_low_byte_first ||
663
 
          (((to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_DATE) || to->type() == DRIZZLE_TYPE_DATETIME))
 
697
          (((to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_DATE) || to->type() == DRIZZLE_TYPE_DATETIME))
664
698
      {
665
 
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
666
 
          if (to->result_type() != STRING_RESULT)
667
 
            return do_field_int;                // Convert SET to number
668
 
        return do_field_string;
 
699
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
 
700
        {
 
701
          if (to->result_type() != STRING_RESULT)
 
702
          {
 
703
            return do_field_int;  // Convert SET to number
 
704
          }
 
705
 
 
706
          return do_field_string;
 
707
        }
669
708
      }
 
709
 
670
710
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
671
711
      {
672
 
        if (!to->eq_def(from))
 
712
        if (!to->eq_def(from))
673
713
        {
674
714
          if (from->real_type() == DRIZZLE_TYPE_ENUM &&
675
715
              to->real_type() == DRIZZLE_TYPE_ENUM)
679
719
        }
680
720
      }
681
721
      else if (to->charset() != from->charset())
682
 
        return do_field_string;
 
722
        return do_field_string;
683
723
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
684
724
      {
685
 
        if (((Field_varstring*) to)->length_bytes !=
686
 
            ((Field_varstring*) from)->length_bytes)
687
 
          return do_field_string;
 
725
        /* Field_blob is not part of the Field_varstring hierarchy,
 
726
          and casting to varstring for calling pack_length_no_ptr()
 
727
          is always incorrect. Previously the below comparison has
 
728
          always evaluated to false as pack_length_no_ptr() for BLOB
 
729
          will return 4 and varstring can only be <= 2.
 
730
          If your brain slightly bleeds as to why this worked for
 
731
          so many years, you are in no way alone.
 
732
        */
 
733
        if (from->flags & BLOB_FLAG)
 
734
          return do_field_string;
 
735
 
 
736
        if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
 
737
            (static_cast<Field_varstring*>(from))->pack_length_no_ptr())
 
738
        {
 
739
          return do_field_string;
 
740
        }
 
741
 
688
742
        if (to_length != from_length)
689
 
          return (((Field_varstring*) to)->length_bytes == 1 ?
 
743
        {
 
744
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
690
745
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
691
 
                                                    do_varstring1_mb) :
 
746
                   do_varstring1_mb) :
692
747
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
693
 
                                                    do_varstring2_mb));
 
748
                   do_varstring2_mb));
 
749
        }
694
750
      }
695
751
      else if (to_length < from_length)
696
 
        return (from->charset()->mbmaxlen == 1 ?
 
752
      {
 
753
        return (from->charset()->mbmaxlen == 1 ?
697
754
                do_cut_string : do_cut_string_complex);
 
755
      }
698
756
      else if (to_length > from_length)
699
757
      {
700
758
        if (to->charset() == &my_charset_bin)
702
760
        else
703
761
          return do_expand_string;
704
762
      }
705
 
 
706
763
    }
707
764
    else if (to->real_type() != from->real_type() ||
708
 
             to_length != from_length ||
 
765
             to_length != from_length ||
709
766
             !compatible_db_low_byte_first)
710
767
    {
711
768
      if (to->result_type() == STRING_RESULT)
712
 
        return do_field_string;
 
769
        return do_field_string;
713
770
      if (to->result_type() == INT_RESULT)
714
 
        return do_field_int;
 
771
        return do_field_int;
 
772
 
715
773
      return do_field_real;
716
774
    }
717
775
    else
718
776
    {
719
777
      if (!to->eq_def(from) || !compatible_db_low_byte_first)
720
778
      {
721
 
        if (to->result_type() == INT_RESULT)
722
 
          return do_field_int;
723
 
        else
724
 
          return do_field_real;
 
779
        if (to->result_type() == INT_RESULT)
 
780
          return do_field_int;
 
781
        else
 
782
          return do_field_real;
725
783
      }
726
784
    }
727
785
  }
 
786
  
728
787
    /* Eq fields */
729
 
  switch (to_length) {
 
788
  switch (to_length)
 
789
  {
730
790
  case 1: return do_field_1;
731
791
  case 2: return do_field_2;
732
792
  case 3: return do_field_3;
734
794
  case 6: return do_field_6;
735
795
  case 8: return do_field_8;
736
796
  }
 
797
  
737
798
  return do_field_eq;
738
799
}
739
800
 
743
804
int field_conv(Field *to,Field *from)
744
805
{
745
806
  if (to->real_type() == from->real_type() &&
746
 
      !(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
 
807
      !(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
747
808
  {
748
809
    /* Please god, will someone rewrite this to be readable :( */
749
 
    if (to->pack_length() == from->pack_length() && 
750
 
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) && 
751
 
        to->real_type() != DRIZZLE_TYPE_ENUM && 
752
 
        (to->real_type() != DRIZZLE_TYPE_NEWDECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
 
810
    if (to->pack_length() == from->pack_length() &&
 
811
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
 
812
        to->real_type() != DRIZZLE_TYPE_ENUM &&
 
813
        (to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
753
814
        from->charset() == to->charset() &&
754
 
        to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
755
 
        (!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) && 
756
 
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
 
815
        to->getTable()->getShare()->db_low_byte_first == from->getTable()->getShare()->db_low_byte_first &&
 
816
        (!(to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
 
817
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->pack_length_no_ptr() == ((Field_varstring*)to)->pack_length_no_ptr()))
757
818
    {                                           // Identical fields
758
 
#ifdef HAVE_purify
759
819
      /* This may happen if one does 'UPDATE ... SET x=x' */
760
820
      if (to->ptr != from->ptr)
761
 
#endif
762
821
        memcpy(to->ptr,from->ptr,to->pack_length());
763
822
      return 0;
764
823
    }
766
825
  if (to->type() == DRIZZLE_TYPE_BLOB)
767
826
  {                                             // Be sure the value is stored
768
827
    Field_blob *blob=(Field_blob*) to;
769
 
    from->val_str(&blob->value);
 
828
    from->val_str_internal(&blob->value);
770
829
    /*
771
830
      Copy value if copy_blobs is set, or source is not a string and
772
831
      we have a pointer to its internal string conversion buffer.
773
832
    */
774
 
    if (to->table->copy_blobs ||
 
833
    if (to->getTable()->copy_blobs ||
775
834
        (!blob->value.is_alloced() &&
776
835
         from->real_type() != DRIZZLE_TYPE_VARCHAR))
777
836
      blob->value.copy();
790
849
  {
791
850
    char buff[MAX_FIELD_WIDTH];
792
851
    String result(buff,sizeof(buff),from->charset());
793
 
    from->val_str(&result);
 
852
    from->val_str_internal(&result);
794
853
    /*
795
854
      We use c_ptr_quick() here to make it easier if to is a float/double
796
855
      as the conversion routines will do a copy of the result doesn't
797
856
      end with \0. Can be replaced with .ptr() when we have our own
798
857
      string->double conversion.
799
858
    */
800
 
    return to->store(result.c_ptr_quick(),result.length(),from->charset());
 
859
    return to->store(result.c_str(),result.length(),from->charset());
801
860
  }
802
861
  else if (from->result_type() == REAL_RESULT)
 
862
  {
803
863
    return to->store(from->val_real());
 
864
  }
804
865
  else if (from->result_type() == DECIMAL_RESULT)
805
866
  {
806
 
    my_decimal buff;
 
867
    type::Decimal buff;
807
868
    return to->store_decimal(from->val_decimal(&buff));
808
869
  }
809
870
  else
 
871
  {
810
872
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
 
873
  }
811
874
}
 
875
 
 
876
} /* namespace drizzled */