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/item/copy_string.h>
32
#include <drizzled/function/str/conv_charset.h>
33
#include <drizzled/virtual_column_info.h>
34
#include <drizzled/sql_base.h>
37
#include <drizzled/field/str.h>
38
#include <drizzled/field/longstr.h>
39
#include <drizzled/field/num.h>
40
#include <drizzled/field/blob.h>
41
#include <drizzled/field/enum.h>
42
#include <drizzled/field/null.h>
43
#include <drizzled/field/date.h>
44
#include <drizzled/field/decimal.h>
45
#include <drizzled/field/real.h>
46
#include <drizzled/field/double.h>
47
#include <drizzled/field/long.h>
48
#include <drizzled/field/int64_t.h>
49
#include <drizzled/field/num.h>
50
#include <drizzled/field/timetype.h>
51
#include <drizzled/field/timestamp.h>
52
#include <drizzled/field/datetime.h>
53
#include <drizzled/field/varstring.h>
56
#if defined(CMATH_NAMESPACE)
57
using namespace CMATH_NAMESPACE;
66
60
const String my_null_string("NULL", 4, default_charset_info);
62
/*****************************************************************************
64
*****************************************************************************/
68
66
bool Item::is_expensive_processor(unsigned char *)
142
145
String *Item::val_string_from_decimal(String *str)
144
type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
147
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);
151
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
152
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
153
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
157
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
155
159
double nr= val_real();
159
double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
163
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
160
164
return (decimal_value);
163
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
168
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
165
170
int64_t nr= val_int();
169
int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
174
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
170
175
return decimal_value;
173
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
179
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
177
183
if (!(res= val_str(&str_value)))
184
return NULL; // NULL or EOM
180
186
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)
187
if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
188
res->ptr(), res->length(), res->charset(),
189
decimal_value) & E_DEC_BAD_NUM)
186
push_warning_printf(&getSession(),
187
DRIZZLE_ERROR::WARN_LEVEL_WARN,
191
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
188
192
ER_TRUNCATED_WRONG_VALUE,
189
193
ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
190
194
str_value.c_ptr());
192
196
return decimal_value;
195
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
200
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
199
if (get_date(ltime, TIME_FUZZY_DATE))
204
if (get_date(<ime, TIME_FUZZY_DATE))
201
decimal_value->set_zero();
206
my_decimal_set_zero(decimal_value);
202
207
null_value= 1; // set NULL, stop processing
205
return date2_class_decimal(<ime, decimal_value);
210
return date2my_decimal(<ime, decimal_value);
208
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
214
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
218
if (get_time(<ime))
214
decimal_value->set_zero();
220
my_decimal_set_zero(decimal_value);
217
return date2_class_decimal(<ime, decimal_value);
223
return date2my_decimal(<ime, decimal_value);
220
227
double Item::val_real_from_decimal()
222
229
/* Note that fix_fields may not be called for Item_avg_field items */
224
type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
231
my_decimal value_buff, *dec_val= val_decimal(&value_buff);
227
class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
234
my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
231
239
int64_t Item::val_int_from_decimal()
233
241
/* Note that fix_fields may not be called for Item_avg_field items */
235
type::Decimal value, *dec_val= val_decimal(&value);
243
my_decimal value, *dec_val= val_decimal(&value);
239
dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
246
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.
250
int Item::save_time_in_field(Field *field)
253
if (get_time(<ime))
254
return set_field_to_null(field);
255
field->set_notnull();
256
return field->store_time(<ime, DRIZZLE_TIMESTAMP_TIME);
260
int Item::save_date_in_field(Field *field)
263
if (get_date(<ime, TIME_FUZZY_DATE))
264
return set_field_to_null(field);
265
field->set_notnull();
266
return field->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
271
Store the string value in field directly
274
Item::save_str_value_in_field()
275
field a pointer to field where to store
276
result the pointer to the string value to be stored
279
The method is used by Item_*::save_in_field implementations
280
when we don't need to calculate the value to store
281
See Item_string::save_in_field() implementation for example
284
Check if the Item is null and stores the NULL or the
285
result value in the field accordingly.
288
Nonzero value if error
272
291
int Item::save_str_value_in_field(Field *field, String *result)
275
294
return set_field_to_null(field);
277
295
field->set_notnull();
279
return field->store(result->ptr(), result->length(), collation.collation);
296
return field->store(result->ptr(), result->length(),
297
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)
302
is_expensive_cache(-1), name(0), orig_name(0), max_length(0), name_length(0),
303
unsigned_flag(false), fixed(0), is_autogenerated_name(true),
304
collation(&my_charset_bin, DERIVATION_COERCIBLE)
309
with_sum_func= false;
310
unsigned_flag= false;
300
313
cmp_context= (Item_result)-1;
302
315
/* Put item in free list so that we can free all items at end */
303
next= getSession().free_list;
304
getSession().free_list= this;
316
Session *session= current_session;
317
next= session->free_list;
318
session->free_list= this;
307
320
Item constructor can be called during execution other then SQL_COM
308
321
command => we should check session->lex->current_select on zero (session->lex
309
322
can be uninitialised)
311
if (getSession().lex->current_select)
324
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++;
326
enum_parsing_place place=
327
session->lex->current_select->parsing_place;
328
if (place == SELECT_LIST ||
330
session->lex->current_select->select_n_having_items++;
335
Constructor used by Item_field, Item_ref & aggregate (sum)
338
Used for duplicating lists in processing queries with temporary
319
341
Item::Item(Session *session, Item *item):
320
342
is_expensive_cache(-1),
321
343
str_value(item->str_value),
322
344
name(item->name),
323
name_length(item->name_length),
324
345
orig_name(item->orig_name),
325
346
max_length(item->max_length),
326
347
marker(item->marker),
327
348
decimals(item->decimals),
329
349
maybe_null(item->maybe_null),
330
350
null_value(item->null_value),
331
351
unsigned_flag(item->unsigned_flag),
332
352
with_sum_func(item->with_sum_func),
333
is_autogenerated_name(item->is_autogenerated_name),
334
with_subselect(item->with_subselect),
335
354
collation(item->collation),
336
cmp_context(item->cmp_context),
355
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;
357
next= session->free_list; // Put in free list
358
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
362
uint32_t Item::decimal_precision() const
351
364
Item_result restype= result_type();
353
366
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);
367
return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
368
(unsigned int)DECIMAL_MAX_PRECISION);
369
return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
359
373
int Item::decimal_int_part() const
361
return class_decimal_int_part(decimal_precision(), decimals);
375
return my_decimal_int_part(decimal_precision(), decimals);
364
379
void Item::print(String *str, enum_query_type)
366
381
str->append(full_name());
369
385
void Item::print_item_w_name(String *str, enum_query_type query_type)
371
387
print(str, query_type);
459
545
for all basic constants we have special checks, and Item_param's
460
546
type() can be only among basic constant types.
462
return type() == item->type() &&
465
! my_strcasecmp(system_charset_info, name, item->name);
548
return type() == item->type() && name && item->name &&
549
!my_strcasecmp(system_charset_info,name,item->name);
468
553
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
470
555
Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
471
556
return conv->safe ? conv : NULL;
474
bool Item::get_date(type::Time <ime,uint32_t fuzzydate)
561
Get the value of the function as a DRIZZLE_TIME structure.
562
As a extra convenience the time structure is reset on error!
565
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);
567
if (result_type() == STRING_RESULT)
570
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
571
if (!(res=val_str(&tmp)) ||
572
str_to_datetime_with_warn(res->ptr(), res->length(),
573
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
578
int64_t value= val_int();
580
if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
583
end= int64_t10_to_str(value, buff, -10);
584
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
585
buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
593
memset(ltime, 0, sizeof(*ltime));
518
bool Item::get_time(type::Time <ime)
598
Get time of first argument.\
600
As a extra convenience the time structure is reset on error!
603
bool Item::get_time(DRIZZLE_TIME *ltime)
520
char buff[type::Time::MAX_STRING_LENGTH];
521
606
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))
607
if (!(res=val_str(&tmp)) ||
608
str_to_time_with_warn(res->ptr(), res->length(), ltime))
610
memset(ltime, 0, sizeof(*ltime));
533
bool Item::get_date_result(type::Time <ime,uint32_t fuzzydate)
617
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
535
return get_date(ltime, fuzzydate);
619
return get_date(ltime,fuzzydate);
538
623
bool Item::is_null()
543
629
void Item::update_null_value ()
545
631
(void) val_int();
548
635
void Item::top_level_item(void)
551
639
void Item::set_result_field(Field *)
554
643
bool Item::is_result_field(void)
559
649
bool Item::is_bool_func(void)
564
655
void Item::save_in_result_field(bool)
567
659
void Item::no_rows_in_result(void)
570
663
Item *Item::copy_or_same(Session *)
575
669
Item *Item::copy_andor_structure(Session *)
580
675
Item *Item::real_item(void)
585
681
const Item *Item::real_item(void) const
590
687
Item *Item::get_tmp_table_item(Session *session)
592
689
return copy_or_same(session);
595
693
const CHARSET_INFO *Item::default_charset()
597
695
return current_session->variables.getCollation();
600
699
const CHARSET_INFO *Item::compare_collation()
605
705
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
607
707
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)
711
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
712
Item_transformer transformer, unsigned char *arg_t)
615
714
if ((this->*analyzer) (arg_p))
616
715
return ((this->*transformer) (arg_t));
620
720
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
622
722
(*traverser)(this, arg);
625
726
bool Item::remove_dependence_processor(unsigned char *)
630
732
bool Item::remove_fixed(unsigned char *)
636
739
bool Item::collect_item_field_processor(unsigned char *)
641
745
bool Item::find_item_in_field_list_processor(unsigned char *)
646
751
bool Item::change_context_processor(unsigned char *)
756
bool Item::reset_query_id_processor(unsigned char *)
651
762
bool Item::register_field_in_read_map(unsigned char *)
768
bool Item::register_field_in_bitmap(unsigned char *)
656
774
bool Item::subst_argument_checker(unsigned char **arg)
1429
Create a field to hold a string value from an item.
1431
If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
1432
If max_length > 0 create a varchar @n
1433
If max_length == 0 create a CHAR(0)
1435
@param table Table for which the field is created
1169
1438
Field *Item::make_string_field(Table *table)
1172
1441
assert(collation.collation);
1173
1442
if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1175
1443
field= new Field_blob(max_length, maybe_null, name,
1176
1444
collation.collation);
1180
table->setVariableWidth();
1181
field= new Field_varstring(max_length, maybe_null, name,
1446
field= new Field_varstring(max_length, maybe_null, name, table->s,
1182
1447
collation.collation);
1186
1450
field->init(table);
1456
Create a field based on field_type of argument.
1458
For now, this is only used to create a field for
1459
IFNULL(x,something) and time functions
1190
1467
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
1193
1470
The field functions defines a field to be not null if null_ptr is not 0
1195
1472
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1198
1475
switch (field_type()) {
1199
case DRIZZLE_TYPE_DECIMAL:
1200
field= new Field_decimal((unsigned char*) 0,
1476
case DRIZZLE_TYPE_NEWDECIMAL:
1477
field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
1478
Field::NONE, name, decimals, 0,
1208
1481
case DRIZZLE_TYPE_LONG:
1209
field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1482
field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1483
name, 0, unsigned_flag);
1211
1485
case DRIZZLE_TYPE_LONGLONG:
1212
field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1486
field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1487
name, 0, unsigned_flag);
1214
1489
case DRIZZLE_TYPE_DOUBLE:
1215
1490
field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1216
name, decimals, 0, unsigned_flag);
1491
name, decimals, 0, unsigned_flag);
1218
1493
case DRIZZLE_TYPE_NULL:
1219
field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1494
field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
1495
name, &my_charset_bin);
1221
1497
case DRIZZLE_TYPE_DATE:
1222
1498
field= new Field_date(maybe_null, name, &my_charset_bin);
1225
case DRIZZLE_TYPE_MICROTIME:
1226
field= new field::Microtime(maybe_null, name);
1500
case DRIZZLE_TYPE_TIME:
1501
field= new Field_time(maybe_null, name, &my_charset_bin);
1229
1503
case DRIZZLE_TYPE_TIMESTAMP:
1230
field= new field::Epoch(maybe_null, name);
1504
field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1232
1506
case DRIZZLE_TYPE_DATETIME:
1233
1507
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:
1510
/* This case should never be chosen */
1512
/* Fall through to make_string_field() */
1240
1513
case DRIZZLE_TYPE_ENUM:
1241
1514
case DRIZZLE_TYPE_VARCHAR:
1242
1515
return make_string_field(table);
1243
1516
case DRIZZLE_TYPE_BLOB:
1517
if (this->type() == Item::TYPE_HOLDER)
1518
field= new Field_blob(max_length, maybe_null, name, collation.collation,
1244
1521
field= new Field_blob(max_length, maybe_null, name, collation.collation);
1245
1522
break; // Blob handled outside of case
1250
1525
field->init(table);
1255
1531
This implementation can lose str_value content, so if the
1256
1532
Item uses str_value to store something, it should
1257
1533
reimplement it's ::save_in_field() as Item_string, for example, does
1259
1536
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)
1597
This is only called from items that is not of type item_field.
1600
bool Item::send(Protocol *protocol, String *buffer)
1379
1602
bool result= false;
1380
1603
enum_field_types f_type;
1382
1605
switch ((f_type=field_type())) {
1383
case DRIZZLE_TYPE_DATE:
1384
1607
case DRIZZLE_TYPE_NULL:
1385
1608
case DRIZZLE_TYPE_ENUM:
1386
1609
case DRIZZLE_TYPE_BLOB:
1387
1610
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());
1611
case DRIZZLE_TYPE_NEWDECIMAL:
1614
if ((res=val_str(buffer)))
1615
result= protocol->store(res->ptr(),res->length(),res->charset());
1397
1618
case DRIZZLE_TYPE_LONG:
1402
result= client->store((int32_t)nr);
1623
result= protocol->store_long(nr);
1405
1626
case DRIZZLE_TYPE_LONGLONG:
1412
result= client->store((uint64_t)nr);
1414
result= client->store((int64_t)nr);
1631
result= protocol->store_int64_t(nr, unsigned_flag);
1418
1634
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);
1636
double nr= val_real();
1638
result= protocol->store(nr, decimals, buffer);
1433
1641
case DRIZZLE_TYPE_DATETIME:
1434
case DRIZZLE_TYPE_MICROTIME:
1435
1642
case DRIZZLE_TYPE_TIMESTAMP:
1645
get_date(&tm, TIME_FUZZY_DATE);
1438
get_date(tm, TIME_FUZZY_DATE);
1440
result= client->store(&tm);
1648
if (f_type == DRIZZLE_TYPE_DATE)
1649
return protocol->store_date(&tm);
1651
result= protocol->store(&tm);
1655
case DRIZZLE_TYPE_TIME:
1660
result= protocol->store_time(&tm);
1444
1664
if (null_value)
1445
result= client->store();
1665
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);
1670
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
1672
return item->type() == DEFAULT_VALUE_ITEM &&
1673
((Item_default_value *)item)->arg->eq(arg, binary_cmp);
1677
bool Item_default_value::fix_fields(Session *session, Item **)
1680
Item_field *field_arg;
1689
if (!arg->fixed && arg->fix_fields(session, &arg))
1693
real_arg= arg->real_item();
1694
if (real_arg->type() != FIELD_ITEM)
1696
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
1700
field_arg= (Item_field *)real_arg;
1701
if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
1703
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
1706
if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
1708
memcpy(def_field, field_arg->field, field_arg->field->size_of());
1709
def_field->move_field_offset((my_ptrdiff_t)
1710
(def_field->table->s->default_values -
1711
def_field->table->record[0]));
1712
set_field(def_field);
1716
context->process_error(session);
1721
void Item_default_value::print(String *str, enum_query_type query_type)
1725
str->append(STRING_WITH_LEN("default"));
1728
str->append(STRING_WITH_LEN("default("));
1729
arg->print(str, query_type);
1734
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
1738
if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
1740
if (field_arg->reset())
1742
my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
1743
ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
1748
push_warning_printf(field_arg->table->in_use,
1749
DRIZZLE_ERROR::WARN_LEVEL_WARN,
1750
ER_NO_DEFAULT_FOR_FIELD,
1751
ER(ER_NO_DEFAULT_FOR_FIELD),
1752
field_arg->field_name);
1756
field_arg->set_default();
1759
return Item_field::save_in_field(field_arg, no_conversions);
1764
This method like the walk method traverses the item tree, but at the
1765
same time it can replace some nodes in the tree.
1768
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
1770
Item *new_item= arg->transform(transformer, args);
1775
Session::change_item_tree() should be called only if the tree was
1776
really transformed, i.e. when a new item has been created.
1777
Otherwise we'll be allocating a lot of unnecessary memory for
1778
change records at each execution.
1780
if (arg != new_item)
1781
current_session->change_item_tree(&arg, new_item);
1782
return (this->*transformer)(args);
1488
1785
Item_result item_cmp_type(Item_result a,Item_result b)
1490
1787
if (a == STRING_RESULT && b == STRING_RESULT)
1491
1788
return STRING_RESULT;
1493
1789
if (a == INT_RESULT && b == INT_RESULT)
1494
1790
return INT_RESULT;
1495
1791
else if (a == ROW_RESULT || b == ROW_RESULT)
1496
1792
return ROW_RESULT;
1498
1793
if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1499
1794
(b == INT_RESULT || b == DECIMAL_RESULT))
1500
1795
return DECIMAL_RESULT;
1502
1796
return REAL_RESULT;
1505
1800
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1507
1802
Item *item= *ref;
1508
1803
Item *new_item= NULL;
1509
1804
if (item->basic_const_item())
1510
return; /* Can't be better */
1805
return; // Can't be better
1511
1806
Item_result res_type=item_cmp_type(comp_item->result_type(),
1512
1807
item->result_type());
1513
char *name=item->name; /* Alloced by memory::sql_alloc */
1808
char *name=item->name; // Alloced by sql_alloc
1515
1810
switch (res_type) {
1516
1811
case STRING_RESULT:
1813
char buff[MAX_FIELD_WIDTH];
1814
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1815
result=item->val_str(&tmp);
1816
if (item->null_value)
1817
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());
1820
uint32_t length= result->length();
1821
char *tmp_str= sql_strmake(result->ptr(), length);
1822
new_item= new Item_string(name, tmp_str, length, result->charset());
1531
1826
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));
1828
int64_t result=item->val_int();
1829
uint32_t length=item->max_length;
1830
bool null_value=item->null_value;
1831
new_item= (null_value ? (Item*) new Item_null(name) :
1832
(Item*) new Item_int(name, result, length));
1540
1835
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).
1836
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1839
Substitute constants only in Item_rows. Don't affect other Items
1840
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));
1842
For such Items more optimal is to detect if it is constant and replace
1843
it with Item_row. This would optimize queries like this:
1844
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1846
Item_row *item_row= (Item_row*) item;
1847
Item_row *comp_item_row= (Item_row*) comp_item;
1851
If item and comp_item are both Item_rows and have same number of cols
1852
then process items in Item_row one by one.
1853
We can't ignore NULL values here as this item may be used with <=>, in
1854
which case NULL's are significant.
1856
assert(item->result_type() == comp_item->result_type());
1857
assert(item_row->cols() == comp_item_row->cols());
1858
col= item_row->cols();
1860
resolve_const_item(session, item_row->addr(col),
1861
comp_item_row->element_index(col));
1570
1865
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));
1866
{ // It must REAL_RESULT
1867
double result= item->val_real();
1868
uint32_t length=item->max_length,decimals=item->decimals;
1869
bool null_value=item->null_value;
1870
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1871
new Item_float(name, result, decimals, length));
1579
1874
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));
1876
my_decimal decimal_value;
1877
my_decimal *result= item->val_decimal(&decimal_value);
1878
uint32_t length= item->max_length, decimals= item->decimals;
1879
bool null_value= item->null_value;
1880
new_item= (null_value ?
1881
(Item*) new Item_null(name) :
1882
(Item*) new Item_decimal(name, result, length, decimals));
1593
1889
session->change_item_tree(ref, new_item);
1893
Return true if the value stored in the field is equal to the const
1896
We need to use this on the range optimizer because in some cases
1897
we can't store the value in the field without some precision/character loss.
1596
1900
bool field_is_equal_to_item(Field *field,Item *item)
1706
2002
To preserve type they needed to be handled separately.
1708
2004
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1709
type == DRIZZLE_TYPE_TIME ||
1710
type == DRIZZLE_TYPE_MICROTIME ||
1711
type == DRIZZLE_TYPE_DATE ||
2005
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
1712
2006
type == DRIZZLE_TYPE_TIMESTAMP)
1714
2007
new_field= item->tmp_table_field_from_field_type(table, 1);
1716
Make sure that the blob fits into a Field_varstring which has
2009
Make sure that the blob fits into a Field_varstring which has
1720
2012
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1721
2013
convert_blob_length <= Field_varstring::MAX_SIZE &&
1722
2014
convert_blob_length)
1724
table->setVariableWidth();
1725
2015
new_field= new Field_varstring(convert_blob_length, maybe_null,
1726
item->name, item->collation.collation);
2016
item->name, table->s,
2017
item->collation.collation);
1730
2019
new_field= item->make_string_field(table);
1732
2020
new_field->set_derivation(item->collation.derivation);
1735
2022
case DECIMAL_RESULT:
2024
uint8_t dec= item->decimals;
2025
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
2026
uint32_t len= item->max_length;
2029
Trying to put too many digits overall in a DECIMAL(prec,dec)
2030
will always throw a warning. We must limit dec to
2031
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;
2036
signed int overflow;
2038
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.
2041
If the value still overflows the field with the corrected dec,
2042
we'll throw out decimals rather than integers. This is still
2043
bad and of course throws a truncation warning.
2044
+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);
2047
overflow= my_decimal_precision_to_length(intg + dec, dec,
2048
item->unsigned_flag) - len;
2051
dec= cmax(0, dec - overflow); // too long, discard fract
2053
len -= item->decimals - dec; // corrected value fits
2056
new_field= new Field_new_decimal(len, maybe_null, item->name,
2057
dec, item->unsigned_flag);
1777
2060
case ROW_RESULT:
1778
2062
// This case should never be choosen
1784
2068
new_field->init(table);
1786
2070
if (copy_func && item->is_result_field())
1787
2071
*((*copy_func)++) = item; // Save for copy_funcs
1789
2072
if (modify_item)
1790
2073
item->set_result_field(new_field);
1792
2074
if (item->type() == Item::NULL_ITEM)
1793
2075
new_field->is_created_from_null_item= true;
1795
2076
return new_field;
1798
Field *create_tmp_field(Session *session,
1804
Field **default_field,
1807
bool make_copy_field,
2079
Field *create_tmp_field(Session *session, Table *table,Item *item,
2080
Item::Type type, Item ***copy_func, Field **from_field,
2081
Field **default_field, bool group, bool modify_item,
2082
bool, bool make_copy_field,
1808
2083
uint32_t convert_blob_length)