~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item.cc

  • Committer: Brian Aker
  • Date: 2008-07-01 20:14:24 UTC
  • Revision ID: brian@tangent.org-20080701201424-rsof5enxl7gkr50p
More cleanup on pread()

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
 
#include <drizzled/server_includes.h>
18
 
#include <drizzled/sql_select.h>
19
 
#include <drizzled/drizzled_error_messages.h>
 
17
#ifdef USE_PRAGMA_IMPLEMENTATION
 
18
#pragma implementation                          // gcc: Class implementation
 
19
#endif
 
20
#include "mysql_priv.h"
 
21
#include <mysql.h>
 
22
#include <m_ctype.h>
 
23
#include "my_dir.h"
 
24
#include "sql_select.h"
20
25
 
21
26
const String my_null_string("NULL", 4, default_charset_info);
22
27
 
39
44
 
40
45
 
41
46
my_decimal *
42
 
Hybrid_type_traits::val_decimal(Hybrid_type *val,
43
 
                                my_decimal *to __attribute__((unused))) const
 
47
Hybrid_type_traits::val_decimal(Hybrid_type *val, my_decimal *to) const
44
48
{
45
49
  double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
46
50
  return val->dec_buf;
48
52
 
49
53
 
50
54
String *
51
 
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8_t decimals) const
 
55
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8 decimals) const
52
56
{
53
57
  to->set_real(val->real, decimals, &my_charset_bin);
54
58
  return to;
67
71
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
68
72
{
69
73
  item->decimals= arg->decimals;
70
 
  item->max_length= cmin(arg->max_length + DECIMAL_LONGLONG_DIGITS,
71
 
                        (unsigned int)DECIMAL_MAX_STR_LENGTH);
 
74
  item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS,
 
75
                        DECIMAL_MAX_STR_LENGTH);
72
76
}
73
77
 
74
78
 
93
97
  @todo
94
98
  what is '4' for scale?
95
99
*/
96
 
void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
 
100
void Hybrid_type_traits_decimal::div(Hybrid_type *val, ulonglong u) const
97
101
{
98
 
  int2my_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
 
102
  int2my_decimal(E_DEC_FATAL_ERROR, u, TRUE, &val->dec_buf[2]);
99
103
  /* XXX: what is '4' for scale? */
100
104
  my_decimal_div(E_DEC_FATAL_ERROR,
101
105
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
105
109
}
106
110
 
107
111
 
108
 
int64_t
 
112
longlong
109
113
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
110
114
{
111
 
  int64_t result;
 
115
  longlong result;
112
116
  my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
113
117
                 unsigned_flag, &result);
114
118
  return result;
126
130
 
127
131
String *
128
132
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
129
 
                                    uint8_t decimals) const
 
133
                                    uint8 decimals) const
130
134
{
131
135
  my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
132
 
                   decimals, false, &val->dec_buf[2]);
 
136
                   decimals, FALSE, &val->dec_buf[2]);
133
137
  my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
134
138
  return to;
135
139
}
143
147
}
144
148
 
145
149
void
146
 
Hybrid_type_traits_integer::fix_length_and_dec(Item *item,
147
 
                                               Item *arg __attribute__((unused))) const
 
150
Hybrid_type_traits_integer::fix_length_and_dec(Item *item, Item *arg) const
148
151
{
149
152
  item->decimals= 0;
150
153
  item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
161
164
 
162
165
void item_init(void)
163
166
{
 
167
  uuid_short_init();
164
168
}
165
169
 
166
170
 
187
191
    return val_real() != 0.0;
188
192
  case ROW_RESULT:
189
193
  default:
190
 
    assert(0);
 
194
    DBUG_ASSERT(0);
191
195
    return 0;                                   // Wrong (but safe)
192
196
  }
193
197
}
205
209
 
206
210
String *Item::val_string_from_int(String *str)
207
211
{
208
 
  int64_t nr= val_int();
 
212
  longlong nr= val_int();
209
213
  if (null_value)
210
214
    return 0;
211
215
  str->set_int(nr, unsigned_flag, &my_charset_bin);
218
222
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
219
223
  if (null_value)
220
224
    return 0;
221
 
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
225
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, FALSE, &dec_buf);
222
226
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
223
227
  return str;
224
228
}
236
240
 
237
241
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
238
242
{
239
 
  int64_t nr= val_int();
 
243
  longlong nr= val_int();
240
244
  if (null_value)
241
245
    return 0;
242
246
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
256
260
                     res->ptr(), res->length(), res->charset(),
257
261
                     decimal_value) & E_DEC_BAD_NUM)
258
262
  {
259
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
263
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
260
264
                        ER_TRUNCATED_WRONG_VALUE,
261
265
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
262
266
                        str_value.c_ptr());
267
271
 
268
272
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
269
273
{
270
 
  assert(fixed == 1);
271
 
  DRIZZLE_TIME ltime;
 
274
  DBUG_ASSERT(fixed == 1);
 
275
  MYSQL_TIME ltime;
272
276
  if (get_date(&ltime, TIME_FUZZY_DATE))
273
277
  {
274
278
    my_decimal_set_zero(decimal_value);
281
285
 
282
286
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
283
287
{
284
 
  assert(fixed == 1);
285
 
  DRIZZLE_TIME ltime;
 
288
  DBUG_ASSERT(fixed == 1);
 
289
  MYSQL_TIME ltime;
286
290
  if (get_time(&ltime))
287
291
  {
288
292
    my_decimal_set_zero(decimal_value);
304
308
}
305
309
 
306
310
 
307
 
int64_t Item::val_int_from_decimal()
 
311
longlong Item::val_int_from_decimal()
308
312
{
309
313
  /* Note that fix_fields may not be called for Item_avg_field items */
310
 
  int64_t result;
 
314
  longlong result;
311
315
  my_decimal value, *dec_val= val_decimal(&value);
312
316
  if (null_value)
313
317
    return 0;
317
321
 
318
322
int Item::save_time_in_field(Field *field)
319
323
{
320
 
  DRIZZLE_TIME ltime;
 
324
  MYSQL_TIME ltime;
321
325
  if (get_time(&ltime))
322
326
    return set_field_to_null(field);
323
327
  field->set_notnull();
324
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
 
328
  return field->store_time(&ltime, MYSQL_TIMESTAMP_TIME);
325
329
}
326
330
 
327
331
 
328
332
int Item::save_date_in_field(Field *field)
329
333
{
330
 
  DRIZZLE_TIME ltime;
 
334
  MYSQL_TIME ltime;
331
335
  if (get_date(&ltime, TIME_FUZZY_DATE))
332
336
    return set_field_to_null(field);
333
337
  field->set_notnull();
334
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
 
338
  return field->store_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
335
339
}
336
340
 
337
341
 
368
372
 
369
373
Item::Item():
370
374
  is_expensive_cache(-1), rsize(0), name(0), orig_name(0), name_length(0),
371
 
  fixed(0), is_autogenerated_name(true),
 
375
  fixed(0), is_autogenerated_name(TRUE),
372
376
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
373
377
{
374
378
  marker= 0;
375
 
  maybe_null= false;
376
 
  null_value= false;
377
 
  with_sum_func= false;
378
 
  unsigned_flag= false;
379
 
  decimals= 0; 
380
 
  max_length= 0;
 
379
  maybe_null=null_value=with_sum_func=unsigned_flag=0;
 
380
  decimals= 0; max_length= 0;
381
381
  with_subselect= 0;
382
382
  cmp_context= (Item_result)-1;
383
383
 
429
429
}
430
430
 
431
431
 
432
 
uint32_t Item::decimal_precision() const
 
432
uint Item::decimal_precision() const
433
433
{
434
434
  Item_result restype= result_type();
435
435
 
436
436
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
437
 
    return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
438
 
               (unsigned int)DECIMAL_MAX_PRECISION);
439
 
  return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
 
437
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
438
               DECIMAL_MAX_PRECISION);
 
439
  return min(max_length, DECIMAL_MAX_PRECISION);
440
440
}
441
441
 
442
442
 
455
455
 
456
456
void Item::cleanup()
457
457
{
 
458
  DBUG_ENTER("Item::cleanup");
458
459
  fixed=0;
459
460
  marker= 0;
460
461
  if (orig_name)
461
462
    name= orig_name;
462
 
  return;
 
463
  DBUG_VOID_RETURN;
463
464
}
464
465
 
465
466
 
469
470
  @param arg   a dummy parameter, is not used here
470
471
*/
471
472
 
472
 
bool Item::cleanup_processor(unsigned char *arg __attribute__((unused)))
 
473
bool Item::cleanup_processor(uchar *arg)
473
474
{
474
475
  if (fixed)
475
476
    cleanup();
476
 
  return false;
 
477
  return FALSE;
477
478
}
478
479
 
479
480
 
524
525
    pointer to newly allocated item is returned.
525
526
*/
526
527
 
527
 
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
 
528
Item* Item::transform(Item_transformer transformer, uchar *arg)
528
529
{
529
530
  return (this->*transformer)(arg);
530
531
}
537
538
   orig_field_name(field_name_arg), context(context_arg),
538
539
   db_name(db_name_arg), table_name(table_name_arg),
539
540
   field_name(field_name_arg),
540
 
   alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
 
541
   alias_name_used(FALSE), cached_field_index(NO_CACHED_FIELD_INDEX),
541
542
   cached_table(0), depended_from(0)
542
543
{
543
544
  name = (char*) field_name_arg;
565
566
 
566
567
void Item_ident::cleanup()
567
568
{
 
569
  DBUG_ENTER("Item_ident::cleanup");
568
570
#ifdef CANT_BE_USED_AS_MEMORY_IS_FREED
569
571
                       db_name ? db_name : "(null)",
570
572
                       orig_db_name ? orig_db_name : "(null)",
578
580
  table_name= orig_table_name;
579
581
  field_name= orig_field_name;
580
582
  depended_from= 0;
581
 
  return;
 
583
  DBUG_VOID_RETURN;
582
584
}
583
585
 
584
 
bool Item_ident::remove_dependence_processor(unsigned char * arg)
 
586
bool Item_ident::remove_dependence_processor(uchar * arg)
585
587
{
 
588
  DBUG_ENTER("Item_ident::remove_dependence_processor");
586
589
  if (depended_from == (st_select_lex *) arg)
587
590
    depended_from= 0;
588
 
  return(0);
 
591
  DBUG_RETURN(0);
589
592
}
590
593
 
591
594
 
603
606
  @param arg  pointer to a List<Item_field>
604
607
 
605
608
  @return
606
 
    false to force the evaluation of collect_item_field_processor
 
609
    FALSE to force the evaluation of collect_item_field_processor
607
610
    for the subsequent items.
608
611
*/
609
612
 
610
 
bool Item_field::collect_item_field_processor(unsigned char *arg)
 
613
bool Item_field::collect_item_field_processor(uchar *arg)
611
614
{
 
615
  DBUG_ENTER("Item_field::collect_item_field_processor");
 
616
  DBUG_PRINT("info", ("%s", field->field_name ? field->field_name : "noname"));
612
617
  List<Item_field> *item_list= (List<Item_field>*) arg;
613
618
  List_iterator<Item_field> item_list_it(*item_list);
614
619
  Item_field *curr_item;
615
620
  while ((curr_item= item_list_it++))
616
621
  {
617
622
    if (curr_item->eq(this, 1))
618
 
      return(false); /* Already in the set. */
 
623
      DBUG_RETURN(FALSE); /* Already in the set. */
619
624
  }
620
625
  item_list->push_back(this);
621
 
  return(false);
 
626
  DBUG_RETURN(FALSE);
622
627
}
623
628
 
624
629
 
633
638
  @param arg   Field being compared, arg must be of type Field
634
639
 
635
640
  @retval
636
 
    true  if 'this' references the field 'arg'
 
641
    TRUE  if 'this' references the field 'arg'
637
642
  @retval
638
 
    false otherwise
 
643
    FALSE otherwise
639
644
*/
640
645
 
641
 
bool Item_field::find_item_in_field_list_processor(unsigned char *arg)
 
646
bool Item_field::find_item_in_field_list_processor(uchar *arg)
642
647
{
643
648
  KEY_PART_INFO *first_non_group_part= *((KEY_PART_INFO **) arg);
644
649
  KEY_PART_INFO *last_part= *(((KEY_PART_INFO **) arg) + 1);
647
652
  for (cur_part= first_non_group_part; cur_part != last_part; cur_part++)
648
653
  {
649
654
    if (field->eq(cur_part->field))
650
 
      return true;
 
655
      return TRUE;
651
656
  }
652
 
  return false;
 
657
  return FALSE;
653
658
}
654
659
 
655
660
 
661
666
    column read set or to register used fields in a view
662
667
*/
663
668
 
664
 
bool Item_field::register_field_in_read_map(unsigned char *arg)
 
669
bool Item_field::register_field_in_read_map(uchar *arg)
665
670
{
666
 
  Table *table= (Table *) arg;
 
671
  TABLE *table= (TABLE *) arg;
667
672
  if (field->table == table || !table)
668
673
    bitmap_set_bit(field->table->read_set, field->field_index);
669
 
  if (field->vcol_info && field->vcol_info->expr_item)
670
 
    return field->vcol_info->expr_item->walk(&Item::register_field_in_read_map, 
671
 
                                             1, arg);
672
674
  return 0;
673
675
}
674
676
 
675
 
/*
676
 
  Mark field in bitmap supplied as *arg
677
 
 
678
 
*/
679
 
 
680
 
bool Item_field::register_field_in_bitmap(unsigned char *arg)
681
 
{
682
 
  MY_BITMAP *bitmap= (MY_BITMAP *) arg;
683
 
  assert(bitmap);
684
 
  bitmap_set_bit(bitmap, field->field_index);
685
 
  return false;
686
 
}
687
 
 
688
 
 
689
 
bool Item::check_cols(uint32_t c)
 
677
 
 
678
bool Item::check_cols(uint c)
690
679
{
691
680
  if (c != 1)
692
681
  {
697
686
}
698
687
 
699
688
 
700
 
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
 
689
void Item::set_name(const char *str, uint length, CHARSET_INFO *cs)
701
690
{
702
691
  if (!length)
703
692
  {
708
697
  }
709
698
  if (cs->ctype)
710
699
  {
711
 
    uint32_t orig_len= length;
 
700
    uint orig_len= length;
712
701
    /*
713
702
      This will probably need a better implementation in the future:
714
703
      a function in CHARSET_INFO structure.
721
710
    if (orig_len != length && !is_autogenerated_name)
722
711
    {
723
712
      if (length == 0)
724
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
713
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
725
714
                            ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
726
715
                            str + length - orig_len);
727
716
      else
728
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
717
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
729
718
                            ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
730
719
                            str + length - orig_len);
731
720
    }
738
727
                                   &res_length);
739
728
  }
740
729
  else
741
 
    name= sql_strmake(str, (name_length= cmin(length,(unsigned int)MAX_ALIAS_NAME)));
 
730
    name= sql_strmake(str, (name_length= min(length,MAX_ALIAS_NAME)));
742
731
}
743
732
 
744
733
 
746
735
  @details
747
736
  This function is called when:
748
737
  - Comparing items in the WHERE clause (when doing where optimization)
749
 
  - When trying to find an order_st BY/GROUP BY item in the SELECT part
 
738
  - When trying to find an ORDER BY/GROUP BY item in the SELECT part
750
739
*/
751
740
 
752
 
bool Item::eq(const Item *item, bool binary_cmp __attribute__((unused))) const
 
741
bool Item::eq(const Item *item, bool binary_cmp) const
753
742
{
754
743
  /*
755
 
    Note, that this is never true if item is a Item_param:
 
744
    Note, that this is never TRUE if item is a Item_param:
756
745
    for all basic constants we have special checks, and Item_param's
757
746
    type() can be only among basic constant types.
758
747
  */
761
750
}
762
751
 
763
752
 
764
 
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
 
753
Item *Item::safe_charset_converter(CHARSET_INFO *tocs)
765
754
{
766
755
  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
767
756
  return conv->safe ? conv : NULL;
779
768
  the latter returns a non-fixed Item, so val_str() crashes afterwards.
780
769
  Override Item_num method, to return a fixed item.
781
770
*/
782
 
Item *Item_num::safe_charset_converter(const CHARSET_INFO * const tocs __attribute__((unused)))
 
771
Item *Item_num::safe_charset_converter(CHARSET_INFO *tocs)
783
772
{
784
773
  Item_string *conv;
785
774
  char buf[64];
794
783
}
795
784
 
796
785
 
797
 
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const tocs __attribute__((unused)))
 
786
Item *Item_static_float_func::safe_charset_converter(CHARSET_INFO *tocs)
798
787
{
799
788
  Item_string *conv;
800
789
  char buf[64];
810
799
}
811
800
 
812
801
 
813
 
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
 
802
Item *Item_string::safe_charset_converter(CHARSET_INFO *tocs)
814
803
{
815
804
  Item_string *conv;
816
 
  uint32_t conv_errors;
 
805
  uint conv_errors;
817
806
  char *ptr;
818
807
  String tmp, cstr, *ostr= val_str(&tmp);
819
808
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
838
827
}
839
828
 
840
829
 
841
 
Item *Item_param::safe_charset_converter(const CHARSET_INFO * const tocs)
 
830
Item *Item_param::safe_charset_converter(CHARSET_INFO *tocs)
842
831
{
843
832
  if (const_item())
844
833
  {
845
 
    uint32_t cnv_errors;
 
834
    uint cnv_errors;
846
835
    String *ostr= val_str(&cnvstr);
847
836
    cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
848
837
                            ostr->charset(), tocs, &cnv_errors);
856
845
}
857
846
 
858
847
 
859
 
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
 
848
Item *Item_static_string_func::safe_charset_converter(CHARSET_INFO *tocs)
860
849
{
861
850
  Item_string *conv;
862
 
  uint32_t conv_errors;
 
851
  uint conv_errors;
863
852
  String tmp, cstr, *ostr= val_str(&tmp);
864
853
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
865
854
  if (conv_errors ||
897
886
 
898
887
 
899
888
/**
900
 
  Get the value of the function as a DRIZZLE_TIME structure.
 
889
  Get the value of the function as a MYSQL_TIME structure.
901
890
  As a extra convenience the time structure is reset on error!
902
891
*/
903
892
 
904
 
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
893
bool Item::get_date(MYSQL_TIME *ltime,uint fuzzydate)
905
894
{
906
895
  if (result_type() == STRING_RESULT)
907
896
  {
909
898
    String tmp(buff,sizeof(buff), &my_charset_bin),*res;
910
899
    if (!(res=val_str(&tmp)) ||
911
900
        str_to_datetime_with_warn(res->ptr(), res->length(),
912
 
                                  ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
901
                                  ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
913
902
      goto err;
914
903
  }
915
904
  else
916
905
  {
917
 
    int64_t value= val_int();
 
906
    longlong value= val_int();
918
907
    int was_cut;
919
 
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
 
908
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == LL(-1))
920
909
    {
921
910
      char buff[22], *end;
922
 
      end= int64_t10_to_str(value, buff, -10);
923
 
      make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
924
 
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
925
 
                                   NULL);
 
911
      end= longlong10_to_str(value, buff, -10);
 
912
      make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
913
                                   buff, (int) (end-buff), MYSQL_TIMESTAMP_NONE,
 
914
                                   NullS);
926
915
      goto err;
927
916
    }
928
917
  }
929
918
  return 0;
930
919
 
931
920
err:
932
 
  memset(ltime, 0, sizeof(*ltime));
 
921
  bzero((char*) ltime,sizeof(*ltime));
933
922
  return 1;
934
923
}
935
924
 
939
928
  As a extra convenience the time structure is reset on error!
940
929
*/
941
930
 
942
 
bool Item::get_time(DRIZZLE_TIME *ltime)
 
931
bool Item::get_time(MYSQL_TIME *ltime)
943
932
{
944
933
  char buff[40];
945
934
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
946
935
  if (!(res=val_str(&tmp)) ||
947
936
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
948
937
  {
949
 
    memset(ltime, 0, sizeof(*ltime));
 
938
    bzero((char*) ltime,sizeof(*ltime));
950
939
    return 1;
951
940
  }
952
941
  return 0;
953
942
}
954
943
 
955
 
const CHARSET_INFO *Item::default_charset()
 
944
CHARSET_INFO *Item::default_charset()
956
945
{
957
946
  return current_thd->variables.collation_connection;
958
947
}
969
958
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
970
959
{
971
960
  int res;
972
 
  Table *table= field->table;
 
961
  TABLE *table= field->table;
973
962
  THD *thd= table->in_use;
974
963
  enum_check_fields tmp= thd->count_cuted_fields;
 
964
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
975
965
  ulong sql_mode= thd->variables.sql_mode;
976
 
  thd->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
 
966
  thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
977
967
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
978
968
  res= save_in_field(field, no_conversions);
979
969
  thd->count_cuted_fields= tmp;
 
970
  dbug_tmp_restore_column_map(table->write_set, old_map);
980
971
  thd->variables.sql_mode= sql_mode;
981
972
  return res;
982
973
}
1055
1046
      Item_ref to allow fields from view being stored in tmp table.
1056
1047
    */
1057
1048
    Item_aggregate_ref *item_ref;
1058
 
    uint32_t el= fields.elements;
 
1049
    uint el= fields.elements;
1059
1050
    Item *real_itm= real_item();
1060
1051
 
1061
1052
    ref_pointer_array[el]= real_itm;
1078
1069
      (left->derivation < right->derivation ||
1079
1070
       (left->derivation == right->derivation &&
1080
1071
        !(right->collation->state & MY_CS_UNICODE))))
1081
 
    return true;
 
1072
    return TRUE;
1082
1073
  /* Allow convert from ASCII */
1083
1074
  if (right->repertoire == MY_REPERTOIRE_ASCII &&
1084
1075
      (left->derivation < right->derivation ||
1085
1076
       (left->derivation == right->derivation &&
1086
1077
        !(left->repertoire == MY_REPERTOIRE_ASCII))))
1087
 
    return true;
 
1078
    return TRUE;
1088
1079
  /* Disallow conversion otherwise */
1089
 
  return false;
 
1080
  return FALSE;
1090
1081
}
1091
1082
 
1092
1083
/**
1125
1116
  @endcode
1126
1117
*/
1127
1118
 
1128
 
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
 
1119
bool DTCollation::aggregate(DTCollation &dt, uint flags)
1129
1120
{
1130
1121
  if (!my_charset_same(collation, dt.collation))
1131
1122
  {
1212
1203
        set(dt);
1213
1204
        return 0;
1214
1205
      }
1215
 
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, 
1216
 
                                                            MY_CS_BINSORT,MYF(0));
 
1206
      CHARSET_INFO *bin= get_charset_by_csname(collation->csname, 
 
1207
                                               MY_CS_BINSORT,MYF(0));
1217
1208
      set(bin, DERIVATION_NONE);
1218
1209
    }
1219
1210
  }
1245
1236
 
1246
1237
 
1247
1238
static
1248
 
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
 
1239
void my_coll_agg_error(Item** args, uint count, const char *fname,
1249
1240
                       int item_sep)
1250
1241
{
1251
1242
  if (count == 2)
1259
1250
 
1260
1251
 
1261
1252
bool agg_item_collations(DTCollation &c, const char *fname,
1262
 
                         Item **av, uint32_t count, uint32_t flags, int item_sep)
 
1253
                         Item **av, uint count, uint flags, int item_sep)
1263
1254
{
1264
 
  uint32_t i;
 
1255
  uint i;
1265
1256
  Item **arg;
1266
1257
  c.set(av[0]->collation);
1267
1258
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1269
1260
    if (c.aggregate((*arg)->collation, flags))
1270
1261
    {
1271
1262
      my_coll_agg_error(av, count, fname, item_sep);
1272
 
      return true;
 
1263
      return TRUE;
1273
1264
    }
1274
1265
  }
1275
1266
  if ((flags & MY_COLL_DISALLOW_NONE) &&
1276
1267
      c.derivation == DERIVATION_NONE)
1277
1268
  {
1278
1269
    my_coll_agg_error(av, count, fname, item_sep);
1279
 
    return true;
 
1270
    return TRUE;
1280
1271
  }
1281
 
  return false;
 
1272
  return FALSE;
1282
1273
}
1283
1274
 
1284
1275
 
1285
1276
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1286
 
                                        Item **av, uint32_t count, uint32_t flags)
 
1277
                                        Item **av, uint count, uint flags)
1287
1278
{
1288
1279
  return (agg_item_collations(c, fname, av, count,
1289
1280
                              flags | MY_COLL_DISALLOW_NONE, 1));
1322
1313
*/
1323
1314
 
1324
1315
bool agg_item_charsets(DTCollation &coll, const char *fname,
1325
 
                       Item **args, uint32_t nargs, uint32_t flags, int item_sep)
 
1316
                       Item **args, uint nargs, uint flags, int item_sep)
1326
1317
{
1327
1318
  Item **arg, *safe_args[2];
1328
1319
 
1329
1320
  memset(safe_args, 0, sizeof(safe_args));
1330
1321
 
1331
1322
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1332
 
    return true;
 
1323
    return TRUE;
1333
1324
 
1334
1325
  /*
1335
1326
    For better error reporting: save the first and the second argument.
1345
1336
  }
1346
1337
 
1347
1338
  THD *thd= current_thd;
1348
 
  bool res= false;
1349
 
  uint32_t i;
 
1339
  Query_arena *arena, backup;
 
1340
  bool res= FALSE;
 
1341
  uint i;
 
1342
  /*
 
1343
    In case we're in statement prepare, create conversion item
 
1344
    in its memory: it will be reused on each execute.
 
1345
  */
 
1346
  arena= NULL;
1350
1347
 
1351
1348
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1352
1349
  {
1353
1350
    Item* conv;
1354
 
    uint32_t dummy_offset;
 
1351
    uint32 dummy_offset;
1355
1352
    if (!String::needs_conversion(0, (*arg)->collation.collation,
1356
1353
                                  coll.collation,
1357
1354
                                  &dummy_offset))
1370
1367
        args[item_sep]= safe_args[1];
1371
1368
      }
1372
1369
      my_coll_agg_error(args, nargs, fname, item_sep);
1373
 
      res= true;
 
1370
      res= TRUE;
1374
1371
      break; // we cannot return here, we need to restore "arena".
1375
1372
    }
1376
1373
    if ((*arg)->type() == Item::FIELD_ITEM)
1392
1389
    */
1393
1390
    conv->fix_fields(thd, arg);
1394
1391
  }
1395
 
 
 
1392
  if (arena)
 
1393
    thd->restore_active_arena(arena, &backup);
1396
1394
  return res;
1397
1395
}
1398
1396
 
1413
1411
/**********************************************/
1414
1412
 
1415
1413
Item_field::Item_field(Field *f)
1416
 
  :Item_ident(0, NULL, *f->table_name, f->field_name),
 
1414
  :Item_ident(0, NullS, *f->table_name, f->field_name),
1417
1415
   item_equal(0), no_const_subst(0),
1418
1416
   have_privileges(0), any_privileges(0)
1419
1417
{
1433
1431
  Item_field (this is important in prepared statements).
1434
1432
*/
1435
1433
 
1436
 
Item_field::Item_field(THD *thd __attribute__((unused)),
1437
 
                       Name_resolution_context *context_arg,
 
1434
Item_field::Item_field(THD *thd, Name_resolution_context *context_arg,
1438
1435
                       Field *f)
1439
1436
  :Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1440
1437
   item_equal(0), no_const_subst(0),
1513
1510
  {
1514
1511
    tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1515
1512
                          (uint) strlen(field_name)+3);
1516
 
    strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
 
1513
    strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
1517
1514
  }
1518
1515
  else
1519
1516
  {
1521
1518
    {
1522
1519
      tmp= (char*) sql_alloc((uint) strlen(table_name) +
1523
1520
                             (uint) strlen(field_name) + 2);
1524
 
      strxmov(tmp, table_name, ".", field_name, NULL);
 
1521
      strxmov(tmp, table_name, ".", field_name, NullS);
1525
1522
    }
1526
1523
    else
1527
1524
      tmp= (char*) field_name;
1529
1526
  return tmp;
1530
1527
}
1531
1528
 
1532
 
void Item_ident::print(String *str,
1533
 
                       enum_query_type query_type __attribute__((unused)))
 
1529
void Item_ident::print(String *str, enum_query_type query_type)
1534
1530
{
1535
1531
  THD *thd= current_thd;
1536
1532
  char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1540
1536
  {
1541
1537
    if (table_name && table_name[0])
1542
1538
    {
1543
 
      my_stpcpy(t_name_buff, table_name);
 
1539
      strmov(t_name_buff, table_name);
1544
1540
      my_casedn_str(files_charset_info, t_name_buff);
1545
1541
      t_name= t_name_buff;
1546
1542
    }
1547
1543
    if (db_name && db_name[0])
1548
1544
    {
1549
 
      my_stpcpy(d_name_buff, db_name);
 
1545
      strmov(d_name_buff, db_name);
1550
1546
      my_casedn_str(files_charset_info, d_name_buff);
1551
1547
      d_name= d_name_buff;
1552
1548
    }
1585
1581
/* ARGSUSED */
1586
1582
String *Item_field::val_str(String *str)
1587
1583
{
1588
 
  assert(fixed == 1);
 
1584
  DBUG_ASSERT(fixed == 1);
1589
1585
  if ((null_value=field->is_null()))
1590
1586
    return 0;
1591
1587
  str->set_charset(str_value.charset());
1595
1591
 
1596
1592
double Item_field::val_real()
1597
1593
{
1598
 
  assert(fixed == 1);
 
1594
  DBUG_ASSERT(fixed == 1);
1599
1595
  if ((null_value=field->is_null()))
1600
1596
    return 0.0;
1601
1597
  return field->val_real();
1602
1598
}
1603
1599
 
1604
1600
 
1605
 
int64_t Item_field::val_int()
 
1601
longlong Item_field::val_int()
1606
1602
{
1607
 
  assert(fixed == 1);
 
1603
  DBUG_ASSERT(fixed == 1);
1608
1604
  if ((null_value=field->is_null()))
1609
1605
    return 0;
1610
1606
  return field->val_int();
1627
1623
  return result_field->val_str(str,&str_value);
1628
1624
}
1629
1625
 
1630
 
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1626
bool Item_field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1631
1627
{
1632
1628
  if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1633
1629
  {
1634
 
    memset(ltime, 0, sizeof(*ltime));
 
1630
    bzero((char*) ltime,sizeof(*ltime));
1635
1631
    return 1;
1636
1632
  }
1637
1633
  return 0;
1638
1634
}
1639
1635
 
1640
 
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1636
bool Item_field::get_date_result(MYSQL_TIME *ltime,uint fuzzydate)
1641
1637
{
1642
1638
  if ((null_value=result_field->is_null()) ||
1643
1639
      result_field->get_date(ltime,fuzzydate))
1644
1640
  {
1645
 
    memset(ltime, 0, sizeof(*ltime));
 
1641
    bzero((char*) ltime,sizeof(*ltime));
1646
1642
    return 1;
1647
1643
  }
1648
1644
  return 0;
1649
1645
}
1650
1646
 
1651
 
bool Item_field::get_time(DRIZZLE_TIME *ltime)
 
1647
bool Item_field::get_time(MYSQL_TIME *ltime)
1652
1648
{
1653
1649
  if ((null_value=field->is_null()) || field->get_time(ltime))
1654
1650
  {
1655
 
    memset(ltime, 0, sizeof(*ltime));
 
1651
    bzero((char*) ltime,sizeof(*ltime));
1656
1652
    return 1;
1657
1653
  }
1658
1654
  return 0;
1665
1661
  return result_field->val_real();
1666
1662
}
1667
1663
 
1668
 
int64_t Item_field::val_int_result()
 
1664
longlong Item_field::val_int_result()
1669
1665
{
1670
1666
  if ((null_value=result_field->is_null()))
1671
1667
    return 0;
1684
1680
bool Item_field::val_bool_result()
1685
1681
{
1686
1682
  if ((null_value= result_field->is_null()))
1687
 
    return false;
 
1683
    return FALSE;
1688
1684
  switch (result_field->result_type()) {
1689
1685
  case INT_RESULT:
1690
1686
    return result_field->val_int() != 0;
1701
1697
    return result_field->val_real() != 0.0;
1702
1698
  case ROW_RESULT:
1703
1699
  default:
1704
 
    assert(0);
 
1700
    DBUG_ASSERT(0);
1705
1701
    return 0;                                   // Shut up compiler
1706
1702
  }
1707
1703
}
1708
1704
 
1709
1705
 
1710
 
bool Item_field::eq(const Item *item,
1711
 
                    bool binary_cmp __attribute__((unused))) const
 
1706
bool Item_field::eq(const Item *item, bool binary_cmp) const
1712
1707
{
1713
1708
  Item *real_item= ((Item *) item)->real_item();
1714
1709
  if (real_item->type() != FIELD_ITEM)
1715
1710
    return 0;
1716
 
 
 
1711
  
1717
1712
  Item_field *item_field= (Item_field*) real_item;
1718
1713
  if (item_field->field && field)
1719
1714
    return item_field->field == field;
1746
1741
}
1747
1742
 
1748
1743
 
1749
 
void Item_field::fix_after_pullout(st_select_lex *new_parent,
1750
 
                                   Item **ref __attribute__((unused)))
 
1744
void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **ref)
1751
1745
{
1752
1746
  if (new_parent == depended_from)
1753
1747
    depended_from= NULL;
1768
1762
  return new_item;
1769
1763
}
1770
1764
 
1771
 
int64_t Item_field::val_int_endpoint(bool left_endp __attribute__((unused)),
1772
 
                                      bool *incl_endp __attribute__((unused)))
 
1765
longlong Item_field::val_int_endpoint(bool left_endp, bool *incl_endp)
1773
1766
{
1774
 
  int64_t res= val_int();
1775
 
  return null_value? INT64_MIN : res;
 
1767
  longlong res= val_int();
 
1768
  return null_value? LONGLONG_MIN : res;
1776
1769
}
1777
1770
 
1778
1771
/**
1779
 
  Create an item from a string we KNOW points to a valid int64_t
 
1772
  Create an item from a string we KNOW points to a valid longlong
1780
1773
  end \\0 terminated number string.
1781
1774
  This is always 'signed'. Unsigned values are created with Item_uint()
1782
1775
*/
1783
1776
 
1784
 
Item_int::Item_int(const char *str_arg, uint32_t length)
 
1777
Item_int::Item_int(const char *str_arg, uint length)
1785
1778
{
1786
1779
  char *end_ptr= (char*) str_arg + length;
1787
1780
  int error;
1801
1794
String *Item_int::val_str(String *str)
1802
1795
{
1803
1796
  // following assert is redundant, because fixed=1 assigned in constructor
1804
 
  assert(fixed == 1);
 
1797
  DBUG_ASSERT(fixed == 1);
1805
1798
  str->set(value, &my_charset_bin);
1806
1799
  return str;
1807
1800
}
1808
1801
 
1809
 
void Item_int::print(String *str,
1810
 
                     enum_query_type query_type __attribute__((unused)))
 
1802
void Item_int::print(String *str, enum_query_type query_type)
1811
1803
{
1812
1804
  // my_charset_bin is good enough for numbers
1813
1805
  str_value.set(value, &my_charset_bin);
1815
1807
}
1816
1808
 
1817
1809
 
1818
 
Item_uint::Item_uint(const char *str_arg, uint32_t length):
 
1810
Item_uint::Item_uint(const char *str_arg, uint length):
1819
1811
  Item_int(str_arg, length)
1820
1812
{
1821
1813
  unsigned_flag= 1;
1822
1814
}
1823
1815
 
1824
1816
 
1825
 
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
 
1817
Item_uint::Item_uint(const char *str_arg, longlong i, uint length):
1826
1818
  Item_int(str_arg, i, length)
1827
1819
{
1828
1820
  unsigned_flag= 1;
1832
1824
String *Item_uint::val_str(String *str)
1833
1825
{
1834
1826
  // following assert is redundant, because fixed=1 assigned in constructor
1835
 
  assert(fixed == 1);
1836
 
  str->set((uint64_t) value, &my_charset_bin);
 
1827
  DBUG_ASSERT(fixed == 1);
 
1828
  str->set((ulonglong) value, &my_charset_bin);
1837
1829
  return str;
1838
1830
}
1839
1831
 
1840
1832
 
1841
 
void Item_uint::print(String *str,
1842
 
                      enum_query_type query_type __attribute__((unused)))
 
1833
void Item_uint::print(String *str, enum_query_type query_type)
1843
1834
{
1844
1835
  // latin1 is good enough for numbers
1845
 
  str_value.set((uint64_t) value, default_charset());
 
1836
  str_value.set((ulonglong) value, default_charset());
1846
1837
  str->append(str_value);
1847
1838
}
1848
1839
 
1849
1840
 
1850
 
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1851
 
                           const CHARSET_INFO * const charset)
 
1841
Item_decimal::Item_decimal(const char *str_arg, uint length,
 
1842
                           CHARSET_INFO *charset)
1852
1843
{
1853
1844
  str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1854
1845
  name= (char*) str_arg;
1855
 
  decimals= (uint8_t) decimal_value.frac;
 
1846
  decimals= (uint8) decimal_value.frac;
1856
1847
  fixed= 1;
1857
1848
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1858
1849
                                             decimals, unsigned_flag);
1859
1850
}
1860
1851
 
1861
 
Item_decimal::Item_decimal(int64_t val, bool unsig)
 
1852
Item_decimal::Item_decimal(longlong val, bool unsig)
1862
1853
{
1863
1854
  int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1864
 
  decimals= (uint8_t) decimal_value.frac;
 
1855
  decimals= (uint8) decimal_value.frac;
1865
1856
  fixed= 1;
1866
1857
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1867
1858
                                             decimals, unsigned_flag);
1868
1859
}
1869
1860
 
1870
1861
 
1871
 
Item_decimal::Item_decimal(double val,
1872
 
                           int precision __attribute__((unused)),
1873
 
                           int scale __attribute__((unused)))
 
1862
Item_decimal::Item_decimal(double val, int precision, int scale)
1874
1863
{
1875
1864
  double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1876
 
  decimals= (uint8_t) decimal_value.frac;
 
1865
  decimals= (uint8) decimal_value.frac;
1877
1866
  fixed= 1;
1878
1867
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1879
1868
                                             decimals, unsigned_flag);
1881
1870
 
1882
1871
 
1883
1872
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1884
 
                           uint32_t decimal_par, uint32_t length)
 
1873
                           uint decimal_par, uint length)
1885
1874
{
1886
1875
  my_decimal2decimal(val_arg, &decimal_value);
1887
1876
  name= (char*) str;
1888
 
  decimals= (uint8_t) decimal_par;
 
1877
  decimals= (uint8) decimal_par;
1889
1878
  max_length= length;
1890
1879
  fixed= 1;
1891
1880
}
1894
1883
Item_decimal::Item_decimal(my_decimal *value_par)
1895
1884
{
1896
1885
  my_decimal2decimal(value_par, &decimal_value);
1897
 
  decimals= (uint8_t) decimal_value.frac;
 
1886
  decimals= (uint8) decimal_value.frac;
1898
1887
  fixed= 1;
1899
1888
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1900
1889
                                             decimals, unsigned_flag);
1901
1890
}
1902
1891
 
1903
1892
 
1904
 
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
 
1893
Item_decimal::Item_decimal(const uchar *bin, int precision, int scale)
1905
1894
{
1906
1895
  binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1907
1896
                    &decimal_value, precision, scale);
1908
 
  decimals= (uint8_t) decimal_value.frac;
 
1897
  decimals= (uint8) decimal_value.frac;
1909
1898
  fixed= 1;
1910
1899
  max_length= my_decimal_precision_to_length(precision, decimals,
1911
1900
                                             unsigned_flag);
1912
1901
}
1913
1902
 
1914
1903
 
1915
 
int64_t Item_decimal::val_int()
 
1904
longlong Item_decimal::val_int()
1916
1905
{
1917
 
  int64_t result;
 
1906
  longlong result;
1918
1907
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1919
1908
  return result;
1920
1909
}
1933
1922
  return result;
1934
1923
}
1935
1924
 
1936
 
void Item_decimal::print(String *str,
1937
 
                         enum_query_type query_type __attribute__((unused)))
 
1925
void Item_decimal::print(String *str, enum_query_type query_type)
1938
1926
{
1939
1927
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1940
1928
  str->append(str_value);
1941
1929
}
1942
1930
 
1943
1931
 
1944
 
bool Item_decimal::eq(const Item *item,
1945
 
                      bool binary_cmp __attribute__((unused))) const
 
1932
bool Item_decimal::eq(const Item *item, bool binary_cmp) const
1946
1933
{
1947
1934
  if (type() == item->type() && item->basic_const_item())
1948
1935
  {
1963
1950
void Item_decimal::set_decimal_value(my_decimal *value_par)
1964
1951
{
1965
1952
  my_decimal2decimal(value_par, &decimal_value);
1966
 
  decimals= (uint8_t) decimal_value.frac;
 
1953
  decimals= (uint8) decimal_value.frac;
1967
1954
  unsigned_flag= !decimal_value.sign();
1968
1955
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1969
1956
                                             decimals, unsigned_flag);
1973
1960
String *Item_float::val_str(String *str)
1974
1961
{
1975
1962
  // following assert is redundant, because fixed=1 assigned in constructor
1976
 
  assert(fixed == 1);
 
1963
  DBUG_ASSERT(fixed == 1);
1977
1964
  str->set_real(value,decimals,&my_charset_bin);
1978
1965
  return str;
1979
1966
}
1982
1969
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1983
1970
{
1984
1971
  // following assert is redundant, because fixed=1 assigned in constructor
1985
 
  assert(fixed == 1);
 
1972
  DBUG_ASSERT(fixed == 1);
1986
1973
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1987
1974
  return (decimal_value);
1988
1975
}
2027
2014
 
2028
2015
double Item_string::val_real()
2029
2016
{
2030
 
  assert(fixed == 1);
 
2017
  DBUG_ASSERT(fixed == 1);
2031
2018
  int error;
2032
2019
  char *end, *org_end;
2033
2020
  double tmp;
2034
 
  const CHARSET_INFO * const cs= str_value.charset();
 
2021
  CHARSET_INFO *cs= str_value.charset();
2035
2022
 
2036
2023
  org_end= (char*) str_value.ptr() + str_value.length();
2037
2024
  tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
2042
2029
      We can use str_value.ptr() here as Item_string is gurantee to put an
2043
2030
      end \0 here.
2044
2031
    */
2045
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2032
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2046
2033
                        ER_TRUNCATED_WRONG_VALUE,
2047
2034
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
2048
2035
                        str_value.ptr());
2055
2042
  @todo
2056
2043
  Give error if we wanted a signed integer and we got an unsigned one
2057
2044
*/
2058
 
int64_t Item_string::val_int()
 
2045
longlong Item_string::val_int()
2059
2046
{
2060
 
  assert(fixed == 1);
 
2047
  DBUG_ASSERT(fixed == 1);
2061
2048
  int err;
2062
 
  int64_t tmp;
 
2049
  longlong tmp;
2063
2050
  char *end= (char*) str_value.ptr()+ str_value.length();
2064
2051
  char *org_end= end;
2065
 
  const CHARSET_INFO * const cs= str_value.charset();
 
2052
  CHARSET_INFO *cs= str_value.charset();
2066
2053
 
2067
2054
  tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2068
2055
  /*
2072
2059
  if (err > 0 ||
2073
2060
      (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2074
2061
  {
2075
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2062
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2076
2063
                        ER_TRUNCATED_WRONG_VALUE,
2077
2064
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2078
2065
                        str_value.ptr());
2087
2074
}
2088
2075
 
2089
2076
 
2090
 
bool Item_null::eq(const Item *item,
2091
 
                   bool binary_cmp __attribute__((unused))) const
 
2077
bool Item_null::eq(const Item *item, bool binary_cmp) const
2092
2078
{ return item->type() == type(); }
2093
2079
 
2094
2080
 
2095
2081
double Item_null::val_real()
2096
2082
{
2097
2083
  // following assert is redundant, because fixed=1 assigned in constructor
2098
 
  assert(fixed == 1);
 
2084
  DBUG_ASSERT(fixed == 1);
2099
2085
  null_value=1;
2100
2086
  return 0.0;
2101
2087
}
2102
 
int64_t Item_null::val_int()
 
2088
longlong Item_null::val_int()
2103
2089
{
2104
2090
  // following assert is redundant, because fixed=1 assigned in constructor
2105
 
  assert(fixed == 1);
 
2091
  DBUG_ASSERT(fixed == 1);
2106
2092
  null_value=1;
2107
2093
  return 0;
2108
2094
}
2109
2095
/* ARGSUSED */
2110
 
String *Item_null::val_str(String *str __attribute__((unused)))
 
2096
String *Item_null::val_str(String *str)
2111
2097
{
2112
2098
  // following assert is redundant, because fixed=1 assigned in constructor
2113
 
  assert(fixed == 1);
 
2099
  DBUG_ASSERT(fixed == 1);
2114
2100
  null_value=1;
2115
2101
  return 0;
2116
2102
}
2117
2103
 
2118
 
my_decimal *Item_null::val_decimal(my_decimal *decimal_value __attribute__((unused)))
 
2104
my_decimal *Item_null::val_decimal(my_decimal *decimal_value)
2119
2105
{
2120
2106
  return 0;
2121
2107
}
2122
2108
 
2123
2109
 
2124
 
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
 
2110
Item *Item_null::safe_charset_converter(CHARSET_INFO *tocs)
2125
2111
{
2126
2112
  collation.set(tocs);
2127
2113
  return this;
2136
2122
 
2137
2123
static void
2138
2124
default_set_param_func(Item_param *param,
2139
 
                       unsigned char **pos __attribute__((unused)),
 
2125
                       uchar **pos __attribute__((unused)),
2140
2126
                       ulong len __attribute__((unused)))
2141
2127
{
2142
2128
  param->set_null();
2143
2129
}
2144
2130
 
2145
2131
 
2146
 
Item_param::Item_param(uint32_t pos_in_query_arg) :
 
2132
Item_param::Item_param(uint pos_in_query_arg) :
2147
2133
  state(NO_VALUE),
2148
2134
  item_result_type(STRING_RESULT),
2149
2135
  /* Don't pretend to be a literal unless value for this item is set. */
2150
2136
  item_type(PARAM_ITEM),
2151
 
  param_type(DRIZZLE_TYPE_VARCHAR),
 
2137
  param_type(MYSQL_TYPE_VARCHAR),
2152
2138
  pos_in_query(pos_in_query_arg),
2153
2139
  set_param_func(default_set_param_func),
2154
 
  limit_clause_param(false)
 
2140
  limit_clause_param(FALSE)
2155
2141
{
2156
2142
  name= (char*) "?";
2157
2143
  /* 
2167
2153
 
2168
2154
void Item_param::set_null()
2169
2155
{
 
2156
  DBUG_ENTER("Item_param::set_null");
2170
2157
  /* These are cleared after each execution by reset() method */
2171
2158
  null_value= 1;
2172
2159
  /* 
2178
2165
  decimals= 0;
2179
2166
  state= NULL_VALUE;
2180
2167
  item_type= Item::NULL_ITEM;
2181
 
  return;
 
2168
  DBUG_VOID_RETURN;
2182
2169
}
2183
2170
 
2184
 
void Item_param::set_int(int64_t i, uint32_t max_length_arg)
 
2171
void Item_param::set_int(longlong i, uint32 max_length_arg)
2185
2172
{
2186
 
  value.integer= (int64_t) i;
 
2173
  DBUG_ENTER("Item_param::set_int");
 
2174
  value.integer= (longlong) i;
2187
2175
  state= INT_VALUE;
2188
2176
  max_length= max_length_arg;
2189
2177
  decimals= 0;
2190
2178
  maybe_null= 0;
2191
 
  return;
 
2179
  DBUG_VOID_RETURN;
2192
2180
}
2193
2181
 
2194
2182
void Item_param::set_double(double d)
2195
2183
{
 
2184
  DBUG_ENTER("Item_param::set_double");
2196
2185
  value.real= d;
2197
2186
  state= REAL_VALUE;
2198
2187
  max_length= DBL_DIG + 8;
2199
2188
  decimals= NOT_FIXED_DEC;
2200
2189
  maybe_null= 0;
2201
 
  return;
 
2190
  DBUG_VOID_RETURN;
2202
2191
}
2203
2192
 
2204
2193
 
2214
2203
    internal decimal value.
2215
2204
*/
2216
2205
 
2217
 
void Item_param::set_decimal(char *str, ulong length)
 
2206
void Item_param::set_decimal(const char *str, ulong length)
2218
2207
{
2219
2208
  char *end;
 
2209
  DBUG_ENTER("Item_param::set_decimal");
2220
2210
 
2221
 
  end= str+length;
2222
 
  str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
 
2211
  end= (char*) str+length;
 
2212
  str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2223
2213
  state= DECIMAL_VALUE;
2224
2214
  decimals= decimal_value.frac;
2225
2215
  max_length= my_decimal_precision_to_length(decimal_value.precision(),
2226
2216
                                             decimals, unsigned_flag);
2227
2217
  maybe_null= 0;
2228
 
  return;
 
2218
  DBUG_VOID_RETURN;
2229
2219
}
2230
2220
 
2231
2221
 
2232
2222
/**
2233
 
  Set parameter value from DRIZZLE_TIME value.
 
2223
  Set parameter value from MYSQL_TIME value.
2234
2224
 
2235
2225
  @param tm              datetime value to set (time_type is ignored)
2236
2226
  @param type            type of datetime value
2242
2232
    the fact that even wrong value sent over binary protocol fits into
2243
2233
    MAX_DATE_STRING_REP_LENGTH buffer.
2244
2234
*/
2245
 
void Item_param::set_time(DRIZZLE_TIME *tm,
2246
 
                          enum enum_drizzle_timestamp_type time_type,
2247
 
                          uint32_t max_length_arg)
 
2235
void Item_param::set_time(MYSQL_TIME *tm, timestamp_type time_type,
 
2236
                          uint32 max_length_arg)
2248
2237
 
2238
  DBUG_ENTER("Item_param::set_time");
 
2239
 
2249
2240
  value.time= *tm;
2250
2241
  value.time.time_type= time_type;
2251
2242
 
2252
2243
  if (value.time.year > 9999 || value.time.month > 12 ||
2253
2244
      value.time.day > 31 ||
2254
 
      ((time_type != DRIZZLE_TIMESTAMP_TIME) && value.time.hour > 23) ||
 
2245
      ((time_type != MYSQL_TIMESTAMP_TIME) && value.time.hour > 23) ||
2255
2246
      value.time.minute > 59 || value.time.second > 59)
2256
2247
  {
2257
2248
    char buff[MAX_DATE_STRING_REP_LENGTH];
2258
 
    uint32_t length= my_TIME_to_str(&value.time, buff);
2259
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2249
    uint length= my_TIME_to_str(&value.time, buff);
 
2250
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2260
2251
                                 buff, length, time_type, 0);
2261
 
    set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
 
2252
    set_zero_time(&value.time, MYSQL_TIMESTAMP_ERROR);
2262
2253
  }
2263
2254
 
2264
2255
  state= TIME_VALUE;
2265
2256
  maybe_null= 0;
2266
2257
  max_length= max_length_arg;
2267
2258
  decimals= 0;
2268
 
  return;
 
2259
  DBUG_VOID_RETURN;
2269
2260
}
2270
2261
 
2271
2262
 
2272
2263
bool Item_param::set_str(const char *str, ulong length)
2273
2264
{
 
2265
  DBUG_ENTER("Item_param::set_str");
2274
2266
  /*
2275
2267
    Assign string with no conversion: data is converted only after it's
2276
2268
    been written to the binary log.
2277
2269
  */
2278
 
  uint32_t dummy_errors;
 
2270
  uint dummy_errors;
2279
2271
  if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2280
2272
                     &dummy_errors))
2281
 
    return(true);
 
2273
    DBUG_RETURN(TRUE);
2282
2274
  state= STRING_VALUE;
2283
2275
  max_length= length;
2284
2276
  maybe_null= 0;
2285
2277
  /* max_length and decimals are set after charset conversion */
2286
 
  /* sic: str may be not null-terminated */
2287
 
  return(false);
 
2278
  /* sic: str may be not null-terminated, don't add DBUG_PRINT here */
 
2279
  DBUG_RETURN(FALSE);
2288
2280
}
2289
2281
 
2290
2282
 
2291
2283
bool Item_param::set_longdata(const char *str, ulong length)
2292
2284
{
 
2285
  DBUG_ENTER("Item_param::set_longdata");
 
2286
 
2293
2287
  /*
2294
2288
    If client character set is multibyte, end of long data packet
2295
2289
    may hit at the middle of a multibyte character.  Additionally,
2300
2294
    write query to the binary log and only then perform conversion.
2301
2295
  */
2302
2296
  if (str_value.append(str, length, &my_charset_bin))
2303
 
    return(true);
 
2297
    DBUG_RETURN(TRUE);
2304
2298
  state= LONG_DATA_VALUE;
2305
2299
  maybe_null= 0;
2306
2300
 
2307
 
  return(false);
 
2301
  DBUG_RETURN(FALSE);
2308
2302
}
2309
2303
 
2310
2304
 
2322
2316
 
2323
2317
bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry)
2324
2318
{
 
2319
  DBUG_ENTER("Item_param::set_from_user_var");
2325
2320
  if (entry && entry->value)
2326
2321
  {
2327
2322
    item_result_type= entry->type;
2328
2323
    unsigned_flag= entry->unsigned_flag;
2329
2324
    if (limit_clause_param)
2330
2325
    {
2331
 
      bool unused;
 
2326
      my_bool unused;
2332
2327
      set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2333
2328
      item_type= Item::INT_ITEM;
2334
 
      return(!unsigned_flag && value.integer < 0 ? 1 : 0);
 
2329
      DBUG_RETURN(!unsigned_flag && value.integer < 0 ? 1 : 0);
2335
2330
    }
2336
2331
    switch (item_result_type) {
2337
2332
    case REAL_RESULT:
2339
2334
      item_type= Item::REAL_ITEM;
2340
2335
      break;
2341
2336
    case INT_RESULT:
2342
 
      set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
 
2337
      set_int(*(longlong*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
2343
2338
      item_type= Item::INT_ITEM;
2344
2339
      break;
2345
2340
    case STRING_RESULT:
2346
2341
    {
2347
 
      const CHARSET_INFO * const fromcs= entry->collation.collation;
2348
 
      const CHARSET_INFO * const tocs= thd->variables.collation_connection;
2349
 
      uint32_t dummy_offset;
 
2342
      CHARSET_INFO *fromcs= entry->collation.collation;
 
2343
      CHARSET_INFO *tocs= thd->variables.collation_connection;
 
2344
      uint32 dummy_offset;
2350
2345
 
2351
2346
      value.cs_info.character_set_of_placeholder= 
2352
2347
        value.cs_info.character_set_client= fromcs;
2365
2360
      item_type= Item::STRING_ITEM;
2366
2361
 
2367
2362
      if (set_str((const char *)entry->value, entry->length))
2368
 
        return(1);
 
2363
        DBUG_RETURN(1);
2369
2364
      break;
2370
2365
    }
2371
2366
    case DECIMAL_RESULT:
2380
2375
      break;
2381
2376
    }
2382
2377
    default:
2383
 
      assert(0);
 
2378
      DBUG_ASSERT(0);
2384
2379
      set_null();
2385
2380
    }
2386
2381
  }
2387
2382
  else
2388
2383
    set_null();
2389
2384
 
2390
 
  return(0);
 
2385
  DBUG_RETURN(0);
2391
2386
}
2392
2387
 
2393
2388
/**
2400
2395
 
2401
2396
void Item_param::reset()
2402
2397
{
 
2398
  DBUG_ENTER("Item_param::reset");
2403
2399
  /* Shrink string buffer if it's bigger than max possible CHAR column */
2404
2400
  if (str_value.alloced_length() > MAX_CHAR_WIDTH)
2405
2401
    str_value.free();
2421
2417
    contain a literal of some kind.
2422
2418
    In all other cases when this object is accessed its value is
2423
2419
    set (this assumption is guarded by 'state' and
2424
 
    assertS(state != NO_VALUE) in all Item_param::get_*
 
2420
    DBUG_ASSERTS(state != NO_VALUE) in all Item_param::get_*
2425
2421
    methods).
2426
2422
  */
2427
 
  return;
 
2423
  DBUG_VOID_RETURN;
2428
2424
}
2429
2425
 
2430
2426
 
2450
2446
    return set_field_to_null_with_conversions(field, no_conversions);
2451
2447
  case NO_VALUE:
2452
2448
  default:
2453
 
    assert(0);
 
2449
    DBUG_ASSERT(0);
2454
2450
  }
2455
2451
  return 1;
2456
2452
}
2457
2453
 
2458
2454
 
2459
 
bool Item_param::get_time(DRIZZLE_TIME *res)
 
2455
bool Item_param::get_time(MYSQL_TIME *res)
2460
2456
{
2461
2457
  if (state == TIME_VALUE)
2462
2458
  {
2471
2467
}
2472
2468
 
2473
2469
 
2474
 
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
 
2470
bool Item_param::get_date(MYSQL_TIME *res, uint fuzzydate)
2475
2471
{
2476
2472
  if (state == TIME_VALUE)
2477
2473
  {
2508
2504
      This works for example when user says SELECT ?+0.0 and supplies
2509
2505
      time value for the placeholder.
2510
2506
    */
2511
 
    return uint64_t2double(TIME_to_uint64_t(&value.time));
 
2507
    return ulonglong2double(TIME_to_ulonglong(&value.time));
2512
2508
  case NULL_VALUE:
2513
2509
    return 0.0;
2514
2510
  default:
2515
 
    assert(0);
 
2511
    DBUG_ASSERT(0);
2516
2512
  }
2517
2513
  return 0.0;
2518
2514
2519
2515
 
2520
2516
 
2521
 
int64_t Item_param::val_int() 
 
2517
longlong Item_param::val_int() 
2522
2518
2523
2519
  switch (state) {
2524
2520
  case REAL_VALUE:
2525
 
    return (int64_t) rint(value.real);
 
2521
    return (longlong) rint(value.real);
2526
2522
  case INT_VALUE:
2527
2523
    return value.integer;
2528
2524
  case DECIMAL_VALUE:
2529
2525
  {
2530
 
    int64_t i;
 
2526
    longlong i;
2531
2527
    my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
2532
2528
    return i;
2533
2529
  }
2539
2535
                         str_value.length(), 10, (char**) 0, &dummy_err);
2540
2536
    }
2541
2537
  case TIME_VALUE:
2542
 
    return (int64_t) TIME_to_uint64_t(&value.time);
 
2538
    return (longlong) TIME_to_ulonglong(&value.time);
2543
2539
  case NULL_VALUE:
2544
2540
    return 0; 
2545
2541
  default:
2546
 
    assert(0);
 
2542
    DBUG_ASSERT(0);
2547
2543
  }
2548
2544
  return 0;
2549
2545
}
2566
2562
    return dec;
2567
2563
  case TIME_VALUE:
2568
2564
  {
2569
 
    int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
 
2565
    longlong i= (longlong) TIME_to_ulonglong(&value.time);
2570
2566
    int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
2571
2567
    return dec;
2572
2568
  }
2573
2569
  case NULL_VALUE:
2574
2570
    return 0; 
2575
2571
  default:
2576
 
    assert(0);
 
2572
    DBUG_ASSERT(0);
2577
2573
  }
2578
2574
  return 0;
2579
2575
}
2607
2603
  case NULL_VALUE:
2608
2604
    return NULL; 
2609
2605
  default:
2610
 
    assert(0);
 
2606
    DBUG_ASSERT(0);
2611
2607
  }
2612
2608
  return str;
2613
2609
}
2654
2650
      *ptr++= '\'';
2655
2651
      ptr+= (uint) my_TIME_to_str(&value.time, ptr);
2656
2652
      *ptr++= '\'';
2657
 
      str->length((uint32_t) (ptr - buf));
 
2653
      str->length((uint32) (ptr - buf));
2658
2654
      break;
2659
2655
    }
2660
2656
  case STRING_VALUE:
2667
2663
  case NULL_VALUE:
2668
2664
    return &my_null_string;
2669
2665
  default:
2670
 
    assert(0);
 
2666
    DBUG_ASSERT(0);
2671
2667
  }
2672
2668
  return str;
2673
2669
}
2680
2676
 
2681
2677
bool Item_param::convert_str_value(THD *thd)
2682
2678
{
2683
 
  bool rc= false;
 
2679
  bool rc= FALSE;
2684
2680
  if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2685
2681
  {
2686
2682
    /*
2718
2714
bool Item_param::basic_const_item() const
2719
2715
{
2720
2716
  if (state == NO_VALUE || state == TIME_VALUE)
2721
 
    return false;
2722
 
  return true;
 
2717
    return FALSE;
 
2718
  return TRUE;
2723
2719
}
2724
2720
 
2725
2721
 
2744
2740
    break;
2745
2741
  case NO_VALUE:
2746
2742
  default:
2747
 
    assert(0);
 
2743
    DBUG_ASSERT(0);
2748
2744
  };
2749
2745
  return 0;
2750
2746
}
2755
2751
{
2756
2752
  Item *item;
2757
2753
  if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2758
 
    return false;
 
2754
    return FALSE;
2759
2755
  /*
2760
2756
    We need to cast off const to call val_int(). This should be OK for
2761
2757
    a basic constant.
2764
2760
 
2765
2761
  switch (state) {
2766
2762
  case NULL_VALUE:
2767
 
    return true;
 
2763
    return TRUE;
2768
2764
  case INT_VALUE:
2769
2765
    return value.integer == item->val_int() &&
2770
2766
           unsigned_flag == item->unsigned_flag;
2778
2774
  default:
2779
2775
    break;
2780
2776
  }
2781
 
  return false;
 
2777
  return FALSE;
2782
2778
}
2783
2779
 
2784
2780
/* End of Item_param related */
2785
2781
 
2786
 
void Item_param::print(String *str,
2787
 
                       enum_query_type query_type __attribute__((unused)))
 
2782
void Item_param::print(String *str, enum_query_type query_type)
2788
2783
{
2789
2784
  if (state == NO_VALUE)
2790
2785
  {
2814
2809
}
2815
2810
 
2816
2811
/* ARGSUSED */
2817
 
String *Item_copy_string::val_str(String *str __attribute__((unused)))
 
2812
String *Item_copy_string::val_str(String *str)
2818
2813
{
2819
2814
  // Item_copy_string is used without fix_fields call
2820
2815
  if (null_value)
2838
2833
*/
2839
2834
 
2840
2835
/* ARGSUSED */
2841
 
bool Item::fix_fields(THD *thd __attribute__((unused)),
2842
 
                      Item **ref __attribute__((unused)))
 
2836
bool Item::fix_fields(THD *thd, Item **ref)
2843
2837
{
2844
2838
 
2845
2839
  // We do not check fields which are fixed during construction
2846
 
  assert(fixed == 0 || basic_const_item());
 
2840
  DBUG_ASSERT(fixed == 0 || basic_const_item());
2847
2841
  fixed= 1;
2848
 
  return false;
 
2842
  return FALSE;
2849
2843
}
2850
2844
 
2851
2845
double Item_ref_null_helper::val_real()
2852
2846
{
2853
 
  assert(fixed == 1);
 
2847
  DBUG_ASSERT(fixed == 1);
2854
2848
  double tmp= (*ref)->val_result();
2855
2849
  owner->was_null|= null_value= (*ref)->null_value;
2856
2850
  return tmp;
2857
2851
}
2858
2852
 
2859
2853
 
2860
 
int64_t Item_ref_null_helper::val_int()
 
2854
longlong Item_ref_null_helper::val_int()
2861
2855
{
2862
 
  assert(fixed == 1);
2863
 
  int64_t tmp= (*ref)->val_int_result();
 
2856
  DBUG_ASSERT(fixed == 1);
 
2857
  longlong tmp= (*ref)->val_int_result();
2864
2858
  owner->was_null|= null_value= (*ref)->null_value;
2865
2859
  return tmp;
2866
2860
}
2868
2862
 
2869
2863
my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value)
2870
2864
{
2871
 
  assert(fixed == 1);
 
2865
  DBUG_ASSERT(fixed == 1);
2872
2866
  my_decimal *val= (*ref)->val_decimal_result(decimal_value);
2873
2867
  owner->was_null|= null_value= (*ref)->null_value;
2874
2868
  return val;
2877
2871
 
2878
2872
bool Item_ref_null_helper::val_bool()
2879
2873
{
2880
 
  assert(fixed == 1);
 
2874
  DBUG_ASSERT(fixed == 1);
2881
2875
  bool val= (*ref)->val_bool_result();
2882
2876
  owner->was_null|= null_value= (*ref)->null_value;
2883
2877
  return val;
2886
2880
 
2887
2881
String* Item_ref_null_helper::val_str(String* s)
2888
2882
{
2889
 
  assert(fixed == 1);
 
2883
  DBUG_ASSERT(fixed == 1);
2890
2884
  String* tmp= (*ref)->str_result(s);
2891
2885
  owner->was_null|= null_value= (*ref)->null_value;
2892
2886
  return tmp;
2893
2887
}
2894
2888
 
2895
2889
 
2896
 
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
 
2890
bool Item_ref_null_helper::get_date(MYSQL_TIME *ltime, uint fuzzydate)
2897
2891
{  
2898
2892
  return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
2899
2893
}
2925
2919
  current->mark_as_dependent(last);
2926
2920
  if (thd->lex->describe & DESCRIBE_EXTENDED)
2927
2921
  {
2928
 
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
2922
    char warn_buff[MYSQL_ERRMSG_SIZE];
2929
2923
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
2930
2924
            db_name, (db_name[0] ? "." : ""),
2931
2925
            table_name, (table_name [0] ? "." : ""),
2932
2926
            resolved_item->field_name,
2933
2927
            current->select_number, last->select_number);
2934
 
    push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
2928
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
2935
2929
                 ER_WARN_FIELD_RESOLVED, warn_buff);
2936
2930
  }
2937
2931
}
3016
3010
    - NULL if find_item is not in group_list
3017
3011
*/
3018
3012
 
3019
 
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
 
3013
static Item** find_field_in_group_list(Item *find_item, ORDER *group_list)
3020
3014
{
3021
3015
  const char *db_name;
3022
3016
  const char *table_name;
3023
3017
  const char *field_name;
3024
 
  order_st      *found_group= NULL;
 
3018
  ORDER      *found_group= NULL;
3025
3019
  int         found_match_degree= 0;
3026
3020
  Item_ident *cur_field;
3027
3021
  int         cur_match_degree= 0;
3045
3039
    db_name= name_buff;
3046
3040
  }
3047
3041
 
3048
 
  assert(field_name != 0);
 
3042
  DBUG_ASSERT(field_name != 0);
3049
3043
 
3050
 
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
3044
  for (ORDER *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
3051
3045
  {
3052
3046
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
3053
3047
    {
3054
3048
      cur_field= (Item_ident*) *cur_group->item;
3055
3049
      cur_match_degree= 0;
3056
3050
      
3057
 
      assert(cur_field->field_name != 0);
 
3051
      DBUG_ASSERT(cur_field->field_name != 0);
3058
3052
 
3059
3053
      if (!my_strcasecmp(system_charset_info,
3060
3054
                         cur_field->field_name, field_name))
3148
3142
{
3149
3143
  Item **group_by_ref= NULL;
3150
3144
  Item **select_ref= NULL;
3151
 
  order_st *group_list= (order_st*) select->group_list.first;
3152
 
  bool ambiguous_fields= false;
3153
 
  uint32_t counter;
 
3145
  ORDER *group_list= (ORDER*) select->group_list.first;
 
3146
  bool ambiguous_fields= FALSE;
 
3147
  uint counter;
3154
3148
  enum_resolution_type resolution;
3155
3149
 
3156
3150
  /*
3162
3156
                                      &resolution)))
3163
3157
    return NULL; /* Some error occurred. */
3164
3158
  if (resolution == RESOLVED_AGAINST_ALIAS)
3165
 
    ref->alias_name_used= true;
 
3159
    ref->alias_name_used= TRUE;
3166
3160
 
3167
3161
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
3168
3162
  if (select->having_fix_field && !ref->with_sum_func && group_list)
3173
3167
    if (group_by_ref && (select_ref != not_found_item) &&
3174
3168
        !((*group_by_ref)->eq(*select_ref, 0)))
3175
3169
    {
3176
 
      ambiguous_fields= true;
3177
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
3170
      ambiguous_fields= TRUE;
 
3171
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
3178
3172
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
3179
3173
                          current_thd->where);
3180
3174
 
3185
3179
  {
3186
3180
    if (select_ref != not_found_item && !ambiguous_fields)
3187
3181
    {
3188
 
      assert(*select_ref != 0);
 
3182
      DBUG_ASSERT(*select_ref != 0);
3189
3183
      if (!select->ref_pointer_array[counter])
3190
3184
      {
3191
3185
        my_error(ER_ILLEGAL_REFERENCE, MYF(0),
3192
3186
                 ref->name, "forward reference in item list");
3193
3187
        return NULL;
3194
3188
      }
3195
 
      assert((*select_ref)->fixed);
 
3189
      DBUG_ASSERT((*select_ref)->fixed);
3196
3190
      return (select->ref_pointer_array + counter);
3197
3191
    }
3198
3192
    if (group_by_ref)
3199
3193
      return group_by_ref;
3200
 
    assert(false);
 
3194
    DBUG_ASSERT(FALSE);
3201
3195
    return NULL; /* So there is no compiler warning. */
3202
3196
  }
3203
3197
 
3239
3233
  @retval
3240
3234
    1   column succefully resolved and fix_fields() should continue.
3241
3235
  @retval
3242
 
    0   column fully fixed and fix_fields() should return false
 
3236
    0   column fully fixed and fix_fields() should return FALSE
3243
3237
  @retval
3244
3238
    -1  error occured
3245
3239
*/
3249
3243
{
3250
3244
  enum_parsing_place place= NO_MATTER;
3251
3245
  bool field_found= (*from_field != not_found_field);
3252
 
  bool upward_lookup= false;
 
3246
  bool upward_lookup= FALSE;
3253
3247
 
3254
3248
  /*
3255
3249
    If there are outer contexts (outer selects, but current select is
3277
3271
    Item_subselect *prev_subselect_item=
3278
3272
      last_checked_context->select_lex->master_unit()->item;
3279
3273
    last_checked_context= outer_context;
3280
 
    upward_lookup= true;
 
3274
    upward_lookup= TRUE;
3281
3275
 
3282
3276
    place= prev_subselect_item->parsing_place;
3283
3277
    /*
3299
3293
                                            last_name_resolution_table,
3300
3294
                                          reference,
3301
3295
                                          IGNORE_EXCEPT_NON_UNIQUE,
3302
 
                                          true, true)) !=
 
3296
                                          TRUE, TRUE)) !=
3303
3297
        not_found_field)
3304
3298
    {
3305
3299
      if (*from_field)
3382
3376
        return -1; /* Some error occurred (e.g. ambiguous names). */
3383
3377
      if (ref != not_found_item)
3384
3378
      {
3385
 
        assert(*ref && (*ref)->fixed);
 
3379
        DBUG_ASSERT(*ref && (*ref)->fixed);
3386
3380
        prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
3387
3381
        prev_subselect_item->const_item_cache&= (*ref)->const_item();
3388
3382
        break;
3398
3392
    prev_subselect_item->const_item_cache= 0;
3399
3393
  }
3400
3394
 
3401
 
  assert(ref != 0);
 
3395
  DBUG_ASSERT(ref != 0);
3402
3396
  if (!*from_field)
3403
3397
    return -1;
3404
3398
  if (ref == not_found_item && *from_field == not_found_field)
3416
3410
                           context->last_name_resolution_table,
3417
3411
                           reference, REPORT_ALL_ERRORS,
3418
3412
                           !any_privileges &&
3419
 
                           true, true);
 
3413
                           TRUE, TRUE);
3420
3414
    }
3421
3415
    return -1;
3422
3416
  }
3426
3420
    Item_ref *rf;
3427
3421
 
3428
3422
    /* Should have been checked in resolve_ref_in_select_and_group(). */
3429
 
    assert(*ref && (*ref)->fixed);
 
3423
    DBUG_ASSERT(*ref && (*ref)->fixed);
3430
3424
    /*
3431
3425
      Here, a subset of actions performed by Item_ref::set_properties
3432
3426
      is not enough. So we pass ptr to NULL into Item_[direct]_ref
3457
3451
      rf is Item_ref => never substitute other items (in this case)
3458
3452
      during fix_fields() => we can use rf after fix_fields()
3459
3453
    */
3460
 
    assert(!rf->fixed);                // Assured by Item_ref()
 
3454
    DBUG_ASSERT(!rf->fixed);                // Assured by Item_ref()
3461
3455
    if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3462
3456
      return -1;
3463
3457
 
3484
3478
        rf is Item_ref => never substitute other items (in this case)
3485
3479
        during fix_fields() => we can use rf after fix_fields()
3486
3480
      */
3487
 
      assert(!rf->fixed);                // Assured by Item_ref()
 
3481
      DBUG_ASSERT(!rf->fixed);                // Assured by Item_ref()
3488
3482
      if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3489
3483
        return -1;
3490
3484
      return 0;
3534
3528
    view column
3535
3529
 
3536
3530
  @retval
3537
 
    true  if error
 
3531
    TRUE  if error
3538
3532
  @retval
3539
 
    false on success
 
3533
    FALSE on success
3540
3534
*/
3541
3535
 
3542
3536
bool Item_field::fix_fields(THD *thd, Item **reference)
3543
3537
{
3544
 
  assert(fixed == 0);
 
3538
  DBUG_ASSERT(fixed == 0);
3545
3539
  Field *from_field= (Field *)not_found_field;
3546
3540
  bool outer_fixed= false;
3547
3541
 
3560
3554
                                            REPORT_ALL_ERRORS : 
3561
3555
                                            IGNORE_EXCEPT_NON_UNIQUE,
3562
3556
                                          !any_privileges,
3563
 
                                          true)) ==
 
3557
                                          TRUE)) ==
3564
3558
        not_found_field)
3565
3559
    {
3566
3560
      int ret;
3567
3561
      /* Look up in current select's item_list to find aliased fields */
3568
3562
      if (thd->lex->current_select->is_item_list_lookup)
3569
3563
      {
3570
 
        uint32_t counter;
 
3564
        uint counter;
3571
3565
        enum_resolution_type resolution;
3572
3566
        Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3573
3567
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
3575
3569
        if (!res)
3576
3570
          return 1;
3577
3571
        if (resolution == RESOLVED_AGAINST_ALIAS)
3578
 
          alias_name_used= true;
 
3572
          alias_name_used= TRUE;
3579
3573
        if (res != (Item **)not_found_item)
3580
3574
        {
3581
3575
          if ((*res)->type() == Item::FIELD_ITEM)
3622
3616
      }
3623
3617
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3624
3618
        goto error;
3625
 
      outer_fixed= true;
 
3619
      outer_fixed= TRUE;
3626
3620
      if (!ret)
3627
3621
        goto mark_non_agg_field;
3628
3622
    }
3654
3648
      Also we suppose that view can't be changed during PS/SP life.
3655
3649
    */
3656
3650
    if (from_field == view_ref_found)
3657
 
      return false;
 
3651
      return FALSE;
3658
3652
 
3659
3653
    set_field(from_field);
3660
3654
    if (thd->lex->in_sum_func &&
3665
3659
  }
3666
3660
  else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3667
3661
  {
3668
 
    Table *table= field->table;
 
3662
    TABLE *table= field->table;
3669
3663
    MY_BITMAP *current_bitmap, *other_bitmap;
3670
3664
    if (thd->mark_used_columns == MARK_COLUMNS_READ)
3671
3665
    {
3691
3685
  }
3692
3686
  fixed= 1;
3693
3687
mark_non_agg_field:
3694
 
  return false;
 
3688
  return FALSE;
3695
3689
 
3696
3690
error:
3697
3691
  context->process_error(thd);
3698
 
  return true;
 
3692
  return TRUE;
3699
3693
}
3700
3694
 
3701
 
Item *Item_field::safe_charset_converter(const CHARSET_INFO * const tocs)
 
3695
Item *Item_field::safe_charset_converter(CHARSET_INFO *tocs)
3702
3696
{
3703
3697
  no_const_subst= 1;
3704
3698
  return Item::safe_charset_converter(tocs);
3707
3701
 
3708
3702
void Item_field::cleanup()
3709
3703
{
 
3704
  DBUG_ENTER("Item_field::cleanup");
3710
3705
  Item_ident::cleanup();
3711
3706
  /*
3712
3707
    Even if this object was created by direct link to field in setup_wild()
3714
3709
    I.e. we can drop 'field'.
3715
3710
   */
3716
3711
  field= result_field= 0;
3717
 
  null_value= false;
3718
 
  return;
 
3712
  null_value= FALSE;
 
3713
  DBUG_VOID_RETURN;
3719
3714
}
3720
3715
 
3721
3716
/**
3782
3777
    a comparison predicate.
3783
3778
 
3784
3779
  @retval
3785
 
    true   substitution is valid
 
3780
    TRUE   substitution is valid
3786
3781
  @retval
3787
 
    false  otherwise
 
3782
    FALSE  otherwise
3788
3783
*/
3789
3784
 
3790
 
bool Item_field::subst_argument_checker(unsigned char **arg)
 
3785
bool Item_field::subst_argument_checker(uchar **arg)
3791
3786
{
3792
3787
  return (result_type() != STRING_RESULT) || (*arg);
3793
3788
}
3794
3789
 
3795
3790
 
3796
3791
/**
 
3792
  Convert a numeric value to a zero-filled string
 
3793
 
 
3794
  @param[in,out]  item   the item to operate on
 
3795
  @param          field  The field that this value is equated to
 
3796
 
 
3797
  This function converts a numeric value to a string. In this conversion
 
3798
  the zero-fill flag of the field is taken into account.
 
3799
  This is required so the resulting string value can be used instead of
 
3800
  the field reference when propagating equalities.
 
3801
*/
 
3802
 
 
3803
static void convert_zerofill_number_to_string(Item **item, Field_num *field)
 
3804
{
 
3805
  char buff[MAX_FIELD_WIDTH],*pos;
 
3806
  String tmp(buff,sizeof(buff), field->charset()), *res;
 
3807
 
 
3808
  res= (*item)->val_str(&tmp);
 
3809
  field->prepend_zeros(res);
 
3810
  pos= (char *) sql_strmake (res->ptr(), res->length());
 
3811
  *item= new Item_string(pos, res->length(), field->charset());
 
3812
}
 
3813
 
 
3814
 
 
3815
/**
3797
3816
  Set a pointer to the multiple equality the field reference belongs to
3798
3817
  (if any).
3799
3818
 
3817
3836
    - pointer to the field item, otherwise.
3818
3837
*/
3819
3838
 
3820
 
Item *Item_field::equal_fields_propagator(unsigned char *arg)
 
3839
Item *Item_field::equal_fields_propagator(uchar *arg)
3821
3840
{
3822
3841
  if (no_const_subst)
3823
3842
    return this;
3839
3858
  if (!item ||
3840
3859
      (cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
3841
3860
    item= this;
3842
 
 
 
3861
  else if (field && (field->flags & ZEROFILL_FLAG) && IS_NUM(field->type()))
 
3862
  {
 
3863
    if (item && cmp_context != INT_RESULT)
 
3864
      convert_zerofill_number_to_string(&item, (Field_num *)field);
 
3865
    else
 
3866
      item= this;
 
3867
  }
3843
3868
  return item;
3844
3869
}
3845
3870
 
3850
3875
  See comments in Arg_comparator::set_compare_func() for details.
3851
3876
*/
3852
3877
 
3853
 
bool Item_field::set_no_const_sub(unsigned char *arg __attribute__((unused)))
 
3878
bool Item_field::set_no_const_sub(uchar *arg)
3854
3879
{
3855
3880
  if (field->charset() != &my_charset_bin)
3856
3881
    no_const_subst=1;
3857
 
  return false;
 
3882
  return FALSE;
3858
3883
}
3859
3884
 
3860
3885
 
3883
3908
    - this - otherwise.
3884
3909
*/
3885
3910
 
3886
 
Item *Item_field::replace_equal_field(unsigned char *arg __attribute__((unused)))
 
3911
Item *Item_field::replace_equal_field(uchar *arg)
3887
3912
{
3888
3913
  if (item_equal)
3889
3914
  {
3919
3944
  tmp_field->type=              field_type_arg;
3920
3945
  tmp_field->length=max_length;
3921
3946
  tmp_field->decimals=decimals;
 
3947
  if (unsigned_flag)
 
3948
    tmp_field->flags |= UNSIGNED_FLAG;
3922
3949
}
3923
3950
 
3924
3951
void Item::make_field(Send_field *tmp_field)
3929
3956
 
3930
3957
enum_field_types Item::string_field_type() const
3931
3958
{
3932
 
  enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
3933
 
  if (max_length >= 65536)
3934
 
    f_type= DRIZZLE_TYPE_BLOB;
 
3959
  enum_field_types f_type= MYSQL_TYPE_VAR_STRING;
 
3960
  if (max_length >= 16777216)
 
3961
    f_type= MYSQL_TYPE_LONG_BLOB;
 
3962
  else if (max_length >= 65536)
 
3963
    f_type= MYSQL_TYPE_MEDIUM_BLOB;
3935
3964
  return f_type;
3936
3965
}
3937
3966
 
3946
3975
{
3947
3976
  switch (result_type()) {
3948
3977
  case STRING_RESULT:  return string_field_type();
3949
 
  case INT_RESULT:     return DRIZZLE_TYPE_LONGLONG;
3950
 
  case DECIMAL_RESULT: return DRIZZLE_TYPE_NEWDECIMAL;
3951
 
  case REAL_RESULT:    return DRIZZLE_TYPE_DOUBLE;
 
3978
  case INT_RESULT:     return MYSQL_TYPE_LONGLONG;
 
3979
  case DECIMAL_RESULT: return MYSQL_TYPE_NEWDECIMAL;
 
3980
  case REAL_RESULT:    return MYSQL_TYPE_DOUBLE;
3952
3981
  case ROW_RESULT:
3953
3982
  default:
3954
 
    assert(0);
3955
 
    return DRIZZLE_TYPE_VARCHAR;
 
3983
    DBUG_ASSERT(0);
 
3984
    return MYSQL_TYPE_VARCHAR;
3956
3985
  }
3957
3986
}
3958
3987
 
3961
3990
{
3962
3991
  switch (field_type())
3963
3992
  {
3964
 
    case DRIZZLE_TYPE_NEWDATE:
3965
 
    case DRIZZLE_TYPE_DATETIME:
3966
 
    case DRIZZLE_TYPE_TIMESTAMP:
3967
 
      return true;
 
3993
    case MYSQL_TYPE_DATE:
 
3994
    case MYSQL_TYPE_DATETIME:
 
3995
    case MYSQL_TYPE_TIMESTAMP:
 
3996
      return TRUE;
3968
3997
    default:
3969
3998
      break;
3970
3999
  }
3971
 
  return false;
 
4000
  return FALSE;
3972
4001
}
3973
4002
 
3974
4003
 
3975
4004
String *Item::check_well_formed_result(String *str, bool send_error)
3976
4005
{
3977
4006
  /* Check whether we got a well-formed string */
3978
 
  const CHARSET_INFO * const cs= str->charset();
 
4007
  CHARSET_INFO *cs= str->charset();
3979
4008
  int well_formed_error;
3980
 
  uint32_t wlen= cs->cset->well_formed_len(cs,
 
4009
  uint wlen= cs->cset->well_formed_len(cs,
3981
4010
                                       str->ptr(), str->ptr() + str->length(),
3982
4011
                                       str->length(), &well_formed_error);
3983
4012
  if (wlen < str->length())
3984
4013
  {
3985
4014
    THD *thd= current_thd;
3986
4015
    char hexbuf[7];
3987
 
    enum DRIZZLE_ERROR::enum_warning_level level;
3988
 
    uint32_t diff= str->length() - wlen;
 
4016
    enum MYSQL_ERROR::enum_warning_level level;
 
4017
    uint diff= str->length() - wlen;
3989
4018
    set_if_smaller(diff, 3);
3990
4019
    octet2hex(hexbuf, str->ptr() + wlen, diff);
3991
4020
    if (send_error)
3995
4024
      return 0;
3996
4025
    }
3997
4026
    {
3998
 
      level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
 
4027
      level= MYSQL_ERROR::WARN_LEVEL_ERROR;
3999
4028
      null_value= 1;
4000
4029
      str= 0;
4001
4030
    }
4011
4040
  SYNOPSIS
4012
4041
    eq_by_collation()
4013
4042
    item               item to compare with
4014
 
    binary_cmp         true <-> compare as binaries
 
4043
    binary_cmp         TRUE <-> compare as binaries
4015
4044
    cs                 collation to use when comparing strings
4016
4045
 
4017
4046
  DESCRIPTION
4026
4055
    0    otherwise
4027
4056
*/
4028
4057
 
4029
 
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
 
4058
bool Item::eq_by_collation(Item *item, bool binary_cmp, CHARSET_INFO *cs)
4030
4059
{
4031
 
  const CHARSET_INFO *save_cs= 0;
4032
 
  const CHARSET_INFO *save_item_cs= 0;
 
4060
  CHARSET_INFO *save_cs= 0;
 
4061
  CHARSET_INFO *save_item_cs= 0;
4033
4062
  if (collation.collation != cs)
4034
4063
  {
4035
4064
    save_cs= collation.collation;
4059
4088
  @param table          Table for which the field is created
4060
4089
*/
4061
4090
 
4062
 
Field *Item::make_string_field(Table *table)
 
4091
Field *Item::make_string_field(TABLE *table)
4063
4092
{
4064
4093
  Field *field;
4065
 
  assert(collation.collation);
 
4094
  DBUG_ASSERT(collation.collation);
4066
4095
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
4067
4096
    field= new Field_blob(max_length, maybe_null, name,
4068
4097
                          collation.collation);
4069
 
  else
 
4098
  /* Item_type_holder holds the exact type, do not change it */
 
4099
  else if (max_length > 0 &&
 
4100
      (type() != Item::TYPE_HOLDER || field_type() != MYSQL_TYPE_STRING))
4070
4101
    field= new Field_varstring(max_length, maybe_null, name, table->s,
4071
4102
                               collation.collation);
4072
 
 
 
4103
  else
 
4104
    field= new Field_string(max_length, maybe_null, name,
 
4105
                            collation.collation);
4073
4106
  if (field)
4074
4107
    field->init(table);
4075
4108
  return field;
4088
4121
    \#    Created field
4089
4122
*/
4090
4123
 
4091
 
Field *Item::tmp_table_field_from_field_type(Table *table, bool fixed_length __attribute__((unused)))
 
4124
Field *Item::tmp_table_field_from_field_type(TABLE *table, bool fixed_length)
4092
4125
{
4093
4126
  /*
4094
4127
    The field functions defines a field to be not null if null_ptr is not 0
4095
4128
  */
4096
 
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
 
4129
  uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
4097
4130
  Field *field;
4098
4131
 
4099
4132
  switch (field_type()) {
4100
 
  case DRIZZLE_TYPE_NEWDECIMAL:
4101
 
    field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
 
4133
  case MYSQL_TYPE_DECIMAL:
 
4134
  case MYSQL_TYPE_NEWDECIMAL:
 
4135
    field= new Field_new_decimal((uchar*) 0, max_length, null_ptr, 0,
4102
4136
                                 Field::NONE, name, decimals, 0,
4103
4137
                                 unsigned_flag);
4104
4138
    break;
4105
 
  case DRIZZLE_TYPE_TINY:
4106
 
    field= new Field_tiny((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4107
 
                          name, 0, unsigned_flag);
4108
 
    break;
4109
 
  case DRIZZLE_TYPE_LONG:
4110
 
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4111
 
                          name, 0, unsigned_flag);
4112
 
    break;
4113
 
  case DRIZZLE_TYPE_LONGLONG:
4114
 
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4139
  case MYSQL_TYPE_TINY:
 
4140
    field= new Field_tiny((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4141
                          name, 0, unsigned_flag);
 
4142
    break;
 
4143
  case MYSQL_TYPE_SHORT:
 
4144
    field= new Field_short((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4145
                           name, 0, unsigned_flag);
 
4146
    break;
 
4147
  case MYSQL_TYPE_LONG:
 
4148
    field= new Field_long((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4149
                          name, 0, unsigned_flag);
 
4150
    break;
 
4151
#ifdef HAVE_LONG_LONG
 
4152
  case MYSQL_TYPE_LONGLONG:
 
4153
    field= new Field_longlong((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4115
4154
                              name, 0, unsigned_flag);
4116
4155
    break;
4117
 
  case DRIZZLE_TYPE_DOUBLE:
4118
 
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4156
#endif
 
4157
  case MYSQL_TYPE_FLOAT:
 
4158
    field= new Field_float((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4159
                           name, decimals, 0, unsigned_flag);
 
4160
    break;
 
4161
  case MYSQL_TYPE_DOUBLE:
 
4162
    field= new Field_double((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4119
4163
                            name, decimals, 0, unsigned_flag);
4120
4164
    break;
4121
 
  case DRIZZLE_TYPE_NULL:
4122
 
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
 
4165
  case MYSQL_TYPE_NULL:
 
4166
    field= new Field_null((uchar*) 0, max_length, Field::NONE,
4123
4167
                          name, &my_charset_bin);
4124
4168
    break;
4125
 
  case DRIZZLE_TYPE_NEWDATE:
 
4169
  case MYSQL_TYPE_INT24:
 
4170
    field= new Field_medium((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4171
                            name, 0, unsigned_flag);
 
4172
    break;
 
4173
  case MYSQL_TYPE_NEWDATE:
 
4174
  case MYSQL_TYPE_DATE:
4126
4175
    field= new Field_newdate(maybe_null, name, &my_charset_bin);
4127
4176
    break;
4128
 
  case DRIZZLE_TYPE_TIME:
 
4177
  case MYSQL_TYPE_TIME:
4129
4178
    field= new Field_time(maybe_null, name, &my_charset_bin);
4130
4179
    break;
4131
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
4180
  case MYSQL_TYPE_TIMESTAMP:
4132
4181
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
4133
4182
    break;
4134
 
  case DRIZZLE_TYPE_DATETIME:
 
4183
  case MYSQL_TYPE_DATETIME:
4135
4184
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
4136
4185
    break;
 
4186
  case MYSQL_TYPE_YEAR:
 
4187
    field= new Field_year((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4188
                          name);
 
4189
    break;
 
4190
  case MYSQL_TYPE_BIT:
 
4191
    field= new Field_bit_as_char(NULL, max_length, null_ptr, 0,
 
4192
                                 Field::NONE, name);
 
4193
    break;
4137
4194
  default:
4138
4195
    /* This case should never be chosen */
4139
 
    assert(0);
 
4196
    DBUG_ASSERT(0);
 
4197
    /* If something goes awfully wrong, it's better to get a string than die */
 
4198
  case MYSQL_TYPE_STRING:
 
4199
    if (fixed_length && max_length < CONVERT_IF_BIGGER_TO_BLOB)
 
4200
    {
 
4201
      field= new Field_string(max_length, maybe_null, name,
 
4202
                              collation.collation);
 
4203
      break;
 
4204
    }
4140
4205
    /* Fall through to make_string_field() */
4141
 
  case DRIZZLE_TYPE_ENUM:
4142
 
  case DRIZZLE_TYPE_VARCHAR:
 
4206
  case MYSQL_TYPE_ENUM:
 
4207
  case MYSQL_TYPE_SET:
 
4208
  case MYSQL_TYPE_VAR_STRING:
 
4209
  case MYSQL_TYPE_VARCHAR:
4143
4210
    return make_string_field(table);
4144
 
  case DRIZZLE_TYPE_BLOB:
 
4211
  case MYSQL_TYPE_TINY_BLOB:
 
4212
  case MYSQL_TYPE_MEDIUM_BLOB:
 
4213
  case MYSQL_TYPE_LONG_BLOB:
 
4214
  case MYSQL_TYPE_BLOB:
4145
4215
    if (this->type() == Item::TYPE_HOLDER)
4146
4216
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
4147
4217
                            1);
4159
4229
void Item_field::make_field(Send_field *tmp_field)
4160
4230
{
4161
4231
  field->make_field(tmp_field);
4162
 
  assert(tmp_field->table_name != 0);
 
4232
  DBUG_ASSERT(tmp_field->table_name != 0);
4163
4233
  if (name)
4164
4234
    tmp_field->col_name=name;                   // Use user supplied name
4165
4235
  if (table_name)
4191
4261
int Item_field::save_in_field(Field *to, bool no_conversions)
4192
4262
{
4193
4263
  int res;
 
4264
  DBUG_ENTER("Item_field::save_in_field");
4194
4265
  if (result_field->is_null())
4195
4266
  {
4196
4267
    null_value=1;
4199
4270
  else
4200
4271
  {
4201
4272
    to->set_notnull();
 
4273
    DBUG_EXECUTE("info", dbug_print(););
4202
4274
    res= field_conv(to,result_field);
4203
4275
    null_value=0;
4204
4276
  }
4205
 
  return(res);
 
4277
  DBUG_RETURN(res);
4206
4278
}
4207
4279
 
4208
4280
 
4255
4327
  if (result_type() == STRING_RESULT)
4256
4328
  {
4257
4329
    String *result;
4258
 
    const CHARSET_INFO * const cs= collation.collation;
 
4330
    CHARSET_INFO *cs= collation.collation;
4259
4331
    char buff[MAX_FIELD_WIDTH];         // Alloc buffer for small columns
4260
4332
    str_value.set_quick(buff, sizeof(buff), cs);
4261
4333
    result=val_str(&str_value);
4265
4337
      return set_field_to_null_with_conversions(field, no_conversions);
4266
4338
    }
4267
4339
 
4268
 
    /* NOTE: If null_value == false, "result" must be not NULL.  */
 
4340
    /* NOTE: If null_value == FALSE, "result" must be not NULL.  */
4269
4341
 
4270
4342
    field->set_notnull();
4271
4343
    error=field->store(result->ptr(),result->length(),cs);
4299
4371
  }
4300
4372
  else
4301
4373
  {
4302
 
    int64_t nr=val_int();
 
4374
    longlong nr=val_int();
4303
4375
    if (null_value)
4304
4376
      return set_field_to_null_with_conversions(field, no_conversions);
4305
4377
    field->set_notnull();
4309
4381
}
4310
4382
 
4311
4383
 
4312
 
int Item_string::save_in_field(Field *field,
4313
 
                               bool no_conversions __attribute__((unused)))
 
4384
int Item_string::save_in_field(Field *field, bool no_conversions)
4314
4385
{
4315
4386
  String *result;
4316
4387
  result=val_str(&str_value);
4325
4396
}
4326
4397
 
4327
4398
 
4328
 
int Item_int::save_in_field(Field *field,
4329
 
                            bool no_conversions __attribute__((unused)))
 
4399
int Item_int::save_in_field(Field *field, bool no_conversions)
4330
4400
{
4331
 
  int64_t nr=val_int();
 
4401
  longlong nr=val_int();
4332
4402
  if (null_value)
4333
4403
    return set_field_to_null(field);
4334
4404
  field->set_notnull();
4336
4406
}
4337
4407
 
4338
4408
 
4339
 
int Item_decimal::save_in_field(Field *field,
4340
 
                                bool no_conversions __attribute__((unused)))
 
4409
int Item_decimal::save_in_field(Field *field, bool no_conversions)
4341
4410
{
4342
4411
  field->set_notnull();
4343
4412
  return field->store_decimal(&decimal_value);
4344
4413
}
4345
4414
 
4346
4415
 
4347
 
bool Item_int::eq(const Item *arg,
4348
 
                  bool binary_cmp __attribute__((unused))) const
 
4416
bool Item_int::eq(const Item *arg, bool binary_cmp) const
4349
4417
{
4350
4418
  /* No need to check for null value as basic constant can't be NULL */
4351
4419
  if (arg->basic_const_item() && arg->type() == type())
4357
4425
    Item *item= (Item*) arg;
4358
4426
    return item->val_int() == value && item->unsigned_flag == unsigned_flag;
4359
4427
  }
4360
 
  return false;
 
4428
  return FALSE;
4361
4429
}
4362
4430
 
4363
4431
 
4364
4432
Item *Item_int_with_ref::clone_item()
4365
4433
{
4366
 
  assert(ref->const_item());
 
4434
  DBUG_ASSERT(ref->const_item());
4367
4435
  /*
4368
4436
    We need to evaluate the constant to make sure it works with
4369
4437
    parameter markers.
4381
4449
}
4382
4450
 
4383
4451
 
4384
 
static uint32_t nr_of_decimals(const char *str, const char *end)
 
4452
static uint nr_of_decimals(const char *str, const char *end)
4385
4453
{
4386
4454
  const char *decimal_point;
4387
4455
 
4409
4477
  value is not a true double value (overflow)
4410
4478
*/
4411
4479
 
4412
 
Item_float::Item_float(const char *str_arg, uint32_t length)
 
4480
Item_float::Item_float(const char *str_arg, uint length)
4413
4481
{
4414
4482
  int error;
4415
4483
  char *end_not_used;
4421
4489
      Note that we depend on that str_arg is null terminated, which is true
4422
4490
      when we are in the parser
4423
4491
    */
4424
 
    assert(str_arg[length] == 0);
 
4492
    DBUG_ASSERT(str_arg[length] == 0);
4425
4493
    my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg);
4426
4494
  }
4427
4495
  presentation= name=(char*) str_arg;
4428
 
  decimals=(uint8_t) nr_of_decimals(str_arg, str_arg+length);
 
4496
  decimals=(uint8) nr_of_decimals(str_arg, str_arg+length);
4429
4497
  max_length=length;
4430
4498
  fixed= 1;
4431
4499
}
4432
4500
 
4433
4501
 
4434
 
int Item_float::save_in_field(Field *field,
4435
 
                              bool no_conversions __attribute__((unused)))
 
4502
int Item_float::save_in_field(Field *field, bool no_conversions)
4436
4503
{
4437
4504
  double nr= val_real();
4438
4505
  if (null_value)
4442
4509
}
4443
4510
 
4444
4511
 
4445
 
void Item_float::print(String *str,
4446
 
                       enum_query_type query_type __attribute__((unused)))
 
4512
void Item_float::print(String *str, enum_query_type query_type)
4447
4513
{
4448
4514
  if (presentation)
4449
4515
  {
4460
4526
/*
4461
4527
  hex item
4462
4528
  In string context this is a binary string.
4463
 
  In number context this is a int64_t value.
 
4529
  In number context this is a longlong value.
4464
4530
*/
4465
4531
 
4466
 
bool Item_float::eq(const Item *arg,
4467
 
                    bool binary_cmp __attribute__((unused))) const
 
4532
bool Item_float::eq(const Item *arg, bool binary_cmp) const
4468
4533
{
4469
4534
  if (arg->basic_const_item() && arg->type() == type())
4470
4535
  {
4475
4540
    Item *item= (Item*) arg;
4476
4541
    return item->val_real() == value;
4477
4542
  }
4478
 
  return false;
 
4543
  return FALSE;
4479
4544
}
4480
4545
 
4481
4546
 
4482
 
inline uint32_t char_val(char X)
 
4547
inline uint char_val(char X)
4483
4548
{
4484
4549
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
4485
4550
                 X >= 'A' && X <= 'Z' ? X-'A'+10 :
4487
4552
}
4488
4553
 
4489
4554
 
4490
 
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
 
4555
Item_hex_string::Item_hex_string(const char *str, uint str_length)
4491
4556
{
4492
4557
  max_length=(str_length+1)/2;
4493
4558
  char *ptr=(char*) sql_alloc(max_length+1);
4508
4573
  unsigned_flag= 1;
4509
4574
}
4510
4575
 
4511
 
int64_t Item_hex_string::val_int()
 
4576
longlong Item_hex_string::val_int()
4512
4577
{
4513
4578
  // following assert is redundant, because fixed=1 assigned in constructor
4514
 
  assert(fixed == 1);
 
4579
  DBUG_ASSERT(fixed == 1);
4515
4580
  char *end=(char*) str_value.ptr()+str_value.length(),
4516
 
       *ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
 
4581
       *ptr=end-min(str_value.length(),sizeof(longlong));
4517
4582
 
4518
 
  uint64_t value=0;
 
4583
  ulonglong value=0;
4519
4584
  for (; ptr != end ; ptr++)
4520
 
    value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
4521
 
  return (int64_t) value;
 
4585
    value=(value << 8)+ (ulonglong) (uchar) *ptr;
 
4586
  return (longlong) value;
4522
4587
}
4523
4588
 
4524
4589
 
4525
4590
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
4526
4591
{
4527
4592
  // following assert is redundant, because fixed=1 assigned in constructor
4528
 
  assert(fixed == 1);
4529
 
  uint64_t value= (uint64_t)val_int();
4530
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
 
4593
  DBUG_ASSERT(fixed == 1);
 
4594
  ulonglong value= (ulonglong)val_int();
 
4595
  int2my_decimal(E_DEC_FATAL_ERROR, value, TRUE, decimal_value);
4531
4596
  return (decimal_value);
4532
4597
}
4533
4598
 
4534
4599
 
4535
 
int Item_hex_string::save_in_field(Field *field,
4536
 
                                   bool no_conversions __attribute__((unused)))
 
4600
int Item_hex_string::save_in_field(Field *field, bool no_conversions)
4537
4601
{
4538
4602
  field->set_notnull();
4539
4603
  if (field->result_type() == STRING_RESULT)
4540
4604
    return field->store(str_value.ptr(), str_value.length(), 
4541
4605
                        collation.collation);
4542
4606
 
4543
 
  uint64_t nr;
4544
 
  uint32_t length= str_value.length();
 
4607
  ulonglong nr;
 
4608
  uint32 length= str_value.length();
4545
4609
  if (length > 8)
4546
4610
  {
4547
 
    nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
 
4611
    nr= field->flags & UNSIGNED_FLAG ? ULONGLONG_MAX : LONGLONG_MAX;
4548
4612
    goto warn;
4549
4613
  }
4550
 
  nr= (uint64_t) val_int();
4551
 
  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
 
4614
  nr= (ulonglong) val_int();
 
4615
  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > LONGLONG_MAX))
4552
4616
  {
4553
 
    nr= INT64_MAX;
 
4617
    nr= LONGLONG_MAX;
4554
4618
    goto warn;
4555
4619
  }
4556
 
  return field->store((int64_t) nr, true);  // Assume hex numbers are unsigned
 
4620
  return field->store((longlong) nr, TRUE);  // Assume hex numbers are unsigned
4557
4621
 
4558
4622
warn:
4559
 
  if (!field->store((int64_t) nr, true))
4560
 
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
 
4623
  if (!field->store((longlong) nr, TRUE))
 
4624
    field->set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
4561
4625
                       1);
4562
4626
  return 1;
4563
4627
}
4564
4628
 
4565
4629
 
4566
 
void Item_hex_string::print(String *str,
4567
 
                            enum_query_type query_type __attribute__((unused)))
 
4630
void Item_hex_string::print(String *str, enum_query_type query_type)
4568
4631
{
4569
4632
  char *end= (char*) str_value.ptr() + str_value.length(),
4570
 
       *ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
 
4633
       *ptr= end - min(str_value.length(), sizeof(longlong));
4571
4634
  str->append("0x");
4572
4635
  for (; ptr != end ; ptr++)
4573
4636
  {
4574
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
4575
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
 
4637
    str->append(_dig_vec_lower[((uchar) *ptr) >> 4]);
 
4638
    str->append(_dig_vec_lower[((uchar) *ptr) & 0x0F]);
4576
4639
  }
4577
4640
}
4578
4641
 
4585
4648
      return !stringcmp(&str_value, &arg->str_value);
4586
4649
    return !sortcmp(&str_value, &arg->str_value, collation.collation);
4587
4650
  }
4588
 
  return false;
 
4651
  return FALSE;
4589
4652
}
4590
4653
 
4591
4654
 
4592
 
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
 
4655
Item *Item_hex_string::safe_charset_converter(CHARSET_INFO *tocs)
4593
4656
{
4594
4657
  Item_string *conv;
4595
4658
  String tmp, *str= val_str(&tmp);
4605
4668
/*
4606
4669
  bin item.
4607
4670
  In string context this is a binary string.
4608
 
  In number context this is a int64_t value.
 
4671
  In number context this is a longlong value.
4609
4672
*/
4610
4673
  
4611
 
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
 
4674
Item_bin_string::Item_bin_string(const char *str, uint str_length)
4612
4675
{
4613
4676
  const char *end= str + str_length - 1;
4614
 
  unsigned char bits= 0;
4615
 
  uint32_t power= 1;
 
4677
  uchar bits= 0;
 
4678
  uint power= 1;
4616
4679
 
4617
4680
  max_length= (str_length + 7) >> 3;
4618
4681
  char *ptr= (char*) sql_alloc(max_length + 1);
4643
4706
  Pack data in buffer for sending.
4644
4707
*/
4645
4708
 
4646
 
bool Item_null::send(Protocol *protocol,
4647
 
                     String *packet __attribute__((unused)))
 
4709
bool Item_null::send(Protocol *protocol, String *packet)
4648
4710
{
4649
4711
  return protocol->store_null();
4650
4712
}
4660
4722
 
4661
4723
  switch ((f_type=field_type())) {
4662
4724
  default:
4663
 
  case DRIZZLE_TYPE_NULL:
4664
 
  case DRIZZLE_TYPE_ENUM:
4665
 
  case DRIZZLE_TYPE_BLOB:
4666
 
  case DRIZZLE_TYPE_VARCHAR:
4667
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
4725
  case MYSQL_TYPE_NULL:
 
4726
  case MYSQL_TYPE_DECIMAL:
 
4727
  case MYSQL_TYPE_ENUM:
 
4728
  case MYSQL_TYPE_SET:
 
4729
  case MYSQL_TYPE_TINY_BLOB:
 
4730
  case MYSQL_TYPE_MEDIUM_BLOB:
 
4731
  case MYSQL_TYPE_LONG_BLOB:
 
4732
  case MYSQL_TYPE_BLOB:
 
4733
  case MYSQL_TYPE_STRING:
 
4734
  case MYSQL_TYPE_VAR_STRING:
 
4735
  case MYSQL_TYPE_VARCHAR:
 
4736
  case MYSQL_TYPE_BIT:
 
4737
  case MYSQL_TYPE_NEWDECIMAL:
4668
4738
  {
4669
4739
    String *res;
4670
4740
    if ((res=val_str(buffer)))
4671
4741
      result= protocol->store(res->ptr(),res->length(),res->charset());
4672
4742
    break;
4673
4743
  }
4674
 
  case DRIZZLE_TYPE_TINY:
 
4744
  case MYSQL_TYPE_TINY:
4675
4745
  {
4676
 
    int64_t nr;
 
4746
    longlong nr;
4677
4747
    nr= val_int();
4678
4748
    if (!null_value)
4679
4749
      result= protocol->store_tiny(nr);
4680
4750
    break;
4681
4751
  }
4682
 
  case DRIZZLE_TYPE_LONG:
4683
 
  {
4684
 
    int64_t nr;
 
4752
  case MYSQL_TYPE_SHORT:
 
4753
  case MYSQL_TYPE_YEAR:
 
4754
  {
 
4755
    longlong nr;
 
4756
    nr= val_int();
 
4757
    if (!null_value)
 
4758
      result= protocol->store_short(nr);
 
4759
    break;
 
4760
  }
 
4761
  case MYSQL_TYPE_INT24:
 
4762
  case MYSQL_TYPE_LONG:
 
4763
  {
 
4764
    longlong nr;
4685
4765
    nr= val_int();
4686
4766
    if (!null_value)
4687
4767
      result= protocol->store_long(nr);
4688
4768
    break;
4689
4769
  }
4690
 
  case DRIZZLE_TYPE_LONGLONG:
 
4770
  case MYSQL_TYPE_LONGLONG:
4691
4771
  {
4692
 
    int64_t nr;
 
4772
    longlong nr;
4693
4773
    nr= val_int();
4694
4774
    if (!null_value)
4695
 
      result= protocol->store_int64_t(nr, unsigned_flag);
4696
 
    break;
4697
 
  }
4698
 
  case DRIZZLE_TYPE_DOUBLE:
 
4775
      result= protocol->store_longlong(nr, unsigned_flag);
 
4776
    break;
 
4777
  }
 
4778
  case MYSQL_TYPE_FLOAT:
 
4779
  {
 
4780
    float nr;
 
4781
    nr= (float) val_real();
 
4782
    if (!null_value)
 
4783
      result= protocol->store(nr, decimals, buffer);
 
4784
    break;
 
4785
  }
 
4786
  case MYSQL_TYPE_DOUBLE:
4699
4787
  {
4700
4788
    double nr= val_real();
4701
4789
    if (!null_value)
4702
4790
      result= protocol->store(nr, decimals, buffer);
4703
4791
    break;
4704
4792
  }
4705
 
  case DRIZZLE_TYPE_DATETIME:
4706
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
4793
  case MYSQL_TYPE_DATETIME:
 
4794
  case MYSQL_TYPE_DATE:
 
4795
  case MYSQL_TYPE_TIMESTAMP:
4707
4796
  {
4708
 
    DRIZZLE_TIME tm;
 
4797
    MYSQL_TIME tm;
4709
4798
    get_date(&tm, TIME_FUZZY_DATE);
4710
4799
    if (!null_value)
4711
4800
    {
4712
 
      if (f_type == DRIZZLE_TYPE_NEWDATE)
 
4801
      if (f_type == MYSQL_TYPE_DATE)
4713
4802
        return protocol->store_date(&tm);
4714
4803
      else
4715
4804
        result= protocol->store(&tm);
4716
4805
    }
4717
4806
    break;
4718
4807
  }
4719
 
  case DRIZZLE_TYPE_TIME:
 
4808
  case MYSQL_TYPE_TIME:
4720
4809
  {
4721
 
    DRIZZLE_TIME tm;
 
4810
    MYSQL_TIME tm;
4722
4811
    get_time(&tm);
4723
4812
    if (!null_value)
4724
4813
      result= protocol->store_time(&tm);
4731
4820
}
4732
4821
 
4733
4822
 
4734
 
bool Item_field::send(Protocol *protocol,
4735
 
                      String *buffer __attribute__((unused)))
 
4823
bool Item_field::send(Protocol *protocol, String *buffer)
4736
4824
{
4737
4825
  return protocol->store(result_field);
4738
4826
}
4739
4827
 
4740
4828
 
4741
 
void Item_field::update_null_value()
4742
 
{
4743
 
  /*
4744
 
    need to set no_errors to prevent warnings about type conversion
 
4829
void Item_field::update_null_value() 
 
4830
 
4831
  /* 
 
4832
    need to set no_errors to prevent warnings about type conversion 
4745
4833
    popping up.
4746
4834
  */
4747
4835
  THD *thd= field->table->in_use;
4776
4864
    this field    otherwise
4777
4865
*/
4778
4866
 
4779
 
Item *Item_field::update_value_transformer(unsigned char *select_arg)
 
4867
Item *Item_field::update_value_transformer(uchar *select_arg)
4780
4868
{
4781
4869
  SELECT_LEX *select= (SELECT_LEX*)select_arg;
4782
 
  assert(fixed);
 
4870
  DBUG_ASSERT(fixed);
4783
4871
 
4784
 
  if (field->table != select->context.table_list->table)
 
4872
  if (field->table != select->context.table_list->table &&
 
4873
      type() != Item::TRIGGER_FIELD_ITEM)
4785
4874
  {
4786
4875
    List<Item> *all_fields= &select->join->all_fields;
4787
4876
    Item **ref_pointer_array= select->ref_pointer_array;
4818
4907
                   Item **item, const char *table_name_arg,
4819
4908
                   const char *field_name_arg,
4820
4909
                   bool alias_name_used_arg)
4821
 
  :Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
 
4910
  :Item_ident(context_arg, NullS, table_name_arg, field_name_arg),
4822
4911
   result_field(0), ref(item)
4823
4912
{
4824
4913
  alias_name_used= alias_name_used_arg;
4889
4978
    ER_BAD_FIELD_ERROR which we produce now.
4890
4979
 
4891
4980
  @retval
4892
 
    true  if error
 
4981
    TRUE  if error
4893
4982
  @retval
4894
 
    false on success
 
4983
    FALSE on success
4895
4984
*/
4896
4985
 
4897
4986
bool Item_ref::fix_fields(THD *thd, Item **reference)
4898
4987
{
4899
4988
  enum_parsing_place place= NO_MATTER;
4900
 
  assert(fixed == 0);
 
4989
  DBUG_ASSERT(fixed == 0);
4901
4990
  SELECT_LEX *current_sel= thd->lex->current_select;
4902
4991
 
4903
4992
  if (!ref || ref == not_found_item)
4946
5035
            goto error; /* Some error occurred (e.g. ambiguous names). */
4947
5036
          if (ref != not_found_item)
4948
5037
          {
4949
 
            assert(*ref && (*ref)->fixed);
 
5038
            DBUG_ASSERT(*ref && (*ref)->fixed);
4950
5039
            prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
4951
5040
            prev_subselect_item->const_item_cache&= (*ref)->const_item();
4952
5041
            break;
4986
5075
                                             last_name_resolution_table,
4987
5076
                                           reference,
4988
5077
                                           IGNORE_EXCEPT_NON_UNIQUE,
4989
 
                                           true, true);
 
5078
                                           TRUE, TRUE);
4990
5079
          if (! from_field)
4991
5080
            goto error;
4992
5081
          if (from_field == view_ref_found)
4996
5085
              (*reference)->used_tables();
4997
5086
            prev_subselect_item->const_item_cache&=
4998
5087
              (*reference)->const_item();
4999
 
            assert((*reference)->type() == REF_ITEM);
 
5088
            DBUG_ASSERT((*reference)->type() == REF_ITEM);
5000
5089
            mark_as_dependent(thd, last_checked_context->select_lex,
5001
5090
                              context->select_lex, this,
5002
5091
                              ((refer_type == REF_ITEM ||
5007
5096
              view reference found, we substituted it instead of this
5008
5097
              Item, so can quit
5009
5098
            */
5010
 
            return false;
 
5099
            return FALSE;
5011
5100
          }
5012
5101
          if (from_field != not_found_field)
5013
5102
          {
5035
5124
            break;
5036
5125
          }
5037
5126
        }
5038
 
        assert(from_field == not_found_field);
 
5127
        DBUG_ASSERT(from_field == not_found_field);
5039
5128
 
5040
5129
        /* Reference is not found => depend on outer (or just error). */
5041
5130
        prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
5044
5133
        outer_context= outer_context->outer_context;
5045
5134
      } while (outer_context);
5046
5135
 
5047
 
      assert(from_field != 0 && from_field != view_ref_found);
 
5136
      DBUG_ASSERT(from_field != 0 && from_field != view_ref_found);
5048
5137
      if (from_field != not_found_field)
5049
5138
      {
5050
5139
        Item_field* fld;
5063
5152
            last_checked_context->select_lex->nest_level)
5064
5153
          set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5065
5154
                        last_checked_context->select_lex->nest_level);
5066
 
        return false;
 
5155
        return FALSE;
5067
5156
      }
5068
5157
      if (ref == 0)
5069
5158
      {
5073
5162
        goto error;
5074
5163
      }
5075
5164
      /* Should be checked in resolve_ref_in_select_and_group(). */
5076
 
      assert(*ref && (*ref)->fixed);
 
5165
      DBUG_ASSERT(*ref && (*ref)->fixed);
5077
5166
      mark_as_dependent(thd, last_checked_context->select_lex,
5078
5167
                        context->select_lex, this, this);
5079
5168
      /*
5089
5178
    }
5090
5179
  }
5091
5180
 
5092
 
  assert(*ref);
 
5181
  DBUG_ASSERT(*ref);
5093
5182
  /*
5094
5183
    Check if this is an incorrect reference in a group function or forward
5095
5184
    reference. Do not issue an error if this is:
5114
5203
 
5115
5204
  if ((*ref)->check_cols(1))
5116
5205
    goto error;
5117
 
  return false;
 
5206
  return FALSE;
5118
5207
 
5119
5208
error:
5120
5209
  context->process_error(thd);
5121
 
  return true;
 
5210
  return TRUE;
5122
5211
}
5123
5212
 
5124
5213
 
5140
5229
  if ((*ref)->type() == FIELD_ITEM)
5141
5230
    alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
5142
5231
  else
5143
 
    alias_name_used= true; // it is not field, so it is was resolved by alias
 
5232
    alias_name_used= TRUE; // it is not field, so it is was resolved by alias
5144
5233
}
5145
5234
 
5146
5235
 
5147
5236
void Item_ref::cleanup()
5148
5237
{
 
5238
  DBUG_ENTER("Item_ref::cleanup");
5149
5239
  Item_ident::cleanup();
5150
5240
  result_field= 0;
5151
 
  return;
 
5241
  DBUG_VOID_RETURN;
5152
5242
}
5153
5243
 
5154
5244
 
5190
5280
}
5191
5281
 
5192
5282
 
5193
 
int64_t Item_ref::val_int_result()
 
5283
longlong Item_ref::val_int_result()
5194
5284
{
5195
5285
  if (result_field)
5196
5286
  {
5249
5339
      return result_field->val_real() != 0.0;
5250
5340
    case ROW_RESULT:
5251
5341
    default:
5252
 
      assert(0);
 
5342
      DBUG_ASSERT(0);
5253
5343
    }
5254
5344
  }
5255
5345
  return val_bool();
5258
5348
 
5259
5349
double Item_ref::val_real()
5260
5350
{
5261
 
  assert(fixed);
 
5351
  DBUG_ASSERT(fixed);
5262
5352
  double tmp=(*ref)->val_result();
5263
5353
  null_value=(*ref)->null_value;
5264
5354
  return tmp;
5265
5355
}
5266
5356
 
5267
5357
 
5268
 
int64_t Item_ref::val_int()
 
5358
longlong Item_ref::val_int()
5269
5359
{
5270
 
  assert(fixed);
5271
 
  int64_t tmp=(*ref)->val_int_result();
 
5360
  DBUG_ASSERT(fixed);
 
5361
  longlong tmp=(*ref)->val_int_result();
5272
5362
  null_value=(*ref)->null_value;
5273
5363
  return tmp;
5274
5364
}
5276
5366
 
5277
5367
bool Item_ref::val_bool()
5278
5368
{
5279
 
  assert(fixed);
 
5369
  DBUG_ASSERT(fixed);
5280
5370
  bool tmp= (*ref)->val_bool_result();
5281
5371
  null_value= (*ref)->null_value;
5282
5372
  return tmp;
5285
5375
 
5286
5376
String *Item_ref::val_str(String* tmp)
5287
5377
{
5288
 
  assert(fixed);
 
5378
  DBUG_ASSERT(fixed);
5289
5379
  tmp=(*ref)->str_result(tmp);
5290
5380
  null_value=(*ref)->null_value;
5291
5381
  return tmp;
5294
5384
 
5295
5385
bool Item_ref::is_null()
5296
5386
{
5297
 
  assert(fixed);
 
5387
  DBUG_ASSERT(fixed);
5298
5388
  return (*ref)->is_null();
5299
5389
}
5300
5390
 
5301
5391
 
5302
 
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
5392
bool Item_ref::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5303
5393
{
5304
5394
  return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5305
5395
}
5315
5405
int Item_ref::save_in_field(Field *to, bool no_conversions)
5316
5406
{
5317
5407
  int res;
5318
 
  assert(!result_field);
 
5408
  DBUG_ASSERT(!result_field);
5319
5409
  res= (*ref)->save_in_field(to, no_conversions);
5320
5410
  null_value= (*ref)->null_value;
5321
5411
  return res;
5375
5465
}
5376
5466
 
5377
5467
 
5378
 
int64_t Item_direct_ref::val_int()
 
5468
longlong Item_direct_ref::val_int()
5379
5469
{
5380
 
  int64_t tmp=(*ref)->val_int();
 
5470
  longlong tmp=(*ref)->val_int();
5381
5471
  null_value=(*ref)->null_value;
5382
5472
  return tmp;
5383
5473
}
5413
5503
}
5414
5504
 
5415
5505
 
5416
 
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
5506
bool Item_direct_ref::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5417
5507
{
5418
5508
  return (null_value=(*ref)->get_date(ltime,fuzzydate));
5419
5509
}
5426
5516
  @param reference   reference on reference where this item stored
5427
5517
 
5428
5518
  @retval
5429
 
    false   OK
 
5519
    FALSE   OK
5430
5520
  @retval
5431
 
    true    Error
 
5521
    TRUE    Error
5432
5522
*/
5433
5523
 
5434
5524
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
5435
5525
{
5436
5526
  /* view fild reference must be defined */
5437
 
  assert(*ref);
 
5527
  DBUG_ASSERT(*ref);
5438
5528
  /* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
5439
5529
  if (!(*ref)->fixed &&
5440
5530
      ((*ref)->fix_fields(thd, ref)))
5441
 
    return true;
 
5531
    return TRUE;
5442
5532
  return Item_direct_ref::fix_fields(thd, reference);
5443
5533
}
5444
5534
 
5451
5541
    reference   reference on reference where this item stored
5452
5542
 
5453
5543
  RETURN
5454
 
    false   OK
5455
 
    true    Error
 
5544
    FALSE   OK
 
5545
    TRUE    Error
5456
5546
*/
5457
5547
 
5458
5548
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5460
5550
  bool err;
5461
5551
  /* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5462
5552
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5463
 
    return true;
 
5553
    return TRUE;
5464
5554
  err= Item_direct_ref::fix_fields(thd, reference);
5465
5555
  if (!outer_ref)
5466
5556
    outer_ref= *ref;
5478
5568
  }
5479
5569
}
5480
5570
 
5481
 
void Item_ref::fix_after_pullout(st_select_lex *new_parent,
5482
 
                                 Item **refptr __attribute__((unused)))
 
5571
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **refptr)
5483
5572
{
5484
5573
  if (depended_from == new_parent)
5485
5574
  {
5500
5589
  @param binary_cmp  make binary comparison
5501
5590
 
5502
5591
  @retval
5503
 
    true    Referenced item is equal to given item
 
5592
    TRUE    Referenced item is equal to given item
5504
5593
  @retval
5505
 
    false   otherwise
 
5594
    FALSE   otherwise
5506
5595
*/
5507
5596
 
5508
 
bool Item_direct_view_ref::eq(const Item *item,
5509
 
                              bool binary_cmp __attribute__((unused))) const
 
5597
bool Item_direct_view_ref::eq(const Item *item, bool binary_cmp) const
5510
5598
{
5511
5599
  if (item->type() == REF_ITEM)
5512
5600
  {
5517
5605
      return ((*ref)->real_item() == item_ref_ref->real_item());
5518
5606
    }
5519
5607
  }
5520
 
  return false;
 
5608
  return FALSE;
5521
5609
}
5522
5610
 
5523
5611
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
5524
5612
{
5525
 
  return item->type() == DEFAULT_VALUE_ITEM &&
 
5613
  return item->type() == DEFAULT_VALUE_ITEM && 
5526
5614
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
5527
5615
}
5528
5616
 
5529
5617
 
5530
 
bool Item_default_value::fix_fields(THD *thd,
5531
 
                                    Item **items __attribute__((unused)))
 
5618
bool Item_default_value::fix_fields(THD *thd, Item **items)
5532
5619
{
5533
5620
  Item *real_arg;
5534
5621
  Item_field *field_arg;
5535
5622
  Field *def_field;
5536
 
  assert(fixed == 0);
 
5623
  DBUG_ASSERT(fixed == 0);
5537
5624
 
5538
5625
  if (!arg)
5539
5626
  {
5540
5627
    fixed= 1;
5541
 
    return false;
 
5628
    return FALSE;
5542
5629
  }
5543
5630
  if (!arg->fixed && arg->fix_fields(thd, &arg))
5544
5631
    goto error;
5564
5651
                               (def_field->table->s->default_values -
5565
5652
                                def_field->table->record[0]));
5566
5653
  set_field(def_field);
5567
 
  return false;
 
5654
  return FALSE;
5568
5655
 
5569
5656
error:
5570
5657
  context->process_error(thd);
5571
 
  return true;
 
5658
  return TRUE;
5572
5659
}
5573
5660
 
5574
5661
 
5600
5687
 
5601
5688
      {
5602
5689
        push_warning_printf(field_arg->table->in_use,
5603
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
5690
                            MYSQL_ERROR::WARN_LEVEL_WARN,
5604
5691
                            ER_NO_DEFAULT_FOR_FIELD,
5605
5692
                            ER(ER_NO_DEFAULT_FOR_FIELD),
5606
5693
                            field_arg->field_name);
5619
5706
  same time it can replace some nodes in the tree.
5620
5707
*/ 
5621
5708
 
5622
 
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
 
5709
Item *Item_default_value::transform(Item_transformer transformer, uchar *args)
5623
5710
{
5624
5711
  Item *new_item= arg->transform(transformer, args);
5625
5712
  if (!new_item)
5644
5731
}
5645
5732
 
5646
5733
 
5647
 
bool Item_insert_value::fix_fields(THD *thd,
5648
 
                                   Item **items __attribute__((unused)))
 
5734
bool Item_insert_value::fix_fields(THD *thd, Item **items)
5649
5735
{
5650
 
  assert(fixed == 0);
 
5736
  DBUG_ASSERT(fixed == 0);
5651
5737
  /* We should only check that arg is in first table */
5652
5738
  if (!arg->fixed)
5653
5739
  {
5654
5740
    bool res;
5655
 
    TableList *orig_next_table= context->last_name_resolution_table;
 
5741
    TABLE_LIST *orig_next_table= context->last_name_resolution_table;
5656
5742
    context->last_name_resolution_table= context->first_name_resolution_table;
5657
5743
    res= arg->fix_fields(thd, &arg);
5658
5744
    context->last_name_resolution_table= orig_next_table;
5659
5745
    if (res)
5660
 
      return true;
 
5746
      return TRUE;
5661
5747
  }
5662
5748
 
5663
5749
  if (arg->type() == REF_ITEM)
5666
5752
    if (ref->ref[0]->type() != FIELD_ITEM)
5667
5753
    {
5668
5754
      my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
5669
 
      return true;
 
5755
      return TRUE;
5670
5756
    }
5671
5757
    arg= ref->ref[0];
5672
5758
  }
5674
5760
    According to our SQL grammar, VALUES() function can reference
5675
5761
    only to a column.
5676
5762
  */
5677
 
  assert(arg->type() == FIELD_ITEM);
 
5763
  DBUG_ASSERT(arg->type() == FIELD_ITEM);
5678
5764
 
5679
5765
  Item_field *field_arg= (Item_field *)arg;
5680
5766
 
5682
5768
  {
5683
5769
    Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
5684
5770
    if (!def_field)
5685
 
      return true;
 
5771
      return TRUE;
5686
5772
    memcpy(def_field, field_arg->field, field_arg->field->size_of());
5687
5773
    def_field->move_field_offset((my_ptrdiff_t)
5688
5774
                                 (def_field->table->insert_values -
5701
5787
      set_field(tmp_field);
5702
5788
    }
5703
5789
  }
5704
 
  return false;
 
5790
  return FALSE;
5705
5791
}
5706
5792
 
5707
5793
void Item_insert_value::print(String *str, enum_query_type query_type)
5747
5833
      new_item= new Item_null(name);
5748
5834
    else
5749
5835
    {
5750
 
      uint32_t length= result->length();
 
5836
      uint length= result->length();
5751
5837
      char *tmp_str= sql_strmake(result->ptr(), length);
5752
5838
      new_item= new Item_string(name, tmp_str, length, result->charset());
5753
5839
    }
5755
5841
  }
5756
5842
  case INT_RESULT:
5757
5843
  {
5758
 
    int64_t result=item->val_int();
5759
 
    uint32_t length=item->max_length;
 
5844
    longlong result=item->val_int();
 
5845
    uint length=item->max_length;
5760
5846
    bool null_value=item->null_value;
5761
5847
    new_item= (null_value ? (Item*) new Item_null(name) :
5762
5848
               (Item*) new Item_int(name, result, length));
5775
5861
    */
5776
5862
    Item_row *item_row= (Item_row*) item;
5777
5863
    Item_row *comp_item_row= (Item_row*) comp_item;
5778
 
    uint32_t col;
 
5864
    uint col;
5779
5865
    new_item= 0;
5780
5866
    /*
5781
5867
      If item and comp_item are both Item_rows and have same number of cols
5783
5869
      We can't ignore NULL values here as this item may be used with <=>, in
5784
5870
      which case NULL's are significant.
5785
5871
    */
5786
 
    assert(item->result_type() == comp_item->result_type());
5787
 
    assert(item_row->cols() == comp_item_row->cols());
 
5872
    DBUG_ASSERT(item->result_type() == comp_item->result_type());
 
5873
    DBUG_ASSERT(item_row->cols() == comp_item_row->cols());
5788
5874
    col= item_row->cols();
5789
5875
    while (col-- > 0)
5790
5876
      resolve_const_item(thd, item_row->addr(col),
5795
5881
  case REAL_RESULT:
5796
5882
  {                                             // It must REAL_RESULT
5797
5883
    double result= item->val_real();
5798
 
    uint32_t length=item->max_length,decimals=item->decimals;
 
5884
    uint length=item->max_length,decimals=item->decimals;
5799
5885
    bool null_value=item->null_value;
5800
5886
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
5801
5887
               new Item_float(name, result, decimals, length));
5805
5891
  {
5806
5892
    my_decimal decimal_value;
5807
5893
    my_decimal *result= item->val_decimal(&decimal_value);
5808
 
    uint32_t length= item->max_length, decimals= item->decimals;
 
5894
    uint length= item->max_length, decimals= item->decimals;
5809
5895
    bool null_value= item->null_value;
5810
5896
    new_item= (null_value ?
5811
5897
               (Item*) new Item_null(name) :
5813
5899
    break;
5814
5900
  }
5815
5901
  default:
5816
 
    assert(0);
 
5902
    DBUG_ASSERT(0);
5817
5903
  }
5818
5904
  if (new_item)
5819
5905
    thd->change_item_tree(ref, new_item);
5877
5963
    return new Item_cache_row();
5878
5964
  default:
5879
5965
    // should never be in real life
5880
 
    assert(0);
 
5966
    DBUG_ASSERT(0);
5881
5967
    return 0;
5882
5968
  }
5883
5969
}
5902
5988
}
5903
5989
 
5904
5990
 
5905
 
void Item_cache_int::store(Item *item, int64_t val_arg)
 
5991
void Item_cache_int::store(Item *item, longlong val_arg)
5906
5992
{
5907
5993
  value= val_arg;
5908
5994
  null_value= item->null_value;
5912
5998
 
5913
5999
String *Item_cache_int::val_str(String *str)
5914
6000
{
5915
 
  assert(fixed == 1);
 
6001
  DBUG_ASSERT(fixed == 1);
5916
6002
  str->set(value, default_charset());
5917
6003
  return str;
5918
6004
}
5920
6006
 
5921
6007
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
5922
6008
{
5923
 
  assert(fixed == 1);
 
6009
  DBUG_ASSERT(fixed == 1);
5924
6010
  int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
5925
6011
  return decimal_val;
5926
6012
}
5933
6019
}
5934
6020
 
5935
6021
 
5936
 
int64_t Item_cache_real::val_int()
 
6022
longlong Item_cache_real::val_int()
5937
6023
{
5938
 
  assert(fixed == 1);
5939
 
  return (int64_t) rint(value);
 
6024
  DBUG_ASSERT(fixed == 1);
 
6025
  return (longlong) rint(value);
5940
6026
}
5941
6027
 
5942
6028
 
5943
6029
String* Item_cache_real::val_str(String *str)
5944
6030
{
5945
 
  assert(fixed == 1);
 
6031
  DBUG_ASSERT(fixed == 1);
5946
6032
  str->set_real(value, decimals, default_charset());
5947
6033
  return str;
5948
6034
}
5950
6036
 
5951
6037
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
5952
6038
{
5953
 
  assert(fixed == 1);
 
6039
  DBUG_ASSERT(fixed == 1);
5954
6040
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
5955
6041
  return decimal_val;
5956
6042
}
5965
6051
 
5966
6052
double Item_cache_decimal::val_real()
5967
6053
{
5968
 
  assert(fixed);
 
6054
  DBUG_ASSERT(fixed);
5969
6055
  double res;
5970
6056
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
5971
6057
  return res;
5972
6058
}
5973
6059
 
5974
 
int64_t Item_cache_decimal::val_int()
 
6060
longlong Item_cache_decimal::val_int()
5975
6061
{
5976
 
  assert(fixed);
5977
 
  int64_t res;
 
6062
  DBUG_ASSERT(fixed);
 
6063
  longlong res;
5978
6064
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
5979
6065
  return res;
5980
6066
}
5981
6067
 
5982
6068
String* Item_cache_decimal::val_str(String *str)
5983
6069
{
5984
 
  assert(fixed);
5985
 
  my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
 
6070
  DBUG_ASSERT(fixed);
 
6071
  my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, FALSE,
5986
6072
                   &decimal_value);
5987
6073
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
5988
6074
  return str;
5989
6075
}
5990
6076
 
5991
 
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val __attribute__((unused)))
 
6077
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val)
5992
6078
{
5993
 
  assert(fixed);
 
6079
  DBUG_ASSERT(fixed);
5994
6080
  return &decimal_value;
5995
6081
}
5996
6082
 
6018
6104
 
6019
6105
double Item_cache_str::val_real()
6020
6106
{
6021
 
  assert(fixed == 1);
 
6107
  DBUG_ASSERT(fixed == 1);
6022
6108
  int err_not_used;
6023
6109
  char *end_not_used;
6024
6110
  if (value)
6028
6114
}
6029
6115
 
6030
6116
 
6031
 
int64_t Item_cache_str::val_int()
 
6117
longlong Item_cache_str::val_int()
6032
6118
{
6033
 
  assert(fixed == 1);
 
6119
  DBUG_ASSERT(fixed == 1);
6034
6120
  int err;
6035
6121
  if (value)
6036
6122
    return my_strntoll(value->charset(), value->ptr(),
6037
6123
                       value->length(), 10, (char**) 0, &err);
6038
6124
  else
6039
 
    return (int64_t)0;
 
6125
    return (longlong)0;
6040
6126
}
6041
6127
 
6042
6128
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
6043
6129
{
6044
 
  assert(fixed == 1);
 
6130
  DBUG_ASSERT(fixed == 1);
6045
6131
  if (value)
6046
6132
    string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
6047
6133
  else
6053
6139
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
6054
6140
{
6055
6141
  int res= Item_cache::save_in_field(field, no_conversions);
6056
 
 
6057
 
  return res;
 
6142
  return (is_varbinary && field->type() == MYSQL_TYPE_STRING &&
 
6143
          value->length() < field->field_length) ? 1 : res;
6058
6144
}
6059
6145
 
6060
6146
 
6061
 
bool Item_cache_row::allocate(uint32_t num)
 
6147
bool Item_cache_row::allocate(uint num)
6062
6148
{
6063
6149
  item_count= num;
6064
6150
  THD *thd= current_thd;
6072
6158
  example= item;
6073
6159
  if (!values && allocate(item->cols()))
6074
6160
    return 1;
6075
 
  for (uint32_t i= 0; i < item_count; i++)
 
6161
  for (uint i= 0; i < item_count; i++)
6076
6162
  {
6077
6163
    Item *el= item->element_index(i);
6078
6164
    Item_cache *tmp;
6088
6174
{
6089
6175
  null_value= 0;
6090
6176
  item->bring_value();
6091
 
  for (uint32_t i= 0; i < item_count; i++)
 
6177
  for (uint i= 0; i < item_count; i++)
6092
6178
  {
6093
6179
    values[i]->store(item->element_index(i));
6094
6180
    null_value|= values[i]->null_value;
6096
6182
}
6097
6183
 
6098
6184
 
6099
 
void Item_cache_row::illegal_method_call(const char *method __attribute__((unused)))
 
6185
void Item_cache_row::illegal_method_call(const char *method)
6100
6186
{
6101
 
  assert(0);
 
6187
  DBUG_ENTER("Item_cache_row::illegal_method_call");
 
6188
  DBUG_PRINT("error", ("!!! %s method was called for row item", method));
 
6189
  DBUG_ASSERT(0);
6102
6190
  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
6103
 
  return;
 
6191
  DBUG_VOID_RETURN;
6104
6192
}
6105
6193
 
6106
6194
 
6107
 
bool Item_cache_row::check_cols(uint32_t c)
 
6195
bool Item_cache_row::check_cols(uint c)
6108
6196
{
6109
6197
  if (c != item_count)
6110
6198
  {
6117
6205
 
6118
6206
bool Item_cache_row::null_inside()
6119
6207
{
6120
 
  for (uint32_t i= 0; i < item_count; i++)
 
6208
  for (uint i= 0; i < item_count; i++)
6121
6209
  {
6122
6210
    if (values[i]->cols() > 1)
6123
6211
    {
6137
6225
 
6138
6226
void Item_cache_row::bring_value()
6139
6227
{
6140
 
  for (uint32_t i= 0; i < item_count; i++)
 
6228
  for (uint i= 0; i < item_count; i++)
6141
6229
    values[i]->bring_value();
6142
6230
  return;
6143
6231
}
6146
6234
Item_type_holder::Item_type_holder(THD *thd, Item *item)
6147
6235
  :Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6148
6236
{
6149
 
  assert(item->fixed);
 
6237
  DBUG_ASSERT(item->fixed);
6150
6238
  maybe_null= item->maybe_null;
6151
6239
  collation.set(item->collation);
6152
6240
  get_full_info(item);
6190
6278
    Field *field= ((Item_field *) item)->field;
6191
6279
    enum_field_types type= field->real_type();
6192
6280
    if (field->is_created_from_null_item)
6193
 
      return DRIZZLE_TYPE_NULL;
 
6281
      return MYSQL_TYPE_NULL;
 
6282
    /* work around about varchar type field detection */
 
6283
    if (type == MYSQL_TYPE_STRING && field->type() == MYSQL_TYPE_VAR_STRING)
 
6284
      return MYSQL_TYPE_VAR_STRING;
6194
6285
    return type;
6195
6286
  }
6196
6287
  case SUM_FUNC_ITEM:
6215
6306
      */
6216
6307
      switch (item->result_type()) {
6217
6308
      case STRING_RESULT:
6218
 
        return DRIZZLE_TYPE_VARCHAR;
 
6309
        return MYSQL_TYPE_VAR_STRING;
6219
6310
      case INT_RESULT:
6220
 
        return DRIZZLE_TYPE_LONGLONG;
 
6311
        return MYSQL_TYPE_LONGLONG;
6221
6312
      case REAL_RESULT:
6222
 
        return DRIZZLE_TYPE_DOUBLE;
 
6313
        return MYSQL_TYPE_DOUBLE;
6223
6314
      case DECIMAL_RESULT:
6224
 
        return DRIZZLE_TYPE_NEWDECIMAL;
 
6315
        return MYSQL_TYPE_NEWDECIMAL;
6225
6316
      case ROW_RESULT:
6226
6317
      default:
6227
 
        assert(0);
6228
 
        return DRIZZLE_TYPE_VARCHAR;
 
6318
        DBUG_ASSERT(0);
 
6319
        return MYSQL_TYPE_VAR_STRING;
6229
6320
      }
6230
6321
    }
6231
6322
    break;
6243
6334
  @param item    given item to join its parameters with this item ones
6244
6335
 
6245
6336
  @retval
6246
 
    true   error - types are incompatible
 
6337
    TRUE   error - types are incompatible
6247
6338
  @retval
6248
 
    false  OK
 
6339
    FALSE  OK
6249
6340
*/
6250
6341
 
6251
 
bool Item_type_holder::join_types(THD *thd __attribute__((unused)),
6252
 
                                  Item *item)
 
6342
bool Item_type_holder::join_types(THD *thd, Item *item)
6253
6343
{
6254
 
  uint32_t max_length_orig= max_length;
6255
 
  uint32_t decimals_orig= decimals;
 
6344
  uint max_length_orig= max_length;
 
6345
  uint decimals_orig= decimals;
 
6346
  DBUG_ENTER("Item_type_holder::join_types");
 
6347
  DBUG_PRINT("info:", ("was type %d len %d, dec %d name %s",
 
6348
                       fld_type, max_length, decimals,
 
6349
                       (name ? name : "<NULL>")));
 
6350
  DBUG_PRINT("info:", ("in type %d len %d, dec %d",
 
6351
                       get_real_type(item),
 
6352
                       item->max_length, item->decimals));
6256
6353
  fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6257
6354
  {
6258
6355
    int item_decimals= item->decimals;
6259
6356
    /* fix variable decimals which always is NOT_FIXED_DEC */
6260
6357
    if (Field::result_merge_type(fld_type) == INT_RESULT)
6261
6358
      item_decimals= 0;
6262
 
    decimals= cmax((int)decimals, item_decimals);
 
6359
    decimals= max(decimals, item_decimals);
6263
6360
  }
6264
6361
  if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6265
6362
  {
6266
 
    decimals= cmin((int)cmax(decimals, item->decimals), DECIMAL_MAX_SCALE);
6267
 
    int precision= cmin(cmax(prev_decimal_int_part, item->decimal_int_part())
 
6363
    decimals= min(max(decimals, item->decimals), DECIMAL_MAX_SCALE);
 
6364
    int precision= min(max(prev_decimal_int_part, item->decimal_int_part())
6268
6365
                       + decimals, DECIMAL_MAX_PRECISION);
6269
6366
    unsigned_flag&= item->unsigned_flag;
6270
6367
    max_length= my_decimal_precision_to_length(precision, decimals,
6276
6373
  case STRING_RESULT:
6277
6374
  {
6278
6375
    const char *old_cs, *old_derivation;
6279
 
    uint32_t old_max_chars= max_length / collation.collation->mbmaxlen;
 
6376
    uint32 old_max_chars= max_length / collation.collation->mbmaxlen;
6280
6377
    old_cs= collation.collation->name;
6281
6378
    old_derivation= collation.derivation_name();
6282
6379
    if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
6286
6383
               item->collation.collation->name,
6287
6384
               item->collation.derivation_name(),
6288
6385
               "UNION");
6289
 
      return(true);
 
6386
      DBUG_RETURN(TRUE);
6290
6387
    }
6291
6388
    /*
6292
6389
      To figure out max_length, we have to take into account possible
6295
6392
     */
6296
6393
    if (collation.collation != &my_charset_bin)
6297
6394
    {
6298
 
      max_length= cmax(old_max_chars * collation.collation->mbmaxlen,
 
6395
      max_length= max(old_max_chars * collation.collation->mbmaxlen,
6299
6396
                      display_length(item) /
6300
6397
                      item->collation.collation->mbmaxlen *
6301
6398
                      collation.collation->mbmaxlen);
6310
6407
    {
6311
6408
      int delta1= max_length_orig - decimals_orig;
6312
6409
      int delta2= item->max_length - item->decimals;
6313
 
      max_length= cmax(delta1, delta2) + decimals;
6314
 
      if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2) 
 
6410
      max_length= max(delta1, delta2) + decimals;
 
6411
      if (fld_type == MYSQL_TYPE_FLOAT && max_length > FLT_DIG + 2) 
 
6412
      {
 
6413
        max_length= FLT_DIG + 6;
 
6414
        decimals= NOT_FIXED_DEC;
 
6415
      } 
 
6416
      if (fld_type == MYSQL_TYPE_DOUBLE && max_length > DBL_DIG + 2) 
6315
6417
      {
6316
6418
        max_length= DBL_DIG + 7;
6317
6419
        decimals= NOT_FIXED_DEC;
6318
6420
      }
6319
6421
    }
6320
6422
    else
6321
 
      max_length= DBL_DIG+7;
 
6423
      max_length= (fld_type == MYSQL_TYPE_FLOAT) ? FLT_DIG+6 : DBL_DIG+7;
6322
6424
    break;
6323
6425
  }
6324
6426
  default:
6325
 
    max_length= cmax(max_length, display_length(item));
 
6427
    max_length= max(max_length, display_length(item));
6326
6428
  };
6327
6429
  maybe_null|= item->maybe_null;
6328
6430
  get_full_info(item);
6329
6431
 
6330
6432
  /* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
6331
6433
  prev_decimal_int_part= decimal_int_part();
6332
 
  return(false);
 
6434
  DBUG_PRINT("info", ("become type: %d  len: %u  dec: %u",
 
6435
                      (int) fld_type, max_length, (uint) decimals));
 
6436
  DBUG_RETURN(FALSE);
6333
6437
}
6334
6438
 
6335
6439
/**
6341
6445
    length
6342
6446
*/
6343
6447
 
6344
 
uint32_t Item_type_holder::display_length(Item *item)
 
6448
uint32 Item_type_holder::display_length(Item *item)
6345
6449
{
6346
6450
  if (item->type() == Item::FIELD_ITEM)
6347
6451
    return ((Item_field *)item)->max_disp_length();
6348
6452
 
6349
6453
  switch (item->field_type())
6350
6454
  {
6351
 
  case DRIZZLE_TYPE_TIMESTAMP:
6352
 
  case DRIZZLE_TYPE_TIME:
6353
 
  case DRIZZLE_TYPE_DATETIME:
6354
 
  case DRIZZLE_TYPE_NEWDATE:
6355
 
  case DRIZZLE_TYPE_VARCHAR:
6356
 
  case DRIZZLE_TYPE_NEWDECIMAL:
6357
 
  case DRIZZLE_TYPE_ENUM:
6358
 
  case DRIZZLE_TYPE_BLOB:
6359
 
  case DRIZZLE_TYPE_TINY:
 
6455
  case MYSQL_TYPE_DECIMAL:
 
6456
  case MYSQL_TYPE_TIMESTAMP:
 
6457
  case MYSQL_TYPE_DATE:
 
6458
  case MYSQL_TYPE_TIME:
 
6459
  case MYSQL_TYPE_DATETIME:
 
6460
  case MYSQL_TYPE_YEAR:
 
6461
  case MYSQL_TYPE_NEWDATE:
 
6462
  case MYSQL_TYPE_VARCHAR:
 
6463
  case MYSQL_TYPE_BIT:
 
6464
  case MYSQL_TYPE_NEWDECIMAL:
 
6465
  case MYSQL_TYPE_ENUM:
 
6466
  case MYSQL_TYPE_SET:
 
6467
  case MYSQL_TYPE_TINY_BLOB:
 
6468
  case MYSQL_TYPE_MEDIUM_BLOB:
 
6469
  case MYSQL_TYPE_LONG_BLOB:
 
6470
  case MYSQL_TYPE_BLOB:
 
6471
  case MYSQL_TYPE_VAR_STRING:
 
6472
  case MYSQL_TYPE_STRING:
 
6473
  case MYSQL_TYPE_GEOMETRY:
 
6474
    return item->max_length;
 
6475
  case MYSQL_TYPE_TINY:
6360
6476
    return 4;
6361
 
  case DRIZZLE_TYPE_LONG:
 
6477
  case MYSQL_TYPE_SHORT:
 
6478
    return 6;
 
6479
  case MYSQL_TYPE_LONG:
6362
6480
    return MY_INT32_NUM_DECIMAL_DIGITS;
6363
 
  case DRIZZLE_TYPE_DOUBLE:
 
6481
  case MYSQL_TYPE_FLOAT:
 
6482
    return 25;
 
6483
  case MYSQL_TYPE_DOUBLE:
6364
6484
    return 53;
6365
 
  case DRIZZLE_TYPE_NULL:
 
6485
  case MYSQL_TYPE_NULL:
6366
6486
    return 0;
6367
 
  case DRIZZLE_TYPE_LONGLONG:
 
6487
  case MYSQL_TYPE_LONGLONG:
6368
6488
    return 20;
 
6489
  case MYSQL_TYPE_INT24:
 
6490
    return 8;
6369
6491
  default:
6370
 
    assert(0); // we should never go there
 
6492
    DBUG_ASSERT(0); // we should never go there
6371
6493
    return 0;
6372
6494
  }
6373
6495
}
6383
6505
    created field
6384
6506
*/
6385
6507
 
6386
 
Field *Item_type_holder::make_field_by_type(Table *table)
 
6508
Field *Item_type_holder::make_field_by_type(TABLE *table)
6387
6509
{
6388
6510
  /*
6389
6511
    The field functions defines a field to be not null if null_ptr is not 0
6390
6512
  */
6391
 
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
 
6513
  uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
6392
6514
  Field *field;
6393
6515
 
6394
6516
  switch (fld_type) {
6395
 
  case DRIZZLE_TYPE_ENUM:
6396
 
    assert(enum_set_typelib);
6397
 
    field= new Field_enum((unsigned char *) 0, max_length, null_ptr, 0,
 
6517
  case MYSQL_TYPE_ENUM:
 
6518
    DBUG_ASSERT(enum_set_typelib);
 
6519
    field= new Field_enum((uchar *) 0, max_length, null_ptr, 0,
6398
6520
                          Field::NONE, name,
6399
6521
                          get_enum_pack_length(enum_set_typelib->count),
6400
6522
                          enum_set_typelib, collation.collation);
6401
6523
    if (field)
6402
6524
      field->init(table);
6403
6525
    return field;
6404
 
  case DRIZZLE_TYPE_NULL:
 
6526
  case MYSQL_TYPE_SET:
 
6527
    DBUG_ASSERT(enum_set_typelib);
 
6528
    field= new Field_set((uchar *) 0, max_length, null_ptr, 0,
 
6529
                         Field::NONE, name,
 
6530
                         get_set_pack_length(enum_set_typelib->count),
 
6531
                         enum_set_typelib, collation.collation);
 
6532
    if (field)
 
6533
      field->init(table);
 
6534
    return field;
 
6535
  case MYSQL_TYPE_NULL:
6405
6536
    return make_string_field(table);
6406
6537
  default:
6407
6538
    break;
6418
6549
*/
6419
6550
void Item_type_holder::get_full_info(Item *item)
6420
6551
{
6421
 
  if (fld_type == DRIZZLE_TYPE_ENUM)
 
6552
  if (fld_type == MYSQL_TYPE_ENUM ||
 
6553
      fld_type == MYSQL_TYPE_SET)
6422
6554
  {
6423
6555
    if (item->type() == Item::SUM_FUNC_ITEM &&
6424
6556
        (((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
6428
6560
      We can have enum/set type after merging only if we have one enum|set
6429
6561
      field (or MIN|MAX(enum|set field)) and number of NULL fields
6430
6562
    */
6431
 
    assert((enum_set_typelib &&
6432
 
                 get_real_type(item) == DRIZZLE_TYPE_NULL) ||
 
6563
    DBUG_ASSERT((enum_set_typelib &&
 
6564
                 get_real_type(item) == MYSQL_TYPE_NULL) ||
6433
6565
                (!enum_set_typelib &&
6434
6566
                 item->type() == Item::FIELD_ITEM &&
6435
 
                 (get_real_type(item) == DRIZZLE_TYPE_ENUM) &&
 
6567
                 (get_real_type(item) == MYSQL_TYPE_ENUM ||
 
6568
                  get_real_type(item) == MYSQL_TYPE_SET) &&
6436
6569
                 ((Field_enum*)((Item_field *) item)->field)->typelib));
6437
6570
    if (!enum_set_typelib)
6438
6571
    {
6444
6577
 
6445
6578
double Item_type_holder::val_real()
6446
6579
{
6447
 
  assert(0); // should never be called
 
6580
  DBUG_ASSERT(0); // should never be called
6448
6581
  return 0.0;
6449
6582
}
6450
6583
 
6451
6584
 
6452
 
int64_t Item_type_holder::val_int()
 
6585
longlong Item_type_holder::val_int()
6453
6586
{
6454
 
  assert(0); // should never be called
 
6587
  DBUG_ASSERT(0); // should never be called
6455
6588
  return 0;
6456
6589
}
6457
6590
 
6458
6591
my_decimal *Item_type_holder::val_decimal(my_decimal *)
6459
6592
{
6460
 
  assert(0); // should never be called
 
6593
  DBUG_ASSERT(0); // should never be called
6461
6594
  return 0;
6462
6595
}
6463
6596
 
6464
6597
String *Item_type_holder::val_str(String*)
6465
6598
{
6466
 
  assert(0); // should never be called
 
6599
  DBUG_ASSERT(0); // should never be called
6467
6600
  return 0;
6468
6601
}
6469
6602
 
6470
6603
void Item_result_field::cleanup()
6471
6604
{
 
6605
  DBUG_ENTER("Item_result_field::cleanup()");
6472
6606
  Item::cleanup();
6473
6607
  result_field= 0;
6474
 
  return;
 
6608
  DBUG_VOID_RETURN;
6475
6609
}
6476
6610
 
6477
6611
/**
6481
6615
    do nothing
6482
6616
*/
6483
6617
 
6484
 
void dummy_error_processor(THD *thd __attribute__((unused)),
6485
 
                           void *data __attribute__((unused)))
 
6618
void dummy_error_processor(THD *thd, void *data)
6486
6619
{}
6487
6620
 
6488
6621
/**