27
27
#include <drizzled/server_includes.h>
29
static void do_field_eq(Copy_field *copy)
31
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
34
static void do_field_1(Copy_field *copy)
36
copy->to_ptr[0]=copy->from_ptr[0];
39
static void do_field_2(Copy_field *copy)
41
copy->to_ptr[0]=copy->from_ptr[0];
42
copy->to_ptr[1]=copy->from_ptr[1];
45
static void do_field_3(Copy_field *copy)
47
copy->to_ptr[0]=copy->from_ptr[0];
48
copy->to_ptr[1]=copy->from_ptr[1];
49
copy->to_ptr[2]=copy->from_ptr[2];
52
static void do_field_4(Copy_field *copy)
54
copy->to_ptr[0]=copy->from_ptr[0];
55
copy->to_ptr[1]=copy->from_ptr[1];
56
copy->to_ptr[2]=copy->from_ptr[2];
57
copy->to_ptr[3]=copy->from_ptr[3];
60
static void do_field_6(Copy_field *copy)
62
copy->to_ptr[0]=copy->from_ptr[0];
63
copy->to_ptr[1]=copy->from_ptr[1];
64
copy->to_ptr[2]=copy->from_ptr[2];
65
copy->to_ptr[3]=copy->from_ptr[3];
66
copy->to_ptr[4]=copy->from_ptr[4];
67
copy->to_ptr[5]=copy->from_ptr[5];
70
static void do_field_8(Copy_field *copy)
72
copy->to_ptr[0]=copy->from_ptr[0];
73
copy->to_ptr[1]=copy->from_ptr[1];
74
copy->to_ptr[2]=copy->from_ptr[2];
75
copy->to_ptr[3]=copy->from_ptr[3];
76
copy->to_ptr[4]=copy->from_ptr[4];
77
copy->to_ptr[5]=copy->from_ptr[5];
78
copy->to_ptr[6]=copy->from_ptr[6];
79
copy->to_ptr[7]=copy->from_ptr[7];
83
static void do_field_to_null_str(Copy_field *copy)
28
#include <drizzled/error.h>
29
#include <drizzled/table.h>
30
#include <drizzled/session.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>
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>
49
static void do_field_eq(CopyField *copy)
51
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
54
static void do_field_1(CopyField *copy)
56
copy->to_ptr[0]= copy->from_ptr[0];
59
static void do_field_2(CopyField *copy)
61
copy->to_ptr[0]= copy->from_ptr[0];
62
copy->to_ptr[1]= copy->from_ptr[1];
65
static void do_field_3(CopyField *copy)
67
copy->to_ptr[0]= copy->from_ptr[0];
68
copy->to_ptr[1]= copy->from_ptr[1];
69
copy->to_ptr[2]= copy->from_ptr[2];
72
static void do_field_4(CopyField *copy)
74
copy->to_ptr[0]= copy->from_ptr[0];
75
copy->to_ptr[1]= copy->from_ptr[1];
76
copy->to_ptr[2]= copy->from_ptr[2];
77
copy->to_ptr[3]= copy->from_ptr[3];
80
static void do_field_6(CopyField *copy)
82
copy->to_ptr[0]= copy->from_ptr[0];
83
copy->to_ptr[1]= copy->from_ptr[1];
84
copy->to_ptr[2]= copy->from_ptr[2];
85
copy->to_ptr[3]= copy->from_ptr[3];
86
copy->to_ptr[4]= copy->from_ptr[4];
87
copy->to_ptr[5]= copy->from_ptr[5];
90
static void do_field_8(CopyField *copy)
92
copy->to_ptr[0]= copy->from_ptr[0];
93
copy->to_ptr[1]= copy->from_ptr[1];
94
copy->to_ptr[2]= copy->from_ptr[2];
95
copy->to_ptr[3]= copy->from_ptr[3];
96
copy->to_ptr[4]= copy->from_ptr[4];
97
copy->to_ptr[5]= copy->from_ptr[5];
98
copy->to_ptr[6]= copy->from_ptr[6];
99
copy->to_ptr[7]= copy->from_ptr[7];
103
static void do_field_to_null_str(CopyField *copy)
85
105
if (*copy->from_null_ptr & copy->from_bit)
87
107
memset(copy->to_ptr, 0, copy->from_length);
88
copy->to_null_ptr[0]=1; // Always bit 1
108
copy->to_null_ptr[0]= 1; // Always bit 1
92
copy->to_null_ptr[0]=0;
93
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
112
copy->to_null_ptr[0]= 0;
113
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
98
static void do_outer_field_to_null_str(Copy_field *copy)
118
static void do_outer_field_to_null_str(CopyField *copy)
100
120
if (*copy->null_row ||
101
121
(copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
103
123
memset(copy->to_ptr, 0, copy->from_length);
104
copy->to_null_ptr[0]=1; // Always bit 1
124
copy->to_null_ptr[0]= 1; // Always bit 1
108
copy->to_null_ptr[0]=0;
109
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
128
copy->to_null_ptr[0]= 0;
129
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
273
static void do_copy_blob(Copy_field *copy)
293
static void do_copy_blob(CopyField *copy)
275
ulong length=((Field_blob*) copy->from_field)->get_length();
295
ulong length= ((Field_blob*) copy->from_field)->get_length();
276
296
((Field_blob*) copy->to_field)->store_length(length);
277
memcpy(copy->to_ptr,copy->from_ptr,sizeof(char*));
297
memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
280
static void do_conv_blob(Copy_field *copy)
300
static void do_conv_blob(CopyField *copy)
282
302
copy->from_field->val_str(©->tmp);
283
303
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
285
copy->tmp.charset());
305
copy->tmp.charset());
288
308
/** Save blob in copy->tmp for GROUP BY. */
290
static void do_save_blob(Copy_field *copy)
310
static void do_save_blob(CopyField *copy)
292
312
char buff[MAX_FIELD_WIDTH];
293
String res(buff,sizeof(buff),copy->tmp.charset());
313
String res(buff, sizeof(buff), copy->tmp.charset());
294
314
copy->from_field->val_str(&res);
295
315
copy->tmp.copy(res);
296
316
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
298
copy->tmp.charset());
318
copy->tmp.charset());
302
static void do_field_string(Copy_field *copy)
322
static void do_field_string(CopyField *copy)
304
324
char buff[MAX_FIELD_WIDTH];
305
325
copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
401
static void do_expand_binary(Copy_field *copy)
403
const CHARSET_INFO * const cs= copy->from_field->charset();
404
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
405
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
406
copy->to_length-copy->from_length, '\0');
411
static void do_expand_string(Copy_field *copy)
413
const CHARSET_INFO * const cs= copy->from_field->charset();
414
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
415
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
416
copy->to_length-copy->from_length, ' ');
420
static void do_varstring1(Copy_field *copy)
422
uint length= (uint) *(uchar*) copy->from_ptr;
421
static void do_expand_binary(CopyField *copy)
423
const CHARSET_INFO * const cs= copy->from_field->charset();
424
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
425
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
426
copy->to_length-copy->from_length, '\0');
431
static void do_expand_string(CopyField *copy)
433
const CHARSET_INFO * const cs= copy->from_field->charset();
434
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
435
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
436
copy->to_length-copy->from_length, ' ');
440
static void do_varstring1(CopyField *copy)
442
uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
423
443
if (length > copy->to_length- 1)
425
length=copy->to_length - 1;
445
length= copy->to_length - 1;
426
446
if (copy->from_field->table->in_use->count_cuted_fields)
427
447
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
428
448
ER_WARN_DATA_TRUNCATED, 1);
430
*(uchar*) copy->to_ptr= (uchar) length;
450
*(unsigned char*) copy->to_ptr= (unsigned char) length;
431
451
memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
435
static void do_varstring1_mb(Copy_field *copy)
455
static void do_varstring1_mb(CopyField *copy)
437
457
int well_formed_error;
438
458
const CHARSET_INFO * const cs= copy->from_field->charset();
439
uint from_length= (uint) *(uchar*) copy->from_ptr;
440
const uchar *from_ptr= copy->from_ptr + 1;
441
uint to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
442
uint length= cs->cset->well_formed_len(cs, (char*) from_ptr,
459
uint32_t from_length= (uint32_t) *(unsigned char*) copy->from_ptr;
460
const unsigned char *from_ptr= copy->from_ptr + 1;
461
uint32_t to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
462
uint32_t length= cs->cset->well_formed_len(cs, (char*) from_ptr,
443
463
(char*) from_ptr + from_length,
444
464
to_char_length, &well_formed_error);
445
465
if (length < from_length)
447
if (current_thd->count_cuted_fields)
467
if (current_session->count_cuted_fields)
448
468
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
449
469
ER_WARN_DATA_TRUNCATED, 1);
451
*copy->to_ptr= (uchar) length;
471
*copy->to_ptr= (unsigned char) length;
452
472
memcpy(copy->to_ptr + 1, from_ptr, length);
456
static void do_varstring2(Copy_field *copy)
476
static void do_varstring2(CopyField *copy)
458
uint length=uint2korr(copy->from_ptr);
478
uint32_t length= uint2korr(copy->from_ptr);
459
479
if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
461
481
length=copy->to_length-HA_KEY_BLOB_LENGTH;
472
static void do_varstring2_mb(Copy_field *copy)
492
static void do_varstring2_mb(CopyField *copy)
474
494
int well_formed_error;
475
495
const CHARSET_INFO * const cs= copy->from_field->charset();
476
uint char_length= (copy->to_length - HA_KEY_BLOB_LENGTH) / cs->mbmaxlen;
477
uint from_length= uint2korr(copy->from_ptr);
478
const uchar *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
479
uint length= cs->cset->well_formed_len(cs, (char*) from_beg,
480
(char*) from_beg + from_length,
481
char_length, &well_formed_error);
496
uint32_t char_length= (copy->to_length - HA_KEY_BLOB_LENGTH) / cs->mbmaxlen;
497
uint32_t from_length= uint2korr(copy->from_ptr);
498
const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
499
uint32_t length= cs->cset->well_formed_len(cs, (char*) from_beg,
500
(char*) from_beg + from_length,
501
char_length, &well_formed_error);
482
502
if (length < from_length)
484
if (current_thd->count_cuted_fields)
504
if (current_session->count_cuted_fields)
485
505
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
486
506
ER_WARN_DATA_TRUNCATED, 1);
488
508
int2store(copy->to_ptr, length);
489
509
memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
493
513
/***************************************************************************
494
** The different functions that fills in a Copy_field class
514
** The different functions that fills in a CopyField class
495
515
***************************************************************************/
502
522
The 'to' buffer should have a size of field->pack_length()+1
505
void Copy_field::set(uchar *to,Field *from)
525
void CopyField::set(unsigned char *to,Field *from)
509
from_length=from->pack_length();
529
from_length= from->pack_length();
510
530
if (from->maybe_null())
512
from_null_ptr=from->null_ptr;
513
from_bit= from->null_bit;
514
to_ptr[0]= 1; // Null as default value
515
to_null_ptr= (uchar*) to_ptr++;
532
from_null_ptr= from->null_ptr;
533
from_bit= from->null_bit;
534
to_ptr[0]= 1; // Null as default value
535
to_null_ptr= (unsigned char*) to_ptr++;
517
537
if (from->table->maybe_null)
519
null_row= &from->table->null_row;
520
do_copy= do_outer_field_to_null_str;
539
null_row= &from->table->null_row;
540
do_copy= do_outer_field_to_null_str;
523
do_copy= do_field_to_null_str;
543
do_copy= do_field_to_null_str;
527
to_null_ptr= 0; // For easy debugging
528
do_copy= do_field_eq;
547
to_null_ptr= 0; // For easy debugging
548
do_copy= do_field_eq;
536
556
If 'save\ is set to true and the 'from' is a blob field, do_copy is set to
537
557
do_save_blob rather than do_conv_blob. The only differences between them
540
- do_save_blob allocates and uses an intermediate buffer before calling
541
Field_blob::store. Is this in order to trigger the call to
560
- do_save_blob allocates and uses an intermediate buffer before calling
561
Field_blob::store. Is this in order to trigger the call to
542
562
well_formed_copy_nchars, by changing the pointer copy->tmp.ptr()?
543
563
That call will take place anyway in all known cases.
545
- The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
565
- The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
546
566
effect? Truncation is handled by well_formed_copy_nchars anyway.
548
void Copy_field::set(Field *to,Field *from,bool save)
568
void CopyField::set(Field *to,Field *from,bool save)
550
570
if (to->type() == DRIZZLE_TYPE_NULL)
552
to_null_ptr=0; // For easy debugging
572
to_null_ptr= 0; // For easy debugging
560
from_length=from->pack_length();
562
to_length=to_field->pack_length();
580
from_length= from->pack_length();
582
to_length= to_field->pack_length();
564
584
// set up null handling
565
from_null_ptr=to_null_ptr=0;
585
from_null_ptr= to_null_ptr= 0;
566
586
if (from->maybe_null())
568
from_null_ptr= from->null_ptr;
569
from_bit= from->null_bit;
588
from_null_ptr= from->null_ptr;
589
from_bit= from->null_bit;
570
590
if (to_field->real_maybe_null())
572
to_null_ptr= to->null_ptr;
573
to_bit= to->null_bit;
592
to_null_ptr= to->null_ptr;
593
to_bit= to->null_bit;
574
594
if (from_null_ptr)
575
do_copy= do_copy_null;
595
do_copy= do_copy_null;
578
null_row= &from->table->null_row;
579
do_copy= do_outer_field_null;
598
null_row= &from->table->null_row;
599
do_copy= do_outer_field_null;
723
760
!(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
725
762
/* Please god, will someone rewrite this to be readable :( */
726
if (to->pack_length() == from->pack_length() &&
727
!(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
728
to->real_type() != DRIZZLE_TYPE_ENUM &&
763
if (to->pack_length() == from->pack_length() &&
764
!(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
765
to->real_type() != DRIZZLE_TYPE_ENUM &&
729
766
(to->real_type() != DRIZZLE_TYPE_NEWDECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
730
767
from->charset() == to->charset() &&
731
768
to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
732
(!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_NEWDATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
769
(!(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
770
(from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
734
771
{ // Identical fields
736
772
/* This may happen if one does 'UPDATE ... SET x=x' */
737
773
if (to->ptr != from->ptr)
739
774
memcpy(to->ptr,from->ptr,to->pack_length());