27
27
#include "config.h"
29
28
#include <drizzled/error.h>
30
29
#include <drizzled/table.h>
31
30
#include <drizzled/session.h>
33
#include <drizzled/copy_field.h>
32
#include <drizzled/field/str.h>
33
#include <drizzled/field/num.h>
34
34
#include <drizzled/field/blob.h>
35
#include <drizzled/field/enum.h>
36
#include <drizzled/field/null.h>
35
37
#include <drizzled/field/date.h>
36
#include <drizzled/field/datetime.h>
37
38
#include <drizzled/field/decimal.h>
39
#include <drizzled/field/real.h>
38
40
#include <drizzled/field/double.h>
39
#include <drizzled/field/enum.h>
40
#include <drizzled/field/epoch.h>
41
#include <drizzled/field/int32.h>
42
#include <drizzled/field/int64.h>
43
#include <drizzled/field/null.h>
44
#include <drizzled/field/num.h>
45
#include <drizzled/field/num.h>
46
#include <drizzled/field/real.h>
47
#include <drizzled/field/str.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>
48
46
#include <drizzled/field/varstring.h>
191
188
when set to NULL (TIMESTAMP fields which allow setting to NULL
192
189
are handled by first check).
194
if (field->is_timestamp())
191
if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
196
((field::Epoch::pointer) field)->set_time();
193
((Field_timestamp*) field)->set_time();
197
194
return 0; // Ok to set time to NULL
201
197
if (field == field->getTable()->next_number_field)
203
199
field->getTable()->auto_increment_field_not_null= false;
204
200
return 0; // field is set in fill_record()
207
202
if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
209
204
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
213
207
if (!field->getTable()->in_use->no_errors)
214
208
my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
256
249
static void do_copy_not_null(CopyField *copy)
258
if (copy->to_field->hasDefault() and *copy->from_null_ptr & copy->from_bit)
260
copy->to_field->set_default();
262
else if (*copy->from_null_ptr & copy->from_bit)
251
if (*copy->from_null_ptr & copy->from_bit)
264
253
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
265
254
ER_WARN_DATA_TRUNCATED, 1);
266
255
copy->to_field->reset();
270
258
(copy->do_copy2)(copy);
319
302
static void do_conv_blob(CopyField *copy)
321
copy->from_field->val_str_internal(©->tmp);
304
copy->from_field->val_str(©->tmp);
322
305
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
323
306
copy->tmp.length(),
324
307
copy->tmp.charset());
331
314
char buff[MAX_FIELD_WIDTH];
332
315
String res(buff, sizeof(buff), copy->tmp.charset());
333
copy->from_field->val_str_internal(&res);
316
copy->from_field->val_str(&res);
334
317
copy->tmp.copy(res);
335
318
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
336
319
copy->tmp.length(),
343
326
char buff[MAX_FIELD_WIDTH];
344
327
copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
345
copy->from_field->val_str_internal(©->tmp);
328
copy->from_field->val_str(©->tmp);
346
329
copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
347
330
copy->tmp.charset());
386
369
static void do_cut_string(CopyField *copy)
388
371
const CHARSET_INFO * const cs= copy->from_field->charset();
389
memcpy(copy->to_ptr, copy->from_ptr, copy->to_length);
372
memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
391
374
/* Check if we loosed any important characters */
392
375
if (cs->cset->scan(cs,
464
447
length= copy->to_length - 1;
465
448
if (copy->from_field->getTable()->in_use->count_cuted_fields)
467
449
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
468
450
ER_WARN_DATA_TRUNCATED, 1);
471
452
*(unsigned char*) copy->to_ptr= (unsigned char) length;
472
453
memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
486
467
if (length < from_length)
488
469
if (current_session->count_cuted_fields)
490
470
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
491
471
ER_WARN_DATA_TRUNCATED, 1);
494
473
*copy->to_ptr= (unsigned char) length;
495
474
memcpy(copy->to_ptr + 1, from_ptr, length);
504
483
length=copy->to_length-HA_KEY_BLOB_LENGTH;
505
484
if (copy->from_field->getTable()->in_use->count_cuted_fields)
507
485
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
508
486
ER_WARN_DATA_TRUNCATED, 1);
511
488
int2store(copy->to_ptr,length);
512
489
memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, copy->from_ptr + HA_KEY_BLOB_LENGTH,
527
504
if (length < from_length)
529
506
if (current_session->count_cuted_fields)
531
507
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
532
508
ER_WARN_DATA_TRUNCATED, 1);
535
510
int2store(copy->to_ptr, length);
536
511
memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
633
if (to_field->is_timestamp())
606
if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
635
607
do_copy= do_copy_timestamp; // Automatic timestamp
637
608
else if (to_field == to_field->getTable()->next_number_field)
639
609
do_copy= do_copy_next_number;
643
611
do_copy= do_copy_not_null;
647
614
else if (to_field->real_maybe_null())
674
641
if (from_length != to_length || !compatible_db_low_byte_first)
676
643
// Correct pointer to point at char pointer
677
to_ptr+= to_length - to->getTable()->getShare()->sizeBlobPtr();
678
from_ptr+= from_length- from->getTable()->getShare()->sizeBlobPtr();
644
to_ptr+= to_length - to->getTable()->getShare()->blob_ptr_size;
645
from_ptr+= from_length- from->getTable()->getShare()->blob_ptr_size;
679
646
return do_copy_blob;
721
688
return do_field_string;
722
689
else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
724
/* Field_blob is not part of the Field_varstring hierarchy,
725
and casting to varstring for calling pack_length_no_ptr()
726
is always incorrect. Previously the below comparison has
727
always evaluated to false as pack_length_no_ptr() for BLOB
728
will return 4 and varstring can only be <= 2.
729
If your brain slightly bleeds as to why this worked for
730
so many years, you are in no way alone.
732
if (from->flags & BLOB_FLAG)
733
return do_field_string;
735
if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
736
(static_cast<Field_varstring*>(from))->pack_length_no_ptr())
691
if (((Field_varstring*) to)->pack_length_no_ptr() !=
692
((Field_varstring*) from)->pack_length_no_ptr())
738
694
return do_field_string;
741
697
if (to_length != from_length)
743
699
return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
744
700
(from->charset()->mbmaxlen == 1 ? do_varstring1 :
746
702
(from->charset()->mbmaxlen == 1 ? do_varstring2 :
750
706
else if (to_length < from_length)
824
780
if (to->type() == DRIZZLE_TYPE_BLOB)
825
781
{ // Be sure the value is stored
826
782
Field_blob *blob=(Field_blob*) to;
827
from->val_str_internal(&blob->value);
783
from->val_str(&blob->value);
829
785
Copy value if copy_blobs is set, or source is not a string and
830
786
we have a pointer to its internal string conversion buffer.
849
805
char buff[MAX_FIELD_WIDTH];
850
806
String result(buff,sizeof(buff),from->charset());
851
from->val_str_internal(&result);
807
from->val_str(&result);
853
809
We use c_ptr_quick() here to make it easier if to is a float/double
854
810
as the conversion routines will do a copy of the result doesn't
858
814
return to->store(result.c_ptr_quick(),result.length(),from->charset());
860
816
else if (from->result_type() == REAL_RESULT)
862
817
return to->store(from->val_real());
864
818
else if (from->result_type() == DECIMAL_RESULT)
867
821
return to->store_decimal(from->val_decimal(&buff));
871
824
return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
875
827
} /* namespace drizzled */