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"
37
#include "drizzled/field/blob.h"
38
#include "drizzled/field/enum.h"
39
#include "drizzled/field/null.h"
40
#include "drizzled/field/date.h"
41
#include "drizzled/field/decimal.h"
42
#include "drizzled/field/real.h"
43
#include "drizzled/field/double.h"
44
#include "drizzled/field/int32.h"
45
#include "drizzled/field/int64.h"
46
#include "drizzled/field/num.h"
47
#include "drizzled/field/timestamp.h"
48
#include "drizzled/field/datetime.h"
49
#include "drizzled/field/varstring.h"
50
#include "drizzled/internal/m_string.h"
21
#include <drizzled/sql_select.h>
22
#include <drizzled/error.h>
23
#include <drizzled/show.h>
24
#include <drizzled/item/cmpfunc.h>
25
#include <drizzled/item/cache_row.h>
26
#include <drizzled/item/type_holder.h>
27
#include <drizzled/item/sum.h>
28
#include <drizzled/item/copy_string.h>
29
#include <drizzled/function/str/conv_charset.h>
30
#include <drizzled/sql_base.h>
31
#include <drizzled/util/convert.h>
32
#include <drizzled/plugin/client.h>
33
#include <drizzled/time_functions.h>
34
#include <drizzled/field/str.h>
35
#include <drizzled/field/num.h>
36
#include <drizzled/field/blob.h>
37
#include <drizzled/field/date.h>
38
#include <drizzled/field/datetime.h>
39
#include <drizzled/field/decimal.h>
40
#include <drizzled/field/double.h>
41
#include <drizzled/field/enum.h>
42
#include <drizzled/field/epoch.h>
43
#include <drizzled/field/int32.h>
44
#include <drizzled/field/int64.h>
45
#include <drizzled/field/microtime.h>
46
#include <drizzled/field/null.h>
47
#include <drizzled/field/real.h>
48
#include <drizzled/field/size.h>
49
#include <drizzled/field/time.h>
50
#include <drizzled/field/varstring.h>
51
#include <drizzled/current_session.h>
52
#include <drizzled/session.h>
53
#include <drizzled/internal/m_string.h>
54
#include <drizzled/item/ref.h>
55
#include <drizzled/item/subselect.h>
56
#include <drizzled/sql_lex.h>
57
#include <drizzled/system_variables.h>
138
145
String *Item::val_string_from_decimal(String *str)
140
my_decimal dec_buf, *dec= val_decimal(&dec_buf);
147
type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
144
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
145
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
151
class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
152
class_decimal2string(&dec_buf, 0, str);
149
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
156
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
151
158
double nr= val_real();
155
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
162
double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
156
163
return (decimal_value);
159
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
166
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
161
168
int64_t nr= val_int();
165
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
172
int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
166
173
return decimal_value;
169
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
176
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
176
183
end_ptr= (char*) res->ptr()+ res->length();
177
if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
184
if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
181
decimal_value) & E_DEC_BAD_NUM)
187
res->charset()) & E_DEC_BAD_NUM)
183
push_warning_printf(current_session,
189
push_warning_printf(&getSession(),
184
190
DRIZZLE_ERROR::WARN_LEVEL_WARN,
185
191
ER_TRUNCATED_WRONG_VALUE,
186
192
ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
189
195
return decimal_value;
192
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
198
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
196
if (get_date(<ime, TIME_FUZZY_DATE))
202
if (get_date(ltime, TIME_FUZZY_DATE))
198
my_decimal_set_zero(decimal_value);
204
decimal_value->set_zero();
199
205
null_value= 1; // set NULL, stop processing
202
return date2my_decimal(<ime, decimal_value);
208
return date2_class_decimal(<ime, decimal_value);
205
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
211
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
209
if (get_time(<ime))
211
my_decimal_set_zero(decimal_value);
217
decimal_value->set_zero();
214
return date2my_decimal(<ime, decimal_value);
220
return date2_class_decimal(<ime, decimal_value);
217
223
double Item::val_real_from_decimal()
219
225
/* Note that fix_fields may not be called for Item_avg_field items */
221
my_decimal value_buff, *dec_val= val_decimal(&value_buff);
227
type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
224
my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
230
class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
230
236
/* Note that fix_fields may not be called for Item_avg_field items */
232
my_decimal value, *dec_val= val_decimal(&value);
238
type::Decimal value, *dec_val= val_decimal(&value);
235
my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
242
dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
239
int Item::save_time_in_field(Field *field)
247
bool Item::save_time_in_field(Field *field)
242
if (get_time(<ime))
243
252
return set_field_to_null(field);
244
254
field->set_notnull();
245
return field->store_time(<ime, DRIZZLE_TIMESTAMP_TIME);
256
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
248
int Item::save_date_in_field(Field *field)
259
bool Item::save_date_in_field(Field *field)
251
if (get_date(<ime, TIME_FUZZY_DATE))
263
if (get_date(ltime, TIME_FUZZY_DATE))
252
264
return set_field_to_null(field);
253
266
field->set_notnull();
254
return field->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
268
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
281
297
with_sum_func(false),
282
298
is_autogenerated_name(true),
283
299
with_subselect(false),
284
collation(&my_charset_bin, DERIVATION_COERCIBLE)
300
collation(&my_charset_bin, DERIVATION_COERCIBLE),
301
_session(*current_session)
286
303
cmp_context= (Item_result)-1;
288
305
/* Put item in free list so that we can free all items at end */
289
Session *session= current_session;
290
next= session->free_list;
291
session->free_list= this;
306
next= getSession().free_list;
307
getSession().free_list= this;
294
310
Item constructor can be called during execution other then SQL_COM
295
command => we should check session->lex->current_select on zero (session->lex
311
command => we should check session->lex().current_select on zero (session->lex
296
312
can be uninitialised)
298
if (session->lex->current_select)
314
if (getSession().lex().current_select)
300
enum_parsing_place place= session->lex->current_select->parsing_place;
316
enum_parsing_place place= getSession().lex().current_select->parsing_place;
301
317
if (place == SELECT_LIST || place == IN_HAVING)
302
session->lex->current_select->select_n_having_items++;
318
getSession().lex().current_select->select_n_having_items++;
320
336
is_autogenerated_name(item->is_autogenerated_name),
321
337
with_subselect(item->with_subselect),
322
338
collation(item->collation),
323
cmp_context(item->cmp_context)
339
cmp_context(item->cmp_context),
325
342
/* Put this item in the session's free list */
326
next= session->free_list;
327
session->free_list= this;
343
next= getSession().free_list;
344
getSession().free_list= this;
330
347
uint32_t Item::float_length(uint32_t decimals_par) const
337
354
Item_result restype= result_type();
339
356
if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
340
return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
357
return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
341
358
(uint32_t) DECIMAL_MAX_PRECISION);
342
359
return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
345
362
int Item::decimal_int_part() const
347
return my_decimal_int_part(decimal_precision(), decimals);
364
return class_decimal_int_part(decimal_precision(), decimals);
350
void Item::print(String *str, enum_query_type)
367
void Item::print(String *str)
352
369
str->append(full_name());
355
void Item::print_item_w_name(String *str, enum_query_type query_type)
372
void Item::print_item_w_name(String *str)
357
print(str, query_type);
422
439
if (orig_len != length && ! is_autogenerated_name)
425
push_warning_printf(current_session,
442
push_warning_printf(&getSession(),
426
443
DRIZZLE_ERROR::WARN_LEVEL_WARN,
427
444
ER_NAME_BECOMES_EMPTY,
428
445
ER(ER_NAME_BECOMES_EMPTY),
429
446
str + length - orig_len);
431
push_warning_printf(current_session,
448
push_warning_printf(&getSession(),
432
449
DRIZZLE_ERROR::WARN_LEVEL_WARN,
433
450
ER_REMOVED_SPACES,
434
451
ER(ER_REMOVED_SPACES),
451
468
! my_strcasecmp(system_charset_info, name, item->name);
454
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
471
Item *Item::safe_charset_converter(const charset_info_st * const tocs)
456
473
Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
457
474
return conv->safe ? conv : NULL;
460
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
477
bool Item::get_date(type::Time <ime,uint32_t fuzzydate)
462
if (result_type() == STRING_RESULT)
465
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
466
if (!(res=val_str(&tmp)) ||
467
str_to_datetime_with_warn(res->ptr(), res->length(),
468
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
473
int64_t value= val_int();
475
if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
478
end= internal::int64_t10_to_str(value, buff, -10);
479
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
480
buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
488
memset(ltime, 0, sizeof(*ltime));
485
else if (result_type() == STRING_RESULT)
487
char buff[type::Time::MAX_STRING_LENGTH];
488
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
489
if (!(res=val_str(&tmp)) ||
490
str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
491
<ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
498
int64_t value= val_int();
499
type::datetime_t date_value;
501
ltime.convert(date_value, value, fuzzydate);
503
if (not type::is_valid(date_value))
505
char buff[DECIMAL_LONGLONG_DIGITS], *end;
506
end= internal::int64_t10_to_str(value, buff, -10);
507
make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
508
buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
492
bool Item::get_time(DRIZZLE_TIME *ltime)
521
bool Item::get_time(type::Time <ime)
523
char buff[type::Time::MAX_STRING_LENGTH];
495
524
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
496
if (!(res=val_str(&tmp)) ||
497
str_to_time_with_warn(res->ptr(), res->length(), ltime))
525
if (!(res=val_str(&tmp)) or
526
str_to_time_with_warn(&getSession(), res->ptr(), res->length(), <ime))
499
memset(ltime, 0, sizeof(*ltime));
505
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
536
bool Item::get_date_result(type::Time <ime,uint32_t fuzzydate)
507
return get_date(ltime,fuzzydate);
538
return get_date(ltime, fuzzydate);
510
541
bool Item::is_null()
753
784
Item_ref to allow fields from view being stored in tmp table.
755
786
Item_aggregate_ref *item_ref;
756
uint32_t el= fields.elements;
787
uint32_t el= fields.size();
757
788
Item *real_itm= real_item();
759
790
ref_pointer_array[el]= real_itm;
760
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
791
if (!(item_ref= new Item_aggregate_ref(&session->lex().current_select->context,
761
792
ref_pointer_array + el, 0, name)))
762
793
return; /* fatal_error is set */
763
794
if (type() == SUM_FUNC_ITEM)
764
795
item_ref->depended_from= ((Item_sum *) this)->depended_from();
765
796
fields.push_front(real_itm);
766
session->change_item_tree(ref, item_ref);
1058
1091
bool Item::is_datetime()
1060
switch (field_type())
1062
case DRIZZLE_TYPE_DATE:
1063
case DRIZZLE_TYPE_DATETIME:
1064
case DRIZZLE_TYPE_TIMESTAMP:
1066
case DRIZZLE_TYPE_BLOB:
1067
case DRIZZLE_TYPE_VARCHAR:
1068
case DRIZZLE_TYPE_DOUBLE:
1069
case DRIZZLE_TYPE_DECIMAL:
1070
case DRIZZLE_TYPE_ENUM:
1071
case DRIZZLE_TYPE_LONG:
1072
case DRIZZLE_TYPE_LONGLONG:
1073
case DRIZZLE_TYPE_NULL:
1074
case DRIZZLE_TYPE_UUID:
1093
return field::isDateTime(field_type());
1082
1096
String *Item::check_well_formed_result(String *str, bool send_error)
1084
1098
/* Check whether we got a well-formed string */
1085
const CHARSET_INFO * const cs= str->charset();
1099
const charset_info_st * const cs= str->charset();
1086
1100
int well_formed_error;
1087
1101
uint32_t wlen= cs->cset->well_formed_len(cs,
1088
1102
str->ptr(), str->ptr() + str->length(),
1089
1103
str->length(), &well_formed_error);
1090
1104
if (wlen < str->length())
1092
Session *session= current_session;
1093
1106
char hexbuf[7];
1094
1107
enum DRIZZLE_ERROR::enum_warning_level level;
1095
1108
uint32_t diff= str->length() - wlen;
1109
push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1122
push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
1110
1123
ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1115
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
1128
bool Item::eq_by_collation(Item *item, bool binary_cmp, const charset_info_st * const cs)
1117
const CHARSET_INFO *save_cs= 0;
1118
const CHARSET_INFO *save_item_cs= 0;
1130
const charset_info_st *save_cs= 0;
1131
const charset_info_st *save_item_cs= 0;
1119
1132
if (collation.collation != cs)
1121
1134
save_cs= collation.collation;
1186
1197
name, decimals, 0, unsigned_flag);
1188
1199
case DRIZZLE_TYPE_NULL:
1189
field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1200
field= new Field_null((unsigned char*) 0, max_length, name);
1191
1202
case DRIZZLE_TYPE_DATE:
1192
field= new Field_date(maybe_null, name, &my_charset_bin);
1203
field= new Field_date(maybe_null, name);
1206
case DRIZZLE_TYPE_MICROTIME:
1207
field= new field::Microtime(maybe_null, name);
1194
1210
case DRIZZLE_TYPE_TIMESTAMP:
1195
field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1211
field= new field::Epoch(maybe_null, name);
1197
1213
case DRIZZLE_TYPE_DATETIME:
1198
field= new Field_datetime(maybe_null, name, &my_charset_bin);
1214
field= new Field_datetime(maybe_null, name);
1216
case DRIZZLE_TYPE_TIME:
1217
field= new field::Time(maybe_null, name);
1219
case DRIZZLE_TYPE_BOOLEAN:
1200
1220
case DRIZZLE_TYPE_UUID:
1201
1221
case DRIZZLE_TYPE_ENUM:
1202
1222
case DRIZZLE_TYPE_VARCHAR:
1431
uint32_t Item::max_char_length() const
1433
return max_length / collation.collation->mbmaxlen;
1436
void Item::fix_length_and_charset(uint32_t max_char_length_arg, charset_info_st *cs)
1438
max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1439
collation.collation= cs;
1442
void Item::fix_char_length(uint32_t max_char_length_arg)
1444
max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1447
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
1449
uint64_t max_result_length= max_char_length_arg *
1450
collation.collation->mbmaxlen;
1452
if (max_result_length >= MAX_BLOB_WIDTH)
1454
max_length= MAX_BLOB_WIDTH;
1459
max_length= max_result_length;
1463
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
1465
collation.set(&my_charset_bin);
1466
fix_char_length(max_char_length_arg);
1401
1469
Item_result item_cmp_type(Item_result a,Item_result b)
1403
1471
if (a == STRING_RESULT && b == STRING_RESULT)
1530
1598
if (res_type == DECIMAL_RESULT)
1532
my_decimal item_buf, *item_val,
1600
type::Decimal item_buf, *item_val,
1533
1601
field_buf, *field_val;
1534
1602
item_val= item->val_decimal(&item_buf);
1535
1603
if (item->null_value)
1536
1604
return 1; // This must be true
1537
1605
field_val= field->val_decimal(&field_buf);
1538
return !my_decimal_cmp(item_val, field_val);
1606
return !class_decimal_cmp(item_val, field_val);
1541
1609
double result= item->val_real();
1592
1660
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1593
1661
Int32 -> make them field::Int64.
1595
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1596
new_field=new field::Int64(item->max_length, maybe_null,
1597
item->name, item->unsigned_flag);
1663
if (item->unsigned_flag)
1665
new_field= new field::Size(item->max_length, maybe_null,
1666
item->name, item->unsigned_flag);
1668
else if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1670
new_field= new field::Int64(item->max_length, maybe_null,
1671
item->name, item->unsigned_flag);
1599
new_field=new field::Int32(item->max_length, maybe_null,
1600
item->name, item->unsigned_flag);
1675
new_field= new field::Int32(item->max_length, maybe_null,
1676
item->name, item->unsigned_flag);
1603
1681
case STRING_RESULT:
1604
1682
assert(item->collation.collation);
1606
enum enum_field_types type;
1608
1685
DATE/TIME fields have STRING_RESULT result type.
1609
1686
To preserve type they needed to be handled separately.
1611
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1612
type == DRIZZLE_TYPE_DATE ||
1613
type == DRIZZLE_TYPE_TIMESTAMP)
1688
if (field::isDateTime(item->field_type()))
1615
1690
new_field= item->tmp_table_field_from_field_type(table, 1);
1800
1875
std::ostream& operator<<(std::ostream& output, const Item &item)
1803
output << item.name;
1805
output << drizzled::display::type(item.type());
1877
switch (item.type())
1879
case drizzled::Item::SUBSELECT_ITEM :
1880
case drizzled::Item::FIELD_ITEM :
1881
case drizzled::Item::SUM_FUNC_ITEM :
1882
case drizzled::Item::STRING_ITEM :
1883
case drizzled::Item::INT_ITEM :
1884
case drizzled::Item::REAL_ITEM :
1885
case drizzled::Item::NULL_ITEM :
1886
case drizzled::Item::VARBIN_ITEM :
1887
case drizzled::Item::COPY_STR_ITEM :
1888
case drizzled::Item::FIELD_AVG_ITEM :
1889
case drizzled::Item::DEFAULT_VALUE_ITEM :
1890
case drizzled::Item::PROC_ITEM :
1891
case drizzled::Item::COND_ITEM :
1892
case drizzled::Item::REF_ITEM :
1893
case drizzled::Item::FIELD_STD_ITEM :
1894
case drizzled::Item::FIELD_VARIANCE_ITEM :
1895
case drizzled::Item::INSERT_VALUE_ITEM :
1896
case drizzled::Item::ROW_ITEM:
1897
case drizzled::Item::CACHE_ITEM :
1898
case drizzled::Item::TYPE_HOLDER :
1899
case drizzled::Item::PARAM_ITEM :
1900
case drizzled::Item::DECIMAL_ITEM :
1901
case drizzled::Item::FUNC_ITEM :
1902
case drizzled::Item::BOOLEAN_ITEM :
1905
output << item.full_name();
1907
output << drizzled::display::type(item.type());
1808
1913
return output; // for multiple << operators.