~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

Refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
#include "drizzled/sql_select.h"
22
 
#include "drizzled/error.h"
23
 
#include "drizzled/show.h"
24
 
#include "drizzled/item/cmpfunc.h"
25
 
#include "drizzled/item/cache_row.h"
26
 
#include "drizzled/item/type_holder.h"
27
 
#include "drizzled/item/sum.h"
28
 
#include "drizzled/item/copy_string.h"
29
 
#include "drizzled/function/str/conv_charset.h"
30
 
#include "drizzled/sql_base.h"
31
 
#include "drizzled/util/convert.h"
32
 
#include "drizzled/plugin/client.h"
33
 
#include "drizzled/time_functions.h"
34
 
 
35
 
#include "drizzled/field/str.h"
36
 
#include "drizzled/field/num.h"
37
 
#include "drizzled/field/blob.h"
38
 
#include "drizzled/field/enum.h"
39
 
#include "drizzled/field/null.h"
40
 
#include "drizzled/field/date.h"
41
 
#include "drizzled/field/decimal.h"
42
 
#include "drizzled/field/real.h"
43
 
#include "drizzled/field/double.h"
44
 
#include "drizzled/field/int32.h"
45
 
#include "drizzled/field/int64.h"
46
 
#include "drizzled/field/num.h"
47
 
#include "drizzled/field/timestamp.h"
48
 
#include "drizzled/field/datetime.h"
49
 
#include "drizzled/field/varstring.h"
50
 
#include "drizzled/internal/m_string.h"
 
20
#include <config.h>
 
21
#include <drizzled/sql_select.h>
 
22
#include <drizzled/error.h>
 
23
#include <drizzled/show.h>
 
24
#include <drizzled/item/cmpfunc.h>
 
25
#include <drizzled/item/cache_row.h>
 
26
#include <drizzled/item/type_holder.h>
 
27
#include <drizzled/item/sum.h>
 
28
#include <drizzled/item/copy_string.h>
 
29
#include <drizzled/function/str/conv_charset.h>
 
30
#include <drizzled/sql_base.h>
 
31
#include <drizzled/util/convert.h>
 
32
#include <drizzled/plugin/client.h>
 
33
#include <drizzled/time_functions.h>
 
34
#include <drizzled/field/str.h>
 
35
#include <drizzled/field/num.h>
 
36
#include <drizzled/field/blob.h>
 
37
#include <drizzled/field/date.h>
 
38
#include <drizzled/field/datetime.h>
 
39
#include <drizzled/field/decimal.h>
 
40
#include <drizzled/field/double.h>
 
41
#include <drizzled/field/enum.h>
 
42
#include <drizzled/field/epoch.h>
 
43
#include <drizzled/field/int32.h>
 
44
#include <drizzled/field/int64.h>
 
45
#include <drizzled/field/microtime.h>
 
46
#include <drizzled/field/null.h>
 
47
#include <drizzled/field/real.h>
 
48
#include <drizzled/field/size.h>
 
49
#include <drizzled/field/time.h>
 
50
#include <drizzled/field/varstring.h>
 
51
#include <drizzled/current_session.h>
 
52
#include <drizzled/session.h>
 
53
#include <drizzled/internal/m_string.h>
 
54
#include <drizzled/item/ref.h>
 
55
#include <drizzled/item/subselect.h>
 
56
#include <drizzled/sql_lex.h>
 
57
#include <drizzled/system_variables.h>
51
58
 
52
59
#include <cstdio>
53
60
#include <math.h>
95
102
 
96
103
    case DECIMAL_RESULT:
97
104
    {
98
 
      my_decimal decimal_value;
99
 
      my_decimal *val= val_decimal(&decimal_value);
 
105
      type::Decimal decimal_value;
 
106
      type::Decimal *val= val_decimal(&decimal_value);
100
107
      if (val)
101
 
        return !my_decimal_is_zero(val);
 
108
        return not val->isZero();
102
109
      return false;
103
110
    }
104
111
 
137
144
 
138
145
String *Item::val_string_from_decimal(String *str)
139
146
{
140
 
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
 
147
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
141
148
  if (null_value)
142
149
    return NULL;
143
150
 
144
 
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
145
 
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
 
151
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
152
  class_decimal2string(&dec_buf, 0, str);
146
153
  return str;
147
154
}
148
155
 
149
 
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
 
156
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
150
157
{
151
158
  double nr= val_real();
152
159
  if (null_value)
153
160
    return NULL;
154
161
 
155
 
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
162
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
156
163
  return (decimal_value);
157
164
}
158
165
 
159
 
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
 
166
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
160
167
{
161
168
  int64_t nr= val_int();
162
169
  if (null_value)
163
170
    return NULL;
164
171
 
165
 
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
172
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
166
173
  return decimal_value;
167
174
}
168
175
 
169
 
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
 
176
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
170
177
{
171
178
  String *res;
172
 
  char *end_ptr;
173
179
  if (!(res= val_str(&str_value)))
174
180
    return NULL;
175
181
 
176
 
  end_ptr= (char*) res->ptr()+ res->length();
177
 
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
 
182
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
178
183
                     res->ptr(), 
179
184
                     res->length(), 
180
 
                     res->charset(),
181
 
                     decimal_value) & E_DEC_BAD_NUM)
 
185
                     res->charset()) & E_DEC_BAD_NUM)
182
186
  {
183
 
    push_warning_printf(current_session, 
 
187
    push_warning_printf(&getSession(), 
184
188
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
185
189
                        ER_TRUNCATED_WRONG_VALUE,
186
190
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
189
193
  return decimal_value;
190
194
}
191
195
 
192
 
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
 
196
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
193
197
{
194
198
  assert(fixed);
195
 
  DRIZZLE_TIME ltime;
196
 
  if (get_date(&ltime, TIME_FUZZY_DATE))
 
199
  type::Time ltime;
 
200
  if (get_date(ltime, TIME_FUZZY_DATE))
197
201
  {
198
 
    my_decimal_set_zero(decimal_value);
 
202
    decimal_value->set_zero();
199
203
    null_value= 1;                               // set NULL, stop processing
200
204
    return NULL;
201
205
  }
202
 
  return date2my_decimal(&ltime, decimal_value);
 
206
  return date2_class_decimal(&ltime, decimal_value);
203
207
}
204
208
 
205
 
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
 
209
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
206
210
{
207
211
  assert(fixed);
208
 
  DRIZZLE_TIME ltime;
209
 
  if (get_time(&ltime))
 
212
  type::Time ltime;
 
213
  if (get_time(ltime))
210
214
  {
211
 
    my_decimal_set_zero(decimal_value);
 
215
    decimal_value->set_zero();
212
216
    return NULL;
213
217
  }
214
 
  return date2my_decimal(&ltime, decimal_value);
 
218
  return date2_class_decimal(&ltime, decimal_value);
215
219
}
216
220
 
217
221
double Item::val_real_from_decimal()
218
222
{
219
223
  /* Note that fix_fields may not be called for Item_avg_field items */
220
224
  double result;
221
 
  my_decimal value_buff, *dec_val= val_decimal(&value_buff);
 
225
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
222
226
  if (null_value)
223
227
    return 0.0;
224
 
  my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
 
228
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
225
229
  return result;
226
230
}
227
231
 
229
233
{
230
234
  /* Note that fix_fields may not be called for Item_avg_field items */
231
235
  int64_t result;
232
 
  my_decimal value, *dec_val= val_decimal(&value);
 
236
  type::Decimal value, *dec_val= val_decimal(&value);
 
237
 
233
238
  if (null_value)
234
239
    return 0;
235
 
  my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
 
240
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
241
 
236
242
  return result;
237
243
}
238
244
 
239
 
int Item::save_time_in_field(Field *field)
 
245
bool Item::save_time_in_field(Field *field)
240
246
{
241
 
  DRIZZLE_TIME ltime;
242
 
  if (get_time(&ltime))
 
247
  type::Time ltime;
 
248
 
 
249
  if (get_time(ltime))
243
250
    return set_field_to_null(field);
 
251
 
244
252
  field->set_notnull();
245
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
 
253
 
 
254
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
246
255
}
247
256
 
248
 
int Item::save_date_in_field(Field *field)
 
257
bool Item::save_date_in_field(Field *field)
249
258
{
250
 
  DRIZZLE_TIME ltime;
251
 
  if (get_date(&ltime, TIME_FUZZY_DATE))
 
259
  type::Time ltime;
 
260
 
 
261
  if (get_date(ltime, TIME_FUZZY_DATE))
252
262
    return set_field_to_null(field);
 
263
 
253
264
  field->set_notnull();
254
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
 
265
 
 
266
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
255
267
}
256
268
 
257
269
/**
262
274
{
263
275
  if (null_value)
264
276
    return set_field_to_null(field);
 
277
 
265
278
  field->set_notnull();
 
279
 
266
280
  return field->store(result->ptr(), result->length(), collation.collation);
267
281
}
268
282
 
281
295
  with_sum_func(false),
282
296
  is_autogenerated_name(true),
283
297
  with_subselect(false),
284
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
 
298
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
 
299
  _session(*current_session)
285
300
{
286
301
  cmp_context= (Item_result)-1;
287
302
 
288
303
  /* Put item in free list so that we can free all items at end */
289
 
  Session *session= current_session;
290
 
  next= session->free_list;
291
 
  session->free_list= this;
 
304
  next= getSession().free_list;
 
305
  getSession().free_list= this;
292
306
 
293
307
  /*
294
308
    Item constructor can be called during execution other then SQL_COM
295
 
    command => we should check session->lex->current_select on zero (session->lex
 
309
    command => we should check session->lex().current_select on zero (session->lex
296
310
    can be uninitialised)
297
311
  */
298
 
  if (session->lex->current_select)
 
312
  if (getSession().lex().current_select)
299
313
  {
300
 
    enum_parsing_place place= session->lex->current_select->parsing_place;
 
314
    enum_parsing_place place= getSession().lex().current_select->parsing_place;
301
315
    if (place == SELECT_LIST || place == IN_HAVING)
302
 
      session->lex->current_select->select_n_having_items++;
 
316
      getSession().lex().current_select->select_n_having_items++;
303
317
  }
304
318
}
305
319
 
320
334
  is_autogenerated_name(item->is_autogenerated_name),
321
335
  with_subselect(item->with_subselect),
322
336
  collation(item->collation),
323
 
  cmp_context(item->cmp_context)
 
337
  cmp_context(item->cmp_context),
 
338
  _session(*session)
324
339
{
325
340
  /* Put this item in the session's free list */
326
 
  next= session->free_list;
327
 
  session->free_list= this;
 
341
  next= getSession().free_list;
 
342
  getSession().free_list= this;
328
343
}
329
344
 
330
345
uint32_t Item::float_length(uint32_t decimals_par) const
337
352
  Item_result restype= result_type();
338
353
 
339
354
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
340
 
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
355
    return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
341
356
               (uint32_t) DECIMAL_MAX_PRECISION);
342
357
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
343
358
}
344
359
 
345
360
int Item::decimal_int_part() const
346
361
{
347
 
  return my_decimal_int_part(decimal_precision(), decimals);
 
362
  return class_decimal_int_part(decimal_precision(), decimals);
348
363
}
349
364
 
350
 
void Item::print(String *str, enum_query_type)
 
365
void Item::print(String *str)
351
366
{
352
367
  str->append(full_name());
353
368
}
354
369
 
355
 
void Item::print_item_w_name(String *str, enum_query_type query_type)
 
370
void Item::print_item_w_name(String *str)
356
371
{
357
 
  print(str, query_type);
 
372
  print(str);
358
373
 
359
374
  if (name)
360
375
  {
401
416
  return false;
402
417
}
403
418
 
404
 
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
 
419
void Item::set_name(const char *str, uint32_t length, const charset_info_st * const cs)
405
420
{
406
421
  if (!length)
407
422
  {
422
437
    if (orig_len != length && ! is_autogenerated_name)
423
438
    {
424
439
      if (length == 0)
425
 
        push_warning_printf(current_session, 
 
440
        push_warning_printf(&getSession(), 
426
441
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
427
442
                            ER_NAME_BECOMES_EMPTY, 
428
443
                            ER(ER_NAME_BECOMES_EMPTY),
429
444
                            str + length - orig_len);
430
445
      else
431
 
        push_warning_printf(current_session, 
 
446
        push_warning_printf(&getSession(),
432
447
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
433
448
                            ER_REMOVED_SPACES, 
434
449
                            ER(ER_REMOVED_SPACES),
451
466
         ! my_strcasecmp(system_charset_info, name, item->name);
452
467
}
453
468
 
454
 
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
 
469
Item *Item::safe_charset_converter(const charset_info_st * const tocs)
455
470
{
456
471
  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
457
472
  return conv->safe ? conv : NULL;
458
473
}
459
474
 
460
 
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
475
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
461
476
{
462
 
  if (result_type() == STRING_RESULT)
463
 
  {
464
 
    char buff[40];
465
 
    String tmp(buff,sizeof(buff), &my_charset_bin),*res;
466
 
    if (!(res=val_str(&tmp)) ||
467
 
        str_to_datetime_with_warn(res->ptr(), res->length(),
468
 
                                  ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
469
 
      goto err;
470
 
  }
471
 
  else
472
 
  {
473
 
    int64_t value= val_int();
474
 
    int was_cut;
475
 
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
476
 
    {
477
 
      char buff[22], *end;
478
 
      end= internal::int64_t10_to_str(value, buff, -10);
479
 
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
480
 
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
481
 
                                   NULL);
482
 
      goto err;
483
 
    }
484
 
  }
485
 
  return false;
486
 
 
487
 
err:
488
 
  memset(ltime, 0, sizeof(*ltime));
 
477
  do
 
478
  {
 
479
    if (is_null())
 
480
    {
 
481
      break;
 
482
    }
 
483
    else if (result_type() == STRING_RESULT)
 
484
    {
 
485
      char buff[type::Time::MAX_STRING_LENGTH];
 
486
      String tmp(buff,sizeof(buff), &my_charset_bin),*res;
 
487
      if (!(res=val_str(&tmp)) ||
 
488
          str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
 
489
                                    &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
 
490
      {
 
491
        break;
 
492
      }
 
493
    }
 
494
    else
 
495
    {
 
496
      int64_t value= val_int();
 
497
      type::datetime_t date_value;
 
498
 
 
499
      ltime.convert(date_value, value, fuzzydate);
 
500
 
 
501
      if (not type::is_valid(date_value))
 
502
      {
 
503
        char buff[DECIMAL_LONGLONG_DIGITS], *end;
 
504
        end= internal::int64_t10_to_str(value, buff, -10);
 
505
        make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
506
                                     buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
 
507
        break;
 
508
      }
 
509
    }
 
510
 
 
511
    return false;
 
512
  } while (0);
 
513
 
 
514
  ltime.reset();
 
515
 
489
516
  return true;
490
517
}
491
518
 
492
 
bool Item::get_time(DRIZZLE_TIME *ltime)
 
519
bool Item::get_time(type::Time &ltime)
493
520
{
494
 
  char buff[40];
 
521
  char buff[type::Time::MAX_STRING_LENGTH];
495
522
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
496
 
  if (!(res=val_str(&tmp)) ||
497
 
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
523
  if (!(res=val_str(&tmp)) or
 
524
      str_to_time_with_warn(&getSession(), res->ptr(), res->length(), &ltime))
498
525
  {
499
 
    memset(ltime, 0, sizeof(*ltime));
 
526
    ltime.reset();
 
527
 
500
528
    return true;
501
529
  }
 
530
 
502
531
  return false;
503
532
}
504
533
 
505
 
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
534
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
506
535
{
507
 
  return get_date(ltime,fuzzydate);
 
536
  return get_date(ltime, fuzzydate);
508
537
}
509
538
 
510
539
bool Item::is_null()
564
593
  return copy_or_same(session);
565
594
}
566
595
 
567
 
const CHARSET_INFO *Item::default_charset()
 
596
const charset_info_st *Item::default_charset()
568
597
{
569
598
  return current_session->variables.getCollation();
570
599
}
571
600
 
572
 
const CHARSET_INFO *Item::compare_collation()
 
601
const charset_info_st *Item::compare_collation()
573
602
{
574
603
  return NULL;
575
604
}
711
740
                  const char *table_name_arg, const char *field_name_arg)
712
741
    :Item_ref(context_arg, item, table_name_arg, field_name_arg) {}
713
742
 
714
 
  virtual inline void print (String *str, enum_query_type query_type)
 
743
  virtual inline void print (String *str)
715
744
  {
716
745
    if (ref)
717
 
      (*ref)->print(str, query_type);
 
746
      (*ref)->print(str);
718
747
    else
719
 
      Item_ident::print(str, query_type);
 
748
      Item_ident::print(str);
720
749
  }
721
750
};
722
751
 
753
782
      Item_ref to allow fields from view being stored in tmp table.
754
783
    */
755
784
    Item_aggregate_ref *item_ref;
756
 
    uint32_t el= fields.elements;
 
785
    uint32_t el= fields.size();
757
786
    Item *real_itm= real_item();
758
787
 
759
788
    ref_pointer_array[el]= real_itm;
760
 
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
 
789
    if (!(item_ref= new Item_aggregate_ref(&session->lex().current_select->context,
761
790
                                           ref_pointer_array + el, 0, name)))
762
791
      return; /* fatal_error is set */
763
792
    if (type() == SUM_FUNC_ITEM)
764
793
      item_ref->depended_from= ((Item_sum *) this)->depended_from();
765
794
    fields.push_front(real_itm);
766
 
    session->change_item_tree(ref, item_ref);
 
795
    *ref= item_ref;
767
796
  }
768
797
}
769
798
 
790
819
  if (mark_item)
791
820
    mark_item->depended_from= last;
792
821
  current->mark_as_dependent(last);
793
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
822
  if (session->lex().describe & DESCRIBE_EXTENDED)
794
823
  {
795
824
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
796
825
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
821
850
  {
822
851
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
823
852
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
824
 
    prev_subselect_item->const_item_cache= 0;
 
853
    prev_subselect_item->const_item_cache= false;
825
854
  }
826
855
  {
827
856
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
836
865
    }
837
866
    else
838
867
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
839
 
    prev_subselect_item->const_item_cache= 0;
 
868
    prev_subselect_item->const_item_cache= false;
840
869
    mark_as_dependent(session, last_select, current_sel, resolved_item,
841
870
                      dependent);
842
871
  }
856
885
    - the found item on success
857
886
    - NULL if find_item is not in group_list
858
887
*/
859
 
static Item** find_field_in_group_list(Item *find_item, Order *group_list)
 
888
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
860
889
{
861
890
  const char *db_name;
862
891
  const char *table_name;
912
941
        if (cur_field->db_name && db_name)
913
942
        {
914
943
          /* If field_name is also qualified by a database name. */
915
 
          if (strcasecmp(cur_field->db_name, db_name))
 
944
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
 
945
          {
916
946
            /* Same field names, different databases. */
917
947
            return NULL;
 
948
          }
918
949
          ++cur_match_degree;
919
950
        }
920
951
      }
933
964
          best match, they must reference the same column, otherwise the field
934
965
          is ambiguous.
935
966
        */
936
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where);
 
967
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
937
968
        return NULL;
938
969
      }
939
970
    }
941
972
 
942
973
  if (found_group)
943
974
    return found_group->item;
944
 
  else
945
 
    return NULL;
 
975
 
 
976
  return NULL;
946
977
}
947
978
 
948
979
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
969
1000
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
970
1001
  if (select->having_fix_field && !ref->with_sum_func && group_list)
971
1002
  {
972
 
    group_by_ref= find_field_in_group_list(ref, group_list);
 
1003
    group_by_ref= find_field_in_group_list(session, ref, group_list);
973
1004
 
974
1005
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
975
1006
    if (group_by_ref && (select_ref != not_found_item) &&
978
1009
      ambiguous_fields= true;
979
1010
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
980
1011
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
981
 
                          current_session->where);
 
1012
                          session->where());
982
1013
 
983
1014
    }
984
1015
  }
1007
1038
}
1008
1039
 
1009
1040
void Item::init_make_field(SendField *tmp_field,
1010
 
                           enum enum_field_types field_type_arg)
 
1041
                           enum enum_field_types field_type_arg)
1011
1042
{
1012
1043
  char *empty_name= (char*) "";
1013
1044
  tmp_field->db_name=   empty_name;
1057
1088
 
1058
1089
bool Item::is_datetime()
1059
1090
{
1060
 
  switch (field_type())
1061
 
  {
1062
 
    case DRIZZLE_TYPE_DATE:
1063
 
    case DRIZZLE_TYPE_DATETIME:
1064
 
    case DRIZZLE_TYPE_TIMESTAMP:
1065
 
      return true;
1066
 
    case DRIZZLE_TYPE_BLOB:
1067
 
    case DRIZZLE_TYPE_VARCHAR:
1068
 
    case DRIZZLE_TYPE_DOUBLE:
1069
 
    case DRIZZLE_TYPE_DECIMAL:
1070
 
    case DRIZZLE_TYPE_ENUM:
1071
 
    case DRIZZLE_TYPE_LONG:
1072
 
    case DRIZZLE_TYPE_LONGLONG:
1073
 
    case DRIZZLE_TYPE_NULL:
1074
 
    case DRIZZLE_TYPE_UUID:
1075
 
      return false;
1076
 
  }
1077
 
 
1078
 
  assert(0);
1079
 
  abort();
 
1091
  return field::isDateTime(field_type());
1080
1092
}
1081
1093
 
1082
1094
String *Item::check_well_formed_result(String *str, bool send_error)
1083
1095
{
1084
1096
  /* Check whether we got a well-formed string */
1085
 
  const CHARSET_INFO * const cs= str->charset();
 
1097
  const charset_info_st * const cs= str->charset();
1086
1098
  int well_formed_error;
1087
1099
  uint32_t wlen= cs->cset->well_formed_len(cs,
1088
1100
                                       str->ptr(), str->ptr() + str->length(),
1089
1101
                                       str->length(), &well_formed_error);
1090
1102
  if (wlen < str->length())
1091
1103
  {
1092
 
    Session *session= current_session;
1093
1104
    char hexbuf[7];
1094
1105
    enum DRIZZLE_ERROR::enum_warning_level level;
1095
1106
    uint32_t diff= str->length() - wlen;
1106
1117
      null_value= 1;
1107
1118
      str= 0;
1108
1119
    }
1109
 
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
 
1120
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
1110
1121
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1111
1122
  }
1112
1123
  return str;
1113
1124
}
1114
1125
 
1115
 
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
 
1126
bool Item::eq_by_collation(Item *item, bool binary_cmp, const charset_info_st * const cs)
1116
1127
{
1117
 
  const CHARSET_INFO *save_cs= 0;
1118
 
  const CHARSET_INFO *save_item_cs= 0;
 
1128
  const charset_info_st *save_cs= 0;
 
1129
  const charset_info_st *save_item_cs= 0;
1119
1130
  if (collation.collation != cs)
1120
1131
  {
1121
1132
    save_cs= collation.collation;
1171
1182
                                 0,
1172
1183
                                 Field::NONE,
1173
1184
                                 name,
1174
 
                                 decimals,
1175
 
                                 0,
1176
 
                                 unsigned_flag);
 
1185
                                 decimals);
1177
1186
    break;
1178
1187
  case DRIZZLE_TYPE_LONG:
1179
1188
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1186
1195
                            name, decimals, 0, unsigned_flag);
1187
1196
    break;
1188
1197
  case DRIZZLE_TYPE_NULL:
1189
 
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
 
1198
    field= new Field_null((unsigned char*) 0, max_length, name);
1190
1199
    break;
1191
1200
  case DRIZZLE_TYPE_DATE:
1192
 
    field= new Field_date(maybe_null, name, &my_charset_bin);
1193
 
    break;
 
1201
    field= new Field_date(maybe_null, name);
 
1202
    break;
 
1203
 
 
1204
  case DRIZZLE_TYPE_MICROTIME:
 
1205
    field= new field::Microtime(maybe_null, name);
 
1206
    break;
 
1207
 
1194
1208
  case DRIZZLE_TYPE_TIMESTAMP:
1195
 
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
 
1209
    field= new field::Epoch(maybe_null, name);
1196
1210
    break;
1197
1211
  case DRIZZLE_TYPE_DATETIME:
1198
 
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1199
 
    break;
 
1212
    field= new Field_datetime(maybe_null, name);
 
1213
    break;
 
1214
  case DRIZZLE_TYPE_TIME:
 
1215
    field= new field::Time(maybe_null, name);
 
1216
    break;
 
1217
  case DRIZZLE_TYPE_BOOLEAN:
1200
1218
  case DRIZZLE_TYPE_UUID:
1201
1219
  case DRIZZLE_TYPE_ENUM:
1202
1220
  case DRIZZLE_TYPE_VARCHAR:
1223
1241
  if (result_type() == STRING_RESULT)
1224
1242
  {
1225
1243
    String *result;
1226
 
    const CHARSET_INFO * const cs= collation.collation;
 
1244
    const charset_info_st * const cs= collation.collation;
1227
1245
    char buff[MAX_FIELD_WIDTH];         // Alloc buffer for small columns
1228
1246
    str_value.set_quick(buff, sizeof(buff), cs);
1229
1247
    result=val_str(&str_value);
1258
1276
  }
1259
1277
  else if (result_type() == DECIMAL_RESULT)
1260
1278
  {
1261
 
    my_decimal decimal_value;
1262
 
    my_decimal *value= val_decimal(&decimal_value);
 
1279
    type::Decimal decimal_value;
 
1280
    type::Decimal *value= val_decimal(&decimal_value);
1263
1281
    if (null_value)
1264
1282
      return set_field_to_null_with_conversions(field, no_conversions);
1265
1283
    field->set_notnull();
1346
1364
  case DRIZZLE_TYPE_ENUM:
1347
1365
  case DRIZZLE_TYPE_BLOB:
1348
1366
  case DRIZZLE_TYPE_VARCHAR:
 
1367
  case DRIZZLE_TYPE_BOOLEAN:
1349
1368
  case DRIZZLE_TYPE_UUID:
1350
1369
  case DRIZZLE_TYPE_DECIMAL:
1351
1370
    {
1382
1401
        result= client->store(nr, decimals, buffer);
1383
1402
      break;
1384
1403
    }
 
1404
  case DRIZZLE_TYPE_TIME:
 
1405
    {
 
1406
      type::Time tm;
 
1407
      get_time(tm);
 
1408
      if (not null_value)
 
1409
        result= client->store(&tm);
 
1410
      break;
 
1411
    }
1385
1412
  case DRIZZLE_TYPE_DATETIME:
 
1413
  case DRIZZLE_TYPE_MICROTIME:
1386
1414
  case DRIZZLE_TYPE_TIMESTAMP:
1387
1415
    {
1388
 
      DRIZZLE_TIME tm;
1389
 
      get_date(&tm, TIME_FUZZY_DATE);
 
1416
      type::Time tm;
 
1417
      get_date(tm, TIME_FUZZY_DATE);
1390
1418
      if (!null_value)
1391
1419
        result= client->store(&tm);
1392
1420
      break;
1398
1426
  return result;
1399
1427
}
1400
1428
 
 
1429
uint32_t Item::max_char_length() const
 
1430
{
 
1431
  return max_length / collation.collation->mbmaxlen;
 
1432
}
 
1433
 
 
1434
void Item::fix_length_and_charset(uint32_t max_char_length_arg, charset_info_st *cs)
 
1435
 
1436
  max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
 
1437
  collation.collation= cs;
 
1438
}
 
1439
 
 
1440
void Item::fix_char_length(uint32_t max_char_length_arg)
 
1441
 
1442
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
 
1443
}
 
1444
 
 
1445
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
 
1446
 
1447
  uint64_t max_result_length= max_char_length_arg *
 
1448
    collation.collation->mbmaxlen;
 
1449
 
 
1450
  if (max_result_length >= MAX_BLOB_WIDTH)
 
1451
  { 
 
1452
    max_length= MAX_BLOB_WIDTH;
 
1453
    maybe_null= false;
 
1454
  }
 
1455
  else
 
1456
  {
 
1457
    max_length= max_result_length;
 
1458
  }
 
1459
}
 
1460
 
 
1461
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
 
1462
 
1463
  collation.set(&my_charset_bin);
 
1464
  fix_char_length(max_char_length_arg);
 
1465
}
 
1466
 
1401
1467
Item_result item_cmp_type(Item_result a,Item_result b)
1402
1468
{
1403
1469
  if (a == STRING_RESULT && b == STRING_RESULT)
1491
1557
    }
1492
1558
  case DECIMAL_RESULT:
1493
1559
    {
1494
 
      my_decimal decimal_value;
1495
 
      my_decimal *result= item->val_decimal(&decimal_value);
 
1560
      type::Decimal decimal_value;
 
1561
      type::Decimal *result= item->val_decimal(&decimal_value);
1496
1562
      uint32_t length= item->max_length, decimals= item->decimals;
1497
1563
      bool null_value= item->null_value;
1498
1564
      new_item= (null_value ?
1503
1569
  }
1504
1570
 
1505
1571
  if (new_item)
1506
 
    session->change_item_tree(ref, new_item);
 
1572
    *ref= new_item;
1507
1573
}
1508
1574
 
1509
1575
bool field_is_equal_to_item(Field *field,Item *item)
1529
1595
 
1530
1596
  if (res_type == DECIMAL_RESULT)
1531
1597
  {
1532
 
    my_decimal item_buf, *item_val,
 
1598
    type::Decimal item_buf, *item_val,
1533
1599
               field_buf, *field_val;
1534
1600
    item_val= item->val_decimal(&item_buf);
1535
1601
    if (item->null_value)
1536
1602
      return 1;                                 // This must be true
1537
1603
    field_val= field->val_decimal(&field_buf);
1538
 
    return !my_decimal_cmp(item_val, field_val);
 
1604
    return !class_decimal_cmp(item_val, field_val);
1539
1605
  }
1540
1606
 
1541
1607
  double result= item->val_real();
1592
1658
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1593
1659
      Int32 -> make them field::Int64.
1594
1660
    */
1595
 
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1596
 
      new_field=new field::Int64(item->max_length, maybe_null,
1597
 
                                 item->name, item->unsigned_flag);
 
1661
    if (item->unsigned_flag)
 
1662
    {
 
1663
      new_field= new field::Size(item->max_length, maybe_null,
 
1664
                                  item->name, item->unsigned_flag);
 
1665
    }
 
1666
    else if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1667
    {
 
1668
      new_field= new field::Int64(item->max_length, maybe_null,
 
1669
                                  item->name, item->unsigned_flag);
 
1670
    }
1598
1671
    else
1599
 
      new_field=new field::Int32(item->max_length, maybe_null,
1600
 
                                 item->name, item->unsigned_flag);
 
1672
    {
 
1673
      new_field= new field::Int32(item->max_length, maybe_null,
 
1674
                                  item->name, item->unsigned_flag);
 
1675
    }
 
1676
 
1601
1677
    break;
1602
1678
 
1603
1679
  case STRING_RESULT:
1604
1680
    assert(item->collation.collation);
1605
1681
 
1606
 
    enum enum_field_types type;
1607
1682
    /*
1608
1683
      DATE/TIME fields have STRING_RESULT result type.
1609
1684
      To preserve type they needed to be handled separately.
1610
1685
    */
1611
 
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1612
 
        type == DRIZZLE_TYPE_DATE ||
1613
 
        type == DRIZZLE_TYPE_TIMESTAMP)
 
1686
    if (field::isDateTime(item->field_type()))
1614
1687
    {
1615
1688
      new_field= item->tmp_table_field_from_field_type(table, 1);
1616
1689
      /*
1658
1731
          +1: for decimal point
1659
1732
        */
1660
1733
 
1661
 
        overflow= my_decimal_precision_to_length(intg + dec, dec,
 
1734
        overflow= class_decimal_precision_to_length(intg + dec, dec,
1662
1735
                                                 item->unsigned_flag) - len;
1663
1736
 
1664
1737
        if (overflow > 0)
1799
1872
 
1800
1873
std::ostream& operator<<(std::ostream& output, const Item &item)
1801
1874
{
1802
 
  output << "Item:(";
1803
 
  output <<  item.name;
1804
 
  output << ", ";
1805
 
  output << drizzled::display::type(item.type());
1806
 
  output << ")";
 
1875
  switch (item.type())
 
1876
  {
 
1877
  case drizzled::Item::SUBSELECT_ITEM :
 
1878
  case drizzled::Item::FIELD_ITEM :
 
1879
  case drizzled::Item::SUM_FUNC_ITEM :
 
1880
  case drizzled::Item::STRING_ITEM :
 
1881
  case drizzled::Item::INT_ITEM :
 
1882
  case drizzled::Item::REAL_ITEM :
 
1883
  case drizzled::Item::NULL_ITEM :
 
1884
  case drizzled::Item::VARBIN_ITEM :
 
1885
  case drizzled::Item::COPY_STR_ITEM :
 
1886
  case drizzled::Item::FIELD_AVG_ITEM :
 
1887
  case drizzled::Item::DEFAULT_VALUE_ITEM :
 
1888
  case drizzled::Item::PROC_ITEM :
 
1889
  case drizzled::Item::COND_ITEM :
 
1890
  case drizzled::Item::REF_ITEM :
 
1891
  case drizzled::Item::FIELD_STD_ITEM :
 
1892
  case drizzled::Item::FIELD_VARIANCE_ITEM :
 
1893
  case drizzled::Item::INSERT_VALUE_ITEM :
 
1894
  case drizzled::Item::ROW_ITEM:
 
1895
  case drizzled::Item::CACHE_ITEM :
 
1896
  case drizzled::Item::TYPE_HOLDER :
 
1897
  case drizzled::Item::PARAM_ITEM :
 
1898
  case drizzled::Item::DECIMAL_ITEM :
 
1899
  case drizzled::Item::FUNC_ITEM :
 
1900
  case drizzled::Item::BOOLEAN_ITEM :
 
1901
    {
 
1902
      output << "Item:(";
 
1903
      output <<  item.full_name();
 
1904
      output << ", ";
 
1905
      output << drizzled::display::type(item.type());
 
1906
      output << ")";
 
1907
    }
 
1908
    break;
 
1909
  }
1807
1910
 
1808
1911
  return output;  // for multiple << operators.
1809
1912
}