~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-29 12:04:36 UTC
  • mto: (2257.1.1 build) (2276.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2258.
  • Revision ID: olafvdspek@gmail.com-20110329120436-vozkuer8vqgh027p
Always call assert()

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