~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field_conv.cc

  • Committer: Monty Taylor
  • Date: 2008-11-02 03:17:55 UTC
  • mto: (520.4.45 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: monty@inaugust.com-20081102031755-cpzp7dlqoa7rcs1v
Removed a bunch of unusued tests and defines from autoconf.
Fixed the way we were doing cmath - and moved it out of global.h. Still probably
isn't right for Sun Studio, but we're getting closer I think.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    gives much more speed.
25
25
*/
26
26
 
27
 
#include "config.h"
 
27
#include <drizzled/server_includes.h>
28
28
#include <drizzled/error.h>
29
 
#include <drizzled/table.h>
30
 
#include <drizzled/session.h>
31
 
 
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>
38
 
#include <drizzled/field/decimal.h>
39
 
#include <drizzled/field/real.h>
40
 
#include <drizzled/field/double.h>
41
 
#include <drizzled/field/long.h>
42
 
#include <drizzled/field/int64_t.h>
43
 
#include <drizzled/field/num.h>
44
 
#include <drizzled/field/timestamp.h>
45
 
#include <drizzled/field/datetime.h>
46
 
#include <drizzled/field/varstring.h>
47
 
 
48
 
namespace drizzled
49
 
{
50
 
 
51
 
static void do_field_eq(CopyField *copy)
52
 
{
53
 
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
54
 
}
55
 
 
56
 
static void do_field_1(CopyField *copy)
57
 
{
58
 
  copy->to_ptr[0]= copy->from_ptr[0];
59
 
}
60
 
 
61
 
static void do_field_2(CopyField *copy)
62
 
{
63
 
  copy->to_ptr[0]= copy->from_ptr[0];
64
 
  copy->to_ptr[1]= copy->from_ptr[1];
65
 
}
66
 
 
67
 
static void do_field_3(CopyField *copy)
68
 
{
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];
72
 
}
73
 
 
74
 
static void do_field_4(CopyField *copy)
75
 
{
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];
80
 
}
81
 
 
82
 
static void do_field_6(CopyField *copy)
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];
90
 
}
91
 
 
92
 
static void do_field_8(CopyField *copy)
93
 
{
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];
102
 
}
103
 
 
104
 
 
105
 
static void do_field_to_null_str(CopyField *copy)
 
29
 
 
30
static void do_field_eq(Copy_field *copy)
 
31
{
 
32
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
33
}
 
34
 
 
35
static void do_field_1(Copy_field *copy)
 
36
{
 
37
  copy->to_ptr[0]=copy->from_ptr[0];
 
38
}
 
39
 
 
40
static void do_field_2(Copy_field *copy)
 
41
{
 
42
  copy->to_ptr[0]=copy->from_ptr[0];
 
43
  copy->to_ptr[1]=copy->from_ptr[1];
 
44
}
 
45
 
 
46
static void do_field_3(Copy_field *copy)
 
47
{
 
48
  copy->to_ptr[0]=copy->from_ptr[0];
 
49
  copy->to_ptr[1]=copy->from_ptr[1];
 
50
  copy->to_ptr[2]=copy->from_ptr[2];
 
51
}
 
52
 
 
53
static void do_field_4(Copy_field *copy)
 
54
{
 
55
  copy->to_ptr[0]=copy->from_ptr[0];
 
56
  copy->to_ptr[1]=copy->from_ptr[1];
 
57
  copy->to_ptr[2]=copy->from_ptr[2];
 
58
  copy->to_ptr[3]=copy->from_ptr[3];
 
59
}
 
60
 
 
61
static void do_field_6(Copy_field *copy)
 
62
{                                               // For blob field
 
63
  copy->to_ptr[0]=copy->from_ptr[0];
 
64
  copy->to_ptr[1]=copy->from_ptr[1];
 
65
  copy->to_ptr[2]=copy->from_ptr[2];
 
66
  copy->to_ptr[3]=copy->from_ptr[3];
 
67
  copy->to_ptr[4]=copy->from_ptr[4];
 
68
  copy->to_ptr[5]=copy->from_ptr[5];
 
69
}
 
70
 
 
71
static void do_field_8(Copy_field *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
  copy->to_ptr[3]=copy->from_ptr[3];
 
77
  copy->to_ptr[4]=copy->from_ptr[4];
 
78
  copy->to_ptr[5]=copy->from_ptr[5];
 
79
  copy->to_ptr[6]=copy->from_ptr[6];
 
80
  copy->to_ptr[7]=copy->from_ptr[7];
 
81
}
 
82
 
 
83
 
 
84
static void do_field_to_null_str(Copy_field *copy)
106
85
{
107
86
  if (*copy->from_null_ptr & copy->from_bit)
108
87
  {
109
88
    memset(copy->to_ptr, 0, copy->from_length);
110
 
    copy->to_null_ptr[0]= 1;  // Always bit 1
 
89
    copy->to_null_ptr[0]=1;                     // Always bit 1
111
90
  }
112
91
  else
113
92
  {
114
 
    copy->to_null_ptr[0]= 0;
115
 
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
 
93
    copy->to_null_ptr[0]=0;
 
94
    memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
116
95
  }
117
96
}
118
97
 
119
98
 
120
 
static void do_outer_field_to_null_str(CopyField *copy)
 
99
static void do_outer_field_to_null_str(Copy_field *copy)
121
100
{
122
101
  if (*copy->null_row ||
123
102
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
124
103
  {
125
104
    memset(copy->to_ptr, 0, copy->from_length);
126
 
    copy->to_null_ptr[0]= 1;  // Always bit 1
 
105
    copy->to_null_ptr[0]=1;                     // Always bit 1
127
106
  }
128
107
  else
129
108
  {
130
 
    copy->to_null_ptr[0]= 0;
131
 
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
 
109
    copy->to_null_ptr[0]=0;
 
110
    memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
132
111
  }
133
112
}
134
113
 
210
189
}
211
190
 
212
191
 
213
 
static void do_skip(CopyField *)
 
192
static void do_skip(Copy_field *copy __attribute__((unused)))
214
193
{
215
194
}
216
195
 
217
196
 
218
 
static void do_copy_null(CopyField *copy)
 
197
static void do_copy_null(Copy_field *copy)
219
198
{
220
199
  if (*copy->from_null_ptr & copy->from_bit)
221
200
  {
222
 
    *copy->to_null_ptr|= copy->to_bit;
 
201
    *copy->to_null_ptr|=copy->to_bit;
223
202
    copy->to_field->reset();
224
203
  }
225
204
  else
230
209
}
231
210
 
232
211
 
233
 
static void do_outer_field_null(CopyField *copy)
 
212
static void do_outer_field_null(Copy_field *copy)
234
213
{
235
214
  if (*copy->null_row ||
236
215
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
246
225
}
247
226
 
248
227
 
249
 
static void do_copy_not_null(CopyField *copy)
 
228
static void do_copy_not_null(Copy_field *copy)
250
229
{
251
230
  if (*copy->from_null_ptr & copy->from_bit)
252
231
  {
259
238
}
260
239
 
261
240
 
262
 
static void do_copy_maybe_null(CopyField *copy)
 
241
static void do_copy_maybe_null(Copy_field *copy)
263
242
{
264
243
  *copy->to_null_ptr&= ~copy->to_bit;
265
244
  (copy->do_copy2)(copy);
267
246
 
268
247
/* timestamp and next_number has special handling in case of NULL values */
269
248
 
270
 
static void do_copy_timestamp(CopyField *copy)
 
249
static void do_copy_timestamp(Copy_field *copy)
271
250
{
272
251
  if (*copy->from_null_ptr & copy->from_bit)
273
252
  {
279
258
}
280
259
 
281
260
 
282
 
static void do_copy_next_number(CopyField *copy)
 
261
static void do_copy_next_number(Copy_field *copy)
283
262
{
284
263
  if (*copy->from_null_ptr & copy->from_bit)
285
264
  {
292
271
}
293
272
 
294
273
 
295
 
static void do_copy_blob(CopyField *copy)
 
274
static void do_copy_blob(Copy_field *copy)
296
275
{
297
 
  ulong length= ((Field_blob*) copy->from_field)->get_length();
 
276
  ulong length=((Field_blob*) copy->from_field)->get_length();
298
277
  ((Field_blob*) copy->to_field)->store_length(length);
299
 
  memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
 
278
  memcpy(copy->to_ptr,copy->from_ptr,sizeof(char*));
300
279
}
301
280
 
302
 
static void do_conv_blob(CopyField *copy)
 
281
static void do_conv_blob(Copy_field *copy)
303
282
{
304
283
  copy->from_field->val_str(&copy->tmp);
305
284
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
306
 
                                         copy->tmp.length(),
307
 
                                         copy->tmp.charset());
 
285
                                         copy->tmp.length(),
 
286
                                         copy->tmp.charset());
308
287
}
309
288
 
310
289
/** Save blob in copy->tmp for GROUP BY. */
311
290
 
312
 
static void do_save_blob(CopyField *copy)
 
291
static void do_save_blob(Copy_field *copy)
313
292
{
314
293
  char buff[MAX_FIELD_WIDTH];
315
 
  String res(buff, sizeof(buff), copy->tmp.charset());
 
294
  String res(buff,sizeof(buff),copy->tmp.charset());
316
295
  copy->from_field->val_str(&res);
317
296
  copy->tmp.copy(res);
318
297
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
319
 
                                         copy->tmp.length(),
320
 
                                         copy->tmp.charset());
 
298
                                         copy->tmp.length(),
 
299
                                         copy->tmp.charset());
321
300
}
322
301
 
323
302
 
324
 
static void do_field_string(CopyField *copy)
 
303
static void do_field_string(Copy_field *copy)
325
304
{
326
305
  char buff[MAX_FIELD_WIDTH];
327
306
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
331
310
}
332
311
 
333
312
 
334
 
static void do_field_enum(CopyField *copy)
 
313
static void do_field_enum(Copy_field *copy)
335
314
{
336
315
  if (copy->from_field->val_int() == 0)
337
316
    ((Field_enum *) copy->to_field)->store_type((uint64_t) 0);
340
319
}
341
320
 
342
321
 
343
 
static void do_field_int(CopyField *copy)
 
322
static void do_field_int(Copy_field *copy)
344
323
{
345
324
  int64_t value= copy->from_field->val_int();
346
325
  copy->to_field->store(value,
347
326
                        test(copy->from_field->flags & UNSIGNED_FLAG));
348
327
}
349
328
 
350
 
static void do_field_real(CopyField *copy)
 
329
static void do_field_real(Copy_field *copy)
351
330
{
352
331
  double value=copy->from_field->val_real();
353
332
  copy->to_field->store(value);
354
333
}
355
334
 
356
335
 
357
 
static void do_field_decimal(CopyField *copy)
 
336
static void do_field_decimal(Copy_field *copy)
358
337
{
359
338
  my_decimal value;
360
339
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
366
345
  from string.
367
346
*/
368
347
 
369
 
static void do_cut_string(CopyField *copy)
 
348
static void do_cut_string(Copy_field *copy)
370
349
{
371
350
  const CHARSET_INFO * const cs= copy->from_field->charset();
372
351
  memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
388
367
  from string.
389
368
*/
390
369
 
391
 
static void do_cut_string_complex(CopyField *copy)
 
370
static void do_cut_string_complex(Copy_field *copy)
392
371
{                                               // Shorter string field
393
372
  int well_formed_error;
394
373
  const CHARSET_INFO * const cs= copy->from_field->charset();
395
374
  const unsigned char *from_end= copy->from_ptr + copy->from_length;
396
375
  uint32_t copy_length= cs->cset->well_formed_len(cs,
397
376
                                              (char*) copy->from_ptr,
398
 
                                              (char*) from_end,
 
377
                                              (char*) from_end, 
399
378
                                              copy->to_length / cs->mbmaxlen,
400
379
                                              &well_formed_error);
401
380
  if (copy->to_length < copy_length)
420
399
 
421
400
 
422
401
 
423
 
static void do_expand_binary(CopyField *copy)
424
 
{
425
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
426
 
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
427
 
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
428
 
                 copy->to_length-copy->from_length, '\0');
429
 
}
430
 
 
431
 
 
432
 
 
433
 
static void do_expand_string(CopyField *copy)
434
 
{
435
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
436
 
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
437
 
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
438
 
                 copy->to_length-copy->from_length, ' ');
439
 
}
440
 
 
441
 
 
442
 
static void do_varstring1(CopyField *copy)
443
 
{
444
 
  uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
 
402
static void do_expand_binary(Copy_field *copy)
 
403
{
 
404
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
405
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
406
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
 
407
                     copy->to_length-copy->from_length, '\0');
 
408
}
 
409
 
 
410
 
 
411
 
 
412
static void do_expand_string(Copy_field *copy)
 
413
{
 
414
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
415
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
416
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
 
417
                     copy->to_length-copy->from_length, ' ');
 
418
}
 
419
 
 
420
 
 
421
static void do_varstring1(Copy_field *copy)
 
422
{
 
423
  uint32_t length= (uint) *(unsigned char*) copy->from_ptr;
445
424
  if (length > copy->to_length- 1)
446
425
  {
447
 
    length= copy->to_length - 1;
 
426
    length=copy->to_length - 1;
448
427
    if (copy->from_field->table->in_use->count_cuted_fields)
449
428
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
450
429
                                  ER_WARN_DATA_TRUNCATED, 1);
454
433
}
455
434
 
456
435
 
457
 
static void do_varstring1_mb(CopyField *copy)
 
436
static void do_varstring1_mb(Copy_field *copy)
458
437
{
459
438
  int well_formed_error;
460
439
  const CHARSET_INFO * const cs= copy->from_field->charset();
461
 
  uint32_t from_length= (uint32_t) *(unsigned char*) copy->from_ptr;
 
440
  uint32_t from_length= (uint) *(unsigned char*) copy->from_ptr;
462
441
  const unsigned char *from_ptr= copy->from_ptr + 1;
463
442
  uint32_t to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
464
443
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_ptr,
475
454
}
476
455
 
477
456
 
478
 
static void do_varstring2(CopyField *copy)
 
457
static void do_varstring2(Copy_field *copy)
479
458
{
480
 
  uint32_t length= uint2korr(copy->from_ptr);
 
459
  uint32_t length=uint2korr(copy->from_ptr);
481
460
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
482
461
  {
483
462
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
491
470
}
492
471
 
493
472
 
494
 
static void do_varstring2_mb(CopyField *copy)
 
473
static void do_varstring2_mb(Copy_field *copy)
495
474
{
496
475
  int well_formed_error;
497
476
  const CHARSET_INFO * const cs= copy->from_field->charset();
499
478
  uint32_t from_length= uint2korr(copy->from_ptr);
500
479
  const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
501
480
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_beg,
502
 
                                             (char*) from_beg + from_length,
503
 
                                             char_length, &well_formed_error);
 
481
                                         (char*) from_beg + from_length,
 
482
                                         char_length, &well_formed_error);
504
483
  if (length < from_length)
505
484
  {
506
485
    if (current_session->count_cuted_fields)
507
486
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
508
487
                                  ER_WARN_DATA_TRUNCATED, 1);
509
 
  }
 
488
  }  
510
489
  int2store(copy->to_ptr, length);
511
490
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
512
491
}
513
 
 
 
492
 
514
493
 
515
494
/***************************************************************************
516
 
** The different functions that fills in a CopyField class
 
495
** The different functions that fills in a Copy_field class
517
496
***************************************************************************/
518
497
 
519
498
/**
524
503
  The 'to' buffer should have a size of field->pack_length()+1
525
504
*/
526
505
 
527
 
void CopyField::set(unsigned char *to,Field *from)
 
506
void Copy_field::set(unsigned char *to,Field *from)
528
507
{
529
 
  from_ptr= from->ptr;
530
 
  to_ptr= to;
531
 
  from_length= from->pack_length();
 
508
  from_ptr=from->ptr;
 
509
  to_ptr=to;
 
510
  from_length=from->pack_length();
532
511
  if (from->maybe_null())
533
512
  {
534
 
    from_null_ptr= from->null_ptr;
535
 
    from_bit= from->null_bit;
536
 
    to_ptr[0]= 1;                             // Null as default value
537
 
    to_null_ptr= (unsigned char*) to_ptr++;
538
 
    to_bit= 1;
 
513
    from_null_ptr=from->null_ptr;
 
514
    from_bit=     from->null_bit;
 
515
    to_ptr[0]=    1;                            // Null as default value
 
516
    to_null_ptr=  (unsigned char*) to_ptr++;
 
517
    to_bit=       1;
539
518
    if (from->table->maybe_null)
540
519
    {
541
 
      null_row= &from->table->null_row;
542
 
      do_copy= do_outer_field_to_null_str;
 
520
      null_row=   &from->table->null_row;
 
521
      do_copy=    do_outer_field_to_null_str;
543
522
    }
544
523
    else
545
 
      do_copy= do_field_to_null_str;
 
524
      do_copy=    do_field_to_null_str;
546
525
  }
547
526
  else
548
527
  {
549
 
    to_null_ptr= 0;                           // For easy debugging
550
 
    do_copy= do_field_eq;
 
528
    to_null_ptr=  0;                            // For easy debugging
 
529
    do_copy=      do_field_eq;
551
530
  }
552
531
}
553
532
 
554
533
 
555
534
/*
556
 
  To do:
 
535
  To do: 
557
536
 
558
537
  If 'save\ is set to true and the 'from' is a blob field, do_copy is set to
559
538
  do_save_blob rather than do_conv_blob.  The only differences between them
560
539
  appears to be:
561
540
 
562
 
  - do_save_blob allocates and uses an intermediate buffer before calling
563
 
    Field_blob::store. Is this in order to trigger the call to
 
541
  - do_save_blob allocates and uses an intermediate buffer before calling 
 
542
    Field_blob::store. Is this in order to trigger the call to 
564
543
    well_formed_copy_nchars, by changing the pointer copy->tmp.ptr()?
565
544
    That call will take place anyway in all known cases.
566
545
 
567
 
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
 
546
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended 
568
547
    effect? Truncation is handled by well_formed_copy_nchars anyway.
569
548
 */
570
 
void CopyField::set(Field *to,Field *from,bool save)
 
549
void Copy_field::set(Field *to,Field *from,bool save)
571
550
{
572
551
  if (to->type() == DRIZZLE_TYPE_NULL)
573
552
  {
574
 
    to_null_ptr= 0;           // For easy debugging
575
 
    to_ptr= 0;
576
 
    do_copy= do_skip;
 
553
    to_null_ptr=0;                              // For easy debugging
 
554
    to_ptr=0;
 
555
    do_copy=do_skip;
577
556
    return;
578
557
  }
579
 
  from_field= from;
580
 
  to_field= to;
581
 
  from_ptr= from->ptr;
582
 
  from_length= from->pack_length();
583
 
  to_ptr= to->ptr;
584
 
  to_length= to_field->pack_length();
 
558
  from_field=from;
 
559
  to_field=to;
 
560
  from_ptr=from->ptr;
 
561
  from_length=from->pack_length();
 
562
  to_ptr=  to->ptr;
 
563
  to_length=to_field->pack_length();
585
564
 
586
565
  // set up null handling
587
 
  from_null_ptr= to_null_ptr= 0;
 
566
  from_null_ptr=to_null_ptr=0;
588
567
  if (from->maybe_null())
589
568
  {
590
 
    from_null_ptr= from->null_ptr;
591
 
    from_bit= from->null_bit;
 
569
    from_null_ptr=      from->null_ptr;
 
570
    from_bit=           from->null_bit;
592
571
    if (to_field->real_maybe_null())
593
572
    {
594
 
      to_null_ptr= to->null_ptr;
595
 
      to_bit= to->null_bit;
 
573
      to_null_ptr=      to->null_ptr;
 
574
      to_bit=           to->null_bit;
596
575
      if (from_null_ptr)
597
 
        do_copy= do_copy_null;
 
576
        do_copy=        do_copy_null;
598
577
      else
599
578
      {
600
 
        null_row= &from->table->null_row;
601
 
        do_copy= do_outer_field_null;
 
579
        null_row=       &from->table->null_row;
 
580
        do_copy=        do_outer_field_null;
602
581
      }
603
582
    }
604
583
    else
613
592
  }
614
593
  else if (to_field->real_maybe_null())
615
594
  {
616
 
    to_null_ptr= to->null_ptr;
617
 
    to_bit= to->null_bit;
 
595
    to_null_ptr=        to->null_ptr;
 
596
    to_bit=             to->null_bit;
618
597
    do_copy= do_copy_maybe_null;
619
598
  }
620
599
  else
621
 
   do_copy= 0;
 
600
   do_copy=0;
622
601
 
623
602
  if ((to->flags & BLOB_FLAG) && save)
624
603
    do_copy2= do_save_blob;
625
604
  else
626
605
    do_copy2= get_copy_func(to,from);
627
 
  if (!do_copy)         // Not null
628
 
    do_copy= do_copy2;
 
606
  if (!do_copy)                                 // Not null
 
607
    do_copy=do_copy2;
629
608
}
630
609
 
631
610
 
632
 
CopyField::Copy_func *
633
 
CopyField::get_copy_func(Field *to,Field *from)
 
611
Copy_field::Copy_func *
 
612
Copy_field::get_copy_func(Field *to,Field *from)
634
613
{
635
614
  bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
636
615
                                     from->table->s->db_low_byte_first);
641
620
    if (from_length != to_length || !compatible_db_low_byte_first)
642
621
    {
643
622
      // Correct pointer to point at char pointer
644
 
      to_ptr+= to_length - to->table->s->blob_ptr_size;
 
623
      to_ptr+=   to_length - to->table->s->blob_ptr_size;
645
624
      from_ptr+= from_length- from->table->s->blob_ptr_size;
646
625
      return do_copy_blob;
647
626
    }
650
629
  {
651
630
    if (to->result_type() == DECIMAL_RESULT)
652
631
      return do_field_decimal;
653
 
      
654
632
    // Check if identical fields
655
633
    if (from->result_type() == STRING_RESULT)
656
634
    {
660
638
      */
661
639
      if (to->real_type() != from->real_type() ||
662
640
          !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))
 
641
          (((to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_NEWDATE) || to->type() == DRIZZLE_TYPE_DATETIME))
664
642
      {
665
 
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
666
 
        {
667
 
          if (to->result_type() != STRING_RESULT)
668
 
          {
669
 
            return do_field_int;  // Convert SET to number
670
 
          }
671
 
          
672
 
          return do_field_string;
673
 
        }
 
643
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
 
644
          if (to->result_type() != STRING_RESULT)
 
645
            return do_field_int;                // Convert SET to number
 
646
        return do_field_string;
674
647
      }
675
 
      
676
648
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
677
649
      {
678
 
        if (!to->eq_def(from))
 
650
        if (!to->eq_def(from))
679
651
        {
680
652
          if (from->real_type() == DRIZZLE_TYPE_ENUM &&
681
653
              to->real_type() == DRIZZLE_TYPE_ENUM)
685
657
        }
686
658
      }
687
659
      else if (to->charset() != from->charset())
688
 
        return do_field_string;
 
660
        return do_field_string;
689
661
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
690
662
      {
691
663
        if (((Field_varstring*) to)->length_bytes !=
692
664
            ((Field_varstring*) from)->length_bytes)
693
 
        {
694
665
          return do_field_string;
695
 
        }
696
 
        
697
666
        if (to_length != from_length)
698
 
        {
699
667
          return (((Field_varstring*) to)->length_bytes == 1 ?
700
668
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
701
669
                                                    do_varstring1_mb) :
702
670
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
703
671
                                                    do_varstring2_mb));
704
 
        }
705
672
      }
706
673
      else if (to_length < from_length)
707
 
      {
708
 
        return (from->charset()->mbmaxlen == 1 ?
 
674
        return (from->charset()->mbmaxlen == 1 ?
709
675
                do_cut_string : do_cut_string_complex);
710
 
      }
711
676
      else if (to_length > from_length)
712
677
      {
713
678
        if (to->charset() == &my_charset_bin)
715
680
        else
716
681
          return do_expand_string;
717
682
      }
 
683
 
718
684
    }
719
685
    else if (to->real_type() != from->real_type() ||
720
 
             to_length != from_length ||
 
686
             to_length != from_length ||
721
687
             !compatible_db_low_byte_first)
722
688
    {
723
689
      if (to->result_type() == STRING_RESULT)
724
 
        return do_field_string;
 
690
        return do_field_string;
725
691
      if (to->result_type() == INT_RESULT)
726
 
        return do_field_int;
727
 
 
 
692
        return do_field_int;
728
693
      return do_field_real;
729
694
    }
730
695
    else
731
696
    {
732
697
      if (!to->eq_def(from) || !compatible_db_low_byte_first)
733
698
      {
734
 
        if (to->result_type() == INT_RESULT)
735
 
          return do_field_int;
736
 
        else
737
 
          return do_field_real;
 
699
        if (to->result_type() == INT_RESULT)
 
700
          return do_field_int;
 
701
        else
 
702
          return do_field_real;
738
703
      }
739
704
    }
740
705
  }
741
 
  
742
706
    /* Eq fields */
743
 
  switch (to_length)
744
 
  {
 
707
  switch (to_length) {
745
708
  case 1: return do_field_1;
746
709
  case 2: return do_field_2;
747
710
  case 3: return do_field_3;
749
712
  case 6: return do_field_6;
750
713
  case 8: return do_field_8;
751
714
  }
752
 
  
753
715
  return do_field_eq;
754
716
}
755
717
 
762
724
      !(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
763
725
  {
764
726
    /* Please god, will someone rewrite this to be readable :( */
765
 
    if (to->pack_length() == from->pack_length() &&
766
 
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
767
 
        to->real_type() != DRIZZLE_TYPE_ENUM &&
768
 
        (to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
 
727
    if (to->pack_length() == from->pack_length() && 
 
728
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) && 
 
729
        to->real_type() != DRIZZLE_TYPE_ENUM && 
 
730
        (to->real_type() != DRIZZLE_TYPE_NEWDECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
769
731
        from->charset() == to->charset() &&
770
732
        to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
771
 
        (!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
 
733
        (!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_NEWDATE && to->type() != DRIZZLE_TYPE_DATETIME)) && 
772
734
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
773
735
    {                                           // Identical fields
 
736
#ifdef HAVE_purify
774
737
      /* This may happen if one does 'UPDATE ... SET x=x' */
775
738
      if (to->ptr != from->ptr)
 
739
#endif
776
740
        memcpy(to->ptr,from->ptr,to->pack_length());
777
741
      return 0;
778
742
    }
823
787
  else
824
788
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
825
789
}
826
 
 
827
 
} /* namespace drizzled */