~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
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/long.h"
45
 
#include "drizzled/field/int64_t.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
 
 
21
#include <drizzled/server_includes.h>
 
22
#include <drizzled/sql_select.h>
 
23
#include <drizzled/error.h>
 
24
#include <drizzled/show.h>
 
25
#include <drizzled/item/cmpfunc.h>
 
26
#include <drizzled/item/cache_row.h>
 
27
#include <drizzled/item/type_holder.h>
 
28
#include <drizzled/item/sum.h>
 
29
#include <drizzled/item/copy_string.h>
 
30
#include <drizzled/function/str/conv_charset.h>
 
31
#include <drizzled/virtual_column_info.h>
 
32
#include <drizzled/sql_base.h>
 
33
#include <drizzled/util/convert.h>
 
34
 
 
35
 
 
36
#include <drizzled/field/str.h>
 
37
#include <drizzled/field/longstr.h>
 
38
#include <drizzled/field/num.h>
 
39
#include <drizzled/field/blob.h>
 
40
#include <drizzled/field/enum.h>
 
41
#include <drizzled/field/null.h>
 
42
#include <drizzled/field/date.h>
 
43
#include <drizzled/field/decimal.h>
 
44
#include <drizzled/field/real.h>
 
45
#include <drizzled/field/double.h>
 
46
#include <drizzled/field/long.h>
 
47
#include <drizzled/field/int64_t.h>
 
48
#include <drizzled/field/num.h>
 
49
#include <drizzled/field/timestamp.h>
 
50
#include <drizzled/field/datetime.h>
 
51
#include <drizzled/field/varstring.h>
51
52
 
52
53
#include <math.h>
53
 
#include <algorithm>
54
 
#include <float.h>
55
 
 
56
 
using namespace std;
57
 
 
58
 
namespace drizzled
59
 
{
60
54
 
61
55
const String my_null_string("NULL", 4, default_charset_info);
62
56
 
 
57
/*****************************************************************************
 
58
** Item functions
 
59
*****************************************************************************/
 
60
 
63
61
bool Item::is_expensive_processor(unsigned char *)
64
62
{
65
63
  return false;
68
66
void Item::fix_after_pullout(Select_Lex *, Item **)
69
67
{}
70
68
 
 
69
 
71
70
Field *Item::tmp_table_field(Table *)
72
71
{
73
72
  return NULL;
74
73
}
75
74
 
 
75
 
76
76
const char *Item::full_name(void) const
77
77
{
78
78
  return name ? name : "???";
79
79
}
80
80
 
 
81
 
81
82
int64_t Item::val_int_endpoint(bool, bool *)
82
83
{
83
84
  assert(0);
84
85
  return 0;
85
86
}
86
87
 
87
 
/** @todo Make this functions class dependent */
 
88
 
 
89
/**
 
90
  @todo
 
91
    Make this functions class dependent
 
92
*/
 
93
 
88
94
bool Item::val_bool()
89
95
{
90
 
  switch(result_type()) 
 
96
  switch(result_type()) {
 
97
  case INT_RESULT:
 
98
    return val_int() != 0;
 
99
  case DECIMAL_RESULT:
91
100
  {
92
 
    case INT_RESULT:
93
 
      return val_int() != 0;
94
 
    case DECIMAL_RESULT:
95
 
    {
96
 
      my_decimal decimal_value;
97
 
      my_decimal *val= val_decimal(&decimal_value);
98
 
      if (val)
99
 
        return !my_decimal_is_zero(val);
100
 
      return false;
101
 
    }
102
 
    case REAL_RESULT:
103
 
    case STRING_RESULT:
104
 
      return val_real() != 0.0;
105
 
    case ROW_RESULT:
106
 
    default:
107
 
      assert(0);
108
 
      return false;
 
101
    my_decimal decimal_value;
 
102
    my_decimal *val= val_decimal(&decimal_value);
 
103
    if (val)
 
104
      return !my_decimal_is_zero(val);
 
105
    return false;
 
106
  }
 
107
  case REAL_RESULT:
 
108
  case STRING_RESULT:
 
109
    return val_real() != 0.0;
 
110
  case ROW_RESULT:
 
111
  default:
 
112
    assert(0);
 
113
    return false;                                   // Wrong (but safe)
109
114
  }
110
115
}
111
116
 
 
117
 
112
118
String *Item::val_string_from_real(String *str)
113
119
{
114
120
  double nr= val_real();
115
121
  if (null_value)
116
 
    return NULL;
 
122
    return NULL;                                        /* purecov: inspected */
117
123
 
118
 
  str->set_real(nr, decimals, &my_charset_bin);
 
124
  str->set_real(nr,decimals, &my_charset_bin);
119
125
  return str;
120
126
}
121
127
 
 
128
 
122
129
String *Item::val_string_from_int(String *str)
123
130
{
124
131
  int64_t nr= val_int();
129
136
  return str;
130
137
}
131
138
 
 
139
 
132
140
String *Item::val_string_from_decimal(String *str)
133
141
{
134
142
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
140
148
  return str;
141
149
}
142
150
 
 
151
 
143
152
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
144
153
{
145
154
  double nr= val_real();
150
159
  return (decimal_value);
151
160
}
152
161
 
 
162
 
153
163
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
154
164
{
155
165
  int64_t nr= val_int();
160
170
  return decimal_value;
161
171
}
162
172
 
 
173
 
163
174
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
164
175
{
165
176
  String *res;
166
177
  char *end_ptr;
167
178
  if (!(res= val_str(&str_value)))
168
 
    return NULL;
 
179
    return NULL;                                   // NULL or EOM
169
180
 
170
181
  end_ptr= (char*) res->ptr()+ res->length();
171
182
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
172
 
                     res->ptr(), 
173
 
                     res->length(), 
174
 
                     res->charset(),
 
183
                     res->ptr(), res->length(), res->charset(),
175
184
                     decimal_value) & E_DEC_BAD_NUM)
176
185
  {
177
 
    push_warning_printf(current_session, 
178
 
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
186
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
179
187
                        ER_TRUNCATED_WRONG_VALUE,
180
188
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
181
189
                        str_value.c_ptr());
183
191
  return decimal_value;
184
192
}
185
193
 
 
194
 
186
195
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
187
196
{
188
 
  assert(fixed);
 
197
  assert(fixed == 1);
189
198
  DRIZZLE_TIME ltime;
190
199
  if (get_date(&ltime, TIME_FUZZY_DATE))
191
200
  {
196
205
  return date2my_decimal(&ltime, decimal_value);
197
206
}
198
207
 
 
208
 
199
209
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
200
210
{
201
 
  assert(fixed);
 
211
  assert(fixed == 1);
202
212
  DRIZZLE_TIME ltime;
203
213
  if (get_time(&ltime))
204
214
  {
208
218
  return date2my_decimal(&ltime, decimal_value);
209
219
}
210
220
 
 
221
 
211
222
double Item::val_real_from_decimal()
212
223
{
213
224
  /* Note that fix_fields may not be called for Item_avg_field items */
219
230
  return result;
220
231
}
221
232
 
 
233
 
222
234
int64_t Item::val_int_from_decimal()
223
235
{
224
236
  /* Note that fix_fields may not be called for Item_avg_field items */
239
251
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
240
252
}
241
253
 
 
254
 
242
255
int Item::save_date_in_field(Field *field)
243
256
{
244
257
  DRIZZLE_TIME ltime;
248
261
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
249
262
}
250
263
 
251
 
/**
252
 
 * Check if the Item is null and stores the NULL or the
253
 
 * result value in the field accordingly.
254
 
 */
 
264
 
 
265
/*
 
266
  Store the string value in field directly
 
267
 
 
268
  SYNOPSIS
 
269
    Item::save_str_value_in_field()
 
270
    field   a pointer to field where to store
 
271
    result  the pointer to the string value to be stored
 
272
 
 
273
  DESCRIPTION
 
274
    The method is used by Item_*::save_in_field implementations
 
275
    when we don't need to calculate the value to store
 
276
    See Item_string::save_in_field() implementation for example
 
277
 
 
278
  IMPLEMENTATION
 
279
    Check if the Item is null and stores the NULL or the
 
280
    result value in the field accordingly.
 
281
 
 
282
  RETURN
 
283
    Nonzero value if error
 
284
*/
 
285
 
255
286
int Item::save_str_value_in_field(Field *field, String *result)
256
287
{
257
288
  if (null_value)
258
289
    return set_field_to_null(field);
259
290
  field->set_notnull();
260
 
  return field->store(result->ptr(), result->length(), collation.collation);
 
291
  return field->store(result->ptr(), result->length(),
 
292
                      collation.collation);
261
293
}
262
294
 
 
295
 
263
296
Item::Item():
264
 
  is_expensive_cache(-1),
265
 
  name(0), 
266
 
  name_length(0),
267
 
  orig_name(0), 
268
 
  max_length(0), 
269
 
  marker(0),
270
 
  decimals(0),
271
 
  fixed(false),
272
 
  maybe_null(false),
273
 
  null_value(false),
274
 
  unsigned_flag(false), 
275
 
  with_sum_func(false),
276
 
  is_autogenerated_name(true),
277
 
  with_subselect(false),
 
297
  is_expensive_cache(-1), name(0), orig_name(0), max_length(0), name_length(0),
 
298
  unsigned_flag(false), fixed(0), is_autogenerated_name(true),
278
299
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
279
300
{
 
301
  marker= 0;
 
302
  maybe_null= false;
 
303
  null_value= false;
 
304
  with_sum_func= false;
 
305
  unsigned_flag= false;
 
306
  decimals= 0;
 
307
  with_subselect= 0;
280
308
  cmp_context= (Item_result)-1;
281
309
 
282
310
  /* Put item in free list so that we can free all items at end */
283
311
  Session *session= current_session;
284
312
  next= session->free_list;
285
313
  session->free_list= this;
286
 
 
287
314
  /*
288
315
    Item constructor can be called during execution other then SQL_COM
289
316
    command => we should check session->lex->current_select on zero (session->lex
291
318
  */
292
319
  if (session->lex->current_select)
293
320
  {
294
 
    enum_parsing_place place= session->lex->current_select->parsing_place;
295
 
    if (place == SELECT_LIST || place == IN_HAVING)
 
321
    enum_parsing_place place=
 
322
      session->lex->current_select->parsing_place;
 
323
    if (place == SELECT_LIST ||
 
324
        place == IN_HAVING)
296
325
      session->lex->current_select->select_n_having_items++;
297
326
  }
298
327
}
299
328
 
 
329
/**
 
330
  Constructor used by Item_field, Item_ref & aggregate (sum)
 
331
  functions.
 
332
 
 
333
  Used for duplicating lists in processing queries with temporary
 
334
  tables.
 
335
*/
300
336
Item::Item(Session *session, Item *item):
301
337
  is_expensive_cache(-1),
302
338
  str_value(item->str_value),
303
339
  name(item->name),
304
 
  name_length(item->name_length),
305
340
  orig_name(item->orig_name),
306
341
  max_length(item->max_length),
307
342
  marker(item->marker),
308
343
  decimals(item->decimals),
309
 
  fixed(item->fixed),
310
344
  maybe_null(item->maybe_null),
311
345
  null_value(item->null_value),
312
346
  unsigned_flag(item->unsigned_flag),
313
347
  with_sum_func(item->with_sum_func),
314
 
  is_autogenerated_name(item->is_autogenerated_name),
315
 
  with_subselect(item->with_subselect),
 
348
  fixed(item->fixed),
316
349
  collation(item->collation),
317
350
  cmp_context(item->cmp_context)
318
351
{
319
 
  /* Put this item in the session's free list */
320
 
  next= session->free_list;
 
352
  next= session->free_list;                             // Put in free list
321
353
  session->free_list= this;
322
354
}
323
355
 
324
 
uint32_t Item::float_length(uint32_t decimals_par) const
325
 
{
326
 
  return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
327
 
}
328
356
 
329
357
uint32_t Item::decimal_precision() const
330
358
{
331
359
  Item_result restype= result_type();
332
360
 
333
361
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
334
 
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
335
 
               (uint32_t) DECIMAL_MAX_PRECISION);
336
 
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
 
362
    return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
363
               (unsigned int)DECIMAL_MAX_PRECISION);
 
364
  return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
337
365
}
338
366
 
 
367
 
339
368
int Item::decimal_int_part() const
340
369
{
341
370
  return my_decimal_int_part(decimal_precision(), decimals);
342
371
}
343
372
 
 
373
 
344
374
void Item::print(String *str, enum_query_type)
345
375
{
346
376
  str->append(full_name());
347
377
}
348
378
 
 
379
 
349
380
void Item::print_item_w_name(String *str, enum_query_type query_type)
350
381
{
351
382
  print(str, query_type);
357
388
  }
358
389
}
359
390
 
 
391
 
360
392
void Item::split_sum_func(Session *, Item **, List<Item> &)
361
393
{}
362
394
 
 
395
 
363
396
void Item::cleanup()
364
397
{
365
 
  fixed= false;
 
398
  fixed=0;
366
399
  marker= 0;
367
400
  if (orig_name)
368
401
    name= orig_name;
369
402
  return;
370
403
}
371
404
 
 
405
 
 
406
/**
 
407
  cleanup() item if it is 'fixed'.
 
408
 
 
409
  @param arg   a dummy parameter, is not used here
 
410
*/
 
411
 
 
412
bool Item::cleanup_processor(unsigned char *)
 
413
{
 
414
  if (fixed)
 
415
    cleanup();
 
416
  return false;
 
417
}
 
418
 
 
419
 
 
420
/**
 
421
  rename item (used for views, cleanup() return original name).
 
422
 
 
423
  @param new_name       new name of item;
 
424
*/
 
425
 
372
426
void Item::rename(char *new_name)
373
427
{
374
428
  /*
375
429
    we can compare pointers to names here, because if name was not changed,
376
430
    pointer will be same
377
431
  */
378
 
  if (! orig_name && new_name != name)
 
432
  if (!orig_name && new_name != name)
379
433
    orig_name= name;
380
434
  name= new_name;
381
435
}
382
436
 
 
437
 
 
438
/**
 
439
  Traverse item tree possibly transforming it (replacing items).
 
440
 
 
441
  If you don't need to transform an item tree, but only traverse
 
442
  it, please use Item::walk() instead.
 
443
 
 
444
  @param transformer    functor that performs transformation of a subtree
 
445
  @param arg            opaque argument passed to the functor
 
446
 
 
447
  @return
 
448
    Returns pointer to the new subtree root.  Session::change_item_tree()
 
449
    should be called for it if transformation took place, i.e. if a
 
450
    pointer to newly allocated item is returned.
 
451
*/
 
452
 
383
453
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
384
454
{
385
455
  return (this->*transformer)(arg);
386
456
}
387
457
 
 
458
 
388
459
bool Item::check_cols(uint32_t c)
389
460
{
390
461
  if (c != 1)
391
462
  {
392
463
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
393
 
    return true;
 
464
    return 1;
394
465
  }
395
466
  return false;
396
467
}
397
468
 
 
469
 
398
470
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
399
471
{
400
472
  if (!length)
407
479
  if (cs->ctype)
408
480
  {
409
481
    uint32_t orig_len= length;
410
 
    while (length && ! my_isgraph(cs, *str))
411
 
    {
412
 
      /* Fix problem with yacc */
 
482
    /*
 
483
      This will probably need a better implementation in the future:
 
484
      a function in CHARSET_INFO structure.
 
485
    */
 
486
    while (length && !my_isgraph(cs,*str))
 
487
    {                                           // Fix problem with yacc
413
488
      length--;
414
489
      str++;
415
490
    }
416
 
    if (orig_len != length && ! is_autogenerated_name)
 
491
    if (orig_len != length && !is_autogenerated_name)
417
492
    {
418
493
      if (length == 0)
419
 
        push_warning_printf(current_session, 
420
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
421
 
                            ER_NAME_BECOMES_EMPTY, 
422
 
                            ER(ER_NAME_BECOMES_EMPTY),
 
494
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
495
                            ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
423
496
                            str + length - orig_len);
424
497
      else
425
 
        push_warning_printf(current_session, 
426
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
427
 
                            ER_REMOVED_SPACES, 
428
 
                            ER(ER_REMOVED_SPACES),
 
498
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
499
                            ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
429
500
                            str + length - orig_len);
430
501
    }
431
502
  }
432
 
  name= memory::sql_strmake(str, length);
 
503
  if (!my_charset_same(cs, system_charset_info))
 
504
  {
 
505
    size_t res_length;
 
506
    name= sql_strmake_with_convert(str, name_length= length, cs,
 
507
                                   length, system_charset_info,
 
508
                                   &res_length);
 
509
  }
 
510
  else
 
511
      name= sql_strmake(str, length);
433
512
}
434
513
 
 
514
 
 
515
/**
 
516
  @details
 
517
  This function is called when:
 
518
  - Comparing items in the WHERE clause (when doing where optimization)
 
519
  - When trying to find an order_st BY/GROUP BY item in the SELECT part
 
520
*/
 
521
 
435
522
bool Item::eq(const Item *item, bool) const
436
523
{
437
524
  /*
439
526
    for all basic constants we have special checks, and Item_param's
440
527
    type() can be only among basic constant types.
441
528
  */
442
 
  return type() == item->type() && 
443
 
         name && 
444
 
         item->name &&
445
 
         ! my_strcasecmp(system_charset_info, name, item->name);
 
529
  return type() == item->type() && name && item->name &&
 
530
    !my_strcasecmp(system_charset_info,name,item->name);
446
531
}
447
532
 
 
533
 
448
534
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
449
535
{
450
536
  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
451
537
  return conv->safe ? conv : NULL;
452
538
}
453
539
 
 
540
 
 
541
/**
 
542
  Get the value of the function as a DRIZZLE_TIME structure.
 
543
  As a extra convenience the time structure is reset on error!
 
544
*/
 
545
 
454
546
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
455
547
{
456
548
  if (result_type() == STRING_RESULT)
469
561
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
470
562
    {
471
563
      char buff[22], *end;
472
 
      end= internal::int64_t10_to_str(value, buff, -10);
 
564
      end= int64_t10_to_str(value, buff, -10);
473
565
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
474
566
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
475
567
                                   NULL);
483
575
  return true;
484
576
}
485
577
 
 
578
/**
 
579
  Get time of first argument.\
 
580
 
 
581
  As a extra convenience the time structure is reset on error!
 
582
*/
 
583
 
486
584
bool Item::get_time(DRIZZLE_TIME *ltime)
487
585
{
488
586
  char buff[40];
496
594
  return false;
497
595
}
498
596
 
 
597
 
499
598
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
500
599
{
501
600
  return get_date(ltime,fuzzydate);
502
601
}
503
602
 
 
603
 
504
604
bool Item::is_null()
505
605
{
506
606
  return false;
507
607
}
508
608
 
 
609
 
509
610
void Item::update_null_value ()
510
611
{
511
612
  (void) val_int();
512
613
}
513
614
 
 
615
 
514
616
void Item::top_level_item(void)
515
617
{}
516
618
 
 
619
 
517
620
void Item::set_result_field(Field *)
518
621
{}
519
622
 
 
623
 
520
624
bool Item::is_result_field(void)
521
625
{
522
626
  return false;
523
627
}
524
628
 
 
629
 
525
630
bool Item::is_bool_func(void)
526
631
{
527
632
  return false;
528
633
}
529
634
 
 
635
 
530
636
void Item::save_in_result_field(bool)
531
637
{}
532
638
 
 
639
 
533
640
void Item::no_rows_in_result(void)
534
641
{}
535
642
 
 
643
 
536
644
Item *Item::copy_or_same(Session *)
537
645
{
538
646
  return this;
539
647
}
540
648
 
 
649
 
541
650
Item *Item::copy_andor_structure(Session *)
542
651
{
543
652
  return this;
544
653
}
545
654
 
 
655
 
546
656
Item *Item::real_item(void)
547
657
{
548
658
  return this;
549
659
}
550
660
 
 
661
 
551
662
const Item *Item::real_item(void) const
552
663
{
553
664
  return this;
554
665
}
555
666
 
 
667
 
556
668
Item *Item::get_tmp_table_item(Session *session)
557
669
{
558
670
  return copy_or_same(session);
559
671
}
560
672
 
 
673
 
561
674
const CHARSET_INFO *Item::default_charset()
562
675
{
563
676
  return current_session->variables.getCollation();
564
677
}
565
678
 
 
679
 
566
680
const CHARSET_INFO *Item::compare_collation()
567
681
{
568
682
  return NULL;
569
683
}
570
684
 
 
685
 
571
686
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
572
687
{
573
688
  return (this->*processor)(arg);
574
689
}
575
690
 
576
 
Item* Item::compile(Item_analyzer analyzer, 
577
 
                    unsigned char **arg_p,
578
 
                    Item_transformer transformer, 
579
 
                    unsigned char *arg_t)
 
691
 
 
692
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
 
693
                    Item_transformer transformer, unsigned char *arg_t)
580
694
{
581
695
  if ((this->*analyzer) (arg_p))
582
696
    return ((this->*transformer) (arg_t));
583
697
  return NULL;
584
698
}
585
699
 
 
700
 
586
701
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
587
702
{
588
703
  (*traverser)(this, arg);
589
704
}
590
705
 
 
706
 
591
707
bool Item::remove_dependence_processor(unsigned char *)
592
708
{
593
709
  return false;
594
710
}
595
711
 
 
712
 
596
713
bool Item::remove_fixed(unsigned char *)
597
714
{
598
 
  fixed= false;
 
715
  fixed= 0;
599
716
  return false;
600
717
}
601
718
 
 
719
 
602
720
bool Item::collect_item_field_processor(unsigned char *)
603
721
{
604
722
  return false;
605
723
}
606
724
 
 
725
 
607
726
bool Item::find_item_in_field_list_processor(unsigned char *)
608
727
{
609
728
  return false;
610
729
}
611
730
 
 
731
 
612
732
bool Item::change_context_processor(unsigned char *)
613
733
{
614
734
  return false;
615
735
}
616
736
 
 
737
bool Item::reset_query_id_processor(unsigned char *)
 
738
{
 
739
  return false;
 
740
}
 
741
 
 
742
 
617
743
bool Item::register_field_in_read_map(unsigned char *)
618
744
{
619
745
  return false;
620
746
}
621
747
 
 
748
 
 
749
bool Item::register_field_in_bitmap(unsigned char *)
 
750
{
 
751
  return false;
 
752
}
 
753
 
 
754
 
622
755
bool Item::subst_argument_checker(unsigned char **arg)
623
756
{
624
757
  if (*arg)
626
759
  return true;
627
760
}
628
761
 
 
762
 
 
763
bool Item::check_vcol_func_processor(unsigned char *)
 
764
{
 
765
  return true;
 
766
}
 
767
 
 
768
 
629
769
Item *Item::equal_fields_propagator(unsigned char *)
630
770
{
631
771
  return this;
632
772
}
633
773
 
 
774
 
634
775
bool Item::set_no_const_sub(unsigned char *)
635
776
{
636
777
  return false;
637
778
}
638
779
 
 
780
 
639
781
Item *Item::replace_equal_field(unsigned char *)
640
782
{
641
783
  return this;
642
784
}
643
785
 
 
786
 
644
787
uint32_t Item::cols()
645
788
{
646
789
  return 1;
647
790
}
648
791
 
 
792
 
649
793
Item* Item::element_index(uint32_t)
650
794
{
651
795
  return this;
652
796
}
653
797
 
 
798
 
654
799
Item** Item::addr(uint32_t)
655
800
{
656
801
  return NULL;
657
802
}
658
803
 
 
804
 
659
805
bool Item::null_inside()
660
806
{
661
807
  return false;
662
808
}
663
809
 
 
810
 
664
811
void Item::bring_value()
665
812
{}
666
813
 
 
814
 
667
815
Item *Item::neg_transformer(Session *)
668
816
{
669
817
  return NULL;
670
818
}
671
819
 
 
820
 
672
821
Item *Item::update_value_transformer(unsigned char *)
673
822
{
674
823
  return this;
675
824
}
676
825
 
 
826
 
677
827
void Item::delete_self()
678
828
{
679
829
  cleanup();
685
835
  return false;
686
836
}
687
837
 
 
838
 
688
839
bool Item::is_expensive()
689
840
{
690
841
  if (is_expensive_cache < 0)
693
844
  return test(is_expensive_cache);
694
845
}
695
846
 
 
847
 
 
848
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
 
849
{
 
850
  int res;
 
851
  Table *table= field->table;
 
852
  Session *session= table->in_use;
 
853
  enum_check_fields tmp= session->count_cuted_fields;
 
854
  ulong sql_mode= session->variables.sql_mode;
 
855
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
 
856
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
857
  res= save_in_field(field, no_conversions);
 
858
  session->count_cuted_fields= tmp;
 
859
  session->variables.sql_mode= sql_mode;
 
860
  return res;
 
861
}
 
862
 
 
863
 
696
864
/*
697
865
 need a special class to adjust printing : references to aggregate functions
698
866
 must not be printed as refs because the aggregate functions that are added to
714
882
  }
715
883
};
716
884
 
 
885
 
 
886
/**
 
887
  Move SUM items out from item tree and replace with reference.
 
888
 
 
889
  @param session                        Thread handler
 
890
  @param ref_pointer_array      Pointer to array of reference fields
 
891
  @param fields         All fields in select
 
892
  @param ref                    Pointer to item
 
893
  @param skip_registered       <=> function be must skipped for registered
 
894
                               SUM items
 
895
 
 
896
  @note
 
897
    This is from split_sum_func() for items that should be split
 
898
 
 
899
    All found SUM items are added FIRST in the fields list and
 
900
    we replace the item with a reference.
 
901
 
 
902
    session->fatal_error() may be called if we are out of memory
 
903
*/
 
904
 
717
905
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
718
906
                          List<Item> &fields, Item **ref,
719
907
                          bool skip_registered)
720
908
{
721
909
  /* An item of type Item_sum  is registered <=> ref_by != 0 */
722
 
  if (type() == SUM_FUNC_ITEM && 
723
 
      skip_registered &&
 
910
  if (type() == SUM_FUNC_ITEM && skip_registered &&
724
911
      ((Item_sum *) this)->ref_by)
725
912
    return;
726
 
 
727
913
  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
728
914
      (type() == FUNC_ITEM &&
729
915
       (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
753
939
    ref_pointer_array[el]= real_itm;
754
940
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
755
941
                                           ref_pointer_array + el, 0, name)))
756
 
      return; /* fatal_error is set */
 
942
      return;                                   // fatal_error is set
757
943
    if (type() == SUM_FUNC_ITEM)
758
944
      item_ref->depended_from= ((Item_sum *) this)->depended_from();
759
945
    fields.push_front(real_itm);
764
950
/*
765
951
  Functions to convert item to field (for send_fields)
766
952
*/
 
953
 
 
954
/* ARGSUSED */
767
955
bool Item::fix_fields(Session *, Item **)
768
956
{
769
 
  /* We do not check fields which are fixed during construction */
770
 
  assert(! fixed || basic_const_item());
771
 
  fixed= true;
 
957
 
 
958
  // We do not check fields which are fixed during construction
 
959
  assert(fixed == 0 || basic_const_item());
 
960
  fixed= 1;
772
961
  return false;
773
962
}
774
963
 
 
964
 
 
965
/**
 
966
  Mark item and Select_Lexs as dependent if item was resolved in
 
967
  outer SELECT.
 
968
 
 
969
  @param session             thread handler
 
970
  @param last            select from which current item depend
 
971
  @param current         current select
 
972
  @param resolved_item   item which was resolved in outer SELECT(for warning)
 
973
  @param mark_item       item which should be marked (can be differ in case of
 
974
                         substitution)
 
975
*/
 
976
 
775
977
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
776
978
                              Item_ident *resolved_item,
777
979
                              Item_ident *mark_item)
787
989
  if (session->lex->describe & DESCRIBE_EXTENDED)
788
990
  {
789
991
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
790
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
 
992
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
791
993
            db_name, (db_name[0] ? "." : ""),
792
994
            table_name, (table_name [0] ? "." : ""),
793
995
            resolved_item->field_name,
797
999
  }
798
1000
}
799
1001
 
 
1002
 
 
1003
/**
 
1004
  Mark range of selects and resolved identifier (field/reference)
 
1005
  item as dependent.
 
1006
 
 
1007
  @param session             thread handler
 
1008
  @param last_select     select where resolved_item was resolved
 
1009
  @param current_sel     current select (select where resolved_item was placed)
 
1010
  @param found_field     field which was found during resolving
 
1011
  @param found_item      Item which was found during resolving (if resolved
 
1012
                         identifier belongs to VIEW)
 
1013
  @param resolved_item   Identifier which was resolved
 
1014
 
 
1015
  @note
 
1016
    We have to mark all items between current_sel (including) and
 
1017
    last_select (excluding) as dependend (select before last_select should
 
1018
    be marked with actual table mask used by resolved item, all other with
 
1019
    OUTER_REF_TABLE_BIT) and also write dependence information to Item of
 
1020
    resolved identifier.
 
1021
*/
 
1022
 
800
1023
void mark_select_range_as_dependent(Session *session,
801
1024
                                    Select_Lex *last_select,
802
1025
                                    Select_Lex *current_sel,
813
1036
  for (; previous_select->outer_select() != last_select;
814
1037
       previous_select= previous_select->outer_select())
815
1038
  {
816
 
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
 
1039
    Item_subselect *prev_subselect_item=
 
1040
      previous_select->master_unit()->item;
817
1041
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
818
1042
    prev_subselect_item->const_item_cache= 0;
819
1043
  }
820
1044
  {
821
 
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
 
1045
    Item_subselect *prev_subselect_item=
 
1046
      previous_select->master_unit()->item;
822
1047
    Item_ident *dependent= resolved_item;
823
1048
    if (found_field == view_ref_found)
824
1049
    {
825
1050
      Item::Type type= found_item->type();
826
 
      prev_subselect_item->used_tables_cache|= found_item->used_tables();
 
1051
      prev_subselect_item->used_tables_cache|=
 
1052
        found_item->used_tables();
827
1053
      dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
828
1054
                  (Item_ident*) found_item :
829
1055
                  0);
830
1056
    }
831
1057
    else
832
 
      prev_subselect_item->used_tables_cache|= found_field->table->map;
 
1058
      prev_subselect_item->used_tables_cache|=
 
1059
        found_field->table->map;
833
1060
    prev_subselect_item->const_item_cache= 0;
834
1061
    mark_as_dependent(session, last_select, current_sel, resolved_item,
835
1062
                      dependent);
836
1063
  }
837
1064
}
838
1065
 
 
1066
 
839
1067
/**
840
1068
  Search a GROUP BY clause for a field with a certain name.
841
1069
 
850
1078
    - the found item on success
851
1079
    - NULL if find_item is not in group_list
852
1080
*/
 
1081
 
853
1082
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
854
1083
{
855
1084
  const char *db_name;
856
1085
  const char *table_name;
857
1086
  const char *field_name;
858
 
  order_st *found_group= NULL;
859
 
  int found_match_degree= 0;
 
1087
  order_st      *found_group= NULL;
 
1088
  int         found_match_degree= 0;
860
1089
  Item_ident *cur_field;
861
 
  int cur_match_degree= 0;
862
 
  char name_buff[NAME_LEN+1];
 
1090
  int         cur_match_degree= 0;
 
1091
  char        name_buff[NAME_LEN+1];
863
1092
 
864
1093
  if (find_item->type() == Item::FIELD_ITEM ||
865
1094
      find_item->type() == Item::REF_ITEM)
866
1095
  {
867
 
    db_name= ((Item_ident*) find_item)->db_name;
 
1096
    db_name=    ((Item_ident*) find_item)->db_name;
868
1097
    table_name= ((Item_ident*) find_item)->table_name;
869
1098
    field_name= ((Item_ident*) find_item)->field_name;
870
1099
  }
871
1100
  else
872
1101
    return NULL;
873
1102
 
874
 
  if (db_name)
 
1103
  if (db_name && lower_case_table_names)
875
1104
  {
876
1105
    /* Convert database to lower case for comparison */
877
1106
    strncpy(name_buff, db_name, sizeof(name_buff)-1);
890
1119
 
891
1120
      assert(cur_field->field_name != 0);
892
1121
 
893
 
      if (! my_strcasecmp(system_charset_info, cur_field->field_name, field_name))
 
1122
      if (!my_strcasecmp(system_charset_info,
 
1123
                         cur_field->field_name, field_name))
894
1124
        ++cur_match_degree;
895
1125
      else
896
1126
        continue;
906
1136
        if (cur_field->db_name && db_name)
907
1137
        {
908
1138
          /* If field_name is also qualified by a database name. */
909
 
          if (strcasecmp(cur_field->db_name, db_name))
 
1139
          if (strcmp(cur_field->db_name, db_name))
910
1140
            /* Same field names, different databases. */
911
1141
            return NULL;
912
1142
          ++cur_match_degree;
918
1148
        found_match_degree= cur_match_degree;
919
1149
        found_group= cur_group;
920
1150
      }
921
 
      else if (found_group &&
922
 
               (cur_match_degree == found_match_degree) &&
 
1151
      else if (found_group && (cur_match_degree == found_match_degree) &&
923
1152
               ! (*(found_group->item))->eq(cur_field, 0))
924
1153
      {
925
1154
        /*
927
1156
          best match, they must reference the same column, otherwise the field
928
1157
          is ambiguous.
929
1158
        */
930
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where);
 
1159
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
 
1160
                 find_item->full_name(), current_session->where);
931
1161
        return NULL;
932
1162
      }
933
1163
    }
939
1169
    return NULL;
940
1170
}
941
1171
 
942
 
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
 
1172
 
 
1173
/**
 
1174
  Resolve a column reference in a sub-select.
 
1175
 
 
1176
  Resolve a column reference (usually inside a HAVING clause) against the
 
1177
  SELECT and GROUP BY clauses of the query described by 'select'. The name
 
1178
  resolution algorithm searches both the SELECT and GROUP BY clauses, and in
 
1179
  case of a name conflict prefers GROUP BY column names over SELECT names. If
 
1180
  both clauses contain different fields with the same names, a warning is
 
1181
  issued that name of 'ref' is ambiguous. We extend ANSI SQL in that when no
 
1182
  GROUP BY column is found, then a HAVING name is resolved as a possibly
 
1183
  derived SELECT column. This extension is allowed only if the
 
1184
  MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled.
 
1185
 
 
1186
  @param session     current thread
 
1187
  @param ref     column reference being resolved
 
1188
  @param select  the select that ref is resolved against
 
1189
 
 
1190
  @note
 
1191
    The resolution procedure is:
 
1192
    - Search for a column or derived column named col_ref_i [in table T_j]
 
1193
    in the SELECT clause of Q.
 
1194
    - Search for a column named col_ref_i [in table T_j]
 
1195
    in the GROUP BY clause of Q.
 
1196
    - If found different columns with the same name in GROUP BY and SELECT
 
1197
    - issue a warning and return the GROUP BY column,
 
1198
    - otherwise
 
1199
    - if the MODE_ONLY_FULL_GROUP_BY mode is enabled return error
 
1200
    - else return the found SELECT column.
 
1201
 
 
1202
 
 
1203
  @return
 
1204
    - NULL - there was an error, and the error was already reported
 
1205
    - not_found_item - the item was not resolved, no error was reported
 
1206
    - resolved item - if the item was resolved
 
1207
*/
 
1208
 
 
1209
Item**
 
1210
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
943
1211
{
944
1212
  Item **group_by_ref= NULL;
945
1213
  Item **select_ref= NULL;
999
1267
  return (Item**) not_found_item;
1000
1268
}
1001
1269
 
1002
 
void Item::init_make_field(SendField *tmp_field,
 
1270
void Item::init_make_field(Send_field *tmp_field,
1003
1271
                           enum enum_field_types field_type_arg)
1004
1272
{
1005
1273
  char *empty_name= (char*) "";
1006
 
  tmp_field->db_name=   empty_name;
1007
 
  tmp_field->org_table_name= empty_name;
1008
 
  tmp_field->org_col_name= empty_name;
1009
 
  tmp_field->table_name= empty_name;
1010
 
  tmp_field->col_name= name;
1011
 
  tmp_field->charsetnr= collation.collation->number;
1012
 
  tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) |
1013
 
                    (my_binary_compare(collation.collation) ?
1014
 
                      BINARY_FLAG : 0);
1015
 
  tmp_field->type= field_type_arg;
1016
 
  tmp_field->length= max_length;
1017
 
  tmp_field->decimals= decimals;
 
1274
  tmp_field->db_name=           empty_name;
 
1275
  tmp_field->org_table_name=    empty_name;
 
1276
  tmp_field->org_col_name=      empty_name;
 
1277
  tmp_field->table_name=        empty_name;
 
1278
  tmp_field->col_name=          name;
 
1279
  tmp_field->charsetnr=         collation.collation->number;
 
1280
  tmp_field->flags=             (maybe_null ? 0 : NOT_NULL_FLAG) |
 
1281
                                (my_binary_compare(collation.collation) ?
 
1282
                                 BINARY_FLAG : 0);
 
1283
  tmp_field->type=              field_type_arg;
 
1284
  tmp_field->length=max_length;
 
1285
  tmp_field->decimals=decimals;
1018
1286
}
1019
1287
 
1020
 
void Item::make_field(SendField *tmp_field)
 
1288
void Item::make_field(Send_field *tmp_field)
1021
1289
{
1022
1290
  init_make_field(tmp_field, field_type());
1023
1291
}
1024
1292
 
 
1293
 
1025
1294
enum_field_types Item::string_field_type() const
1026
1295
{
1027
1296
  enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
1033
1302
enum_field_types Item::field_type() const
1034
1303
{
1035
1304
  switch (result_type()) {
1036
 
  case STRING_RESULT:  
1037
 
    return string_field_type();
1038
 
  case INT_RESULT:     
1039
 
    return DRIZZLE_TYPE_LONGLONG;
1040
 
  case DECIMAL_RESULT: 
1041
 
    return DRIZZLE_TYPE_DECIMAL;
1042
 
  case REAL_RESULT:    
1043
 
    return DRIZZLE_TYPE_DOUBLE;
 
1305
  case STRING_RESULT:  return string_field_type();
 
1306
  case INT_RESULT:     return DRIZZLE_TYPE_LONGLONG;
 
1307
  case DECIMAL_RESULT: return DRIZZLE_TYPE_NEWDECIMAL;
 
1308
  case REAL_RESULT:    return DRIZZLE_TYPE_DOUBLE;
1044
1309
  case ROW_RESULT:
1045
1310
  default:
1046
1311
    assert(0);
1048
1313
  }
1049
1314
}
1050
1315
 
 
1316
 
1051
1317
bool Item::is_datetime()
1052
1318
{
1053
1319
  switch (field_type())
1062
1328
  return false;
1063
1329
}
1064
1330
 
 
1331
 
1065
1332
String *Item::check_well_formed_result(String *str, bool send_error)
1066
1333
{
1067
1334
  /* Check whether we got a well-formed string */
1095
1362
  return str;
1096
1363
}
1097
1364
 
 
1365
/*
 
1366
  Compare two items using a given collation
 
1367
 
 
1368
  SYNOPSIS
 
1369
    eq_by_collation()
 
1370
    item               item to compare with
 
1371
    binary_cmp         true <-> compare as binaries
 
1372
    cs                 collation to use when comparing strings
 
1373
 
 
1374
  DESCRIPTION
 
1375
    This method works exactly as Item::eq if the collation cs coincides with
 
1376
    the collation of the compared objects. Otherwise, first the collations that
 
1377
    differ from cs are replaced for cs and then the items are compared by
 
1378
    Item::eq. After the comparison the original collations of items are
 
1379
    restored.
 
1380
 
 
1381
  RETURN
 
1382
    1    compared items has been detected as equal
 
1383
    0    otherwise
 
1384
*/
 
1385
 
1098
1386
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
1099
1387
{
1100
1388
  const CHARSET_INFO *save_cs= 0;
1117
1405
  return res;
1118
1406
}
1119
1407
 
 
1408
 
 
1409
/**
 
1410
  Create a field to hold a string value from an item.
 
1411
 
 
1412
  If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
 
1413
  If max_length > 0 create a varchar @n
 
1414
  If max_length == 0 create a CHAR(0)
 
1415
 
 
1416
  @param table          Table for which the field is created
 
1417
*/
 
1418
 
1120
1419
Field *Item::make_string_field(Table *table)
1121
1420
{
1122
1421
  Field *field;
1133
1432
  return field;
1134
1433
}
1135
1434
 
 
1435
 
 
1436
/**
 
1437
  Create a field based on field_type of argument.
 
1438
 
 
1439
  For now, this is only used to create a field for
 
1440
  IFNULL(x,something) and time functions
 
1441
 
 
1442
  @retval
 
1443
    NULL  error
 
1444
  @retval
 
1445
    \#    Created field
 
1446
*/
 
1447
 
1136
1448
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
1137
1449
{
1138
1450
  /*
1142
1454
  Field *field;
1143
1455
 
1144
1456
  switch (field_type()) {
1145
 
  case DRIZZLE_TYPE_DECIMAL:
1146
 
    field= new Field_decimal((unsigned char*) 0,
1147
 
                                 max_length,
1148
 
                                 null_ptr,
1149
 
                                 0,
1150
 
                                 Field::NONE,
1151
 
                                 name,
1152
 
                                 decimals,
1153
 
                                 0,
 
1457
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1458
    field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
 
1459
                                 Field::NONE, name, decimals, 0,
1154
1460
                                 unsigned_flag);
1155
1461
    break;
1156
1462
  case DRIZZLE_TYPE_LONG:
1166
1472
                            name, decimals, 0, unsigned_flag);
1167
1473
    break;
1168
1474
  case DRIZZLE_TYPE_NULL:
1169
 
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
 
1475
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
 
1476
                          name, &my_charset_bin);
1170
1477
    break;
1171
1478
  case DRIZZLE_TYPE_DATE:
1172
1479
    field= new Field_date(maybe_null, name, &my_charset_bin);
1197
1504
  return field;
1198
1505
}
1199
1506
 
 
1507
 
1200
1508
/*
1201
1509
  This implementation can lose str_value content, so if the
1202
1510
  Item uses str_value to store something, it should
1203
1511
  reimplement it's ::save_in_field() as Item_string, for example, does
1204
1512
*/
 
1513
 
1205
1514
int Item::save_in_field(Field *field, bool no_conversions)
1206
1515
{
1207
1516
  int error;
1261
1570
  return error;
1262
1571
}
1263
1572
 
1264
 
/**
1265
 
  Check if an item is a constant one and can be cached.
1266
 
 
1267
 
  @param arg [out] TRUE <=> Cache this item.
1268
 
 
1269
 
  @return TRUE  Go deeper in item tree.
1270
 
  @return FALSE Don't go deeper in item tree.
1271
 
*/
1272
 
 
1273
 
bool Item::cache_const_expr_analyzer(unsigned char **arg)
1274
 
{
1275
 
  bool *cache_flag= (bool*)*arg;
1276
 
  if (!*cache_flag)
1277
 
  {
1278
 
    Item *item= real_item();
1279
 
    /*
1280
 
      Cache constant items unless it's a basic constant, constant field or
1281
 
      a subselect (they use their own cache).
1282
 
    */
1283
 
    if (const_item() &&
1284
 
        !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1285
 
          item->type() == SUBSELECT_ITEM ||
1286
 
           /*
1287
 
             Do not cache GET_USER_VAR() function as its const_item() may
1288
 
             return TRUE for the current thread but it still may change
1289
 
             during the execution.
1290
 
           */
1291
 
          (item->type() == Item::FUNC_ITEM &&
1292
 
           ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1293
 
      *cache_flag= true;
1294
 
    return true;
1295
 
  }
1296
 
  return false;
1297
 
}
1298
 
 
1299
 
/**
1300
 
  Cache item if needed.
1301
 
 
1302
 
  @param arg   TRUE <=> Cache this item.
1303
 
 
1304
 
  @return cache if cache needed.
1305
 
  @return this otherwise.
1306
 
*/
1307
 
 
1308
 
Item* Item::cache_const_expr_transformer(unsigned char *arg)
1309
 
{
1310
 
  if (*(bool*)arg)
1311
 
  {
1312
 
    *((bool*)arg)= false;
1313
 
    Item_cache *cache= Item_cache::get_cache(this);
1314
 
    if (!cache)
1315
 
      return NULL;
1316
 
    cache->setup(this);
1317
 
    cache->store(this);
1318
 
    return cache;
1319
 
  }
1320
 
  return this;
1321
 
}
1322
 
 
1323
 
bool Item::send(plugin::Client *client, String *buffer)
 
1573
 
 
1574
/**
 
1575
  This is only called from items that is not of type item_field.
 
1576
*/
 
1577
 
 
1578
bool Item::send(Protocol *protocol, String *buffer)
1324
1579
{
1325
1580
  bool result= false;
1326
1581
  enum_field_types f_type;
1331
1586
  case DRIZZLE_TYPE_ENUM:
1332
1587
  case DRIZZLE_TYPE_BLOB:
1333
1588
  case DRIZZLE_TYPE_VARCHAR:
1334
 
  case DRIZZLE_TYPE_DECIMAL:
 
1589
  case DRIZZLE_TYPE_NEWDECIMAL:
1335
1590
  {
1336
1591
    String *res;
1337
1592
    if ((res=val_str(buffer)))
1338
 
      result= client->store(res->ptr(),res->length());
 
1593
      result= protocol->store(res->ptr(),res->length(),res->charset());
1339
1594
    break;
1340
1595
  }
1341
1596
  case DRIZZLE_TYPE_LONG:
1343
1598
    int64_t nr;
1344
1599
    nr= val_int();
1345
1600
    if (!null_value)
1346
 
      result= client->store((int32_t)nr);
 
1601
      result= protocol->store((int32_t)nr);
1347
1602
    break;
1348
1603
  }
1349
1604
  case DRIZZLE_TYPE_LONGLONG:
1353
1608
    if (!null_value)
1354
1609
    {
1355
1610
      if (unsigned_flag)
1356
 
        result= client->store((uint64_t)nr);
 
1611
        result= protocol->store((uint64_t)nr);
1357
1612
      else
1358
 
        result= client->store((int64_t)nr);
 
1613
        result= protocol->store((int64_t)nr);
1359
1614
    }
1360
1615
    break;
1361
1616
  }
1363
1618
  {
1364
1619
    double nr= val_real();
1365
1620
    if (!null_value)
1366
 
      result= client->store(nr, decimals, buffer);
 
1621
      result= protocol->store(nr, decimals, buffer);
1367
1622
    break;
1368
1623
  }
1369
1624
  case DRIZZLE_TYPE_DATETIME:
1372
1627
    DRIZZLE_TIME tm;
1373
1628
    get_date(&tm, TIME_FUZZY_DATE);
1374
1629
    if (!null_value)
1375
 
      result= client->store(&tm);
 
1630
      result= protocol->store(&tm);
1376
1631
    break;
1377
1632
  }
1378
1633
  }
1379
1634
  if (null_value)
1380
 
    result= client->store();
 
1635
    result= protocol->store();
1381
1636
  return result;
1382
1637
}
1383
1638
 
 
1639
 
 
1640
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
 
1641
{
 
1642
  return item->type() == DEFAULT_VALUE_ITEM &&
 
1643
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
 
1644
}
 
1645
 
 
1646
 
 
1647
bool Item_default_value::fix_fields(Session *session, Item **)
 
1648
{
 
1649
  Item *real_arg;
 
1650
  Item_field *field_arg;
 
1651
  Field *def_field;
 
1652
  assert(fixed == 0);
 
1653
 
 
1654
  if (!arg)
 
1655
  {
 
1656
    fixed= 1;
 
1657
    return false;
 
1658
  }
 
1659
  if (!arg->fixed && arg->fix_fields(session, &arg))
 
1660
    goto error;
 
1661
 
 
1662
 
 
1663
  real_arg= arg->real_item();
 
1664
  if (real_arg->type() != FIELD_ITEM)
 
1665
  {
 
1666
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
 
1667
    goto error;
 
1668
  }
 
1669
 
 
1670
  field_arg= (Item_field *)real_arg;
 
1671
  if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
 
1672
  {
 
1673
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
 
1674
    goto error;
 
1675
  }
 
1676
  if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
 
1677
    goto error;
 
1678
  memcpy(def_field, field_arg->field, field_arg->field->size_of());
 
1679
  def_field->move_field_offset((my_ptrdiff_t)
 
1680
                               (def_field->table->s->default_values -
 
1681
                                def_field->table->record[0]));
 
1682
  set_field(def_field);
 
1683
  return false;
 
1684
 
 
1685
error:
 
1686
  context->process_error(session);
 
1687
  return true;
 
1688
}
 
1689
 
 
1690
 
 
1691
void Item_default_value::print(String *str, enum_query_type query_type)
 
1692
{
 
1693
  if (!arg)
 
1694
  {
 
1695
    str->append(STRING_WITH_LEN("default"));
 
1696
    return;
 
1697
  }
 
1698
  str->append(STRING_WITH_LEN("default("));
 
1699
  arg->print(str, query_type);
 
1700
  str->append(')');
 
1701
}
 
1702
 
 
1703
 
 
1704
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
 
1705
{
 
1706
  if (!arg)
 
1707
  {
 
1708
    if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
 
1709
    {
 
1710
      if (field_arg->reset())
 
1711
      {
 
1712
        my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
 
1713
                   ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
 
1714
        return -1;
 
1715
      }
 
1716
 
 
1717
      {
 
1718
        push_warning_printf(field_arg->table->in_use,
 
1719
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1720
                            ER_NO_DEFAULT_FOR_FIELD,
 
1721
                            ER(ER_NO_DEFAULT_FOR_FIELD),
 
1722
                            field_arg->field_name);
 
1723
      }
 
1724
      return 1;
 
1725
    }
 
1726
    field_arg->set_default();
 
1727
    return 0;
 
1728
  }
 
1729
  return Item_field::save_in_field(field_arg, no_conversions);
 
1730
}
 
1731
 
 
1732
 
 
1733
/**
 
1734
  This method like the walk method traverses the item tree, but at the
 
1735
  same time it can replace some nodes in the tree.
 
1736
*/
 
1737
 
 
1738
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
 
1739
{
 
1740
  Item *new_item= arg->transform(transformer, args);
 
1741
  if (!new_item)
 
1742
    return NULL;
 
1743
 
 
1744
  /*
 
1745
    Session::change_item_tree() should be called only if the tree was
 
1746
    really transformed, i.e. when a new item has been created.
 
1747
    Otherwise we'll be allocating a lot of unnecessary memory for
 
1748
    change records at each execution.
 
1749
  */
 
1750
  if (arg != new_item)
 
1751
    current_session->change_item_tree(&arg, new_item);
 
1752
  return (this->*transformer)(args);
 
1753
}
 
1754
 
1384
1755
Item_result item_cmp_type(Item_result a,Item_result b)
1385
1756
{
1386
1757
  if (a == STRING_RESULT && b == STRING_RESULT)
1395
1766
  return REAL_RESULT;
1396
1767
}
1397
1768
 
 
1769
 
1398
1770
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1399
1771
{
1400
1772
  Item *item= *ref;
1401
1773
  Item *new_item= NULL;
1402
1774
  if (item->basic_const_item())
1403
 
    return; /* Can't be better */
 
1775
    return;                                     // Can't be better
1404
1776
  Item_result res_type=item_cmp_type(comp_item->result_type(),
1405
1777
                                     item->result_type());
1406
 
  char *name=item->name; /* Alloced by memory::sql_alloc */
 
1778
  char *name=item->name;                        // Alloced by sql_alloc
1407
1779
 
1408
1780
  switch (res_type) {
1409
1781
  case STRING_RESULT:
1416
1788
    else
1417
1789
    {
1418
1790
      uint32_t length= result->length();
1419
 
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
 
1791
      char *tmp_str= sql_strmake(result->ptr(), length);
1420
1792
      new_item= new Item_string(name, tmp_str, length, result->charset());
1421
1793
    }
1422
1794
    break;
1487
1859
    session->change_item_tree(ref, new_item);
1488
1860
}
1489
1861
 
 
1862
/**
 
1863
  Return true if the value stored in the field is equal to the const
 
1864
  item.
 
1865
 
 
1866
  We need to use this on the range optimizer because in some cases
 
1867
  we can't store the value in the field without some precision/character loss.
 
1868
*/
 
1869
 
1490
1870
bool field_is_equal_to_item(Field *field,Item *item)
1491
1871
{
1492
1872
 
1522
1902
  return result == field->val_real();
1523
1903
}
1524
1904
 
 
1905
/**
 
1906
  Dummy error processor used by default by Name_resolution_context.
 
1907
 
 
1908
  @note
 
1909
    do nothing
 
1910
*/
 
1911
 
1525
1912
void dummy_error_processor(Session *, void *)
1526
1913
{}
1527
1914
 
1548
1935
  @retval
1549
1936
    new_created field
1550
1937
*/
 
1938
 
1551
1939
static Field *create_tmp_field_from_item(Session *,
1552
1940
                                         Item *item, Table *table,
1553
1941
                                         Item ***copy_func, bool modify_item,
1617
2005
    {
1618
2006
      signed int overflow;
1619
2007
 
1620
 
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
 
2008
      dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
1621
2009
 
1622
2010
      /*
1623
2011
        If the value still overflows the field with the corrected dec,
1630
2018
                                               item->unsigned_flag) - len;
1631
2019
 
1632
2020
      if (overflow > 0)
1633
 
        dec= max(0, dec - overflow);            // too long, discard fract
 
2021
        dec= cmax(0, dec - overflow);            // too long, discard fract
1634
2022
      else
1635
 
        len-= item->decimals - dec;             // corrected value fits
 
2023
        len -= item->decimals - dec;            // corrected value fits
1636
2024
    }
1637
2025
 
1638
 
    new_field= new Field_decimal(len,
1639
 
                                 maybe_null,
1640
 
                                 item->name,
1641
 
                                 dec,
1642
 
                                 item->unsigned_flag);
 
2026
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
2027
                                     dec, item->unsigned_flag);
1643
2028
    break;
1644
2029
  }
1645
2030
  case ROW_RESULT:
1661
2046
  return new_field;
1662
2047
}
1663
2048
 
1664
 
Field *create_tmp_field(Session *session,
1665
 
                        Table *table,
1666
 
                        Item *item,
1667
 
                        Item::Type type,
1668
 
                        Item ***copy_func,
1669
 
                        Field **from_field,
1670
 
                        Field **default_field,
1671
 
                        bool group,
1672
 
                        bool modify_item,
1673
 
                        bool make_copy_field,
 
2049
Field *create_tmp_field(Session *session, Table *table,Item *item,
 
2050
                        Item::Type type, Item ***copy_func, Field **from_field,
 
2051
                        Field **default_field, bool group, bool modify_item,
 
2052
                        bool, bool make_copy_field,
1674
2053
                        uint32_t convert_blob_length)
1675
2054
{
1676
2055
  Field *result;
1760
2139
  }
1761
2140
}
1762
2141
 
1763
 
} /* namespace drizzled */
 
2142
 
 
2143
/**
 
2144
  Wrapper of hide_view_error call for Name_resolution_context error
 
2145
  processor.
 
2146
 
 
2147
  @note
 
2148
    hide view underlying tables details in error messages
 
2149
*/
 
2150
 
 
2151
/*****************************************************************************
 
2152
** Instantiate templates
 
2153
*****************************************************************************/
 
2154
 
 
2155
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
2156
template class List<Item>;
 
2157
template class List_iterator<Item>;
 
2158
template class List_iterator_fast<Item>;
 
2159
template class List_iterator_fast<Item_field>;
 
2160
template class List<List_item>;
 
2161
#endif