~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to sql/field.cc

Merge with 4.0.3
Some simple optimzations, more comments and indentation changes.
Add ` around database in 'use database' in binary log.
Moved max_error_count and max_warning_count to variables struct.
Removed SHOW_WARNS_COUNT and SHOW_ERRORS_COUNT calls.
Changed string functions to use character set of first string argument as default return characterset
(Each string function can change the above assumption if needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
{
80
80
  const char *end=str+length;
81
81
 
82
 
  while (str != end && isspace(*str))   // Allow start space
 
82
  // Allow start space
 
83
  while (str != end && my_isspace(system_charset_info,*str))
83
84
    str++; /* purecov: inspected */
84
85
  if (str != end && (*str == '-' || *str == '+'))
85
86
    str++;
87
88
    return 0;                                   // Error: Empty string
88
89
  for (; str != end ; str++)
89
90
  {
90
 
    if (!isdigit(*str))
 
91
    if (!my_isdigit(system_charset_info,*str))
91
92
    {
92
93
      if (*str == '.')
93
94
      {                                         // Allow '.0000'
95
96
        if (str == end)
96
97
          return 1;
97
98
      }
98
 
      if (!isspace(*str))
 
99
      if (!my_isspace(system_charset_info,*str))
99
100
        return 0;
100
101
      for (str++ ; str != end ; str++)
101
 
        if (!isspace(*str))
 
102
        if (!my_isspace(system_charset_info,*str))
102
103
          return 0;
103
104
      return 1;
104
105
    }
109
110
 
110
111
static bool test_if_real(const char *str,int length)
111
112
{
112
 
  while (length && isspace(*str))
 
113
  while (length && my_isspace(system_charset_info,*str))
113
114
  {                                             // Allow start space
114
115
    length--; str++;
115
116
  }
118
119
  if (*str == '+' || *str == '-')
119
120
  {
120
121
    length--; str++;
121
 
    if (!length || !(isdigit(*str) || *str == '.'))
 
122
    if (!length || !(my_isdigit(system_charset_info,*str) || *str == '.'))
122
123
      return 0;
123
124
  }
124
 
  while (length && isdigit(*str))
 
125
  while (length && my_isdigit(system_charset_info,*str))
125
126
  {
126
127
    length--; str++;
127
128
  }
130
131
  if (*str == '.')
131
132
  {
132
133
    length--; str++;
133
 
    while (length && isdigit(*str))
 
134
    while (length && my_isdigit(system_charset_info,*str))
134
135
    {
135
136
      length--; str++;
136
137
    }
139
140
    return 1;
140
141
  if (*str == 'E' || *str == 'e')
141
142
  {
142
 
    if (length < 3 || (str[1] != '+' && str[1] != '-') || !isdigit(str[2]))
 
143
    if (length < 3 || (str[1] != '+' && str[1] != '-') || 
 
144
        !my_isdigit(system_charset_info,str[2]))
143
145
      return 0;
144
146
    length-=3;
145
147
    str+=3;
146
 
    while (length && isdigit(*str))
 
148
    while (length && my_isdigit(system_charset_info,*str))
147
149
    {
148
150
      length--; str++;
149
151
    }
150
152
  }
151
153
  for (; length ; length--, str++)
152
154
  {                                             // Allow end space
153
 
    if (!isspace(*str))
 
155
    if (!my_isspace(system_charset_info,*str))
154
156
      return 0;
155
157
  }
156
158
  return 1;
174
176
   field_length(length_arg),null_bit(null_bit_arg)
175
177
{
176
178
  flags=null_ptr ? 0: NOT_NULL_FLAG;
 
179
  comment.str= (char*) "";
 
180
  comment.length=0;
177
181
}
178
182
 
179
183
uint Field::offset()
198
202
  if (is_null())
199
203
    return net_store_null(packet);
200
204
  char buff[MAX_FIELD_WIDTH];
201
 
  String tmp(buff,sizeof(buff));
 
205
  String tmp(buff,sizeof(buff),default_charset_info);
202
206
  val_str(&tmp,&tmp);
203
207
  CONVERT *convert;
204
208
  if ((convert=thd->variables.convert_set))
218
222
 
219
223
void Field_num::make_field(Send_field *field)
220
224
{
 
225
  /* table_cache_key is not set for temp tables */
 
226
  field->db_name=table->table_cache_key ? table->table_cache_key : ""; 
 
227
  field->org_table_name=table->real_name;
221
228
  field->table_name=table_name;
222
 
  field->col_name=field_name;
 
229
  field->col_name=field->org_col_name=field_name;
223
230
  field->length=field_length;
224
231
  field->type=type();
225
232
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
229
236
 
230
237
void Field_str::make_field(Send_field *field)
231
238
{
 
239
  /* table_cache_key is not set for temp tables */
 
240
  field->db_name=table->table_cache_key ? table->table_cache_key : ""; 
 
241
  field->org_table_name=table->real_name;
232
242
  field->table_name=table_name;
233
 
  field->col_name=field_name;
 
243
  field->col_name=field->org_col_name=field_name;
234
244
  field->length=field_length;
235
245
  field->type=type();
236
246
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
261
271
bool Field::get_date(TIME *ltime,bool fuzzydate)
262
272
{
263
273
  char buff[40];
264
 
  String tmp(buff,sizeof(buff)),tmp2,*res;
 
274
  String tmp(buff,sizeof(buff),default_charset_info),tmp2,*res;
265
275
  if (!(res=val_str(&tmp,&tmp2)) ||
266
276
      str_to_TIME(res->ptr(),res->length(),ltime,fuzzydate) == TIMESTAMP_NONE)
267
277
    return 1;
271
281
bool Field::get_time(TIME *ltime)
272
282
{
273
283
  char buff[40];
274
 
  String tmp(buff,sizeof(buff)),tmp2,*res;
 
284
  String tmp(buff,sizeof(buff),default_charset_info),tmp2,*res;
275
285
  if (!(res=val_str(&tmp,&tmp2)) ||
276
286
      str_to_time(res->ptr(),res->length(),ltime))
277
287
    return 1;
285
295
  char buff[25];
286
296
  switch (type)  {
287
297
  case TIMESTAMP_NONE:
288
 
    store("",0);                        // Probably an error
 
298
    store("",0,default_charset_info);   // Probably an error
289
299
    break;
290
300
  case TIMESTAMP_DATE:
291
301
    sprintf(buff,"%04d-%02d-%02d", ltime->year,ltime->month,ltime->day);
292
 
    store(buff,10);
 
302
    store(buff,10,default_charset_info);
293
303
    break;
294
304
  case TIMESTAMP_FULL:
295
305
    sprintf(buff,"%04d-%02d-%02d %02d:%02d:%02d",
296
306
            ltime->year,ltime->month,ltime->day,
297
307
            ltime->hour,ltime->minute,ltime->second);
298
 
    store(buff,19);
 
308
    store(buff,19,default_charset_info);
299
309
    break;
300
310
  case TIMESTAMP_TIME:
301
 
    sprintf(buff, "%02d:%02d:%02d",
302
 
            ltime->hour,ltime->minute,ltime->second);
303
 
    store(buff,(uint) strlen(buff));
 
311
  {
 
312
    ulong length= my_sprintf(buff, (buff, "%02d:%02d:%02d",
 
313
                                    ltime->hour,ltime->minute,ltime->second));
 
314
    store(buff,(uint) length, default_charset_info);
304
315
    break;
305
316
  }
306
317
}
319
330
void
320
331
Field_decimal::reset(void)
321
332
{
322
 
  Field_decimal::store("0",1);
 
333
  Field_decimal::store("0",1,default_charset_info);
323
334
}
324
335
 
325
336
void Field_decimal::overflow(bool negative)
361
372
}
362
373
 
363
374
 
364
 
void Field_decimal::store(const char *from,uint len)
 
375
int Field_decimal::store(const char *from, uint len, CHARSET_INFO *cs)
365
376
{
366
377
  const char *end= from+len;
367
378
  /* The pointer where the field value starts (i.e., "where to write") */
372
383
    specified), '+' or '-'
373
384
  */
374
385
  char sign_char=0;
375
 
 /* The pointers where prezeros start and stop */
 
386
  /* The pointers where prezeros start and stop */
376
387
  const char *pre_zeros_from, *pre_zeros_end;
377
 
 /* The pointers where digits at the left of '.' start and stop */
 
388
  /* The pointers where digits at the left of '.' start and stop */
378
389
  const char *int_digits_from, *int_digits_end;
379
 
 /* The pointers where digits at the right of '.' start and stop */
 
390
  /* The pointers where digits at the right of '.' start and stop */
380
391
  const char *frac_digits_from, *frac_digits_end;
381
 
 /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
 
392
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
382
393
  char expo_sign_char=0;
383
394
  uint exponent=0;                              // value of the exponent
384
395
  /*
388
399
  const char *int_digits_tail_from;
389
400
  /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
390
401
  uint int_digits_added_zeros;
391
 
 /*
392
 
   Pointer used when digits move from the right of the '.' to the left
393
 
   of the '.'
394
 
 */
 
402
  /*
 
403
    Pointer used when digits move from the right of the '.' to the left
 
404
    of the '.'
 
405
  */
395
406
  const char *frac_digits_head_end;
396
 
 /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
 
407
  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
397
408
  uint frac_digits_added_zeros;
398
409
  char *pos,*tmp_left_pos,*tmp_right_pos;
399
410
  /* Pointers that are used as limits (begin and end of the field buffer) */
400
411
  char *left_wall,*right_wall;
401
412
  char tmp_char;
402
 
 /*
403
 
   To remember if current_thd->cuted_fields has already been incremented,
404
 
   to do that only once
405
 
 */
 
413
  /*
 
414
    To remember if current_thd->cuted_fields has already been incremented,
 
415
    to do that only once
 
416
  */
406
417
  bool is_cuted_fields_incr=0;
407
418
 
408
419
  LINT_INIT(int_digits_tail_from);
412
423
 
413
424
  /*
414
425
    There are three steps in this function :
415
 
      - parse the input string
416
 
      - modify the position of digits around the decimal dot '.' 
417
 
        according to the exponent value (if specified)
418
 
        - write the formatted number
 
426
    - parse the input string
 
427
    - modify the position of digits around the decimal dot '.' 
 
428
    according to the exponent value (if specified)
 
429
    - write the formatted number
419
430
  */
420
431
 
421
432
  if ((tmp_dec=dec))
422
433
    tmp_dec++;
423
434
 
424
 
  for (; from !=end && isspace(*from); from++) ; // Read spaces
 
435
  /* skip pre-space */
 
436
  while (from != end && my_isspace(system_charset_info,*from))
 
437
    from++;
425
438
  if (from == end)
426
439
  {
427
440
    current_thd->cuted_fields++;
440
453
      if (sign_char=='-')
441
454
      {
442
455
        Field_decimal::overflow(1);
443
 
        return;
 
456
        return 1;
444
457
      }
445
458
      /* 
446
 
        Defining this will not store "+" for unsigned decimal type even if
447
 
        it is passed in numeric string. This will make some tests to fail
 
459
         Defining this will not store "+" for unsigned decimal type even if
 
460
         it is passed in numeric string. This will make some tests to fail
448
461
      */         
449
462
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
450
463
      else 
457
470
  for (; from!=end && *from == '0'; from++) ;   // Read prezeros
458
471
  pre_zeros_end=int_digits_from=from;      
459
472
  /* Read non zero digits at the left of '.'*/
460
 
  for (; from!=end && isdigit(*from);from++) ;
 
473
  for (; from != end && my_isdigit(system_charset_info, *from) ; from++) ;
461
474
  int_digits_end=from;
462
475
  if (from!=end && *from == '.')                // Some '.' ?
463
476
    from++;
464
477
  frac_digits_from= from;
465
478
  /* Read digits at the right of '.' */
466
 
  for (;from!=end && isdigit(*from); from++) ;
 
479
  for (;from!=end && my_isdigit(system_charset_info, (*from); from++) ;
467
480
  frac_digits_end=from;
468
481
  // Some exponentiation symbol ?
469
482
  if (from != end && (*from == 'e' || *from == 'E'))
479
492
      (the value of the field will be overflow anyway, or 0 anyway, 
480
493
      it does not change anything if the exponent is 2^32 or more
481
494
    */
482
 
    for (;from!=end && isdigit(*from); from++) 
 
495
    for (;from!=end && my_isdigit(system_charset_info, (*from)); from++)
483
496
      exponent=10*exponent+(*from-'0');
484
497
  }
485
498
  
504
517
    Now "move" digits around the decimal dot according to the exponent value,
505
518
    and add necessary zeros.
506
519
    Examples :
507
 
      - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
508
 
      - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
509
 
        between '.' and '1'
 
520
    - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
 
521
    - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
 
522
    between '.' and '1'
510
523
    - 1234.5E-3 : '234' moves at the right of '.'
511
 
      These moves are implemented with pointers which point at the begin
512
 
      and end of each moved segment. Examples :
 
524
    These moves are implemented with pointers which point at the begin
 
525
    and end of each moved segment. Examples :
513
526
    - 1234.5E-3 : before the code below is executed, the int_digits part is
514
 
      from '1' to '4' and the frac_digits part from '5' to '5'. After the code
515
 
      below, the int_digits part is from '1' to '1', the frac_digits_head
516
 
      part is from '2' to '4', and the frac_digits part from '5' to '5'.
 
527
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
 
528
    below, the int_digits part is from '1' to '1', the frac_digits_head
 
529
    part is from '2' to '4', and the frac_digits part from '5' to '5'.
517
530
    - 1234.5E3 : before the code below is executed, the int_digits part is
518
 
      from '1' to '4' and the frac_digits part from '5' to '5'. After the code
519
 
      below, the int_digits part is from '1' to '4', the int_digits_tail
520
 
      part is from '5' to '5', the frac_digits part is empty, and
521
 
      int_digits_added_zeros=2 (to make 1234500).
 
531
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
 
532
    below, the int_digits part is from '1' to '4', the int_digits_tail
 
533
    part is from '5' to '5', the frac_digits part is empty, and
 
534
    int_digits_added_zeros=2 (to make 1234500).
522
535
  */
523
536
  
524
537
  if (!expo_sign_char)
573
586
  {
574
587
    // too big number, change to max or min number
575
588
    Field_decimal::overflow(sign_char == '-');
576
 
    return;
 
589
    return 1;
577
590
  }
578
591
 
579
592
  /*
655
668
        {
656
669
          if (current_thd->count_cuted_fields && !is_cuted_fields_incr) 
657
670
            break; // Go on below to see if we lose non zero digits
658
 
          return;
 
671
          return 0;
659
672
        }
660
673
        *pos++='0';
661
674
      }
668
681
          {
669
682
            if (!is_cuted_fields_incr)
670
683
              current_thd->cuted_fields++;
671
 
            return;
 
684
            return 0;
672
685
          }
673
686
          continue;
674
687
        }
685
698
        {
686
699
          if (!is_cuted_fields_incr)
687
700
            current_thd->cuted_fields++;
688
 
          return;
 
701
          return 0;
689
702
        }
690
703
        continue;
691
704
      }
695
708
    while (pos != right_wall)
696
709
      *pos++='0';                       // Fill with zeros at right of '.'
697
710
  }
 
711
  return 0;
698
712
}
699
713
 
700
714
 
701
 
void Field_decimal::store(double nr)
 
715
int Field_decimal::store(double nr)
702
716
{
703
717
  if (unsigned_flag && nr < 0)
704
718
  {
705
719
    overflow(1);
706
 
    return;
 
720
    return 1;
707
721
  }
708
722
  
709
723
#ifdef HAVE_FINITE
710
724
  if (!finite(nr)) // Handle infinity as special case
711
725
  {
712
726
    overflow(nr < 0.0);
713
 
    return;      
 
727
    return 1;
714
728
  }
715
729
#endif
716
 
  
 
730
 
717
731
  reg4 uint i,length;
718
732
  char fyllchar,*to;
719
733
  char buff[320];
728
742
  length=(uint) strlen(buff);
729
743
 
730
744
  if (length > field_length)
 
745
  {
731
746
    overflow(nr < 0.0);
 
747
    return 1;
 
748
  }
732
749
  else
733
750
  {
734
751
    to=ptr;
735
752
    for (i=field_length-length ; i-- > 0 ;)
736
753
      *to++ = fyllchar;
737
754
    memcpy(to,buff,length);
 
755
    return 0;
738
756
  }
739
757
}
740
758
 
741
759
 
742
 
void Field_decimal::store(longlong nr)
 
760
int Field_decimal::store(longlong nr)
743
761
{
744
762
  if (unsigned_flag && nr < 0)
745
763
  {
746
764
    overflow(1);
747
 
    return;
 
765
    return 1;
748
766
  }
749
767
  char buff[22];
750
768
  uint length=(uint) (longlong10_to_str(nr,buff,-10)-buff);
751
769
  uint int_part=field_length- (dec  ? dec+1 : 0);
752
770
 
753
771
  if (length > int_part)
 
772
  {
754
773
    overflow(test(nr < 0L));                    /* purecov: inspected */
 
774
    return 1;
 
775
  }
755
776
  else
756
777
  {
757
778
    char fyllchar = zerofill ? (char) '0' : (char) ' ';
764
785
      to[length]='.';
765
786
      bfill(to+length+1,dec,'0');
766
787
    }
 
788
    return 0;
767
789
  }
768
790
}
769
791
 
797
819
  if (field_length < tmp_length)                // Error in data
798
820
    val_ptr->length(0);
799
821
  else
800
 
    val_ptr->set((const char*) str,field_length-tmp_length);
 
822
    val_ptr->set((const char*) str,field_length-tmp_length,default_charset_info);
801
823
  return val_ptr;
802
824
}
803
825
 
814
836
  for (end=a_ptr+field_length;
815
837
       a_ptr != end &&
816
838
         (*a_ptr == *b_ptr ||
817
 
          ((isspace(*a_ptr)  || *a_ptr == '+' || *a_ptr == '0') &&
818
 
           (isspace(*b_ptr) || *b_ptr == '+' || *b_ptr == '0')));
 
839
          ((my_isspace(system_charset_info,*a_ptr)  || *a_ptr == '+' || 
 
840
            *a_ptr == '0') &&
 
841
           (my_isspace(system_charset_info,*b_ptr) || *b_ptr == '+' || 
 
842
            *b_ptr == '0')));
819
843
       a_ptr++,b_ptr++)
820
844
  {
821
845
    if (*a_ptr == '-')                          // If both numbers are negative
842
866
  char *str,*end;
843
867
  for (str=ptr,end=ptr+length;
844
868
       str != end &&
845
 
         ((isspace(*str) || *str == '+' || *str == '0')) ;
846
 
 
 
869
         ((my_isspace(system_charset_info,*str) || *str == '+' ||
 
870
           *str == '0')) ;
847
871
       str++)
848
872
    *to++=' ';
849
873
  if (str == end)
854
878
    *to++=1;                                    // Smaller than any number
855
879
    str++;
856
880
    while (str != end)
857
 
      if (isdigit(*str))
 
881
      if (my_isdigit(system_charset_info,*str))
858
882
        *to++= (char) ('9' - *str++);
859
883
      else
860
884
        *to++= *str++;
862
886
  else memcpy(to,str,(uint) (end-str));
863
887
}
864
888
 
 
889
 
865
890
void Field_decimal::sql_type(String &res) const
866
891
{
867
892
  uint tmp=field_length;
878
903
** tiny int
879
904
****************************************************************************/
880
905
 
881
 
void Field_tiny::store(const char *from,uint len)
 
906
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
882
907
{
883
 
  String tmp_str(from,len);
 
908
  String tmp_str(from,len,default_charset_info);
884
909
  long tmp= strtol(tmp_str.c_ptr(),NULL,10);
 
910
  int error= 0;
885
911
 
886
912
  if (unsigned_flag)
887
913
  {
889
915
    {
890
916
      tmp=0; /* purecov: inspected */
891
917
      current_thd->cuted_fields++; /* purecov: inspected */
 
918
      error= 1;
892
919
    }
893
920
    else if (tmp > 255)
894
921
    {
895
922
      tmp= 255;
896
923
      current_thd->cuted_fields++;
 
924
      error= 1;
897
925
    }
898
926
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
 
927
    {
899
928
      current_thd->cuted_fields++;
 
929
      error= 1;
 
930
    }
900
931
  }
901
932
  else
902
933
  {
904
935
    {
905
936
      tmp= -128;
906
937
      current_thd->cuted_fields++;
 
938
      error= 1;
907
939
    }
908
940
    else if (tmp >= 128)
909
941
    {
910
942
      tmp= 127;
911
943
      current_thd->cuted_fields++;
 
944
      error= 1;
912
945
    }
913
946
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
 
947
    {
914
948
      current_thd->cuted_fields++;
 
949
      error= 1;
 
950
    }
915
951
  }
916
952
  ptr[0]= (char) tmp;
 
953
  return error;
917
954
}
918
955
 
919
956
 
920
 
void Field_tiny::store(double nr)
 
957
int Field_tiny::store(double nr)
921
958
{
 
959
  int error= 0;
922
960
  nr=rint(nr);
923
961
  if (unsigned_flag)
924
962
  {
926
964
    {
927
965
      *ptr=0;
928
966
      current_thd->cuted_fields++;
 
967
      error= 1;
929
968
    }
930
969
    else if (nr > 255.0)
931
970
    {
932
971
      *ptr=(char) 255;
933
972
      current_thd->cuted_fields++;
 
973
      error= 1;
934
974
    }
935
975
    else
936
976
      *ptr=(char) nr;
941
981
    {
942
982
      *ptr= (char) -128;
943
983
      current_thd->cuted_fields++;
 
984
      error= 1;
944
985
    }
945
986
    else if (nr > 127.0)
946
987
    {
947
988
      *ptr=127;
948
989
      current_thd->cuted_fields++;
 
990
      error= 1;
949
991
    }
950
992
    else
951
993
      *ptr=(char) nr;
952
994
  }
 
995
  return error;
953
996
}
954
997
 
955
 
void Field_tiny::store(longlong nr)
 
998
int Field_tiny::store(longlong nr)
956
999
{
 
1000
  int error= 0;
957
1001
  if (unsigned_flag)
958
1002
  {
959
1003
    if (nr < 0L)
960
1004
    {
961
1005
      *ptr=0;
962
1006
      current_thd->cuted_fields++;
 
1007
      error= 1;
963
1008
    }
964
1009
    else if (nr > 255L)
965
1010
    {
966
1011
      *ptr= (char) 255;
967
1012
      current_thd->cuted_fields++;
 
1013
      error= 1;
968
1014
    }
969
1015
    else
970
1016
      *ptr=(char) nr;
975
1021
    {
976
1022
      *ptr= (char) -128;
977
1023
      current_thd->cuted_fields++;
 
1024
      error= 1;
978
1025
    }
979
1026
    else if (nr > 127L)
980
1027
    {
981
1028
      *ptr=127;
982
1029
      current_thd->cuted_fields++;
 
1030
      error= 1;
983
1031
    }
984
1032
    else
985
1033
      *ptr=(char) nr;
986
1034
  }
 
1035
  return error;
987
1036
}
988
1037
 
989
1038
 
1049
1098
// Note:  Sometimes this should be fixed to use one strtol() to use
1050
1099
// len and check for garbage after number.
1051
1100
 
1052
 
void Field_short::store(const char *from,uint len)
 
1101
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
1053
1102
{
1054
 
  String tmp_str(from,len);
 
1103
  String tmp_str(from,len,default_charset_info);
1055
1104
  long tmp= strtol(tmp_str.c_ptr(),NULL,10);
 
1105
  int error= 0;
1056
1106
  if (unsigned_flag)
1057
1107
  {
1058
1108
    if (tmp < 0)
1059
1109
    {
1060
1110
      tmp=0;
1061
1111
      current_thd->cuted_fields++;
 
1112
      error= 1;
1062
1113
    }
1063
1114
    else if (tmp > (uint16) ~0)
1064
1115
    {
1065
1116
      tmp=(uint16) ~0;
1066
1117
      current_thd->cuted_fields++;
 
1118
      error= 1;
1067
1119
    }
1068
1120
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
 
1121
    {
1069
1122
      current_thd->cuted_fields++;
 
1123
      error= 1;
 
1124
    }
1070
1125
  }
1071
1126
  else
1072
1127
  {
1074
1129
    {
1075
1130
      tmp= INT_MIN16;
1076
1131
      current_thd->cuted_fields++;
 
1132
      error= 1;
1077
1133
    }
1078
1134
    else if (tmp > INT_MAX16)
1079
1135
    {
1080
1136
      tmp=INT_MAX16;
1081
1137
      current_thd->cuted_fields++;
 
1138
      error= 1;
1082
1139
    }
1083
1140
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
 
1141
    {
1084
1142
      current_thd->cuted_fields++;
 
1143
      error= 1;
 
1144
    }
1085
1145
  }
1086
1146
#ifdef WORDS_BIGENDIAN
1087
1147
  if (table->db_low_byte_first)
1091
1151
  else
1092
1152
#endif
1093
1153
    shortstore(ptr,(short) tmp);
 
1154
  return error;
1094
1155
}
1095
1156
 
1096
1157
 
1097
 
void Field_short::store(double nr)
 
1158
int Field_short::store(double nr)
1098
1159
{
 
1160
  int error= 0;
1099
1161
  int16 res;
1100
1162
  nr=rint(nr);
1101
1163
  if (unsigned_flag)
1104
1166
    {
1105
1167
      res=0;
1106
1168
      current_thd->cuted_fields++;
 
1169
      error= 1;
1107
1170
    }
1108
1171
    else if (nr > (double) (uint16) ~0)
1109
1172
    {
1110
1173
      res=(int16) (uint16) ~0;
1111
1174
      current_thd->cuted_fields++;
 
1175
      error= 1;
1112
1176
    }
1113
1177
    else
1114
1178
      res=(int16) (uint16) nr;
1119
1183
    {
1120
1184
      res=INT_MIN16;
1121
1185
      current_thd->cuted_fields++;
 
1186
      error= 1;
1122
1187
    }
1123
1188
    else if (nr > (double) INT_MAX16)
1124
1189
    {
1125
1190
      res=INT_MAX16;
1126
1191
      current_thd->cuted_fields++;
 
1192
      error= 1;
1127
1193
    }
1128
1194
    else
1129
1195
      res=(int16) nr;
1136
1202
  else
1137
1203
#endif
1138
1204
    shortstore(ptr,res);
 
1205
  return error;
1139
1206
}
1140
1207
 
1141
 
void Field_short::store(longlong nr)
 
1208
int Field_short::store(longlong nr)
1142
1209
{
 
1210
  int error= 0;
1143
1211
  int16 res;
1144
1212
  if (unsigned_flag)
1145
1213
  {
1147
1215
    {
1148
1216
      res=0;
1149
1217
      current_thd->cuted_fields++;
 
1218
      error= 1;
1150
1219
    }
1151
1220
    else if (nr > (longlong) (uint16) ~0)
1152
1221
    {
1153
1222
      res=(int16) (uint16) ~0;
1154
1223
      current_thd->cuted_fields++;
 
1224
      error= 1;
1155
1225
    }
1156
1226
    else
1157
1227
      res=(int16) (uint16) nr;
1162
1232
    {
1163
1233
      res=INT_MIN16;
1164
1234
      current_thd->cuted_fields++;
 
1235
      error= 1;
1165
1236
    }
1166
1237
    else if (nr > INT_MAX16)
1167
1238
    {
1168
1239
      res=INT_MAX16;
1169
1240
      current_thd->cuted_fields++;
 
1241
      error= 1;
1170
1242
    }
1171
1243
    else
1172
1244
      res=(int16) nr;
1179
1251
  else
1180
1252
#endif
1181
1253
    shortstore(ptr,res);
 
1254
  return error;
1182
1255
}
1183
1256
 
1184
1257
 
1289
1362
// Note:  Sometimes this should be fixed to use one strtol() to use
1290
1363
// len and check for garbage after number.
1291
1364
 
1292
 
void Field_medium::store(const char *from,uint len)
 
1365
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
1293
1366
{
1294
 
  String tmp_str(from,len);
 
1367
  String tmp_str(from,len,default_charset_info);
1295
1368
  long tmp= strtol(tmp_str.c_ptr(),NULL,10);
 
1369
  int error= 0;
1296
1370
 
1297
1371
  if (unsigned_flag)
1298
1372
  {
1300
1374
    {
1301
1375
      tmp=0;
1302
1376
      current_thd->cuted_fields++;
 
1377
      error= 1;
1303
1378
    }
1304
1379
    else if (tmp >= (long) (1L << 24))
1305
1380
    {
1306
1381
      tmp=(long) (1L << 24)-1L;
1307
1382
      current_thd->cuted_fields++;
 
1383
      error= 1;
1308
1384
    }
1309
1385
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
 
1386
    {
1310
1387
      current_thd->cuted_fields++;
 
1388
      error= 1;
 
1389
    }
1311
1390
  }
1312
1391
  else
1313
1392
  {
1315
1394
    {
1316
1395
      tmp= INT_MIN24;
1317
1396
      current_thd->cuted_fields++;
 
1397
      error= 1;
1318
1398
    }
1319
1399
    else if (tmp > INT_MAX24)
1320
1400
    {
1321
1401
      tmp=INT_MAX24;
1322
1402
      current_thd->cuted_fields++;
 
1403
      error= 1;
1323
1404
    }
1324
1405
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
 
1406
    {
1325
1407
      current_thd->cuted_fields++;
 
1408
      error= 1;
 
1409
    }
1326
1410
  }
1327
1411
 
1328
1412
  int3store(ptr,tmp);
 
1413
  return error;
1329
1414
}
1330
1415
 
1331
1416
 
1332
 
void Field_medium::store(double nr)
 
1417
int Field_medium::store(double nr)
1333
1418
{
 
1419
  int error= 0;
1334
1420
  nr=rint(nr);
1335
1421
  if (unsigned_flag)
1336
1422
  {
1338
1424
    {
1339
1425
      int3store(ptr,0);
1340
1426
      current_thd->cuted_fields++;
 
1427
      error= 1;
1341
1428
    }
1342
1429
    else if (nr >= (double) (long) (1L << 24))
1343
1430
    {
1344
1431
      uint32 tmp=(uint32) (1L << 24)-1L;
1345
1432
      int3store(ptr,tmp);
1346
1433
      current_thd->cuted_fields++;
 
1434
      error= 1;
1347
1435
    }
1348
1436
    else
1349
1437
      int3store(ptr,(uint32) nr);
1355
1443
      long tmp=(long) INT_MIN24;
1356
1444
      int3store(ptr,tmp);
1357
1445
      current_thd->cuted_fields++;
 
1446
      error= 1;
1358
1447
    }
1359
1448
    else if (nr > (double) INT_MAX24)
1360
1449
    {
1361
1450
      long tmp=(long) INT_MAX24;
1362
1451
      int3store(ptr,tmp);
1363
1452
      current_thd->cuted_fields++;
 
1453
      error= 1;
1364
1454
    }
1365
1455
    else
1366
1456
      int3store(ptr,(long) nr);
1367
1457
  }
 
1458
  return error;
1368
1459
}
1369
1460
 
1370
 
void Field_medium::store(longlong nr)
 
1461
int Field_medium::store(longlong nr)
1371
1462
{
 
1463
  int error= 0;
1372
1464
  if (unsigned_flag)
1373
1465
  {
1374
1466
    if (nr < 0L)
1375
1467
    {
1376
1468
      int3store(ptr,0);
1377
1469
      current_thd->cuted_fields++;
 
1470
      error= 1;
1378
1471
    }
1379
1472
    else if (nr >= (longlong) (long) (1L << 24))
1380
1473
    {
1381
1474
      long tmp=(long) (1L << 24)-1L;;
1382
1475
      int3store(ptr,tmp);
1383
1476
      current_thd->cuted_fields++;
 
1477
      error= 1;
1384
1478
    }
1385
1479
    else
1386
1480
      int3store(ptr,(uint32) nr);
1392
1486
      long tmp=(long) INT_MIN24;
1393
1487
      int3store(ptr,tmp);
1394
1488
      current_thd->cuted_fields++;
 
1489
      error= 1;
1395
1490
    }
1396
1491
    else if (nr > (longlong) INT_MAX24)
1397
1492
    {
1398
1493
      long tmp=(long) INT_MAX24;
1399
1494
      int3store(ptr,tmp);
1400
1495
      current_thd->cuted_fields++;
 
1496
      error= 1;
1401
1497
    }
1402
1498
    else
1403
1499
      int3store(ptr,(long) nr);
1404
1500
  }
 
1501
  return error;
1405
1502
}
1406
1503
 
1407
1504
 
1474
1571
// Note:  Sometimes this should be fixed to use one strtol() to use
1475
1572
// len and check for garbage after number.
1476
1573
 
1477
 
void Field_long::store(const char *from,uint len)
 
1574
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
1478
1575
{
1479
 
  while (len && isspace(*from))
 
1576
  while (len && my_isspace(system_charset_info,*from))
1480
1577
  {
1481
1578
    len--; from++;
1482
1579
  }
1483
1580
  long tmp;
1484
 
  String tmp_str(from,len);
 
1581
  int error= 0;
 
1582
  String tmp_str(from,len,default_charset_info);
1485
1583
  errno=0;
1486
1584
  if (unsigned_flag)
1487
1585
  {
1489
1587
    {
1490
1588
      tmp=0;                                    // Set negative to 0
1491
1589
      errno=ERANGE;
 
1590
      error= 1;
1492
1591
    }
1493
1592
    else
1494
1593
      tmp=(long) strtoul(tmp_str.c_ptr(),NULL,10);
1496
1595
  else
1497
1596
    tmp=strtol(tmp_str.c_ptr(),NULL,10);
1498
1597
  if (errno || current_thd->count_cuted_fields && !test_if_int(from,len))
 
1598
  {
1499
1599
    current_thd->cuted_fields++;
 
1600
    error= 1;
 
1601
  }
1500
1602
#ifdef WORDS_BIGENDIAN
1501
1603
  if (table->db_low_byte_first)
1502
1604
  {
1505
1607
  else
1506
1608
#endif
1507
1609
    longstore(ptr,tmp);
 
1610
  return error;
1508
1611
}
1509
1612
 
1510
1613
 
1511
 
void Field_long::store(double nr)
 
1614
int Field_long::store(double nr)
1512
1615
{
 
1616
  int error= 0;
1513
1617
  int32 res;
1514
1618
  nr=rint(nr);
1515
1619
  if (unsigned_flag)
1518
1622
    {
1519
1623
      res=0;
1520
1624
      current_thd->cuted_fields++;
 
1625
      error= 1;
1521
1626
    }
1522
1627
    else if (nr > (double) (ulong) ~0L)
1523
1628
    {
1524
1629
      res=(int32) (uint32) ~0L;
1525
1630
      current_thd->cuted_fields++;
 
1631
      error= 1;
1526
1632
    }
1527
1633
    else
1528
1634
      res=(int32) (ulong) nr;
1533
1639
    {
1534
1640
      res=(int32) INT_MIN32;
1535
1641
      current_thd->cuted_fields++;
 
1642
      error= 1;
1536
1643
    }
1537
1644
    else if (nr > (double) INT_MAX32)
1538
1645
    {
1539
1646
      res=(int32) INT_MAX32;
1540
1647
      current_thd->cuted_fields++;
 
1648
      error= 1;
1541
1649
    }
1542
1650
    else
1543
1651
      res=(int32) nr;
1550
1658
  else
1551
1659
#endif
1552
1660
    longstore(ptr,res);
 
1661
  return error;
1553
1662
}
1554
1663
 
1555
1664
 
1556
 
void Field_long::store(longlong nr)
 
1665
int Field_long::store(longlong nr)
1557
1666
{
 
1667
  int error= 0;
1558
1668
  int32 res;
1559
1669
  if (unsigned_flag)
1560
1670
  {
1562
1672
    {
1563
1673
      res=0;
1564
1674
      current_thd->cuted_fields++;
 
1675
      error= 1;
1565
1676
    }
1566
1677
    else if (nr >= (LL(1) << 32))
1567
1678
    {
1568
1679
      res=(int32) (uint32) ~0L;
1569
1680
      current_thd->cuted_fields++;
 
1681
      error= 1;
1570
1682
    }
1571
1683
    else
1572
1684
      res=(int32) (uint32) nr;
1577
1689
    {
1578
1690
      res=(int32) INT_MIN32;
1579
1691
      current_thd->cuted_fields++;
 
1692
      error= 1;
1580
1693
    }
1581
1694
    else if (nr > (longlong) INT_MAX32)
1582
1695
    {
1583
1696
      res=(int32) INT_MAX32;
1584
1697
      current_thd->cuted_fields++;
 
1698
      error= 1;
1585
1699
    }
1586
1700
    else
1587
1701
      res=(int32) nr;
1594
1708
  else
1595
1709
#endif
1596
1710
    longstore(ptr,res);
 
1711
  return error;
1597
1712
}
1598
1713
 
1599
1714
 
1702
1817
** longlong int
1703
1818
****************************************************************************/
1704
1819
 
1705
 
void Field_longlong::store(const char *from,uint len)
 
1820
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
1706
1821
{
1707
 
  while (len && isspace(*from))
 
1822
  while (len && my_isspace(system_charset_info,*from))
1708
1823
  {                                             // For easy error check
1709
1824
    len--; from++;
1710
1825
  }
1711
1826
  longlong tmp;
1712
 
  String tmp_str(from,len);
 
1827
  String tmp_str(from,len,default_charset_info);
 
1828
  int error= 0;
1713
1829
  errno=0;
1714
1830
  if (unsigned_flag)
1715
1831
  {
1717
1833
    {
1718
1834
      tmp=0;                                    // Set negative to 0
1719
1835
      errno=ERANGE;
 
1836
      error= 1;
1720
1837
    }
1721
1838
    else
1722
1839
      tmp=(longlong) strtoull(tmp_str.c_ptr(),NULL,10);
1724
1841
  else
1725
1842
    tmp=strtoll(tmp_str.c_ptr(),NULL,10);
1726
1843
  if (errno || current_thd->count_cuted_fields && !test_if_int(from,len))
 
1844
  {
1727
1845
    current_thd->cuted_fields++;
 
1846
    error= 1;
 
1847
  }
1728
1848
#ifdef WORDS_BIGENDIAN
1729
1849
  if (table->db_low_byte_first)
1730
1850
  {
1733
1853
  else
1734
1854
#endif
1735
1855
    longlongstore(ptr,tmp);
 
1856
  return error;
1736
1857
}
1737
1858
 
1738
1859
 
1739
 
void Field_longlong::store(double nr)
 
1860
int Field_longlong::store(double nr)
1740
1861
{
 
1862
  int error= 0;
1741
1863
  longlong res;
1742
1864
  nr=rint(nr);
1743
1865
  if (unsigned_flag)
1746
1868
    {
1747
1869
      res=0;
1748
1870
      current_thd->cuted_fields++;
 
1871
      error= 1;
1749
1872
    }
1750
1873
    else if (nr >= (double) ~ (ulonglong) 0)
1751
1874
    {
1752
1875
      res= ~(longlong) 0;
1753
1876
      current_thd->cuted_fields++;
 
1877
      error= 1;
1754
1878
    }
1755
1879
    else
1756
1880
      res=(longlong) (ulonglong) nr;
1761
1885
    {
1762
1886
      res=(longlong) LONGLONG_MIN;
1763
1887
      current_thd->cuted_fields++;
 
1888
      error= 1;
1764
1889
    }
1765
1890
    else if (nr >= (double) LONGLONG_MAX)
1766
1891
    {
1767
1892
      res=(longlong) LONGLONG_MAX;
1768
1893
      current_thd->cuted_fields++;
 
1894
      error= 1;
1769
1895
    }
1770
1896
    else
1771
1897
      res=(longlong) nr;
1778
1904
  else
1779
1905
#endif
1780
1906
    longlongstore(ptr,res);
 
1907
  return error;
1781
1908
}
1782
1909
 
1783
1910
 
1784
 
void Field_longlong::store(longlong nr)
 
1911
int Field_longlong::store(longlong nr)
1785
1912
{
1786
1913
#ifdef WORDS_BIGENDIAN
1787
1914
  if (table->db_low_byte_first)
1791
1918
  else
1792
1919
#endif
1793
1920
    longlongstore(ptr,nr);
 
1921
  return 0;
1794
1922
}
1795
1923
 
1796
1924
 
1909
2037
** single precision float
1910
2038
****************************************************************************/
1911
2039
 
1912
 
void Field_float::store(const char *from,uint len)
 
2040
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
1913
2041
{
1914
 
  String tmp_str(from,len);
 
2042
  String tmp_str(from,len,default_charset_info);
1915
2043
  errno=0;
1916
2044
  Field_float::store(atof(tmp_str.c_ptr()));
1917
2045
  if (errno || current_thd->count_cuted_fields && !test_if_real(from,len))
 
2046
  {
1918
2047
    current_thd->cuted_fields++;
 
2048
    return 1;
 
2049
  }
 
2050
  return (errno) ? 1 : 0;
1919
2051
}
1920
2052
 
1921
2053
 
1922
 
void Field_float::store(double nr)
 
2054
int Field_float::store(double nr)
1923
2055
{
1924
2056
  float j;
 
2057
  int error= 0;
1925
2058
  if (dec < NOT_FIXED_DEC)
1926
2059
    nr=floor(nr*log_10[dec]+0.5)/log_10[dec]; // To fixed point
1927
2060
  if (unsigned_flag && nr < 0)
1928
2061
  {
1929
2062
    current_thd->cuted_fields++;
1930
2063
    nr=0;
 
2064
    error= 1;
1931
2065
  }
1932
2066
  if (nr < -FLT_MAX)
1933
2067
  {
1934
2068
    j= -FLT_MAX;
1935
2069
    current_thd->cuted_fields++;
 
2070
    error= 1;
1936
2071
  }
1937
2072
  else if (nr > FLT_MAX)
1938
2073
  {
1939
2074
    j=FLT_MAX;
1940
2075
    current_thd->cuted_fields++;
 
2076
    error= 1;
1941
2077
  }
1942
2078
  else
1943
2079
    j= (float) nr;
1949
2085
  else
1950
2086
#endif
1951
2087
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
 
2088
  return error;
1952
2089
}
1953
2090
 
1954
2091
 
1955
 
void Field_float::store(longlong nr)
 
2092
int Field_float::store(longlong nr)
1956
2093
{
 
2094
  int error= 0;
1957
2095
  float j= (float) nr;
1958
2096
  if (unsigned_flag && j < 0)
1959
2097
  {
1960
2098
    current_thd->cuted_fields++;
1961
2099
    j=0;
 
2100
    error= 1;
1962
2101
  }
1963
2102
#ifdef WORDS_BIGENDIAN
1964
2103
  if (table->db_low_byte_first)
1968
2107
  else
1969
2108
#endif
1970
2109
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
 
2110
  return error;
1971
2111
}
1972
2112
 
1973
2113
 
2159
2299
** double precision floating point numbers
2160
2300
****************************************************************************/
2161
2301
 
2162
 
void Field_double::store(const char *from,uint len)
 
2302
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
2163
2303
{
2164
 
  String tmp_str(from,len);
 
2304
  String tmp_str(from,len,default_charset_info);
2165
2305
  errno=0;
 
2306
  int error= 0;
2166
2307
  double j= atof(tmp_str.c_ptr());
2167
2308
  if (errno || current_thd->count_cuted_fields && !test_if_real(from,len))
 
2309
  {
2168
2310
    current_thd->cuted_fields++;
 
2311
    error= 1;
 
2312
  }
2169
2313
  if (unsigned_flag && j < 0)
2170
2314
  {
2171
2315
    current_thd->cuted_fields++;
2172
2316
    j=0;
 
2317
    error= 1;
2173
2318
  }
2174
2319
#ifdef WORDS_BIGENDIAN
2175
2320
  if (table->db_low_byte_first)
2179
2324
  else
2180
2325
#endif
2181
2326
    doublestore(ptr,j);
 
2327
  return error;
2182
2328
}
2183
2329
 
2184
2330
 
2185
 
void Field_double::store(double nr)
 
2331
int Field_double::store(double nr)
2186
2332
{
 
2333
  int error= 0;
2187
2334
  if (dec < NOT_FIXED_DEC)
2188
2335
    nr=floor(nr*log_10[dec]+0.5)/log_10[dec]; // To fixed point
2189
2336
  if (unsigned_flag && nr < 0)
2190
2337
  {
2191
2338
    current_thd->cuted_fields++;
2192
2339
    nr=0;
 
2340
    error= 1;
2193
2341
  }
2194
2342
#ifdef WORDS_BIGENDIAN
2195
2343
  if (table->db_low_byte_first)
2199
2347
  else
2200
2348
#endif
2201
2349
    doublestore(ptr,nr);
 
2350
  return error;
2202
2351
}
2203
2352
 
2204
2353
 
2205
 
void Field_double::store(longlong nr)
 
2354
int Field_double::store(longlong nr)
2206
2355
{
2207
2356
  double j= (double) nr;
 
2357
  int error= 0;
2208
2358
  if (unsigned_flag && j < 0)
2209
2359
  {
2210
2360
    current_thd->cuted_fields++;
 
2361
    error= 1;
2211
2362
    j=0;
2212
2363
  }
2213
2364
#ifdef WORDS_BIGENDIAN
2218
2369
  else
2219
2370
#endif
2220
2371
    doublestore(ptr,j);
 
2372
  return error;
2221
2373
}
2222
2374
 
2223
2375
 
2415
2567
}
2416
2568
 
2417
2569
 
2418
 
void Field_timestamp::store(const char *from,uint len)
 
2570
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
2419
2571
{
2420
2572
  long tmp=(long) str_to_timestamp(from,len);
2421
2573
#ifdef WORDS_BIGENDIAN
2426
2578
  else
2427
2579
#endif
2428
2580
    longstore(ptr,tmp);
 
2581
  return 0;
2429
2582
}
2430
2583
 
2431
2584
void Field_timestamp::fill_and_store(char *from,uint len)
2457
2610
}
2458
2611
 
2459
2612
 
2460
 
void Field_timestamp::store(double nr)
 
2613
int Field_timestamp::store(double nr)
2461
2614
{
 
2615
  int error= 0;
2462
2616
  if (nr < 0 || nr > 99991231235959.0)
2463
2617
  {
2464
 
    nr=0;                                       // Avoid overflow on buff
 
2618
    nr= 0;                                      // Avoid overflow on buff
2465
2619
    current_thd->cuted_fields++;
 
2620
    error= 1;
2466
2621
  }
2467
 
  Field_timestamp::store((longlong) rint(nr));
 
2622
  error|= Field_timestamp::store((longlong) rint(nr));
 
2623
  return error;
2468
2624
}
2469
2625
 
2470
2626
 
2505
2661
}
2506
2662
 
2507
2663
 
2508
 
void Field_timestamp::store(longlong nr)
 
2664
int Field_timestamp::store(longlong nr)
2509
2665
{
2510
2666
  TIME l_time;
2511
2667
  time_t timestamp;
2533
2689
  else
2534
2690
#endif
2535
2691
    longstore(ptr,(uint32) timestamp);
 
2692
  return 0;
2536
2693
}
2537
2694
 
2538
2695
 
2754
2911
** Stored as a 3 byte unsigned int
2755
2912
****************************************************************************/
2756
2913
 
2757
 
void Field_time::store(const char *from,uint len)
 
2914
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
2758
2915
{
2759
2916
  TIME ltime;
2760
2917
  long tmp;
 
2918
  int error= 0;
2761
2919
  if (str_to_time(from,len,&ltime))
 
2920
  {
2762
2921
    tmp=0L;
 
2922
    error= 1;
 
2923
  }
2763
2924
  else
2764
2925
  {
2765
2926
    if (ltime.month)
2769
2930
    {
2770
2931
      tmp=8385959;
2771
2932
      current_thd->cuted_fields++;
 
2933
      error= 1;
2772
2934
    }
2773
2935
  }
2774
2936
  if (ltime.neg)
2775
2937
    tmp= -tmp;
2776
 
  Field_time::store((longlong) tmp);
 
2938
  error |= Field_time::store((longlong) tmp);
 
2939
  return error;
2777
2940
}
2778
2941
 
2779
2942
 
2780
 
void Field_time::store(double nr)
 
2943
int Field_time::store(double nr)
2781
2944
{
2782
2945
  long tmp;
 
2946
  int error= 0;
2783
2947
  if (nr > 8385959.0)
2784
2948
  {
2785
2949
    tmp=8385959L;
2786
2950
    current_thd->cuted_fields++;
 
2951
    error= 1;
2787
2952
  }
2788
2953
  else if (nr < -8385959.0)
2789
2954
  {
2790
2955
    tmp= -8385959L;
2791
2956
    current_thd->cuted_fields++;
 
2957
    error= 1;
2792
2958
  }
2793
2959
  else
2794
2960
  {
2799
2965
    {
2800
2966
      tmp=0;
2801
2967
      current_thd->cuted_fields++;
 
2968
      error= 1;
2802
2969
    }
2803
2970
  }
2804
2971
  int3store(ptr,tmp);
 
2972
  return error;
2805
2973
}
2806
2974
 
2807
2975
 
2808
 
void Field_time::store(longlong nr)
 
2976
int Field_time::store(longlong nr)
2809
2977
{
2810
2978
  long tmp;
 
2979
  int error= 0;
2811
2980
  if (nr > (longlong) 8385959L)
2812
2981
  {
2813
2982
    tmp=8385959L;
2814
2983
    current_thd->cuted_fields++;
 
2984
    error= 1;
2815
2985
  }
2816
2986
  else if (nr < (longlong) -8385959L)
2817
2987
  {
2818
2988
    tmp= -8385959L;
2819
2989
    current_thd->cuted_fields++;
 
2990
    error= 1;
2820
2991
  }
2821
2992
  else
2822
2993
  {
2825
2996
    {
2826
2997
      tmp=0;
2827
2998
      current_thd->cuted_fields++;
 
2999
      error= 1;
2828
3000
    }
2829
3001
  }
2830
3002
  int3store(ptr,tmp);
 
3003
  return error;
2831
3004
}
2832
3005
 
2833
3006
 
2894
3067
 
2895
3068
void Field_time::sql_type(String &res) const
2896
3069
{
2897
 
  res.set("time",4);
 
3070
  res.set("time",4,default_charset_info);
2898
3071
}
2899
3072
 
2900
3073
/****************************************************************************
2903
3076
** Can handle 2 byte or 4 byte years!
2904
3077
****************************************************************************/
2905
3078
 
2906
 
void Field_year::store(const char *from, uint len)
 
3079
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
2907
3080
{
2908
 
  String tmp_str(from,len);
 
3081
  String tmp_str(from,len,default_charset_info);
2909
3082
  long nr= strtol(tmp_str.c_ptr(),NULL,10);
2910
3083
 
2911
3084
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
2912
3085
  {
2913
3086
    *ptr=0;
2914
3087
    current_thd->cuted_fields++;
2915
 
    return;
 
3088
    return 1;
2916
3089
  }
2917
3090
  else if (current_thd->count_cuted_fields && !test_if_int(from,len))
2918
3091
    current_thd->cuted_fields++;
2924
3097
      nr-= 1900;
2925
3098
  }
2926
3099
  *ptr= (char) (unsigned char) nr;
 
3100
  return 0;
2927
3101
}
2928
3102
 
2929
 
void Field_year::store(double nr)
 
3103
int Field_year::store(double nr)
2930
3104
{
2931
3105
  if (nr < 0.0 || nr >= 2155.0)
2932
 
    Field_year::store((longlong) -1);
 
3106
  {
 
3107
    (void) Field_year::store((longlong) -1);
 
3108
    return 1;
 
3109
  }
2933
3110
  else
2934
 
    Field_year::store((longlong) nr);
 
3111
    return Field_year::store((longlong) nr);
2935
3112
}
2936
3113
 
2937
 
void Field_year::store(longlong nr)
 
3114
int Field_year::store(longlong nr)
2938
3115
{
2939
3116
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
2940
3117
  {
2941
3118
    *ptr=0;
2942
3119
    current_thd->cuted_fields++;
2943
 
    return;
 
3120
    return 1;
2944
3121
  }
2945
3122
  if (nr != 0 || field_length != 4)             // 0000 -> 0; 00 -> 2000
2946
3123
  {
2950
3127
      nr-= 1900;
2951
3128
  }
2952
3129
  *ptr= (char) (unsigned char) nr;
 
3130
  return 0;
2953
3131
}
2954
3132
 
2955
3133
 
2992
3170
** Stored as a 4 byte unsigned int
2993
3171
****************************************************************************/
2994
3172
 
2995
 
void Field_date::store(const char *from, uint len)
 
3173
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
2996
3174
{
2997
3175
  TIME l_time;
2998
3176
  uint32 tmp;
 
3177
  int error= 0;
2999
3178
  if (str_to_TIME(from,len,&l_time,1) == TIMESTAMP_NONE)
 
3179
  {
3000
3180
    tmp=0;
 
3181
    error= 1;
 
3182
  }
3001
3183
  else
3002
3184
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
3003
3185
#ifdef WORDS_BIGENDIAN
3008
3190
  else
3009
3191
#endif
3010
3192
    longstore(ptr,tmp);
 
3193
  return error;
3011
3194
}
3012
3195
 
3013
3196
 
3014
 
void Field_date::store(double nr)
 
3197
int Field_date::store(double nr)
3015
3198
{
3016
3199
  long tmp;
 
3200
  int error= 0;
3017
3201
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
3018
3202
    nr=floor(nr/1000000.0);                     // Timestamp to date
3019
3203
  if (nr < 0.0 || nr > 99991231.0)
3020
3204
  {
3021
3205
    tmp=0L;
3022
3206
    current_thd->cuted_fields++;
 
3207
    error= 1;
3023
3208
  }
3024
3209
  else
3025
3210
    tmp=(long) rint(nr);
3031
3216
  else
3032
3217
#endif
3033
3218
    longstore(ptr,tmp);
 
3219
  return error;
3034
3220
}
3035
3221
 
3036
3222
 
3037
 
void Field_date::store(longlong nr)
 
3223
int Field_date::store(longlong nr)
3038
3224
{
3039
3225
  long tmp;
 
3226
  int error= 0;
3040
3227
  if (nr >= LL(19000000000000) && nr < LL(99991231235959))
3041
3228
    nr=nr/LL(1000000);                  // Timestamp to date
3042
3229
  if (nr < 0 || nr > LL(99991231))
3043
3230
  {
3044
3231
    tmp=0L;
3045
3232
    current_thd->cuted_fields++;
 
3233
    error= 1;
3046
3234
  }
3047
3235
  else
3048
3236
    tmp=(long) nr;
3054
3242
  else
3055
3243
#endif
3056
3244
    longstore(ptr,tmp);
 
3245
  return error;
3057
3246
}
3058
3247
 
3059
3248
 
3140
3329
 
3141
3330
void Field_date::sql_type(String &res) const
3142
3331
{
3143
 
  res.set("date",4);
 
3332
  res.set("date",4,default_charset_info);
3144
3333
}
3145
3334
 
3146
3335
/****************************************************************************
3149
3338
** In number context: YYYYMMDD
3150
3339
****************************************************************************/
3151
3340
 
3152
 
void Field_newdate::store(const char *from,uint len)
 
3341
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
3153
3342
{
3154
3343
  TIME l_time;
3155
3344
  long tmp;
 
3345
  int error= 0;
3156
3346
  if (str_to_TIME(from,len,&l_time,1) == TIMESTAMP_NONE)
 
3347
  {
3157
3348
    tmp=0L;
 
3349
    error= 1;
 
3350
  }
3158
3351
  else
3159
3352
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
3160
3353
  int3store(ptr,tmp);
 
3354
  return error;
3161
3355
}
3162
3356
 
3163
 
void Field_newdate::store(double nr)
 
3357
int Field_newdate::store(double nr)
3164
3358
{
3165
3359
  if (nr < 0.0 || nr > 99991231235959.0)
3166
 
    Field_newdate::store((longlong) -1);
 
3360
  {
 
3361
    (void) Field_newdate::store((longlong) -1);
 
3362
    return 1;
 
3363
  }
3167
3364
  else
3168
 
    Field_newdate::store((longlong) rint(nr));
 
3365
    return Field_newdate::store((longlong) rint(nr));
3169
3366
}
3170
3367
 
3171
3368
 
3172
 
void Field_newdate::store(longlong nr)
 
3369
int Field_newdate::store(longlong nr)
3173
3370
{
3174
3371
  int32 tmp;
 
3372
  int error= 0;
3175
3373
  if (nr >= LL(100000000) && nr <= LL(99991231235959))
3176
3374
    nr=nr/LL(1000000);                  // Timestamp to date
3177
3375
  if (nr < 0L || nr > 99991231L)
3178
3376
  {
3179
3377
    tmp=0;
3180
3378
    current_thd->cuted_fields++;
 
3379
    error= 1;
3181
3380
  }
3182
3381
  else
3183
3382
  {
3195
3394
    {
3196
3395
      tmp=0L;                                   // Don't allow date to change
3197
3396
      current_thd->cuted_fields++;
 
3397
      error= 1;
3198
3398
    }
3199
3399
    else
3200
3400
      tmp= day + month*32 + (tmp/10000)*16*32;
3201
3401
  }
3202
3402
  int3store(ptr,(int32) tmp);
 
3403
  return error;
3203
3404
}
3204
3405
 
3205
3406
void Field_newdate::store_time(TIME *ltime,timestamp_type type)
3291
3492
 
3292
3493
void Field_newdate::sql_type(String &res) const
3293
3494
{
3294
 
  res.set("date",4);
 
3495
  res.set("date",4,default_charset_info);
3295
3496
}
3296
3497
 
3297
3498
 
3302
3503
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
3303
3504
****************************************************************************/
3304
3505
 
3305
 
void Field_datetime::store(const char *from,uint len)
 
3506
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
3306
3507
{
3307
3508
  longlong tmp=str_to_datetime(from,len,1);
3308
3509
#ifdef WORDS_BIGENDIAN
3313
3514
  else
3314
3515
#endif
3315
3516
    longlongstore(ptr,tmp);
 
3517
  return 0;
3316
3518
}
3317
3519
 
3318
3520
 
3319
 
void Field_datetime::store(double nr)
 
3521
int Field_datetime::store(double nr)
3320
3522
{
 
3523
  int error= 0;
3321
3524
  if (nr < 0.0 || nr > 99991231235959.0)
3322
3525
  {
3323
3526
    nr=0.0;
3324
3527
    current_thd->cuted_fields++;
 
3528
    error= 1;
3325
3529
  }
3326
 
  Field_datetime::store((longlong) rint(nr));
 
3530
  error |= Field_datetime::store((longlong) rint(nr));
 
3531
  return error;
3327
3532
}
3328
3533
 
3329
3534
 
3330
 
void Field_datetime::store(longlong nr)
 
3535
int Field_datetime::store(longlong nr)
3331
3536
{
 
3537
  int error= 0;
3332
3538
  if (nr < 0 || nr > LL(99991231235959))
3333
3539
  {
3334
3540
    nr=0;
3335
3541
    current_thd->cuted_fields++;
 
3542
    error= 1;
3336
3543
  }
3337
3544
  else
3338
3545
    nr=fix_datetime(nr);
3344
3551
  else
3345
3552
#endif
3346
3553
    longlongstore(ptr,nr);
 
3554
  return error;
3347
3555
}
3348
3556
 
3349
3557
void Field_datetime::store_time(TIME *ltime,timestamp_type type)
3508
3716
 
3509
3717
void Field_datetime::sql_type(String &res) const
3510
3718
{
3511
 
  res.set("datetime",8);
 
3719
  res.set("datetime",8,default_charset_info);
3512
3720
}
3513
3721
 
3514
3722
/****************************************************************************
3518
3726
 
3519
3727
        /* Copy a string and fill with space */
3520
3728
 
3521
 
void Field_string::store(const char *from,uint length)
 
3729
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
3522
3730
{
 
3731
  field_charset=cs;
 
3732
  int error= 0;
3523
3733
#ifdef USE_TIS620
3524
3734
  if (!binary_flag) {
3525
3735
    ThNormalize((uchar *)ptr, field_length, (uchar *)from, length);
3542
3752
      const char *end=from+length;
3543
3753
      for (from+=field_length ; from != end ; from++)
3544
3754
      {
3545
 
        if (!isspace(*from))
 
3755
        if (!my_isspace(field_charset,*from))
3546
3756
        {
3547
3757
          current_thd->cuted_fields++;
 
3758
          error=1;
3548
3759
          break;
3549
3760
        }
3550
3761
      }
3551
3762
    }
3552
3763
  }
3553
3764
#endif /* USE_TIS620 */
 
3765
  return error;
3554
3766
}
3555
3767
 
3556
3768
 
3557
 
void Field_string::store(double nr)
 
3769
int Field_string::store(double nr)
3558
3770
{
3559
3771
  char buff[MAX_FIELD_WIDTH],*end;
3560
3772
  int width=min(field_length,DBL_DIG+5);
3561
3773
  sprintf(buff,"%-*.*g",width,max(width-5,0),nr);
3562
3774
  end=strcend(buff,' ');
3563
 
  Field_string::store(buff,(uint) (end - buff));
 
3775
  return Field_string::store(buff,(uint) (end - buff), default_charset_info);
3564
3776
}
3565
3777
 
3566
3778
 
3567
 
void Field_string::store(longlong nr)
 
3779
int Field_string::store(longlong nr)
3568
3780
{
3569
3781
  char buff[22];
3570
3782
  char *end=longlong10_to_str(nr,buff,-10);
3571
 
  Field_string::store(buff,(uint) (end-buff));
 
3783
  return Field_string::store(buff,(uint) (end-buff), default_charset_info);
3572
3784
}
3573
3785
 
3574
3786
 
3603
3815
#endif
3604
3816
    while (end > ptr && end[-1] == ' ')
3605
3817
      end--;
3606
 
  val_ptr->set((const char*) ptr,(uint) (end - ptr));
 
3818
  val_ptr->set((const char*) ptr,(uint) (end - ptr),field_charset);
3607
3819
  return val_ptr;
3608
3820
}
3609
3821
 
3613
3825
  if (binary_flag)
3614
3826
    return memcmp(a_ptr,b_ptr,field_length);
3615
3827
  else
3616
 
    return my_sortcmp(a_ptr,b_ptr,field_length);
 
3828
    return my_sortcmp(field_charset,a_ptr,b_ptr,field_length);
3617
3829
}
3618
3830
 
3619
3831
void Field_string::sort_string(char *to,uint length)
3623
3835
  else
3624
3836
  {
3625
3837
#ifdef USE_STRCOLL
3626
 
    if (use_strcoll(default_charset_info)) {
3627
 
      uint tmp=my_strnxfrm(default_charset_info,
3628
 
                          (unsigned char *)to, (unsigned char *) ptr,
3629
 
                          length, field_length);
 
3838
    if (use_strcoll(field_charset)) {
 
3839
      uint tmp=my_strnxfrm(field_charset,
 
3840
                          (unsigned char *)to, length,
 
3841
                          (unsigned char *) ptr, field_length);
3630
3842
      if (tmp < length)
3631
3843
        bzero(to + tmp, length - tmp);
3632
3844
    }
3633
3845
    else
3634
3846
#endif
3635
3847
      for (char *from=ptr,*end=ptr+length ; from != end ;)
3636
 
        *to++=(char) my_sort_order[(uint) (uchar) *from++];
 
3848
        *to++=(char) field_charset->sort_order[(uint) (uchar) *from++];
3637
3849
  }
3638
3850
}
3639
3851
 
3648
3860
  res.length((uint) strlen(res.ptr()));
3649
3861
  if (binary_flag)
3650
3862
    res.append(" binary");
 
3863
  else
 
3864
  {
 
3865
    res.append(" character set ");
 
3866
    res.append(field_charset->name);
 
3867
  }
3651
3868
}
3652
3869
 
3653
3870
 
3682
3899
    int cmp= memcmp(a,b,min(a_length,b_length));
3683
3900
    return cmp ? cmp : (int) (a_length - b_length);
3684
3901
  }
3685
 
  return my_sortncmp(a,a_length, b,b_length);
 
3902
  return my_sortncmp(field_charset, a,a_length, b,b_length);
3686
3903
}
3687
3904
 
3688
3905
 
3699
3916
    int cmp= memcmp(ptr,b,min(a_length,b_length));
3700
3917
    return cmp ? cmp : (int) (a_length - b_length);
3701
3918
  }
3702
 
  return my_sortncmp(ptr,a_length, b, b_length);
 
3919
  return my_sortncmp(field_charset, ptr,a_length, b, b_length);
3703
3920
}
3704
3921
 
3705
3922
 
3722
3939
****************************************************************************/
3723
3940
 
3724
3941
 
3725
 
void Field_varstring::store(const char *from,uint length)
 
3942
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
3726
3943
{
 
3944
  int error= 0;
 
3945
  field_charset=cs;
3727
3946
#ifdef USE_TIS620
3728
3947
  if (!binary_flag)
3729
3948
  {
3739
3958
    length=field_length;
3740
3959
    memcpy(ptr+2,from,field_length);
3741
3960
    current_thd->cuted_fields++;
 
3961
    error= 1;
3742
3962
  }
3743
3963
#endif /* USE_TIS620 */
3744
3964
  int2store(ptr,length);
 
3965
  return error;
3745
3966
}
3746
3967
 
3747
3968
 
3748
 
void Field_varstring::store(double nr)
 
3969
int Field_varstring::store(double nr)
3749
3970
{
3750
3971
  char buff[MAX_FIELD_WIDTH],*end;
3751
3972
  int width=min(field_length,DBL_DIG+5);
3752
3973
  sprintf(buff,"%-*.*g",width,max(width-5,0),nr);
3753
3974
  end=strcend(buff,' ');
3754
 
  Field_varstring::store(buff,(uint) (end - buff));
 
3975
  return Field_varstring::store(buff,(uint) (end - buff), default_charset_info);
3755
3976
}
3756
3977
 
3757
3978
 
3758
 
void Field_varstring::store(longlong nr)
 
3979
int Field_varstring::store(longlong nr)
3759
3980
{
3760
3981
  char buff[22];
3761
3982
  char *end=longlong10_to_str(nr,buff,-10);
3762
 
  Field_varstring::store(buff,(uint) (end-buff));
 
3983
  return Field_varstring::store(buff,(uint) (end-buff), default_charset_info);
3763
3984
}
3764
3985
 
3765
3986
 
3791
4012
                                 String *val_ptr)
3792
4013
{
3793
4014
  uint length=uint2korr(ptr);
3794
 
  val_ptr->set((const char*) ptr+2,length);
 
4015
  val_ptr->set((const char*) ptr+2,length,field_charset);
3795
4016
  return val_ptr;
3796
4017
}
3797
4018
 
3804
4025
  if (binary_flag)
3805
4026
    diff=memcmp(a_ptr+2,b_ptr+2,min(a_length,b_length));
3806
4027
  else
3807
 
    diff=my_sortcmp(a_ptr+2,b_ptr+2,min(a_length,b_length));
 
4028
    diff=my_sortcmp(field_charset, a_ptr+2,b_ptr+2,min(a_length,b_length));
3808
4029
  return diff ? diff : (int) (a_length - b_length);
3809
4030
}
3810
4031
 
3816
4037
  else
3817
4038
  {
3818
4039
#ifdef USE_STRCOLL
3819
 
    if (use_strcoll(default_charset_info))
3820
 
      tot_length=my_strnxfrm(default_charset_info,
3821
 
                             (unsigned char *) to, (unsigned char *)ptr+2,
3822
 
                             length, tot_length);
 
4040
    if (use_strcoll(field_charset))
 
4041
      tot_length=my_strnxfrm(field_charset,
 
4042
                             (unsigned char *) to, length,
 
4043
                             (unsigned char *)ptr+2, tot_length);
3823
4044
    else
3824
4045
    {
3825
4046
#endif
3827
4048
      if (tot_length > length)
3828
4049
        tot_length=length;
3829
4050
      for (char *from=ptr+2,*end=from+tot_length ; from != end ;)
3830
 
        *tmp++=(char) my_sort_order[(uint) (uchar) *from++];
 
4051
        *tmp++=(char) field_charset->sort_order[(uint) (uchar) *from++];
3831
4052
#ifdef USE_STRCOLL
3832
4053
    }
3833
4054
#endif
3843
4064
  res.length((uint) strlen(res.ptr()));
3844
4065
  if (binary_flag)
3845
4066
    res.append(" binary");
 
4067
  else
 
4068
  {
 
4069
    res.append(" character set ");
 
4070
    res.append(field_charset->name);
 
4071
  }
3846
4072
}
3847
4073
 
3848
4074
char *Field_varstring::pack(char *to, const char *from, uint max_length)
3898
4124
    int cmp= memcmp(a,b,min(a_length,b_length));
3899
4125
    return cmp ? cmp : (int) (a_length - b_length);
3900
4126
  }
3901
 
  return my_sortncmp(a,a_length, b,b_length);
 
4127
  return my_sortncmp(field_charset, a,a_length, b,b_length);
3902
4128
}
3903
4129
 
3904
4130
int Field_varstring::pack_cmp(const char *b, uint key_length)
3919
4145
    int cmp= memcmp(a,b,min(a_length,b_length));
3920
4146
    return cmp ? cmp : (int) (a_length - b_length);
3921
4147
  }
3922
 
  return my_sortncmp(a,a_length, b,b_length);
 
4148
  return my_sortncmp(field_charset, a,a_length, b,b_length);
3923
4149
}
3924
4150
 
3925
4151
uint Field_varstring::packed_col_length(const char *ptr, uint length)
3944
4170
Field_blob::Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3945
4171
                       enum utype unireg_check_arg, const char *field_name_arg,
3946
4172
                       struct st_table *table_arg,uint blob_pack_length,
3947
 
                       bool binary_arg)
 
4173
                       bool binary_arg, CHARSET_INFO *cs)
3948
4174
  :Field_str(ptr_arg, (1L << min(blob_pack_length,3)*8)-1L,
3949
4175
             null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
3950
 
             table_arg),
3951
 
   packlength(blob_pack_length),binary_flag(binary_arg)
 
4176
             table_arg, cs),
 
4177
   packlength(blob_pack_length),binary_flag(binary_arg), geom_flag(true)
3952
4178
{
3953
4179
  flags|= BLOB_FLAG;
3954
4180
  if (binary_arg)
4039
4265
}
4040
4266
 
4041
4267
 
4042
 
void Field_blob::store(const char *from,uint len)
 
4268
int Field_blob::store(const char *from,uint len,CHARSET_INFO *cs)
4043
4269
{
 
4270
  field_charset=cs;
4044
4271
  if (!len)
4045
4272
  {
4046
4273
    bzero(ptr,Field_blob::pack_length());
4072
4299
    }
4073
4300
    bmove(ptr+packlength,(char*) &from,sizeof(char*));
4074
4301
  }
4075
 
}
4076
 
 
4077
 
 
4078
 
void Field_blob::store(double nr)
4079
 
{
4080
 
  value.set(nr);
4081
 
  Field_blob::store(value.ptr(),(uint) value.length());
4082
 
}
4083
 
 
4084
 
 
4085
 
void Field_blob::store(longlong nr)
4086
 
{
4087
 
  value.set(nr);
4088
 
  Field_blob::store(value.ptr(), (uint) value.length());
 
4302
  return 0;
 
4303
}
 
4304
 
 
4305
 
 
4306
int Field_blob::store(double nr)
 
4307
{
 
4308
  value.set(nr);
 
4309
  return Field_blob::store(value.ptr(),(uint) value.length(),
 
4310
                           default_charset_info);
 
4311
}
 
4312
 
 
4313
 
 
4314
int Field_blob::store(longlong nr)
 
4315
{
 
4316
  value.set(nr);
 
4317
  return Field_blob::store(value.ptr(), (uint) value.length(),
 
4318
                           default_charset_info);
4089
4319
}
4090
4320
 
4091
4321
 
4128
4358
  char *blob;
4129
4359
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
4130
4360
  if (!blob)
4131
 
    val_ptr->set("",0);                         // A bit safer than ->length(0)
 
4361
    val_ptr->set("",0,field_charset);   // A bit safer than ->length(0)
4132
4362
  else
4133
 
    val_ptr->set((const char*) blob,get_length(ptr));
 
4363
    val_ptr->set((const char*) blob,get_length(ptr),field_charset);
4134
4364
  return val_ptr;
4135
4365
}
4136
4366
 
4142
4372
  if (binary_flag)
4143
4373
    diff=memcmp(a,b,min(a_length,b_length));
4144
4374
  else
4145
 
    diff=my_sortcmp(a,b,min(a_length,b_length));
 
4375
    diff=my_sortcmp(field_charset, a,b,min(a_length,b_length));
4146
4376
  return diff ? diff : (int) (a_length - b_length);
4147
4377
}
4148
4378
 
4190
4420
 
4191
4421
/* The following is used only when comparing a key */
4192
4422
 
4193
 
void Field_blob::get_key_image(char *buff,uint length)
 
4423
void Field_blob::get_key_image(char *buff,uint length, imagetype type)
4194
4424
{
 
4425
  length-= HA_KEY_BLOB_LENGTH;
 
4426
  uint32 blob_length= get_length(ptr);
 
4427
  char *blob;
 
4428
 
 
4429
  if (type == itMBR)
 
4430
  {
 
4431
    if (!blob_length)
 
4432
      return;
 
4433
    get_ptr(&blob);
 
4434
 
 
4435
    MBR mbr;
 
4436
    Geometry gobj;
 
4437
    gobj.create_from_wkb(blob,blob_length);
 
4438
    gobj.get_mbr(&mbr);
 
4439
    float8store(buff,    mbr.xmin);
 
4440
    float8store(buff+8,  mbr.xmax);
 
4441
    float8store(buff+16, mbr.ymin);
 
4442
    float8store(buff+24, mbr.ymax);
 
4443
    return;
 
4444
  }
 
4445
 
4195
4446
  length-=HA_KEY_BLOB_LENGTH;
4196
 
  uint32 blob_length=get_length(ptr);
4197
 
  char *blob;
4198
4447
  if ((uint32) length > blob_length)
4199
4448
  {
4200
4449
#ifdef HAVE_purify
4210
4459
void Field_blob::set_key_image(char *buff,uint length)
4211
4460
{
4212
4461
  length=uint2korr(buff);
4213
 
  Field_blob::store(buff+2,length);
4214
 
}
 
4462
  (void) Field_blob::store(buff+2,length, default_charset_info);
 
4463
}
 
4464
 
 
4465
 
 
4466
void Field_geom::get_key_image(char *buff,uint length, imagetype type)
 
4467
{
 
4468
  length-=HA_KEY_BLOB_LENGTH;
 
4469
  ulong blob_length=get_length(ptr);
 
4470
  char *blob;
 
4471
  get_ptr(&blob);
 
4472
  memcpy(buff+2,blob,length);
 
4473
 
 
4474
  MBR mbr;
 
4475
  Geometry gobj;
 
4476
  gobj.create_from_wkb(blob,blob_length);
 
4477
  gobj.get_mbr(&mbr);
 
4478
  float8store(buff,    mbr.xmin);
 
4479
  float8store(buff+8,  mbr.xmax);
 
4480
  float8store(buff+16, mbr.ymin);
 
4481
  float8store(buff+24, mbr.ymax);
 
4482
  return;
 
4483
}
 
4484
 
 
4485
void Field_geom::set_key_image(char *buff,uint length)
 
4486
{
 
4487
}
 
4488
 
4215
4489
 
4216
4490
int Field_blob::key_cmp(const byte *key_ptr, uint max_key_length)
4217
4491
{
4252
4526
    else
4253
4527
    {
4254
4528
#ifdef USE_STRCOLL
4255
 
      if (use_strcoll(default_charset_info))
 
4529
      if (use_strcoll(field_charset))
4256
4530
      {
4257
 
        blob_length=my_strnxfrm(default_charset_info,
4258
 
                                (unsigned char *)to,(unsigned char *)blob,
4259
 
                                length,blob_org_length);
 
4531
        blob_length=my_strnxfrm(field_charset,
 
4532
                                (unsigned char *)to, length, 
 
4533
                                (unsigned char *)blob, blob_org_length);
4260
4534
        if (blob_length >= length)
4261
4535
          return;
4262
4536
        to+=blob_length;
4264
4538
      else
4265
4539
#endif
4266
4540
        for (char *end=blob+blob_length ; blob != end ;)
4267
 
          *to++=(char) my_sort_order[(uint) (uchar) *blob++];
 
4541
          *to++=(char) field_charset->sort_order[(uint) (uchar) *blob++];
4268
4542
    }
4269
4543
    bzero(to,length-blob_length);
4270
4544
  }
4280
4554
  case 3:  str="medium"; break;
4281
4555
  case 4:  str="long"; break;
4282
4556
  }
4283
 
  res.set(str,(uint) strlen(str));
 
4557
  res.set(str,(uint) strlen(str),default_charset_info);
4284
4558
  res.append(binary_flag ? "blob" : "text");
 
4559
  if (!binary_flag)
 
4560
  {
 
4561
    res.append(" character set ");
 
4562
    res.append(field_charset->name);
 
4563
  }
4285
4564
}
4286
4565
 
4287
4566
 
4342
4621
    int cmp= memcmp(a,b,min(a_length,b_length));
4343
4622
    return cmp ? cmp : (int) (a_length - b_length);
4344
4623
  }
4345
 
  return my_sortncmp(a,a_length, b,b_length);
 
4624
  return my_sortncmp(field_charset, a,a_length, b,b_length);
4346
4625
}
4347
4626
 
4348
4627
 
4368
4647
    int cmp= memcmp(a,b,min(a_length,b_length));
4369
4648
    return cmp ? cmp : (int) (a_length - b_length);
4370
4649
  }
4371
 
  return my_sortncmp(a,a_length, b,b_length);
 
4650
  return my_sortncmp(field_charset, a,a_length, b,b_length);
4372
4651
}
4373
4652
 
4374
4653
/* Create a packed key that will be used for storage from a MySQL row */
4479
4758
uint find_enum(TYPELIB *lib,const char *x, uint length)
4480
4759
{
4481
4760
  const char *end=x+length;
4482
 
  while (end > x && isspace(end[-1]))
 
4761
  while (end > x && my_isspace(system_charset_info,end[-1]))
4483
4762
    end--;
4484
4763
 
4485
4764
  const char *i;
4486
4765
  const char *j;
4487
4766
  for (uint pos=0 ; (j=lib->type_names[pos]) ; pos++)
4488
4767
  {
4489
 
    for (i=x ; i != end && toupper(*i) == toupper(*j) ; i++, j++) ;
 
4768
    for (i=x ; i != end && 
 
4769
               my_toupper(system_charset_info,*i) == 
 
4770
               my_toupper(system_charset_info,*j) ; i++, j++) ;
4490
4771
    if (i == end && ! *j)
4491
4772
      return(pos+1);
4492
4773
  }
4499
4780
** (if there isn't a empty value in the enum)
4500
4781
*/
4501
4782
 
4502
 
void Field_enum::store(const char *from,uint length)
 
4783
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
4503
4784
{
 
4785
  int error= 0;
4504
4786
  uint tmp=find_enum(typelib,from,length);
4505
4787
  if (!tmp)
4506
4788
  {
4520
4802
      {
4521
4803
        tmp=0;
4522
4804
        current_thd->cuted_fields++;
 
4805
        error=1;
4523
4806
      }
4524
4807
    }
4525
4808
    else
4526
4809
      current_thd->cuted_fields++;
4527
4810
  }
4528
4811
  store_type((ulonglong) tmp);
4529
 
}
4530
 
 
4531
 
 
4532
 
void Field_enum::store(double nr)
4533
 
{
4534
 
  Field_enum::store((longlong) nr);
4535
 
}
4536
 
 
4537
 
 
4538
 
void Field_enum::store(longlong nr)
4539
 
{
 
4812
  return error;
 
4813
}
 
4814
 
 
4815
 
 
4816
int Field_enum::store(double nr)
 
4817
{
 
4818
  return Field_enum::store((longlong) nr);
 
4819
}
 
4820
 
 
4821
 
 
4822
int Field_enum::store(longlong nr)
 
4823
{
 
4824
  int error= 0;
4540
4825
  if ((uint) nr > typelib->count || nr == 0)
4541
4826
  {
4542
4827
    current_thd->cuted_fields++;
4543
4828
    nr=0;
 
4829
    error=1;
4544
4830
  }
4545
4831
  store_type((ulonglong) (uint) nr);
 
4832
  return error;
4546
4833
}
4547
4834
 
4548
4835
 
4605
4892
    val_ptr->length(0);
4606
4893
  else
4607
4894
    val_ptr->set((const char*) typelib->type_names[tmp-1],
4608
 
                 (uint) strlen(typelib->type_names[tmp-1]));
 
4895
                 (uint) strlen(typelib->type_names[tmp-1]),
 
4896
                 default_charset_info);
4609
4897
  return val_ptr;
4610
4898
}
4611
4899
 
4642
4930
  {
4643
4931
    if (flag)
4644
4932
      res.append(',');
4645
 
    res.append('\'');
4646
 
    append_unescaped(&res,*pos);
4647
 
    res.append('\'');
 
4933
    append_unescaped(&res, *pos, strlen(*pos));
4648
4934
    flag=1;
4649
4935
  }
4650
4936
  res.append(')');
4663
4949
ulonglong find_set(TYPELIB *lib,const char *x,uint length)
4664
4950
{
4665
4951
  const char *end=x+length;
4666
 
  while (end > x && isspace(end[-1]))
 
4952
  while (end > x && my_isspace(system_charset_info, end[-1]))
4667
4953
    end--;
4668
4954
 
4669
4955
  ulonglong found=0;
4670
4956
  if (x != end)
4671
4957
  {
4672
4958
    const char *start=x;
4673
 
    bool error=0;
 
4959
    bool error= 0;
4674
4960
    for (;;)
4675
4961
    {
4676
4962
      const char *pos=start;
4691
4977
}
4692
4978
 
4693
4979
 
4694
 
void Field_set::store(const char *from,uint length)
 
4980
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
4695
4981
{
 
4982
  int error= 0;
4696
4983
  ulonglong tmp=find_set(typelib,from,length);
4697
4984
  if (!tmp && length && length < 22)
4698
4985
  {
4708
4995
    tmp=strtoull(conv,&end,10);
4709
4996
    if (my_errno || end != conv+length ||
4710
4997
        tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
 
4998
    {
4711
4999
      tmp=0;
 
5000
      error=1;
 
5001
    }
4712
5002
    else
4713
5003
      current_thd->cuted_fields--;              // Remove warning from find_set
4714
5004
  }
4715
5005
  store_type(tmp);
 
5006
  return error;
4716
5007
}
4717
5008
 
4718
5009
 
4719
 
void Field_set::store(longlong nr)
 
5010
int Field_set::store(longlong nr)
4720
5011
{
 
5012
  int error= 0;
4721
5013
  if ((ulonglong) nr > (ulonglong) (((longlong) 1 << typelib->count) -
4722
5014
                                    (longlong) 1))
4723
5015
  {
4724
5016
    nr&= (longlong) (((longlong) 1 << typelib->count) - (longlong) 1);
4725
5017
    current_thd->cuted_fields++;
 
5018
    error=1;
4726
5019
  }
4727
5020
  store_type((ulonglong) nr);
 
5021
  return error;
4728
5022
}
4729
5023
 
4730
5024
 
4742
5036
      if (val_buffer->length())
4743
5037
        val_buffer->append(field_separator);
4744
5038
      String str(typelib->type_names[bitnr],
4745
 
                 (uint) strlen(typelib->type_names[bitnr]));
 
5039
                 (uint) strlen(typelib->type_names[bitnr]),
 
5040
                 default_charset_info);
4746
5041
      val_buffer->append(str);
4747
5042
    }
4748
5043
    tmp>>=1;
4762
5057
  {
4763
5058
    if (flag)
4764
5059
      res.append(',');
4765
 
    res.append('\'');
4766
 
    append_unescaped(&res,*pos);
4767
 
    res.append('\'');
 
5060
    append_unescaped(&res, *pos, strlen(*pos));
4768
5061
    flag=1;
4769
5062
  }
4770
5063
  res.append(')');
4789
5082
  if (typelib->count < from_lib->count)
4790
5083
    return 0;
4791
5084
  for (uint i=0 ; i < from_lib->count ; i++)
4792
 
    if (my_strcasecmp(typelib->type_names[i],from_lib->type_names[i]))
 
5085
    if (my_strcasecmp(field_charset,
 
5086
                      typelib->type_names[i],from_lib->type_names[i]))
4793
5087
      return 0;
4794
5088
  return 1;
4795
5089
}
4864
5158
Field *make_field(char *ptr, uint32 field_length,
4865
5159
                  uchar *null_pos, uchar null_bit,
4866
5160
                  uint pack_flag,
 
5161
                  enum_field_types field_type,
4867
5162
                  Field::utype unireg_check,
4868
5163
                  TYPELIB *interval,
4869
5164
                  const char *field_name,
4879
5174
    if (!f_is_packed(pack_flag))
4880
5175
      return new Field_string(ptr,field_length,null_pos,null_bit,
4881
5176
                              unireg_check, field_name, table,
4882
 
                              f_is_binary(pack_flag) != 0);
 
5177
                              f_is_binary(pack_flag) != 0,
 
5178
                              default_charset_info);
4883
5179
 
4884
5180
    uint pack_length=calc_pack_length((enum_field_types)
4885
5181
                                      f_packtype(pack_flag),
4888
5184
    if (f_is_blob(pack_flag))
4889
5185
      return new Field_blob(ptr,null_pos,null_bit,
4890
5186
                            unireg_check, field_name, table,
 
5187
                            pack_length,f_is_binary(pack_flag) != 0,
 
5188
                            default_charset_info);
 
5189
    if (f_is_geom(pack_flag))
 
5190
      return new Field_geom(ptr,null_pos,null_bit,
 
5191
                            unireg_check, field_name, table,
4891
5192
                            pack_length,f_is_binary(pack_flag) != 0);
 
5193
 
4892
5194
    if (interval)
4893
5195
    {
4894
5196
      if (f_is_enum(pack_flag))
4902
5204
    }
4903
5205
  }
4904
5206
 
4905
 
  switch ((enum enum_field_types) f_packtype(pack_flag)) {
 
5207
  switch (field_type) {
4906
5208
  case FIELD_TYPE_DECIMAL:
4907
5209
    return new Field_decimal(ptr,field_length,null_pos,null_bit,
4908
5210
                             unireg_check, field_name, table,
4983
5285
  unireg_check=old_field->unireg_check;
4984
5286
  pack_length=old_field->pack_length();
4985
5287
  sql_type=   old_field->real_type();
 
5288
  charset=    old_field->charset();             // May be NULL ptr
 
5289
  comment=    old_field->comment;
4986
5290
 
4987
5291
  /* Fix if the original table had 4 byte pointer blobs */
4988
5292
  if (flags & BLOB_FLAG)
4991
5295
  decimals= old_field->decimals();
4992
5296
  if (sql_type == FIELD_TYPE_STRING)
4993
5297
  {
 
5298
    /* Change CHAR -> VARCHAR if dynamic record length */
4994
5299
    sql_type=old_field->type();
4995
5300
    decimals=0;
4996
5301
  }
5004
5309
      orig_field)
5005
5310
  {
5006
5311
    char buff[MAX_FIELD_WIDTH],*pos;
5007
 
    String tmp(buff,sizeof(buff));
 
5312
    CHARSET_INFO *field_charset= charset ? charset : default_charset_info;
 
5313
    String tmp(buff,sizeof(buff),field_charset);
5008
5314
 
5009
5315
    /* Get the value from record[2] (the default value row) */
5010
5316
    my_ptrdiff_t diff= (my_ptrdiff_t) (orig_field->table->rec_buff_length*2);
5016
5322
    {
5017
5323
      pos= (char*) sql_memdup(tmp.ptr(),tmp.length()+1);
5018
5324
      pos[tmp.length()]=0;
5019
 
      def=new Item_string(pos,tmp.length());
 
5325
      def=new Item_string(pos,tmp.length(),field_charset);
5020
5326
    }
5021
5327
  }
5022
5328
}