35
35
#include "drizzled/field/str.h"
36
36
#include "drizzled/field/num.h"
37
38
#include "drizzled/field/blob.h"
38
#include "drizzled/field/enum.h"
39
#include "drizzled/field/null.h"
40
39
#include "drizzled/field/date.h"
40
#include "drizzled/field/datetime.h"
41
41
#include "drizzled/field/decimal.h"
42
#include "drizzled/field/real.h"
43
42
#include "drizzled/field/double.h"
43
#include "drizzled/field/enum.h"
44
#include "drizzled/field/epoch.h"
44
45
#include "drizzled/field/int32.h"
45
46
#include "drizzled/field/int64.h"
46
#include "drizzled/field/num.h"
47
#include "drizzled/field/timestamp.h"
48
#include "drizzled/field/datetime.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"
49
52
#include "drizzled/field/varstring.h"
50
54
#include "drizzled/internal/m_string.h"
138
142
String *Item::val_string_from_decimal(String *str)
140
my_decimal dec_buf, *dec= val_decimal(&dec_buf);
144
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);
148
class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
149
class_decimal2string(&dec_buf, 0, str);
149
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
153
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
151
155
double nr= val_real();
155
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
159
double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
156
160
return (decimal_value);
159
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
163
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
161
165
int64_t nr= val_int();
165
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
169
int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
166
170
return decimal_value;
169
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
173
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
176
180
end_ptr= (char*) res->ptr()+ res->length();
177
if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
181
if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
181
decimal_value) & E_DEC_BAD_NUM)
184
res->charset()) & E_DEC_BAD_NUM)
183
push_warning_printf(current_session,
186
push_warning_printf(&getSession(),
184
187
DRIZZLE_ERROR::WARN_LEVEL_WARN,
185
188
ER_TRUNCATED_WRONG_VALUE,
186
189
ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
189
192
return decimal_value;
192
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
195
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
196
if (get_date(<ime, TIME_FUZZY_DATE))
199
if (get_date(ltime, TIME_FUZZY_DATE))
198
my_decimal_set_zero(decimal_value);
201
decimal_value->set_zero();
199
202
null_value= 1; // set NULL, stop processing
202
return date2my_decimal(<ime, decimal_value);
205
return date2_class_decimal(<ime, decimal_value);
205
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
208
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
209
if (get_time(<ime))
211
my_decimal_set_zero(decimal_value);
214
decimal_value->set_zero();
214
return date2my_decimal(<ime, decimal_value);
217
return date2_class_decimal(<ime, decimal_value);
217
220
double Item::val_real_from_decimal()
219
222
/* Note that fix_fields may not be called for Item_avg_field items */
221
my_decimal value_buff, *dec_val= val_decimal(&value_buff);
224
type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
224
my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
227
class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
230
233
/* Note that fix_fields may not be called for Item_avg_field items */
232
my_decimal value, *dec_val= val_decimal(&value);
235
type::Decimal value, *dec_val= val_decimal(&value);
235
my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
239
dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
239
int Item::save_time_in_field(Field *field)
244
bool Item::save_time_in_field(Field *field)
242
if (get_time(<ime))
243
249
return set_field_to_null(field);
244
251
field->set_notnull();
245
return field->store_time(<ime, DRIZZLE_TIMESTAMP_TIME);
253
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
248
int Item::save_date_in_field(Field *field)
256
bool Item::save_date_in_field(Field *field)
251
if (get_date(<ime, TIME_FUZZY_DATE))
260
if (get_date(ltime, TIME_FUZZY_DATE))
252
261
return set_field_to_null(field);
253
263
field->set_notnull();
254
return field->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
265
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
281
294
with_sum_func(false),
282
295
is_autogenerated_name(true),
283
296
with_subselect(false),
284
collation(&my_charset_bin, DERIVATION_COERCIBLE)
297
collation(&my_charset_bin, DERIVATION_COERCIBLE),
298
_session(*current_session)
286
300
cmp_context= (Item_result)-1;
288
302
/* 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;
303
next= getSession().free_list;
304
getSession().free_list= this;
294
307
Item constructor can be called during execution other then SQL_COM
295
308
command => we should check session->lex->current_select on zero (session->lex
296
309
can be uninitialised)
298
if (session->lex->current_select)
311
if (getSession().lex->current_select)
300
enum_parsing_place place= session->lex->current_select->parsing_place;
313
enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
301
314
if (place == SELECT_LIST || place == IN_HAVING)
302
session->lex->current_select->select_n_having_items++;
315
getSession().getLex()->current_select->select_n_having_items++;
320
333
is_autogenerated_name(item->is_autogenerated_name),
321
334
with_subselect(item->with_subselect),
322
335
collation(item->collation),
323
cmp_context(item->cmp_context)
336
cmp_context(item->cmp_context),
325
339
/* Put this item in the session's free list */
326
next= session->free_list;
327
session->free_list= this;
340
next= getSession().free_list;
341
getSession().free_list= this;
330
344
uint32_t Item::float_length(uint32_t decimals_par) const
337
351
Item_result restype= result_type();
339
353
if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
340
return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
354
return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
341
355
(uint32_t) DECIMAL_MAX_PRECISION);
342
356
return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
345
359
int Item::decimal_int_part() const
347
return my_decimal_int_part(decimal_precision(), decimals);
361
return class_decimal_int_part(decimal_precision(), decimals);
350
364
void Item::print(String *str, enum_query_type)
422
436
if (orig_len != length && ! is_autogenerated_name)
425
push_warning_printf(current_session,
439
push_warning_printf(&getSession(),
426
440
DRIZZLE_ERROR::WARN_LEVEL_WARN,
427
441
ER_NAME_BECOMES_EMPTY,
428
442
ER(ER_NAME_BECOMES_EMPTY),
429
443
str + length - orig_len);
431
push_warning_printf(current_session,
445
push_warning_printf(&getSession(),
432
446
DRIZZLE_ERROR::WARN_LEVEL_WARN,
433
447
ER_REMOVED_SPACES,
434
448
ER(ER_REMOVED_SPACES),
457
471
return conv->safe ? conv : NULL;
460
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
474
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));
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);
492
bool Item::get_time(DRIZZLE_TIME *ltime)
518
bool Item::get_time(type::Time <ime)
520
char buff[type::Time::MAX_STRING_LENGTH];
495
521
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))
522
if (!(res=val_str(&tmp)) or
523
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)
533
bool Item::get_date_result(type::Time <ime,uint32_t fuzzydate)
507
return get_date(ltime,fuzzydate);
535
return get_date(ltime, fuzzydate);
510
538
bool Item::is_null()
1191
1221
case DRIZZLE_TYPE_DATE:
1192
1222
field= new Field_date(maybe_null, name, &my_charset_bin);
1225
case DRIZZLE_TYPE_MICROTIME:
1226
field= new field::Microtime(maybe_null, name);
1194
1229
case DRIZZLE_TYPE_TIMESTAMP:
1195
field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1230
field= new field::Epoch(maybe_null, name);
1197
1232
case DRIZZLE_TYPE_DATETIME:
1198
1233
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:
1200
1239
case DRIZZLE_TYPE_UUID:
1201
1240
case DRIZZLE_TYPE_ENUM:
1202
1241
case DRIZZLE_TYPE_VARCHAR:
1382
1422
result= client->store(nr, decimals, buffer);
1425
case DRIZZLE_TYPE_TIME:
1430
result= client->store(&tm);
1385
1433
case DRIZZLE_TYPE_DATETIME:
1434
case DRIZZLE_TYPE_MICROTIME:
1386
1435
case DRIZZLE_TYPE_TIMESTAMP:
1389
get_date(&tm, TIME_FUZZY_DATE);
1438
get_date(tm, TIME_FUZZY_DATE);
1390
1439
if (!null_value)
1391
1440
result= client->store(&tm);
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);
1401
1488
Item_result item_cmp_type(Item_result a,Item_result b)
1403
1490
if (a == STRING_RESULT && b == STRING_RESULT)
1530
1617
if (res_type == DECIMAL_RESULT)
1532
my_decimal item_buf, *item_val,
1619
type::Decimal item_buf, *item_val,
1533
1620
field_buf, *field_val;
1534
1621
item_val= item->val_decimal(&item_buf);
1535
1622
if (item->null_value)
1536
1623
return 1; // This must be true
1537
1624
field_val= field->val_decimal(&field_buf);
1538
return !my_decimal_cmp(item_val, field_val);
1625
return !class_decimal_cmp(item_val, field_val);
1541
1628
double result= item->val_real();
1592
1679
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1593
1680
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);
1682
if (item->unsigned_flag)
1684
new_field= new field::Size(item->max_length, maybe_null,
1685
item->name, item->unsigned_flag);
1687
else if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1689
new_field= new field::Int64(item->max_length, maybe_null,
1690
item->name, item->unsigned_flag);
1599
new_field=new field::Int32(item->max_length, maybe_null,
1600
item->name, item->unsigned_flag);
1694
new_field= new field::Int32(item->max_length, maybe_null,
1695
item->name, item->unsigned_flag);
1603
1700
case STRING_RESULT:
1800
1899
std::ostream& operator<<(std::ostream& output, const Item &item)
1803
output << item.name;
1805
output << drizzled::display::type(item.type());
1901
switch (item.type())
1903
case drizzled::Item::SUBSELECT_ITEM :
1904
case drizzled::Item::FIELD_ITEM :
1905
case drizzled::Item::SUM_FUNC_ITEM :
1906
case drizzled::Item::STRING_ITEM :
1907
case drizzled::Item::INT_ITEM :
1908
case drizzled::Item::REAL_ITEM :
1909
case drizzled::Item::NULL_ITEM :
1910
case drizzled::Item::VARBIN_ITEM :
1911
case drizzled::Item::COPY_STR_ITEM :
1912
case drizzled::Item::FIELD_AVG_ITEM :
1913
case drizzled::Item::DEFAULT_VALUE_ITEM :
1914
case drizzled::Item::PROC_ITEM :
1915
case drizzled::Item::COND_ITEM :
1916
case drizzled::Item::REF_ITEM :
1917
case drizzled::Item::FIELD_STD_ITEM :
1918
case drizzled::Item::FIELD_VARIANCE_ITEM :
1919
case drizzled::Item::INSERT_VALUE_ITEM :
1920
case drizzled::Item::ROW_ITEM:
1921
case drizzled::Item::CACHE_ITEM :
1922
case drizzled::Item::TYPE_HOLDER :
1923
case drizzled::Item::PARAM_ITEM :
1924
case drizzled::Item::DECIMAL_ITEM :
1925
case drizzled::Item::FUNC_ITEM :
1926
case drizzled::Item::BOOLEAN_ITEM :
1929
output << item.full_name();
1931
output << drizzled::display::type(item.type());
1808
1937
return output; // for multiple << operators.