17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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"
35
#include "drizzled/field/str.h"
36
#include "drizzled/field/num.h"
38
#include "drizzled/field/blob.h"
39
#include "drizzled/field/date.h"
40
#include "drizzled/field/datetime.h"
41
#include "drizzled/field/decimal.h"
42
#include "drizzled/field/double.h"
43
#include "drizzled/field/enum.h"
44
#include "drizzled/field/epoch.h"
45
#include "drizzled/field/int32.h"
46
#include "drizzled/field/int64.h"
47
#include "drizzled/field/microtime.h"
48
#include "drizzled/field/null.h"
49
#include "drizzled/field/real.h"
50
#include "drizzled/field/size.h"
51
#include "drizzled/field/time.h"
52
#include "drizzled/field/varstring.h"
54
#include "drizzled/internal/m_string.h"
21
#include <drizzled/server_includes.h>
24
#include <drizzled/sql_select.h>
25
#include <drizzled/error.h>
26
#include <drizzled/show.h>
27
#include <drizzled/item/cmpfunc.h>
28
#include <drizzled/item/cache_row.h>
29
#include <drizzled/item/type_holder.h>
30
#include <drizzled/item/sum.h>
31
#include <drizzled/function/str/conv_charset.h>
32
#include <drizzled/virtual_column_info.h>
33
#include <drizzled/sql_base.h>
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/timetype.h>
50
#include <drizzled/field/timestamp.h>
51
#include <drizzled/field/datetime.h>
52
#include <drizzled/field/varstring.h>
55
#if defined(CMATH_NAMESPACE)
56
using namespace CMATH_NAMESPACE;
66
59
const String my_null_string("NULL", 4, default_charset_info);
61
/*****************************************************************************
63
*****************************************************************************/
66
Init all special items.
68
74
bool Item::is_expensive_processor(unsigned char *)
73
void Item::fix_after_pullout(Select_Lex *, Item **)
79
void Item::fix_after_pullout(st_select_lex *, Item **)
76
83
Field *Item::tmp_table_field(Table *)
81
89
const char *Item::full_name(void) const
83
91
return name ? name : "???";
86
95
int64_t Item::val_int_endpoint(bool, bool *)
92
/** @todo Make this functions class dependent */
104
Make this functions class dependent
93
107
bool Item::val_bool()
109
switch(result_type()) {
111
return val_int() != 0;
98
return val_int() != 0;
102
type::Decimal decimal_value;
103
type::Decimal *val= val_decimal(&decimal_value);
105
return not val->isZero();
111
return val_real() != 0.0;
114
my_decimal decimal_value;
115
my_decimal *val= val_decimal(&decimal_value);
117
return !my_decimal_is_zero(val);
122
return val_real() != 0.0;
126
return false; // Wrong (but safe)
122
131
String *Item::val_string_from_real(String *str)
124
133
double nr= val_real();
135
return NULL; /* purecov: inspected */
128
str->set_real(nr, decimals, &my_charset_bin);
137
str->set_real(nr,decimals, &my_charset_bin);
132
142
String *Item::val_string_from_int(String *str)
134
144
int64_t nr= val_int();
142
153
String *Item::val_string_from_decimal(String *str)
144
type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
155
my_decimal dec_buf, *dec= val_decimal(&dec_buf);
148
class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
149
class_decimal2string(&dec_buf, 0, str);
159
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
160
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
153
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
165
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
155
167
double nr= val_real();
159
double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
171
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
160
172
return (decimal_value);
163
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
176
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
165
178
int64_t nr= val_int();
169
int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
182
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
170
183
return decimal_value;
173
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
187
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
177
191
if (!(res= val_str(&str_value)))
192
return NULL; // NULL or EOM
180
194
end_ptr= (char*) res->ptr()+ res->length();
181
if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
184
res->charset()) & E_DEC_BAD_NUM)
195
if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
196
res->ptr(), res->length(), res->charset(),
197
decimal_value) & E_DEC_BAD_NUM)
186
push_warning_printf(&getSession(),
187
DRIZZLE_ERROR::WARN_LEVEL_WARN,
199
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
188
200
ER_TRUNCATED_WRONG_VALUE,
189
201
ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
190
202
str_value.c_ptr());
192
204
return decimal_value;
195
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
208
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
199
if (get_date(ltime, TIME_FUZZY_DATE))
212
if (get_date(<ime, TIME_FUZZY_DATE))
201
decimal_value->set_zero();
214
my_decimal_set_zero(decimal_value);
202
215
null_value= 1; // set NULL, stop processing
205
return date2_class_decimal(<ime, decimal_value);
218
return date2my_decimal(<ime, decimal_value);
208
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
222
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
226
if (get_time(<ime))
214
decimal_value->set_zero();
228
my_decimal_set_zero(decimal_value);
217
return date2_class_decimal(<ime, decimal_value);
231
return date2my_decimal(<ime, decimal_value);
220
235
double Item::val_real_from_decimal()
222
237
/* Note that fix_fields may not be called for Item_avg_field items */
224
type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
239
my_decimal value_buff, *dec_val= val_decimal(&value_buff);
227
class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
242
my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
231
247
int64_t Item::val_int_from_decimal()
233
249
/* Note that fix_fields may not be called for Item_avg_field items */
235
type::Decimal value, *dec_val= val_decimal(&value);
251
my_decimal value, *dec_val= val_decimal(&value);
239
dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
254
my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
244
bool Item::save_time_in_field(Field *field)
249
return set_field_to_null(field);
251
field->set_notnull();
253
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
256
bool Item::save_date_in_field(Field *field)
260
if (get_date(ltime, TIME_FUZZY_DATE))
261
return set_field_to_null(field);
263
field->set_notnull();
265
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
269
* Check if the Item is null and stores the NULL or the
270
* result value in the field accordingly.
258
int Item::save_time_in_field(Field *field)
261
if (get_time(<ime))
262
return set_field_to_null(field);
263
field->set_notnull();
264
return field->store_time(<ime, DRIZZLE_TIMESTAMP_TIME);
268
int Item::save_date_in_field(Field *field)
271
if (get_date(<ime, TIME_FUZZY_DATE))
272
return set_field_to_null(field);
273
field->set_notnull();
274
return field->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
279
Store the string value in field directly
282
Item::save_str_value_in_field()
283
field a pointer to field where to store
284
result the pointer to the string value to be stored
287
The method is used by Item_*::save_in_field implementations
288
when we don't need to calculate the value to store
289
See Item_string::save_in_field() implementation for example
292
Check if the Item is null and stores the NULL or the
293
result value in the field accordingly.
296
Nonzero value if error
272
299
int Item::save_str_value_in_field(Field *field, String *result)
275
302
return set_field_to_null(field);
277
303
field->set_notnull();
279
return field->store(result->ptr(), result->length(), collation.collation);
304
return field->store(result->ptr(), result->length(),
305
collation.collation);
283
is_expensive_cache(-1),
293
unsigned_flag(false),
294
with_sum_func(false),
295
is_autogenerated_name(true),
296
with_subselect(false),
297
collation(&my_charset_bin, DERIVATION_COERCIBLE),
298
_session(*current_session)
310
is_expensive_cache(-1), name(0), orig_name(0), name_length(0),
311
fixed(0), is_autogenerated_name(true),
312
collation(&my_charset_bin, DERIVATION_COERCIBLE)
317
with_sum_func= false;
318
unsigned_flag= false;
300
322
cmp_context= (Item_result)-1;
302
324
/* Put item in free list so that we can free all items at end */
303
next= getSession().free_list;
304
getSession().free_list= this;
325
Session *session= current_session;
326
next= session->free_list;
327
session->free_list= this;
307
329
Item constructor can be called during execution other then SQL_COM
308
330
command => we should check session->lex->current_select on zero (session->lex
309
331
can be uninitialised)
311
if (getSession().lex->current_select)
333
if (session->lex->current_select)
313
enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
314
if (place == SELECT_LIST || place == IN_HAVING)
315
getSession().getLex()->current_select->select_n_having_items++;
335
enum_parsing_place place=
336
session->lex->current_select->parsing_place;
337
if (place == SELECT_LIST ||
339
session->lex->current_select->select_n_having_items++;
344
Constructor used by Item_field, Item_ref & aggregate (sum)
347
Used for duplicating lists in processing queries with temporary
319
350
Item::Item(Session *session, Item *item):
320
351
is_expensive_cache(-1),
321
352
str_value(item->str_value),
322
353
name(item->name),
323
name_length(item->name_length),
324
354
orig_name(item->orig_name),
325
355
max_length(item->max_length),
326
356
marker(item->marker),
327
357
decimals(item->decimals),
329
358
maybe_null(item->maybe_null),
330
359
null_value(item->null_value),
331
360
unsigned_flag(item->unsigned_flag),
332
361
with_sum_func(item->with_sum_func),
333
is_autogenerated_name(item->is_autogenerated_name),
334
with_subselect(item->with_subselect),
335
363
collation(item->collation),
336
cmp_context(item->cmp_context),
364
cmp_context(item->cmp_context)
339
/* Put this item in the session's free list */
340
next= getSession().free_list;
341
getSession().free_list= this;
366
next= session->free_list; // Put in free list
367
session->free_list= this;
344
uint32_t Item::float_length(uint32_t decimals_par) const
346
return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
349
371
uint32_t Item::decimal_precision() const
351
373
Item_result restype= result_type();
353
375
if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
354
return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
355
(uint32_t) DECIMAL_MAX_PRECISION);
356
return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
376
return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
377
(unsigned int)DECIMAL_MAX_PRECISION);
378
return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
359
382
int Item::decimal_int_part() const
361
return class_decimal_int_part(decimal_precision(), decimals);
384
return my_decimal_int_part(decimal_precision(), decimals);
364
388
void Item::print(String *str, enum_query_type)
366
390
str->append(full_name());
369
394
void Item::print_item_w_name(String *str, enum_query_type query_type)
371
396
print(str, query_type);
400
Session *session= current_session;
375
401
str->append(STRING_WITH_LEN(" AS "));
376
str->append_identifier(name, (uint32_t) strlen(name));
402
append_identifier(session, str, name, (uint) strlen(name));
380
407
void Item::split_sum_func(Session *, Item **, List<Item> &)
383
411
void Item::cleanup()
422
cleanup() item if it is 'fixed'.
424
@param arg a dummy parameter, is not used here
427
bool Item::cleanup_processor(unsigned char *)
436
rename item (used for views, cleanup() return original name).
438
@param new_name new name of item;
392
441
void Item::rename(char *new_name)
395
444
we can compare pointers to names here, because if name was not changed,
396
445
pointer will be same
398
if (! orig_name && new_name != name)
447
if (!orig_name && new_name != name)
454
Traverse item tree possibly transforming it (replacing items).
456
This function is designed to ease transformation of Item trees.
457
Re-execution note: every such transformation is registered for
458
rollback by Session::change_item_tree() and is rolled back at the end
459
of execution by Session::rollback_item_tree_changes().
462
- this function can not be used at prepared statement prepare
463
(in particular, in fix_fields!), as only permanent
464
transformation of Item trees are allowed at prepare.
465
- the transformer function shall allocate new Items in execution
466
memory root (session->mem_root) and not anywhere else: allocated
467
items will be gone in the end of execution.
469
If you don't need to transform an item tree, but only traverse
470
it, please use Item::walk() instead.
473
@param transformer functor that performs transformation of a subtree
474
@param arg opaque argument passed to the functor
477
Returns pointer to the new subtree root. Session::change_item_tree()
478
should be called for it if transformation took place, i.e. if a
479
pointer to newly allocated item is returned.
403
482
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
405
484
return (this->*transformer)(arg);
408
488
bool Item::check_cols(uint32_t c)
412
492
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
418
499
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
459
555
for all basic constants we have special checks, and Item_param's
460
556
type() can be only among basic constant types.
462
return type() == item->type() &&
465
! my_strcasecmp(system_charset_info, name, item->name);
558
return type() == item->type() && name && item->name &&
559
!my_strcasecmp(system_charset_info,name,item->name);
468
563
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
470
565
Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
471
566
return conv->safe ? conv : NULL;
474
bool Item::get_date(type::Time <ime,uint32_t fuzzydate)
571
Get the value of the function as a DRIZZLE_TIME structure.
572
As a extra convenience the time structure is reset on error!
575
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
482
else if (result_type() == STRING_RESULT)
484
char buff[type::Time::MAX_STRING_LENGTH];
485
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
486
if (!(res=val_str(&tmp)) ||
487
str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
488
<ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
495
int64_t value= val_int();
496
type::datetime_t date_value;
498
ltime.convert(date_value, value, fuzzydate);
500
if (not type::is_valid(date_value))
502
char buff[DECIMAL_LONGLONG_DIGITS], *end;
503
end= internal::int64_t10_to_str(value, buff, -10);
504
make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
505
buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
577
if (result_type() == STRING_RESULT)
580
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
581
if (!(res=val_str(&tmp)) ||
582
str_to_datetime_with_warn(res->ptr(), res->length(),
583
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
588
int64_t value= val_int();
590
if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
593
end= int64_t10_to_str(value, buff, -10);
594
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
595
buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
603
memset(ltime, 0, sizeof(*ltime));
518
bool Item::get_time(type::Time <ime)
608
Get time of first argument.\
610
As a extra convenience the time structure is reset on error!
613
bool Item::get_time(DRIZZLE_TIME *ltime)
520
char buff[type::Time::MAX_STRING_LENGTH];
521
616
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
522
if (!(res=val_str(&tmp)) or
523
str_to_time_with_warn(&getSession(), res->ptr(), res->length(), <ime))
617
if (!(res=val_str(&tmp)) ||
618
str_to_time_with_warn(res->ptr(), res->length(), ltime))
620
memset(ltime, 0, sizeof(*ltime));
533
bool Item::get_date_result(type::Time <ime,uint32_t fuzzydate)
627
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
535
return get_date(ltime, fuzzydate);
629
return get_date(ltime,fuzzydate);
538
633
bool Item::is_null()
543
639
void Item::update_null_value ()
545
641
(void) val_int();
548
645
void Item::top_level_item(void)
551
649
void Item::set_result_field(Field *)
554
653
bool Item::is_result_field(void)
559
659
bool Item::is_bool_func(void)
564
665
void Item::save_in_result_field(bool)
567
669
void Item::no_rows_in_result(void)
570
673
Item *Item::copy_or_same(Session *)
575
679
Item *Item::copy_andor_structure(Session *)
580
685
Item *Item::real_item(void)
585
const Item *Item::real_item(void) const
590
691
Item *Item::get_tmp_table_item(Session *session)
592
693
return copy_or_same(session);
595
697
const CHARSET_INFO *Item::default_charset()
597
return current_session->variables.getCollation();
699
return current_session->variables.collation_connection;
600
703
const CHARSET_INFO *Item::compare_collation()
605
709
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
607
711
return (this->*processor)(arg);
610
Item* Item::compile(Item_analyzer analyzer,
611
unsigned char **arg_p,
612
Item_transformer transformer,
613
unsigned char *arg_t)
715
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
716
Item_transformer transformer, unsigned char *arg_t)
615
718
if ((this->*analyzer) (arg_p))
616
719
return ((this->*transformer) (arg_t));
620
724
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
622
726
(*traverser)(this, arg);
625
730
bool Item::remove_dependence_processor(unsigned char *)
630
736
bool Item::remove_fixed(unsigned char *)
636
743
bool Item::collect_item_field_processor(unsigned char *)
641
749
bool Item::find_item_in_field_list_processor(unsigned char *)
646
755
bool Item::change_context_processor(unsigned char *)
760
bool Item::reset_query_id_processor(unsigned char *)
651
766
bool Item::register_field_in_read_map(unsigned char *)
772
bool Item::register_field_in_bitmap(unsigned char *)
656
778
bool Item::subst_argument_checker(unsigned char **arg)
1433
Create a field to hold a string value from an item.
1435
If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
1436
If max_length > 0 create a varchar @n
1437
If max_length == 0 create a CHAR(0)
1439
@param table Table for which the field is created
1169
1442
Field *Item::make_string_field(Table *table)
1172
1445
assert(collation.collation);
1173
1446
if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1175
1447
field= new Field_blob(max_length, maybe_null, name,
1176
1448
collation.collation);
1180
table->setVariableWidth();
1181
field= new Field_varstring(max_length, maybe_null, name,
1450
field= new Field_varstring(max_length, maybe_null, name, table->s,
1182
1451
collation.collation);
1186
1454
field->init(table);
1460
Create a field based on field_type of argument.
1462
For now, this is only used to create a field for
1463
IFNULL(x,something) and time functions
1190
1471
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
1193
1474
The field functions defines a field to be not null if null_ptr is not 0
1195
1476
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1198
1479
switch (field_type()) {
1199
case DRIZZLE_TYPE_DECIMAL:
1200
field= new Field_decimal((unsigned char*) 0,
1480
case DRIZZLE_TYPE_NEWDECIMAL:
1481
field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
1482
Field::NONE, name, decimals, 0,
1208
1485
case DRIZZLE_TYPE_LONG:
1209
field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1486
field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1487
name, 0, unsigned_flag);
1211
1489
case DRIZZLE_TYPE_LONGLONG:
1212
field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1490
field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1491
name, 0, unsigned_flag);
1214
1493
case DRIZZLE_TYPE_DOUBLE:
1215
1494
field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1216
name, decimals, 0, unsigned_flag);
1495
name, decimals, 0, unsigned_flag);
1218
1497
case DRIZZLE_TYPE_NULL:
1219
field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1498
field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
1499
name, &my_charset_bin);
1221
1501
case DRIZZLE_TYPE_DATE:
1222
1502
field= new Field_date(maybe_null, name, &my_charset_bin);
1225
case DRIZZLE_TYPE_MICROTIME:
1226
field= new field::Microtime(maybe_null, name);
1504
case DRIZZLE_TYPE_TIME:
1505
field= new Field_time(maybe_null, name, &my_charset_bin);
1229
1507
case DRIZZLE_TYPE_TIMESTAMP:
1230
field= new field::Epoch(maybe_null, name);
1508
field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1232
1510
case DRIZZLE_TYPE_DATETIME:
1233
1511
field= new Field_datetime(maybe_null, name, &my_charset_bin);
1235
case DRIZZLE_TYPE_TIME:
1236
field= new field::Time(maybe_null, name, &my_charset_bin);
1238
case DRIZZLE_TYPE_BOOLEAN:
1239
case DRIZZLE_TYPE_UUID:
1514
/* This case should never be chosen */
1516
/* Fall through to make_string_field() */
1240
1517
case DRIZZLE_TYPE_ENUM:
1241
1518
case DRIZZLE_TYPE_VARCHAR:
1242
1519
return make_string_field(table);
1243
1520
case DRIZZLE_TYPE_BLOB:
1521
if (this->type() == Item::TYPE_HOLDER)
1522
field= new Field_blob(max_length, maybe_null, name, collation.collation,
1244
1525
field= new Field_blob(max_length, maybe_null, name, collation.collation);
1245
1526
break; // Blob handled outside of case
1250
1529
field->init(table);
1255
1535
This implementation can lose str_value content, so if the
1256
1536
Item uses str_value to store something, it should
1257
1537
reimplement it's ::save_in_field() as Item_string, for example, does
1259
1540
int Item::save_in_field(Field *field, bool no_conversions)
1319
Check if an item is a constant one and can be cached.
1321
@param arg [out] TRUE <=> Cache this item.
1323
@return TRUE Go deeper in item tree.
1324
@return FALSE Don't go deeper in item tree.
1327
bool Item::cache_const_expr_analyzer(unsigned char **arg)
1329
bool *cache_flag= (bool*)*arg;
1332
Item *item= real_item();
1334
Cache constant items unless it's a basic constant, constant field or
1335
a subselect (they use their own cache).
1338
!(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1339
item->type() == SUBSELECT_ITEM ||
1341
Do not cache GET_USER_VAR() function as its const_item() may
1342
return TRUE for the current thread but it still may change
1343
during the execution.
1345
(item->type() == Item::FUNC_ITEM &&
1346
((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1354
Cache item if needed.
1356
@param arg TRUE <=> Cache this item.
1358
@return cache if cache needed.
1359
@return this otherwise.
1362
Item* Item::cache_const_expr_transformer(unsigned char *arg)
1366
*((bool*)arg)= false;
1367
Item_cache *cache= Item_cache::get_cache(this);
1377
bool Item::send(plugin::Client *client, String *buffer)
1601
This is only called from items that is not of type item_field.
1604
bool Item::send(Protocol *protocol, String *buffer)
1379
1606
bool result= false;
1380
1607
enum_field_types f_type;
1382
1609
switch ((f_type=field_type())) {
1383
case DRIZZLE_TYPE_DATE:
1384
1611
case DRIZZLE_TYPE_NULL:
1385
1612
case DRIZZLE_TYPE_ENUM:
1386
1613
case DRIZZLE_TYPE_BLOB:
1387
1614
case DRIZZLE_TYPE_VARCHAR:
1388
case DRIZZLE_TYPE_BOOLEAN:
1389
case DRIZZLE_TYPE_UUID:
1390
case DRIZZLE_TYPE_DECIMAL:
1393
if ((res=val_str(buffer)))
1394
result= client->store(res->ptr(),res->length());
1615
case DRIZZLE_TYPE_NEWDECIMAL:
1618
if ((res=val_str(buffer)))
1619
result= protocol->store(res->ptr(),res->length(),res->charset());
1397
1622
case DRIZZLE_TYPE_LONG:
1402
result= client->store((int32_t)nr);
1627
result= protocol->store_long(nr);
1405
1630
case DRIZZLE_TYPE_LONGLONG:
1412
result= client->store((uint64_t)nr);
1414
result= client->store((int64_t)nr);
1635
result= protocol->store_int64_t(nr, unsigned_flag);
1418
1638
case DRIZZLE_TYPE_DOUBLE:
1420
double nr= val_real();
1422
result= client->store(nr, decimals, buffer);
1425
case DRIZZLE_TYPE_TIME:
1430
result= client->store(&tm);
1640
double nr= val_real();
1642
result= protocol->store(nr, decimals, buffer);
1433
1645
case DRIZZLE_TYPE_DATETIME:
1434
case DRIZZLE_TYPE_MICROTIME:
1435
1646
case DRIZZLE_TYPE_TIMESTAMP:
1649
get_date(&tm, TIME_FUZZY_DATE);
1438
get_date(tm, TIME_FUZZY_DATE);
1440
result= client->store(&tm);
1652
if (f_type == DRIZZLE_TYPE_DATE)
1653
return protocol->store_date(&tm);
1655
result= protocol->store(&tm);
1659
case DRIZZLE_TYPE_TIME:
1664
result= protocol->store_time(&tm);
1444
1668
if (null_value)
1445
result= client->store();
1669
result= protocol->store_null();
1450
uint32_t Item::max_char_length() const
1452
return max_length / collation.collation->mbmaxlen;
1455
void Item::fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs)
1457
max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1458
collation.collation= cs;
1461
void Item::fix_char_length(uint32_t max_char_length_arg)
1463
max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1466
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
1468
uint64_t max_result_length= max_char_length_arg *
1469
collation.collation->mbmaxlen;
1471
if (max_result_length >= MAX_BLOB_WIDTH)
1473
max_length= MAX_BLOB_WIDTH;
1478
max_length= max_result_length;
1482
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
1484
collation.set(&my_charset_bin);
1485
fix_char_length(max_char_length_arg);
1674
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
1676
return item->type() == DEFAULT_VALUE_ITEM &&
1677
((Item_default_value *)item)->arg->eq(arg, binary_cmp);
1681
bool Item_default_value::fix_fields(Session *session, Item **)
1684
Item_field *field_arg;
1693
if (!arg->fixed && arg->fix_fields(session, &arg))
1697
real_arg= arg->real_item();
1698
if (real_arg->type() != FIELD_ITEM)
1700
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
1704
field_arg= (Item_field *)real_arg;
1705
if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
1707
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
1710
if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
1712
memcpy(def_field, field_arg->field, field_arg->field->size_of());
1713
def_field->move_field_offset((my_ptrdiff_t)
1714
(def_field->table->s->default_values -
1715
def_field->table->record[0]));
1716
set_field(def_field);
1720
context->process_error(session);
1725
void Item_default_value::print(String *str, enum_query_type query_type)
1729
str->append(STRING_WITH_LEN("default"));
1732
str->append(STRING_WITH_LEN("default("));
1733
arg->print(str, query_type);
1738
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
1742
if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
1744
if (field_arg->reset())
1746
my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
1747
ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
1752
push_warning_printf(field_arg->table->in_use,
1753
DRIZZLE_ERROR::WARN_LEVEL_WARN,
1754
ER_NO_DEFAULT_FOR_FIELD,
1755
ER(ER_NO_DEFAULT_FOR_FIELD),
1756
field_arg->field_name);
1760
field_arg->set_default();
1763
return Item_field::save_in_field(field_arg, no_conversions);
1768
This method like the walk method traverses the item tree, but at the
1769
same time it can replace some nodes in the tree.
1772
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
1774
Item *new_item= arg->transform(transformer, args);
1779
Session::change_item_tree() should be called only if the tree was
1780
really transformed, i.e. when a new item has been created.
1781
Otherwise we'll be allocating a lot of unnecessary memory for
1782
change records at each execution.
1784
if (arg != new_item)
1785
current_session->change_item_tree(&arg, new_item);
1786
return (this->*transformer)(args);
1488
1789
Item_result item_cmp_type(Item_result a,Item_result b)
1490
1791
if (a == STRING_RESULT && b == STRING_RESULT)
1491
1792
return STRING_RESULT;
1493
1793
if (a == INT_RESULT && b == INT_RESULT)
1494
1794
return INT_RESULT;
1495
1795
else if (a == ROW_RESULT || b == ROW_RESULT)
1496
1796
return ROW_RESULT;
1498
1797
if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1499
1798
(b == INT_RESULT || b == DECIMAL_RESULT))
1500
1799
return DECIMAL_RESULT;
1502
1800
return REAL_RESULT;
1505
1804
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1507
1806
Item *item= *ref;
1508
1807
Item *new_item= NULL;
1509
1808
if (item->basic_const_item())
1510
return; /* Can't be better */
1809
return; // Can't be better
1511
1810
Item_result res_type=item_cmp_type(comp_item->result_type(),
1512
1811
item->result_type());
1513
char *name=item->name; /* Alloced by memory::sql_alloc */
1812
char *name=item->name; // Alloced by sql_alloc
1515
1814
switch (res_type) {
1516
1815
case STRING_RESULT:
1817
char buff[MAX_FIELD_WIDTH];
1818
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1819
result=item->val_str(&tmp);
1820
if (item->null_value)
1821
new_item= new Item_null(name);
1518
char buff[MAX_FIELD_WIDTH];
1519
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1520
result=item->val_str(&tmp);
1521
if (item->null_value)
1522
new_item= new Item_null(name);
1525
uint32_t length= result->length();
1526
char *tmp_str= memory::sql_strmake(result->ptr(), length);
1527
new_item= new Item_string(name, tmp_str, length, result->charset());
1824
uint32_t length= result->length();
1825
char *tmp_str= sql_strmake(result->ptr(), length);
1826
new_item= new Item_string(name, tmp_str, length, result->charset());
1531
1830
case INT_RESULT:
1533
int64_t result=item->val_int();
1534
uint32_t length=item->max_length;
1535
bool null_value=item->null_value;
1536
new_item= (null_value ? (Item*) new Item_null(name) :
1537
(Item*) new Item_int(name, result, length));
1832
int64_t result=item->val_int();
1833
uint32_t length=item->max_length;
1834
bool null_value=item->null_value;
1835
new_item= (null_value ? (Item*) new Item_null(name) :
1836
(Item*) new Item_int(name, result, length));
1540
1839
case ROW_RESULT:
1541
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1544
Substitute constants only in Item_rows. Don't affect other Items
1545
with ROW_RESULT (eg Item_singlerow_subselect).
1840
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1843
Substitute constants only in Item_rows. Don't affect other Items
1844
with ROW_RESULT (eg Item_singlerow_subselect).
1547
For such Items more optimal is to detect if it is constant and replace
1548
it with Item_row. This would optimize queries like this:
1549
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1551
Item_row *item_row= (Item_row*) item;
1552
Item_row *comp_item_row= (Item_row*) comp_item;
1556
If item and comp_item are both Item_rows and have same number of cols
1557
then process items in Item_row one by one.
1558
We can't ignore NULL values here as this item may be used with <=>, in
1559
which case NULL's are significant.
1561
assert(item->result_type() == comp_item->result_type());
1562
assert(item_row->cols() == comp_item_row->cols());
1563
col= item_row->cols();
1565
resolve_const_item(session, item_row->addr(col),
1566
comp_item_row->element_index(col));
1846
For such Items more optimal is to detect if it is constant and replace
1847
it with Item_row. This would optimize queries like this:
1848
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1850
Item_row *item_row= (Item_row*) item;
1851
Item_row *comp_item_row= (Item_row*) comp_item;
1855
If item and comp_item are both Item_rows and have same number of cols
1856
then process items in Item_row one by one.
1857
We can't ignore NULL values here as this item may be used with <=>, in
1858
which case NULL's are significant.
1860
assert(item->result_type() == comp_item->result_type());
1861
assert(item_row->cols() == comp_item_row->cols());
1862
col= item_row->cols();
1864
resolve_const_item(session, item_row->addr(col),
1865
comp_item_row->element_index(col));
1570
1869
case REAL_RESULT:
1571
{ // It must REAL_RESULT
1572
double result= item->val_real();
1573
uint32_t length=item->max_length,decimals=item->decimals;
1574
bool null_value=item->null_value;
1575
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1576
new Item_float(name, result, decimals, length));
1870
{ // It must REAL_RESULT
1871
double result= item->val_real();
1872
uint32_t length=item->max_length,decimals=item->decimals;
1873
bool null_value=item->null_value;
1874
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1875
new Item_float(name, result, decimals, length));
1579
1878
case DECIMAL_RESULT:
1581
type::Decimal decimal_value;
1582
type::Decimal *result= item->val_decimal(&decimal_value);
1583
uint32_t length= item->max_length, decimals= item->decimals;
1584
bool null_value= item->null_value;
1585
new_item= (null_value ?
1586
(Item*) new Item_null(name) :
1587
(Item*) new Item_decimal(name, result, length, decimals));
1880
my_decimal decimal_value;
1881
my_decimal *result= item->val_decimal(&decimal_value);
1882
uint32_t length= item->max_length, decimals= item->decimals;
1883
bool null_value= item->null_value;
1884
new_item= (null_value ?
1885
(Item*) new Item_null(name) :
1886
(Item*) new Item_decimal(name, result, length, decimals));
1593
1893
session->change_item_tree(ref, new_item);
1897
Return true if the value stored in the field is equal to the const
1900
We need to use this on the range optimizer because in some cases
1901
we can't store the value in the field without some precision/character loss.
1596
1904
bool field_is_equal_to_item(Field *field,Item *item)
1706
2006
To preserve type they needed to be handled separately.
1708
2008
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1709
type == DRIZZLE_TYPE_TIME ||
1710
type == DRIZZLE_TYPE_MICROTIME ||
1711
type == DRIZZLE_TYPE_DATE ||
2009
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
1712
2010
type == DRIZZLE_TYPE_TIMESTAMP)
1714
2011
new_field= item->tmp_table_field_from_field_type(table, 1);
1716
Make sure that the blob fits into a Field_varstring which has
2013
Make sure that the blob fits into a Field_varstring which has
1720
2016
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1721
2017
convert_blob_length <= Field_varstring::MAX_SIZE &&
1722
2018
convert_blob_length)
1724
table->setVariableWidth();
1725
2019
new_field= new Field_varstring(convert_blob_length, maybe_null,
1726
item->name, item->collation.collation);
2020
item->name, table->s,
2021
item->collation.collation);
1730
2023
new_field= item->make_string_field(table);
1732
2024
new_field->set_derivation(item->collation.derivation);
1735
2026
case DECIMAL_RESULT:
2028
uint8_t dec= item->decimals;
2029
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
2030
uint32_t len= item->max_length;
2033
Trying to put too many digits overall in a DECIMAL(prec,dec)
2034
will always throw a warning. We must limit dec to
2035
DECIMAL_MAX_SCALE however to prevent an assert() later.
1737
uint8_t dec= item->decimals;
1738
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1739
uint32_t len= item->max_length;
2040
signed int overflow;
2042
dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
1742
Trying to put too many digits overall in a DECIMAL(prec,dec)
1743
will always throw a warning. We must limit dec to
1744
DECIMAL_MAX_SCALE however to prevent an assert() later.
2045
If the value still overflows the field with the corrected dec,
2046
we'll throw out decimals rather than integers. This is still
2047
bad and of course throws a truncation warning.
2048
+1: for decimal point
1749
signed int overflow;
1751
dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1754
If the value still overflows the field with the corrected dec,
1755
we'll throw out decimals rather than integers. This is still
1756
bad and of course throws a truncation warning.
1757
+1: for decimal point
1760
overflow= class_decimal_precision_to_length(intg + dec, dec,
1761
item->unsigned_flag) - len;
1764
dec= max(0, dec - overflow); // too long, discard fract
1766
len-= item->decimals - dec; // corrected value fits
1769
new_field= new Field_decimal(len,
1773
item->unsigned_flag);
2051
overflow= my_decimal_precision_to_length(intg + dec, dec,
2052
item->unsigned_flag) - len;
2055
dec= cmax(0, dec - overflow); // too long, discard fract
2057
len -= item->decimals - dec; // corrected value fits
2060
new_field= new Field_new_decimal(len, maybe_null, item->name,
2061
dec, item->unsigned_flag);
1777
2064
case ROW_RESULT:
1778
2066
// This case should never be choosen
1784
2072
new_field->init(table);
1786
2074
if (copy_func && item->is_result_field())
1787
2075
*((*copy_func)++) = item; // Save for copy_funcs
1789
2076
if (modify_item)
1790
2077
item->set_result_field(new_field);
1792
2078
if (item->type() == Item::NULL_ITEM)
1793
2079
new_field->is_created_from_null_item= true;
1795
2080
return new_field;
1798
Field *create_tmp_field(Session *session,
1804
Field **default_field,
1807
bool make_copy_field,
2083
Field *create_tmp_field(Session *session, Table *table,Item *item,
2084
Item::Type type, Item ***copy_func, Field **from_field,
2085
Field **default_field, bool group, bool modify_item,
2086
bool, bool make_copy_field,
1808
2087
uint32_t convert_blob_length)