1072
Create an item from a string we KNOW points to a valid int64_t
1073
end \\0 terminated number string.
1074
This is always 'signed'. Unsigned values are created with Item_uint()
1077
Item_int::Item_int(const char *str_arg, uint32_t length)
1079
char *end_ptr= (char*) str_arg + length;
1081
value= my_strtoll10(str_arg, &end_ptr, &error);
1082
max_length= (uint) (end_ptr - str_arg);
1083
name= (char*) str_arg;
1088
my_decimal *Item_int::val_decimal(my_decimal *decimal_value)
1090
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
1091
return decimal_value;
1094
String *Item_int::val_str(String *str)
1096
// following assert is redundant, because fixed=1 assigned in constructor
1098
str->set(value, &my_charset_bin);
1102
void Item_int::print(String *str, enum_query_type)
1104
// my_charset_bin is good enough for numbers
1105
str_value.set(value, &my_charset_bin);
1106
str->append(str_value);
1110
Item_uint::Item_uint(const char *str_arg, uint32_t length):
1111
Item_int(str_arg, length)
1117
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
1118
Item_int(str_arg, i, length)
1124
String *Item_uint::val_str(String *str)
1126
// following assert is redundant, because fixed=1 assigned in constructor
1128
str->set((uint64_t) value, &my_charset_bin);
1133
void Item_uint::print(String *str, enum_query_type)
1135
// latin1 is good enough for numbers
1136
str_value.set((uint64_t) value, default_charset());
1137
str->append(str_value);
1141
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1142
const CHARSET_INFO * const charset)
1144
str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1145
name= (char*) str_arg;
1146
decimals= (uint8_t) decimal_value.frac;
1148
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1149
decimals, unsigned_flag);
1152
Item_decimal::Item_decimal(int64_t val, bool unsig)
1154
int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1155
decimals= (uint8_t) decimal_value.frac;
1157
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1158
decimals, unsigned_flag);
1162
Item_decimal::Item_decimal(double val, int, int)
1164
double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1165
decimals= (uint8_t) decimal_value.frac;
1167
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1168
decimals, unsigned_flag);
1172
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1173
uint32_t decimal_par, uint32_t length)
1175
my_decimal2decimal(val_arg, &decimal_value);
1177
decimals= (uint8_t) decimal_par;
1183
Item_decimal::Item_decimal(my_decimal *value_par)
1185
my_decimal2decimal(value_par, &decimal_value);
1186
decimals= (uint8_t) decimal_value.frac;
1188
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1189
decimals, unsigned_flag);
1193
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
1195
binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1196
&decimal_value, precision, scale);
1197
decimals= (uint8_t) decimal_value.frac;
1199
max_length= my_decimal_precision_to_length(precision, decimals,
1204
int64_t Item_decimal::val_int()
1207
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1211
double Item_decimal::val_real()
1214
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1218
String *Item_decimal::val_str(String *result)
1220
result->set_charset(&my_charset_bin);
1221
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1225
void Item_decimal::print(String *str, enum_query_type)
1227
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1228
str->append(str_value);
1232
bool Item_decimal::eq(const Item *item, bool) const
1234
if (type() == item->type() && item->basic_const_item())
1237
We need to cast off const to call val_decimal(). This should
1238
be OK for a basic constant. Additionally, we can pass 0 as
1239
a true decimal constant will return its internal decimal
1240
storage and ignore the argument.
1242
Item *arg= (Item*) item;
1243
my_decimal *value= arg->val_decimal(0);
1244
return !my_decimal_cmp(&decimal_value, value);
1250
void Item_decimal::set_decimal_value(my_decimal *value_par)
1252
my_decimal2decimal(value_par, &decimal_value);
1253
decimals= (uint8_t) decimal_value.frac;
1254
unsigned_flag= !decimal_value.sign();
1255
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1256
decimals, unsigned_flag);
1260
String *Item_float::val_str(String *str)
1262
// following assert is redundant, because fixed=1 assigned in constructor
1264
str->set_real(value,decimals,&my_charset_bin);
1269
int64_t Item_float::val_int()
1272
if (value <= (double) INT64_MIN)
1276
else if (value >= (double) (uint64_t) INT64_MAX)
1280
return (int64_t) rint(value);
1283
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1285
// following assert is redundant, because fixed=1 assigned in constructor
1287
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1288
return (decimal_value);
1292
void Item_string::print(String *str, enum_query_type query_type)
1294
if (query_type == QT_ORDINARY && is_cs_specified())
1297
str->append(collation.collation->csname);
1302
if (query_type == QT_ORDINARY ||
1303
my_charset_same(str_value.charset(), system_charset_info))
1305
str_value.print(str);
1309
Session *session= current_session;
1310
LEX_STRING utf8_lex_str;
1312
session->convert_string(&utf8_lex_str,
1313
system_charset_info,
1314
str_value.c_ptr_safe(),
1316
str_value.charset());
1318
String utf8_str(utf8_lex_str.str,
1319
utf8_lex_str.length,
1320
system_charset_info);
1322
utf8_str.print(str);
1329
double Item_string::val_real()
1333
char *end, *org_end;
1335
const CHARSET_INFO * const cs= str_value.charset();
1337
org_end= (char*) str_value.ptr() + str_value.length();
1338
tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
1340
if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
1343
We can use str_value.ptr() here as Item_string is gurantee to put an
1346
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1347
ER_TRUNCATED_WRONG_VALUE,
1348
ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
1357
Give error if we wanted a signed integer and we got an unsigned one
1359
int64_t Item_string::val_int()
1364
char *end= (char*) str_value.ptr()+ str_value.length();
1366
const CHARSET_INFO * const cs= str_value.charset();
1368
tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
1370
TODO: Give error if we wanted a signed integer and we got an unsigned
1374
(end != org_end && !check_if_only_end_space(cs, end, org_end)))
1376
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1377
ER_TRUNCATED_WRONG_VALUE,
1378
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
1385
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
1387
return val_decimal_from_string(decimal_value);
1391
bool Item_null::eq(const Item *item, bool) const
1392
{ return item->type() == type(); }
1395
double Item_null::val_real()
1397
// following assert is redundant, because fixed=1 assigned in constructor
1402
int64_t Item_null::val_int()
1404
// following assert is redundant, because fixed=1 assigned in constructor
1410
String *Item_null::val_str(String *)
1412
// following assert is redundant, because fixed=1 assigned in constructor
1418
my_decimal *Item_null::val_decimal(my_decimal *)
1424
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
1426
collation.set(tocs);
1430
/*********************** Item_param related ******************************/
1433
Default function of Item_param::set_param_func, so in case
1434
of malformed packet the server won't SIGSEGV.
1438
default_set_param_func(Item_param *param, unsigned char **, ulong)
1444
Item_param::Item_param(uint32_t pos_in_query_arg) :
1446
item_result_type(STRING_RESULT),
1447
/* Don't pretend to be a literal unless value for this item is set. */
1448
item_type(PARAM_ITEM),
1449
param_type(DRIZZLE_TYPE_VARCHAR),
1450
pos_in_query(pos_in_query_arg),
1451
set_param_func(default_set_param_func),
1452
limit_clause_param(false)
1456
Since we can't say whenever this item can be NULL or cannot be NULL
1457
before mysql_stmt_execute(), so we assuming that it can be NULL until
1461
cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
1462
cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
1466
void Item_param::set_null()
1468
/* These are cleared after each execution by reset() method */
1471
Because of NULL and string values we need to set max_length for each new
1472
placeholder value: user can submit NULL for any placeholder type, and
1473
string length can be different in each execution.
1478
item_type= Item::NULL_ITEM;
1482
void Item_param::set_int(int64_t i, uint32_t max_length_arg)
1484
value.integer= (int64_t) i;
1486
max_length= max_length_arg;
1492
void Item_param::set_double(double d)
1496
max_length= DBL_DIG + 8;
1497
decimals= NOT_FIXED_DEC;
1504
Set decimal parameter value from string.
1506
@param str character string
1507
@param length string length
1510
As we use character strings to send decimal values in
1511
binary protocol, we use str2my_decimal to convert it to
1512
internal decimal value.
1515
void Item_param::set_decimal(char *str, ulong length)
1520
str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
1521
state= DECIMAL_VALUE;
1522
decimals= decimal_value.frac;
1523
max_length= my_decimal_precision_to_length(decimal_value.precision(),
1524
decimals, unsigned_flag);
1531
Set parameter value from DRIZZLE_TIME value.
1533
@param tm datetime value to set (time_type is ignored)
1534
@param type type of datetime value
1535
@param max_length_arg max length of datetime value as string
1538
If we value to be stored is not normalized, zero value will be stored
1539
instead and proper warning will be produced. This function relies on
1540
the fact that even wrong value sent over binary protocol fits into
1541
MAX_DATE_STRING_REP_LENGTH buffer.
1543
void Item_param::set_time(DRIZZLE_TIME *tm,
1544
enum enum_drizzle_timestamp_type time_type,
1545
uint32_t max_length_arg)
1548
value.time.time_type= time_type;
1550
if (value.time.year > 9999 || value.time.month > 12 ||
1551
value.time.day > 31 ||
1552
((time_type != DRIZZLE_TIMESTAMP_TIME) && value.time.hour > 23) ||
1553
value.time.minute > 59 || value.time.second > 59)
1555
char buff[MAX_DATE_STRING_REP_LENGTH];
1556
uint32_t length= my_TIME_to_str(&value.time, buff);
1557
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1558
buff, length, time_type, 0);
1559
set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
1564
max_length= max_length_arg;
1570
bool Item_param::set_str(const char *str, ulong length)
1573
Assign string with no conversion: data is converted only after it's
1574
been written to the binary log.
1576
uint32_t dummy_errors;
1577
if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
1580
state= STRING_VALUE;
1583
/* max_length and decimals are set after charset conversion */
1584
/* sic: str may be not null-terminated */
1589
bool Item_param::set_longdata(const char *str, ulong length)
1592
If client character set is multibyte, end of long data packet
1593
may hit at the middle of a multibyte character. Additionally,
1594
if binary log is open we must write long data value to the
1595
binary log in character set of client. This is why we can't
1596
convert long data to connection character set as it comes
1597
(here), and first have to concatenate all pieces together,
1598
write query to the binary log and only then perform conversion.
1600
if (str_value.append(str, length, &my_charset_bin))
1602
state= LONG_DATA_VALUE;
1610
Set parameter value from user variable value.
1612
@param session Current thread
1613
@param entry User variable structure (NULL means use NULL value)
1621
bool Item_param::set_from_user_var(Session *session, const user_var_entry *entry)
1623
if (entry && entry->value)
1625
item_result_type= entry->type;
1626
unsigned_flag= entry->unsigned_flag;
1627
if (limit_clause_param)
1630
set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
1631
item_type= Item::INT_ITEM;
1632
return(!unsigned_flag && value.integer < 0 ? 1 : 0);
1634
switch (item_result_type) {
1636
set_double(*(double*)entry->value);
1637
item_type= Item::REAL_ITEM;
1640
set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
1641
item_type= Item::INT_ITEM;
1645
const CHARSET_INFO * const fromcs= entry->collation.collation;
1646
const CHARSET_INFO * const tocs= session->variables.collation_connection;
1647
uint32_t dummy_offset;
1649
value.cs_info.character_set_of_placeholder=
1650
value.cs_info.character_set_client= fromcs;
1652
Setup source and destination character sets so that they
1653
are different only if conversion is necessary: this will
1654
make later checks easier.
1656
value.cs_info.final_character_set_of_str_value=
1657
String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
1660
Exact value of max_length is not known unless data is converted to
1661
charset of connection, so we have to set it later.
1663
item_type= Item::STRING_ITEM;
1665
if (set_str((const char *)entry->value, entry->length))
1669
case DECIMAL_RESULT:
1671
const my_decimal *ent_value= (const my_decimal *)entry->value;
1672
my_decimal2decimal(ent_value, &decimal_value);
1673
state= DECIMAL_VALUE;
1674
decimals= ent_value->frac;
1675
max_length= my_decimal_precision_to_length(ent_value->precision(),
1676
decimals, unsigned_flag);
1677
item_type= Item::DECIMAL_ITEM;
1692
Resets parameter after execution.
1695
We clear null_value here instead of setting it in set_* methods,
1696
because we want more easily handle case for long data.
1699
void Item_param::reset()
1701
/* Shrink string buffer if it's bigger than max possible CHAR column */
1702
if (str_value.alloced_length() > MAX_CHAR_WIDTH)
1705
str_value.length(0);
1706
str_value_ptr.length(0);
1708
We must prevent all charset conversions until data has been written
1711
str_value.set_charset(&my_charset_bin);
1712
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1717
Don't reset item_type to PARAM_ITEM: it's only needed to guard
1718
us from item optimizations at prepare stage, when item doesn't yet
1719
contain a literal of some kind.
1720
In all other cases when this object is accessed its value is
1721
set (this assumption is guarded by 'state' and
1722
assertS(state != NO_VALUE) in all Item_param::get_*
1729
int Item_param::save_in_field(Field *field, bool no_conversions)
1731
field->set_notnull();
1735
return field->store(value.integer, unsigned_flag);
1737
return field->store(value.real);
1739
return field->store_decimal(&decimal_value);
1741
field->store_time(&value.time, value.time.time_type);
1744
case LONG_DATA_VALUE:
1745
return field->store(str_value.ptr(), str_value.length(),
1746
str_value.charset());
1748
return set_field_to_null_with_conversions(field, no_conversions);
1757
bool Item_param::get_time(DRIZZLE_TIME *res)
1759
if (state == TIME_VALUE)
1765
If parameter value isn't supplied assertion will fire in val_str()
1766
which is called from Item::get_time().
1768
return Item::get_time(res);
1772
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
1774
if (state == TIME_VALUE)
1779
return Item::get_date(res, fuzzydate);
1783
double Item_param::val_real()
1789
return (double) value.integer;
1793
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1797
case LONG_DATA_VALUE:
1801
return my_strntod(str_value.charset(), (char*) str_value.ptr(),
1802
str_value.length(), &end_not_used, &dummy_err);
1806
This works for example when user says SELECT ?+0.0 and supplies
1807
time value for the placeholder.
1809
return uint64_t2double(TIME_to_uint64_t(&value.time));
1819
int64_t Item_param::val_int()
1823
return (int64_t) rint(value.real);
1825
return value.integer;
1829
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
1833
case LONG_DATA_VALUE:
1836
return my_strntoll(str_value.charset(), str_value.ptr(),
1837
str_value.length(), 10, (char**) 0, &dummy_err);
1840
return (int64_t) TIME_to_uint64_t(&value.time);
1850
my_decimal *Item_param::val_decimal(my_decimal *dec)
1854
return &decimal_value;
1856
double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
1859
int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
1862
case LONG_DATA_VALUE:
1863
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
1867
int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
1868
int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
1880
String *Item_param::val_str(String* str)
1884
case LONG_DATA_VALUE:
1885
return &str_value_ptr;
1887
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
1890
str->set(value.integer, &my_charset_bin);
1893
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
1899
if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
1901
str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
1902
str->set_charset(&my_charset_bin);
1914
Return Param item values in string format, for generating the dynamic
1915
query used in update/binary logs.
1918
- Change interface and implementation to fill log data in place
1919
and avoid one more memcpy/alloc between str and log string.
1920
- In case of error we need to notify replication
1921
that binary log contains wrong statement
1924
const String *Item_param::query_val_str(String* str) const
1928
str->set_int(value.integer, unsigned_flag, &my_charset_bin);
1931
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
1934
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
1936
return &my_null_string;
1943
TODO: in case of error we need to notify replication
1944
that binary log contains wrong statement
1946
if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
1949
/* Create date string inplace */
1950
buf= str->c_ptr_quick();
1953
ptr+= (uint) my_TIME_to_str(&value.time, ptr);
1955
str->length((uint32_t) (ptr - buf));
1959
case LONG_DATA_VALUE:
1962
append_query_string(value.cs_info.character_set_client, &str_value, str);
1966
return &my_null_string;
1975
Convert string from client character set to the character set of
1979
bool Item_param::convert_str_value(Session *session)
1982
if (state == STRING_VALUE || state == LONG_DATA_VALUE)
1985
Check is so simple because all charsets were set up properly
1986
in setup_one_conversion_function, where typecode of
1987
placeholder was also taken into account: the variables are different
1988
here only if conversion is really necessary.
1990
if (value.cs_info.final_character_set_of_str_value !=
1991
value.cs_info.character_set_of_placeholder)
1993
rc= session->convert_string(&str_value,
1994
value.cs_info.character_set_of_placeholder,
1995
value.cs_info.final_character_set_of_str_value);
1998
str_value.set_charset(value.cs_info.final_character_set_of_str_value);
1999
/* Here str_value is guaranteed to be in final_character_set_of_str_value */
2001
max_length= str_value.length();
2004
str_value_ptr is returned from val_str(). It must be not alloced
2005
to prevent it's modification by val_str() invoker.
2007
str_value_ptr.set(str_value.ptr(), str_value.length(),
2008
str_value.charset());
2009
/* Synchronize item charset with value charset */
2010
collation.set(str_value.charset(), DERIVATION_COERCIBLE);
2016
bool Item_param::basic_const_item() const
2018
if (state == NO_VALUE || state == TIME_VALUE)
2025
Item_param::clone_item()
2027
/* see comments in the header file */
2030
return new Item_null(name);
2032
return (unsigned_flag ?
2033
new Item_uint(name, value.integer, max_length) :
2034
new Item_int(name, value.integer, max_length));
2036
return new Item_float(name, value.real, decimals, max_length);
2038
case LONG_DATA_VALUE:
2039
return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2040
str_value.charset());
2052
Item_param::eq(const Item *arg, bool binary_cmp) const
2055
if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2058
We need to cast off const to call val_int(). This should be OK for
2067
return value.integer == item->val_int() &&
2068
unsigned_flag == item->unsigned_flag;
2070
return value.real == item->val_real();
2072
case LONG_DATA_VALUE:
2074
return !stringcmp(&str_value, &item->str_value);
2075
return !sortcmp(&str_value, &item->str_value, collation.collation);
2082
/* End of Item_param related */
2084
void Item_param::print(String *str, enum_query_type)
2086
if (state == NO_VALUE)
2092
char buffer[STRING_BUFFER_USUAL_SIZE];
2093
String tmp(buffer, sizeof(buffer), &my_charset_bin);
2095
res= query_val_str(&tmp);
2101
968
/****************************************************************************
2102
969
Item_copy_string
2103
970
****************************************************************************/