~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2008-12-09 17:33:02 UTC
  • mfrom: (656.1.54 devel)
  • Revision ID: brian@tangent.org-20081209173302-aptngvc7efxnatnt
Merge from Monty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
562
562
}
563
563
 
564
564
 
565
 
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const)
566
 
{
567
 
  Item_string *conv;
568
 
  char buf[64];
569
 
  String *s, tmp(buf, sizeof(buf), &my_charset_bin);
570
 
  s= val_str(&tmp);
571
 
  if ((conv= new Item_static_string_func(func_name, s->ptr(), s->length(),
572
 
                                         s->charset())))
573
 
  {
574
 
    conv->str_value.copy();
575
 
    conv->str_value.mark_as_const();
576
 
  }
577
 
  return conv;
578
 
}
579
 
 
580
 
 
581
 
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
582
 
{
583
 
  Item_string *conv;
584
 
  uint32_t conv_errors;
585
 
  char *ptr;
586
 
  String tmp, cstr, *ostr= val_str(&tmp);
587
 
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
588
 
  if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(),
589
 
                                             cstr.charset(),
590
 
                                             collation.derivation)))
591
 
  {
592
 
    /*
593
 
      Safe conversion is not possible (or EOM).
594
 
      We could not convert a string into the requested character set
595
 
      without data loss. The target charset does not cover all the
596
 
      characters from the string. Operation cannot be done correctly.
597
 
    */
598
 
    return NULL;
599
 
  }
600
 
  if (!(ptr= current_session->strmake(cstr.ptr(), cstr.length())))
601
 
    return NULL;
602
 
  conv->str_value.set(ptr, cstr.length(), cstr.charset());
603
 
  /* Ensure that no one is going to change the result string */
604
 
  conv->str_value.mark_as_const();
605
 
  return conv;
606
 
}
607
 
 
608
 
 
609
 
Item *Item_param::safe_charset_converter(const CHARSET_INFO * const tocs)
610
 
{
611
 
  if (const_item())
612
 
  {
613
 
    uint32_t cnv_errors;
614
 
    String *ostr= val_str(&cnvstr);
615
 
    cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
616
 
                            ostr->charset(), tocs, &cnv_errors);
617
 
    if (cnv_errors)
618
 
       return NULL;
619
 
    cnvitem->str_value.mark_as_const();
620
 
    cnvitem->max_length= cnvitem->str_value.numchars() * tocs->mbmaxlen;
621
 
    return cnvitem;
622
 
  }
623
 
  return NULL;
624
 
}
625
 
 
626
 
 
627
 
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
628
 
{
629
 
  Item_string *conv;
630
 
  uint32_t conv_errors;
631
 
  String tmp, cstr, *ostr= val_str(&tmp);
632
 
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
633
 
  if (conv_errors ||
634
 
      !(conv= new Item_static_string_func(func_name,
635
 
                                          cstr.ptr(), cstr.length(),
636
 
                                          cstr.charset(),
637
 
                                          collation.derivation)))
638
 
  {
639
 
    /*
640
 
      Safe conversion is not possible (or EOM).
641
 
      We could not convert a string into the requested character set
642
 
      without data loss. The target charset does not cover all the
643
 
      characters from the string. Operation cannot be done correctly.
644
 
    */
645
 
    return NULL;
646
 
  }
647
 
  conv->str_value.copy();
648
 
  /* Ensure that no one is going to change the result string */
649
 
  conv->str_value.mark_as_const();
650
 
  return conv;
651
 
}
652
 
 
653
 
 
654
 
bool Item_string::eq(const Item *item, bool binary_cmp) const
655
 
{
656
 
  if (type() == item->type() && item->basic_const_item())
657
 
  {
658
 
    if (binary_cmp)
659
 
      return !stringcmp(&str_value, &item->str_value);
660
 
    return (collation.collation == item->collation.collation &&
661
 
            !sortcmp(&str_value, &item->str_value, collation.collation));
662
 
  }
663
 
  return 0;
664
 
}
665
 
 
666
 
 
667
565
/**
668
566
  Get the value of the function as a DRIZZLE_TIME structure.
669
567
  As a extra convenience the time structure is reset on error!
1067
965
  }
1068
966
}
1069
967
 
1070
 
 
1071
 
/**
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()
1075
 
*/
1076
 
 
1077
 
Item_int::Item_int(const char *str_arg, uint32_t length)
1078
 
{
1079
 
  char *end_ptr= (char*) str_arg + length;
1080
 
  int error;
1081
 
  value= my_strtoll10(str_arg, &end_ptr, &error);
1082
 
  max_length= (uint) (end_ptr - str_arg);
1083
 
  name= (char*) str_arg;
1084
 
  fixed= 1;
1085
 
}
1086
 
 
1087
 
 
1088
 
my_decimal *Item_int::val_decimal(my_decimal *decimal_value)
1089
 
{
1090
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
1091
 
  return decimal_value;
1092
 
}
1093
 
 
1094
 
String *Item_int::val_str(String *str)
1095
 
{
1096
 
  // following assert is redundant, because fixed=1 assigned in constructor
1097
 
  assert(fixed == 1);
1098
 
  str->set(value, &my_charset_bin);
1099
 
  return str;
1100
 
}
1101
 
 
1102
 
void Item_int::print(String *str, enum_query_type)
1103
 
{
1104
 
  // my_charset_bin is good enough for numbers
1105
 
  str_value.set(value, &my_charset_bin);
1106
 
  str->append(str_value);
1107
 
}
1108
 
 
1109
 
 
1110
 
Item_uint::Item_uint(const char *str_arg, uint32_t length):
1111
 
  Item_int(str_arg, length)
1112
 
{
1113
 
  unsigned_flag= 1;
1114
 
}
1115
 
 
1116
 
 
1117
 
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
1118
 
  Item_int(str_arg, i, length)
1119
 
{
1120
 
  unsigned_flag= 1;
1121
 
}
1122
 
 
1123
 
 
1124
 
String *Item_uint::val_str(String *str)
1125
 
{
1126
 
  // following assert is redundant, because fixed=1 assigned in constructor
1127
 
  assert(fixed == 1);
1128
 
  str->set((uint64_t) value, &my_charset_bin);
1129
 
  return str;
1130
 
}
1131
 
 
1132
 
 
1133
 
void Item_uint::print(String *str, enum_query_type)
1134
 
{
1135
 
  // latin1 is good enough for numbers
1136
 
  str_value.set((uint64_t) value, default_charset());
1137
 
  str->append(str_value);
1138
 
}
1139
 
 
1140
 
 
1141
 
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1142
 
                           const CHARSET_INFO * const charset)
1143
 
{
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;
1147
 
  fixed= 1;
1148
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1149
 
                                             decimals, unsigned_flag);
1150
 
}
1151
 
 
1152
 
Item_decimal::Item_decimal(int64_t val, bool unsig)
1153
 
{
1154
 
  int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1155
 
  decimals= (uint8_t) decimal_value.frac;
1156
 
  fixed= 1;
1157
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1158
 
                                             decimals, unsigned_flag);
1159
 
}
1160
 
 
1161
 
 
1162
 
Item_decimal::Item_decimal(double val, int, int)
1163
 
{
1164
 
  double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1165
 
  decimals= (uint8_t) decimal_value.frac;
1166
 
  fixed= 1;
1167
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1168
 
                                             decimals, unsigned_flag);
1169
 
}
1170
 
 
1171
 
 
1172
 
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1173
 
                           uint32_t decimal_par, uint32_t length)
1174
 
{
1175
 
  my_decimal2decimal(val_arg, &decimal_value);
1176
 
  name= (char*) str;
1177
 
  decimals= (uint8_t) decimal_par;
1178
 
  max_length= length;
1179
 
  fixed= 1;
1180
 
}
1181
 
 
1182
 
 
1183
 
Item_decimal::Item_decimal(my_decimal *value_par)
1184
 
{
1185
 
  my_decimal2decimal(value_par, &decimal_value);
1186
 
  decimals= (uint8_t) decimal_value.frac;
1187
 
  fixed= 1;
1188
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1189
 
                                             decimals, unsigned_flag);
1190
 
}
1191
 
 
1192
 
 
1193
 
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
1194
 
{
1195
 
  binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1196
 
                    &decimal_value, precision, scale);
1197
 
  decimals= (uint8_t) decimal_value.frac;
1198
 
  fixed= 1;
1199
 
  max_length= my_decimal_precision_to_length(precision, decimals,
1200
 
                                             unsigned_flag);
1201
 
}
1202
 
 
1203
 
 
1204
 
int64_t Item_decimal::val_int()
1205
 
{
1206
 
  int64_t result;
1207
 
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1208
 
  return result;
1209
 
}
1210
 
 
1211
 
double Item_decimal::val_real()
1212
 
{
1213
 
  double result;
1214
 
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1215
 
  return result;
1216
 
}
1217
 
 
1218
 
String *Item_decimal::val_str(String *result)
1219
 
{
1220
 
  result->set_charset(&my_charset_bin);
1221
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1222
 
  return result;
1223
 
}
1224
 
 
1225
 
void Item_decimal::print(String *str, enum_query_type)
1226
 
{
1227
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1228
 
  str->append(str_value);
1229
 
}
1230
 
 
1231
 
 
1232
 
bool Item_decimal::eq(const Item *item, bool) const
1233
 
{
1234
 
  if (type() == item->type() && item->basic_const_item())
1235
 
  {
1236
 
    /*
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.
1241
 
    */
1242
 
    Item *arg= (Item*) item;
1243
 
    my_decimal *value= arg->val_decimal(0);
1244
 
    return !my_decimal_cmp(&decimal_value, value);
1245
 
  }
1246
 
  return 0;
1247
 
}
1248
 
 
1249
 
 
1250
 
void Item_decimal::set_decimal_value(my_decimal *value_par)
1251
 
{
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);
1257
 
}
1258
 
 
1259
 
 
1260
 
String *Item_float::val_str(String *str)
1261
 
{
1262
 
  // following assert is redundant, because fixed=1 assigned in constructor
1263
 
  assert(fixed == 1);
1264
 
  str->set_real(value,decimals,&my_charset_bin);
1265
 
  return str;
1266
 
}
1267
 
 
1268
 
 
1269
 
int64_t Item_float::val_int()
1270
 
{
1271
 
  assert(fixed == 1);
1272
 
  if (value <= (double) INT64_MIN)
1273
 
  {
1274
 
     return INT64_MIN;
1275
 
  }
1276
 
  else if (value >= (double) (uint64_t) INT64_MAX)
1277
 
  {
1278
 
    return INT64_MAX;
1279
 
  }
1280
 
  return (int64_t) rint(value);
1281
 
}
1282
 
 
1283
 
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1284
 
{
1285
 
  // following assert is redundant, because fixed=1 assigned in constructor
1286
 
  assert(fixed == 1);
1287
 
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1288
 
  return (decimal_value);
1289
 
}
1290
 
 
1291
 
 
1292
 
void Item_string::print(String *str, enum_query_type query_type)
1293
 
{
1294
 
  if (query_type == QT_ORDINARY && is_cs_specified())
1295
 
  {
1296
 
    str->append('_');
1297
 
    str->append(collation.collation->csname);
1298
 
  }
1299
 
 
1300
 
  str->append('\'');
1301
 
 
1302
 
  if (query_type == QT_ORDINARY ||
1303
 
      my_charset_same(str_value.charset(), system_charset_info))
1304
 
  {
1305
 
    str_value.print(str);
1306
 
  }
1307
 
  else
1308
 
  {
1309
 
    Session *session= current_session;
1310
 
    LEX_STRING utf8_lex_str;
1311
 
 
1312
 
    session->convert_string(&utf8_lex_str,
1313
 
                        system_charset_info,
1314
 
                        str_value.c_ptr_safe(),
1315
 
                        str_value.length(),
1316
 
                        str_value.charset());
1317
 
 
1318
 
    String utf8_str(utf8_lex_str.str,
1319
 
                    utf8_lex_str.length,
1320
 
                    system_charset_info);
1321
 
 
1322
 
    utf8_str.print(str);
1323
 
  }
1324
 
 
1325
 
  str->append('\'');
1326
 
}
1327
 
 
1328
 
 
1329
 
double Item_string::val_real()
1330
 
{
1331
 
  assert(fixed == 1);
1332
 
  int error;
1333
 
  char *end, *org_end;
1334
 
  double tmp;
1335
 
  const CHARSET_INFO * const cs= str_value.charset();
1336
 
 
1337
 
  org_end= (char*) str_value.ptr() + str_value.length();
1338
 
  tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
1339
 
                  &error);
1340
 
  if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
1341
 
  {
1342
 
    /*
1343
 
      We can use str_value.ptr() here as Item_string is gurantee to put an
1344
 
      end \0 here.
1345
 
    */
1346
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1347
 
                        ER_TRUNCATED_WRONG_VALUE,
1348
 
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
1349
 
                        str_value.ptr());
1350
 
  }
1351
 
  return tmp;
1352
 
}
1353
 
 
1354
 
 
1355
 
/**
1356
 
  @todo
1357
 
  Give error if we wanted a signed integer and we got an unsigned one
1358
 
*/
1359
 
int64_t Item_string::val_int()
1360
 
{
1361
 
  assert(fixed == 1);
1362
 
  int err;
1363
 
  int64_t tmp;
1364
 
  char *end= (char*) str_value.ptr()+ str_value.length();
1365
 
  char *org_end= end;
1366
 
  const CHARSET_INFO * const cs= str_value.charset();
1367
 
 
1368
 
  tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
1369
 
  /*
1370
 
    TODO: Give error if we wanted a signed integer and we got an unsigned
1371
 
    one
1372
 
  */
1373
 
  if (err > 0 ||
1374
 
      (end != org_end && !check_if_only_end_space(cs, end, org_end)))
1375
 
  {
1376
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1377
 
                        ER_TRUNCATED_WRONG_VALUE,
1378
 
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
1379
 
                        str_value.ptr());
1380
 
  }
1381
 
  return tmp;
1382
 
}
1383
 
 
1384
 
 
1385
 
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
1386
 
{
1387
 
  return val_decimal_from_string(decimal_value);
1388
 
}
1389
 
 
1390
 
 
1391
 
bool Item_null::eq(const Item *item, bool) const
1392
 
{ return item->type() == type(); }
1393
 
 
1394
 
 
1395
 
double Item_null::val_real()
1396
 
{
1397
 
  // following assert is redundant, because fixed=1 assigned in constructor
1398
 
  assert(fixed == 1);
1399
 
  null_value=1;
1400
 
  return 0.0;
1401
 
}
1402
 
int64_t Item_null::val_int()
1403
 
{
1404
 
  // following assert is redundant, because fixed=1 assigned in constructor
1405
 
  assert(fixed == 1);
1406
 
  null_value=1;
1407
 
  return 0;
1408
 
}
1409
 
/* ARGSUSED */
1410
 
String *Item_null::val_str(String *)
1411
 
{
1412
 
  // following assert is redundant, because fixed=1 assigned in constructor
1413
 
  assert(fixed == 1);
1414
 
  null_value=1;
1415
 
  return 0;
1416
 
}
1417
 
 
1418
 
my_decimal *Item_null::val_decimal(my_decimal *)
1419
 
{
1420
 
  return 0;
1421
 
}
1422
 
 
1423
 
 
1424
 
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
1425
 
{
1426
 
  collation.set(tocs);
1427
 
  return this;
1428
 
}
1429
 
 
1430
 
/*********************** Item_param related ******************************/
1431
 
 
1432
 
/**
1433
 
  Default function of Item_param::set_param_func, so in case
1434
 
  of malformed packet the server won't SIGSEGV.
1435
 
*/
1436
 
 
1437
 
static void
1438
 
default_set_param_func(Item_param *param, unsigned char **, ulong)
1439
 
{
1440
 
  param->set_null();
1441
 
}
1442
 
 
1443
 
 
1444
 
Item_param::Item_param(uint32_t pos_in_query_arg) :
1445
 
  state(NO_VALUE),
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)
1453
 
{
1454
 
  name= (char*) "?";
1455
 
  /*
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
1458
 
    value is set.
1459
 
  */
1460
 
  maybe_null= 1;
1461
 
  cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
1462
 
  cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
1463
 
}
1464
 
 
1465
 
 
1466
 
void Item_param::set_null()
1467
 
{
1468
 
  /* These are cleared after each execution by reset() method */
1469
 
  null_value= 1;
1470
 
  /*
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.
1474
 
  */
1475
 
  max_length= 0;
1476
 
  decimals= 0;
1477
 
  state= NULL_VALUE;
1478
 
  item_type= Item::NULL_ITEM;
1479
 
  return;
1480
 
}
1481
 
 
1482
 
void Item_param::set_int(int64_t i, uint32_t max_length_arg)
1483
 
{
1484
 
  value.integer= (int64_t) i;
1485
 
  state= INT_VALUE;
1486
 
  max_length= max_length_arg;
1487
 
  decimals= 0;
1488
 
  maybe_null= 0;
1489
 
  return;
1490
 
}
1491
 
 
1492
 
void Item_param::set_double(double d)
1493
 
{
1494
 
  value.real= d;
1495
 
  state= REAL_VALUE;
1496
 
  max_length= DBL_DIG + 8;
1497
 
  decimals= NOT_FIXED_DEC;
1498
 
  maybe_null= 0;
1499
 
  return;
1500
 
}
1501
 
 
1502
 
 
1503
 
/**
1504
 
  Set decimal parameter value from string.
1505
 
 
1506
 
  @param str      character string
1507
 
  @param length   string length
1508
 
 
1509
 
  @note
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.
1513
 
*/
1514
 
 
1515
 
void Item_param::set_decimal(char *str, ulong length)
1516
 
{
1517
 
  char *end;
1518
 
 
1519
 
  end= str+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);
1525
 
  maybe_null= 0;
1526
 
  return;
1527
 
}
1528
 
 
1529
 
 
1530
 
/**
1531
 
  Set parameter value from DRIZZLE_TIME value.
1532
 
 
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
1536
 
 
1537
 
  @note
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.
1542
 
*/
1543
 
void Item_param::set_time(DRIZZLE_TIME *tm,
1544
 
                          enum enum_drizzle_timestamp_type time_type,
1545
 
                          uint32_t max_length_arg)
1546
 
{
1547
 
  value.time= *tm;
1548
 
  value.time.time_type= time_type;
1549
 
 
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)
1554
 
  {
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);
1560
 
  }
1561
 
 
1562
 
  state= TIME_VALUE;
1563
 
  maybe_null= 0;
1564
 
  max_length= max_length_arg;
1565
 
  decimals= 0;
1566
 
  return;
1567
 
}
1568
 
 
1569
 
 
1570
 
bool Item_param::set_str(const char *str, ulong length)
1571
 
{
1572
 
  /*
1573
 
    Assign string with no conversion: data is converted only after it's
1574
 
    been written to the binary log.
1575
 
  */
1576
 
  uint32_t dummy_errors;
1577
 
  if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
1578
 
                     &dummy_errors))
1579
 
    return(true);
1580
 
  state= STRING_VALUE;
1581
 
  max_length= length;
1582
 
  maybe_null= 0;
1583
 
  /* max_length and decimals are set after charset conversion */
1584
 
  /* sic: str may be not null-terminated */
1585
 
  return(false);
1586
 
}
1587
 
 
1588
 
 
1589
 
bool Item_param::set_longdata(const char *str, ulong length)
1590
 
{
1591
 
  /*
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.
1599
 
  */
1600
 
  if (str_value.append(str, length, &my_charset_bin))
1601
 
    return(true);
1602
 
  state= LONG_DATA_VALUE;
1603
 
  maybe_null= 0;
1604
 
 
1605
 
  return(false);
1606
 
}
1607
 
 
1608
 
 
1609
 
/**
1610
 
  Set parameter value from user variable value.
1611
 
 
1612
 
  @param session   Current thread
1613
 
  @param entry User variable structure (NULL means use NULL value)
1614
 
 
1615
 
  @retval
1616
 
    0 OK
1617
 
  @retval
1618
 
    1 Out of memory
1619
 
*/
1620
 
 
1621
 
bool Item_param::set_from_user_var(Session *session, const user_var_entry *entry)
1622
 
{
1623
 
  if (entry && entry->value)
1624
 
  {
1625
 
    item_result_type= entry->type;
1626
 
    unsigned_flag= entry->unsigned_flag;
1627
 
    if (limit_clause_param)
1628
 
    {
1629
 
      bool unused;
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);
1633
 
    }
1634
 
    switch (item_result_type) {
1635
 
    case REAL_RESULT:
1636
 
      set_double(*(double*)entry->value);
1637
 
      item_type= Item::REAL_ITEM;
1638
 
      break;
1639
 
    case INT_RESULT:
1640
 
      set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
1641
 
      item_type= Item::INT_ITEM;
1642
 
      break;
1643
 
    case STRING_RESULT:
1644
 
    {
1645
 
      const CHARSET_INFO * const fromcs= entry->collation.collation;
1646
 
      const CHARSET_INFO * const tocs= session->variables.collation_connection;
1647
 
      uint32_t dummy_offset;
1648
 
 
1649
 
      value.cs_info.character_set_of_placeholder=
1650
 
        value.cs_info.character_set_client= fromcs;
1651
 
      /*
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.
1655
 
      */
1656
 
      value.cs_info.final_character_set_of_str_value=
1657
 
        String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
1658
 
        tocs : fromcs;
1659
 
      /*
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.
1662
 
      */
1663
 
      item_type= Item::STRING_ITEM;
1664
 
 
1665
 
      if (set_str((const char *)entry->value, entry->length))
1666
 
        return(1);
1667
 
      break;
1668
 
    }
1669
 
    case DECIMAL_RESULT:
1670
 
    {
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;
1678
 
      break;
1679
 
    }
1680
 
    default:
1681
 
      assert(0);
1682
 
      set_null();
1683
 
    }
1684
 
  }
1685
 
  else
1686
 
    set_null();
1687
 
 
1688
 
  return(0);
1689
 
}
1690
 
 
1691
 
/**
1692
 
  Resets parameter after execution.
1693
 
 
1694
 
  @note
1695
 
    We clear null_value here instead of setting it in set_* methods,
1696
 
    because we want more easily handle case for long data.
1697
 
*/
1698
 
 
1699
 
void Item_param::reset()
1700
 
{
1701
 
  /* Shrink string buffer if it's bigger than max possible CHAR column */
1702
 
  if (str_value.alloced_length() > MAX_CHAR_WIDTH)
1703
 
    str_value.free();
1704
 
  else
1705
 
    str_value.length(0);
1706
 
  str_value_ptr.length(0);
1707
 
  /*
1708
 
    We must prevent all charset conversions until data has been written
1709
 
    to the binary log.
1710
 
  */
1711
 
  str_value.set_charset(&my_charset_bin);
1712
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1713
 
  state= NO_VALUE;
1714
 
  maybe_null= 1;
1715
 
  null_value= 0;
1716
 
  /*
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_*
1723
 
    methods).
1724
 
  */
1725
 
  return;
1726
 
}
1727
 
 
1728
 
 
1729
 
int Item_param::save_in_field(Field *field, bool no_conversions)
1730
 
{
1731
 
  field->set_notnull();
1732
 
 
1733
 
  switch (state) {
1734
 
  case INT_VALUE:
1735
 
    return field->store(value.integer, unsigned_flag);
1736
 
  case REAL_VALUE:
1737
 
    return field->store(value.real);
1738
 
  case DECIMAL_VALUE:
1739
 
    return field->store_decimal(&decimal_value);
1740
 
  case TIME_VALUE:
1741
 
    field->store_time(&value.time, value.time.time_type);
1742
 
    return 0;
1743
 
  case STRING_VALUE:
1744
 
  case LONG_DATA_VALUE:
1745
 
    return field->store(str_value.ptr(), str_value.length(),
1746
 
                        str_value.charset());
1747
 
  case NULL_VALUE:
1748
 
    return set_field_to_null_with_conversions(field, no_conversions);
1749
 
  case NO_VALUE:
1750
 
  default:
1751
 
    assert(0);
1752
 
  }
1753
 
  return 1;
1754
 
}
1755
 
 
1756
 
 
1757
 
bool Item_param::get_time(DRIZZLE_TIME *res)
1758
 
{
1759
 
  if (state == TIME_VALUE)
1760
 
  {
1761
 
    *res= value.time;
1762
 
    return 0;
1763
 
  }
1764
 
  /*
1765
 
    If parameter value isn't supplied assertion will fire in val_str()
1766
 
    which is called from Item::get_time().
1767
 
  */
1768
 
  return Item::get_time(res);
1769
 
}
1770
 
 
1771
 
 
1772
 
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
1773
 
{
1774
 
  if (state == TIME_VALUE)
1775
 
  {
1776
 
    *res= value.time;
1777
 
    return 0;
1778
 
  }
1779
 
  return Item::get_date(res, fuzzydate);
1780
 
}
1781
 
 
1782
 
 
1783
 
double Item_param::val_real()
1784
 
{
1785
 
  switch (state) {
1786
 
  case REAL_VALUE:
1787
 
    return value.real;
1788
 
  case INT_VALUE:
1789
 
    return (double) value.integer;
1790
 
  case DECIMAL_VALUE:
1791
 
  {
1792
 
    double result;
1793
 
    my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1794
 
    return result;
1795
 
  }
1796
 
  case STRING_VALUE:
1797
 
  case LONG_DATA_VALUE:
1798
 
  {
1799
 
    int dummy_err;
1800
 
    char *end_not_used;
1801
 
    return my_strntod(str_value.charset(), (char*) str_value.ptr(),
1802
 
                      str_value.length(), &end_not_used, &dummy_err);
1803
 
  }
1804
 
  case TIME_VALUE:
1805
 
    /*
1806
 
      This works for example when user says SELECT ?+0.0 and supplies
1807
 
      time value for the placeholder.
1808
 
    */
1809
 
    return uint64_t2double(TIME_to_uint64_t(&value.time));
1810
 
  case NULL_VALUE:
1811
 
    return 0.0;
1812
 
  default:
1813
 
    assert(0);
1814
 
  }
1815
 
  return 0.0;
1816
 
}
1817
 
 
1818
 
 
1819
 
int64_t Item_param::val_int()
1820
 
{
1821
 
  switch (state) {
1822
 
  case REAL_VALUE:
1823
 
    return (int64_t) rint(value.real);
1824
 
  case INT_VALUE:
1825
 
    return value.integer;
1826
 
  case DECIMAL_VALUE:
1827
 
  {
1828
 
    int64_t i;
1829
 
    my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
1830
 
    return i;
1831
 
  }
1832
 
  case STRING_VALUE:
1833
 
  case LONG_DATA_VALUE:
1834
 
    {
1835
 
      int dummy_err;
1836
 
      return my_strntoll(str_value.charset(), str_value.ptr(),
1837
 
                         str_value.length(), 10, (char**) 0, &dummy_err);
1838
 
    }
1839
 
  case TIME_VALUE:
1840
 
    return (int64_t) TIME_to_uint64_t(&value.time);
1841
 
  case NULL_VALUE:
1842
 
    return 0;
1843
 
  default:
1844
 
    assert(0);
1845
 
  }
1846
 
  return 0;
1847
 
}
1848
 
 
1849
 
 
1850
 
my_decimal *Item_param::val_decimal(my_decimal *dec)
1851
 
{
1852
 
  switch (state) {
1853
 
  case DECIMAL_VALUE:
1854
 
    return &decimal_value;
1855
 
  case REAL_VALUE:
1856
 
    double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
1857
 
    return dec;
1858
 
  case INT_VALUE:
1859
 
    int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
1860
 
    return dec;
1861
 
  case STRING_VALUE:
1862
 
  case LONG_DATA_VALUE:
1863
 
    string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
1864
 
    return dec;
1865
 
  case TIME_VALUE:
1866
 
  {
1867
 
    int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
1868
 
    int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
1869
 
    return dec;
1870
 
  }
1871
 
  case NULL_VALUE:
1872
 
    return 0;
1873
 
  default:
1874
 
    assert(0);
1875
 
  }
1876
 
  return 0;
1877
 
}
1878
 
 
1879
 
 
1880
 
String *Item_param::val_str(String* str)
1881
 
{
1882
 
  switch (state) {
1883
 
  case STRING_VALUE:
1884
 
  case LONG_DATA_VALUE:
1885
 
    return &str_value_ptr;
1886
 
  case REAL_VALUE:
1887
 
    str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
1888
 
    return str;
1889
 
  case INT_VALUE:
1890
 
    str->set(value.integer, &my_charset_bin);
1891
 
    return str;
1892
 
  case DECIMAL_VALUE:
1893
 
    if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
1894
 
                          0, 0, 0, str) <= 1)
1895
 
      return str;
1896
 
    return NULL;
1897
 
  case TIME_VALUE:
1898
 
  {
1899
 
    if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
1900
 
      break;
1901
 
    str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
1902
 
    str->set_charset(&my_charset_bin);
1903
 
    return str;
1904
 
  }
1905
 
  case NULL_VALUE:
1906
 
    return NULL;
1907
 
  default:
1908
 
    assert(0);
1909
 
  }
1910
 
  return str;
1911
 
}
1912
 
 
1913
 
/**
1914
 
  Return Param item values in string format, for generating the dynamic
1915
 
  query used in update/binary logs.
1916
 
 
1917
 
  @todo
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
1922
 
*/
1923
 
 
1924
 
const String *Item_param::query_val_str(String* str) const
1925
 
{
1926
 
  switch (state) {
1927
 
  case INT_VALUE:
1928
 
    str->set_int(value.integer, unsigned_flag, &my_charset_bin);
1929
 
    break;
1930
 
  case REAL_VALUE:
1931
 
    str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
1932
 
    break;
1933
 
  case DECIMAL_VALUE:
1934
 
    if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
1935
 
                          0, 0, 0, str) > 1)
1936
 
      return &my_null_string;
1937
 
    break;
1938
 
  case TIME_VALUE:
1939
 
    {
1940
 
      char *buf, *ptr;
1941
 
      str->length(0);
1942
 
      /*
1943
 
        TODO: in case of error we need to notify replication
1944
 
        that binary log contains wrong statement
1945
 
      */
1946
 
      if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
1947
 
        break;
1948
 
 
1949
 
      /* Create date string inplace */
1950
 
      buf= str->c_ptr_quick();
1951
 
      ptr= buf;
1952
 
      *ptr++= '\'';
1953
 
      ptr+= (uint) my_TIME_to_str(&value.time, ptr);
1954
 
      *ptr++= '\'';
1955
 
      str->length((uint32_t) (ptr - buf));
1956
 
      break;
1957
 
    }
1958
 
  case STRING_VALUE:
1959
 
  case LONG_DATA_VALUE:
1960
 
    {
1961
 
      str->length(0);
1962
 
      append_query_string(value.cs_info.character_set_client, &str_value, str);
1963
 
      break;
1964
 
    }
1965
 
  case NULL_VALUE:
1966
 
    return &my_null_string;
1967
 
  default:
1968
 
    assert(0);
1969
 
  }
1970
 
  return str;
1971
 
}
1972
 
 
1973
 
 
1974
 
/**
1975
 
  Convert string from client character set to the character set of
1976
 
  connection.
1977
 
*/
1978
 
 
1979
 
bool Item_param::convert_str_value(Session *session)
1980
 
{
1981
 
  bool rc= false;
1982
 
  if (state == STRING_VALUE || state == LONG_DATA_VALUE)
1983
 
  {
1984
 
    /*
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.
1989
 
    */
1990
 
    if (value.cs_info.final_character_set_of_str_value !=
1991
 
        value.cs_info.character_set_of_placeholder)
1992
 
    {
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);
1996
 
    }
1997
 
    else
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 */
2000
 
 
2001
 
    max_length= str_value.length();
2002
 
    decimals= 0;
2003
 
    /*
2004
 
      str_value_ptr is returned from val_str(). It must be not alloced
2005
 
      to prevent it's modification by val_str() invoker.
2006
 
    */
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);
2011
 
  }
2012
 
  return rc;
2013
 
}
2014
 
 
2015
 
 
2016
 
bool Item_param::basic_const_item() const
2017
 
{
2018
 
  if (state == NO_VALUE || state == TIME_VALUE)
2019
 
    return false;
2020
 
  return true;
2021
 
}
2022
 
 
2023
 
 
2024
 
Item *
2025
 
Item_param::clone_item()
2026
 
{
2027
 
  /* see comments in the header file */
2028
 
  switch (state) {
2029
 
  case NULL_VALUE:
2030
 
    return new Item_null(name);
2031
 
  case INT_VALUE:
2032
 
    return (unsigned_flag ?
2033
 
            new Item_uint(name, value.integer, max_length) :
2034
 
            new Item_int(name, value.integer, max_length));
2035
 
  case REAL_VALUE:
2036
 
    return new Item_float(name, value.real, decimals, max_length);
2037
 
  case STRING_VALUE:
2038
 
  case LONG_DATA_VALUE:
2039
 
    return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2040
 
                           str_value.charset());
2041
 
  case TIME_VALUE:
2042
 
    break;
2043
 
  case NO_VALUE:
2044
 
  default:
2045
 
    assert(0);
2046
 
  };
2047
 
  return 0;
2048
 
}
2049
 
 
2050
 
 
2051
 
bool
2052
 
Item_param::eq(const Item *arg, bool binary_cmp) const
2053
 
{
2054
 
  Item *item;
2055
 
  if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2056
 
    return false;
2057
 
  /*
2058
 
    We need to cast off const to call val_int(). This should be OK for
2059
 
    a basic constant.
2060
 
  */
2061
 
  item= (Item*) arg;
2062
 
 
2063
 
  switch (state) {
2064
 
  case NULL_VALUE:
2065
 
    return true;
2066
 
  case INT_VALUE:
2067
 
    return value.integer == item->val_int() &&
2068
 
           unsigned_flag == item->unsigned_flag;
2069
 
  case REAL_VALUE:
2070
 
    return value.real == item->val_real();
2071
 
  case STRING_VALUE:
2072
 
  case LONG_DATA_VALUE:
2073
 
    if (binary_cmp)
2074
 
      return !stringcmp(&str_value, &item->str_value);
2075
 
    return !sortcmp(&str_value, &item->str_value, collation.collation);
2076
 
  default:
2077
 
    break;
2078
 
  }
2079
 
  return false;
2080
 
}
2081
 
 
2082
 
/* End of Item_param related */
2083
 
 
2084
 
void Item_param::print(String *str, enum_query_type)
2085
 
{
2086
 
  if (state == NO_VALUE)
2087
 
  {
2088
 
    str->append('?');
2089
 
  }
2090
 
  else
2091
 
  {
2092
 
    char buffer[STRING_BUFFER_USUAL_SIZE];
2093
 
    String tmp(buffer, sizeof(buffer), &my_charset_bin);
2094
 
    const String *res;
2095
 
    res= query_val_str(&tmp);
2096
 
    str->append(*res);
2097
 
  }
2098
 
}
2099
 
 
2100
 
 
2101
968
/****************************************************************************
2102
969
  Item_copy_string
2103
970
****************************************************************************/
2748
1615
}
2749
1616
 
2750
1617
 
2751
 
/**
2752
 
  Store null in field.
2753
 
 
2754
 
  This is used on INSERT.
2755
 
  Allow NULL to be inserted in timestamp and auto_increment values.
2756
 
 
2757
 
  @param field          Field where we want to store NULL
2758
 
 
2759
 
  @retval
2760
 
    0   ok
2761
 
  @retval
2762
 
    1   Field doesn't support NULL values and can't handle 'field = NULL'
2763
 
*/
2764
 
 
2765
 
int Item_null::save_in_field(Field *field, bool no_conversions)
2766
 
{
2767
 
  return set_field_to_null_with_conversions(field, no_conversions);
2768
 
}
2769
 
 
2770
 
 
2771
 
/**
2772
 
  Store null in field.
2773
 
 
2774
 
  @param field          Field where we want to store NULL
2775
 
 
2776
 
  @retval
2777
 
    0    OK
2778
 
  @retval
2779
 
    1    Field doesn't support NULL values
2780
 
*/
2781
 
 
2782
 
int Item_null::save_safe_in_field(Field *field)
2783
 
{
2784
 
  return set_field_to_null(field);
2785
 
}
2786
 
 
2787
 
 
2788
1618
/*
2789
1619
  This implementation can lose str_value content, so if the
2790
1620
  Item uses str_value to store something, it should
2851
1681
}
2852
1682
 
2853
1683
 
2854
 
int Item_string::save_in_field(Field *field, bool)
2855
 
{
2856
 
  String *result;
2857
 
  result=val_str(&str_value);
2858
 
  return save_str_value_in_field(field, result);
2859
 
}
2860
 
 
2861
 
 
2862
 
int Item_uint::save_in_field(Field *field, bool no_conversions)
2863
 
{
2864
 
  /* Item_int::save_in_field handles both signed and unsigned. */
2865
 
  return Item_int::save_in_field(field, no_conversions);
2866
 
}
2867
 
 
2868
 
 
2869
 
int Item_int::save_in_field(Field *field, bool)
2870
 
{
2871
 
  int64_t nr=val_int();
2872
 
  if (null_value)
2873
 
    return set_field_to_null(field);
2874
 
  field->set_notnull();
2875
 
  return field->store(nr, unsigned_flag);
2876
 
}
2877
 
 
2878
 
 
2879
 
int Item_decimal::save_in_field(Field *field, bool)
2880
 
{
2881
 
  field->set_notnull();
2882
 
  return field->store_decimal(&decimal_value);
2883
 
}
2884
 
 
2885
 
 
2886
 
bool Item_int::eq(const Item *arg, bool) const
2887
 
{
2888
 
  /* No need to check for null value as basic constant can't be NULL */
2889
 
  if (arg->basic_const_item() && arg->type() == type())
2890
 
  {
2891
 
    /*
2892
 
      We need to cast off const to call val_int(). This should be OK for
2893
 
      a basic constant.
2894
 
    */
2895
 
    Item *item= (Item*) arg;
2896
 
    return item->val_int() == value && item->unsigned_flag == unsigned_flag;
2897
 
  }
2898
 
  return false;
2899
 
}
2900
 
 
2901
 
 
2902
1684
Item *Item_int_with_ref::clone_item()
2903
1685
{
2904
1686
  assert(ref->const_item());
2912
1694
}
2913
1695
 
2914
1696
 
2915
 
static uint32_t nr_of_decimals(const char *str, const char *end)
2916
 
{
2917
 
  const char *decimal_point;
2918
 
 
2919
 
  /* Find position for '.' */
2920
 
  for (;;)
2921
 
  {
2922
 
    if (str == end)
2923
 
      return 0;
2924
 
    if (*str == 'e' || *str == 'E')
2925
 
      return NOT_FIXED_DEC;
2926
 
    if (*str++ == '.')
2927
 
      break;
2928
 
  }
2929
 
  decimal_point= str;
2930
 
  for (; my_isdigit(system_charset_info, *str) ; str++)
2931
 
    ;
2932
 
  if (*str == 'e' || *str == 'E')
2933
 
    return NOT_FIXED_DEC;
2934
 
  return (uint) (str - decimal_point);
2935
 
}
2936
 
 
2937
 
 
2938
 
/**
2939
 
  This function is only called during parsing. We will signal an error if
2940
 
  value is not a true double value (overflow)
2941
 
*/
2942
 
 
2943
 
Item_float::Item_float(const char *str_arg, uint32_t length)
2944
 
{
2945
 
  int error;
2946
 
  char *end_not_used;
2947
 
  value= my_strntod(&my_charset_bin, (char*) str_arg, length, &end_not_used,
2948
 
                    &error);
2949
 
  if (error)
2950
 
  {
2951
 
    /*
2952
 
      Note that we depend on that str_arg is null terminated, which is true
2953
 
      when we are in the parser
2954
 
    */
2955
 
    assert(str_arg[length] == 0);
2956
 
    my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg);
2957
 
  }
2958
 
  presentation= name=(char*) str_arg;
2959
 
  decimals=(uint8_t) nr_of_decimals(str_arg, str_arg+length);
2960
 
  max_length=length;
2961
 
  fixed= 1;
2962
 
}
2963
 
 
2964
 
 
2965
 
int Item_float::save_in_field(Field *field, bool)
2966
 
{
2967
 
  double nr= val_real();
2968
 
  if (null_value)
2969
 
    return set_field_to_null(field);
2970
 
  field->set_notnull();
2971
 
  return field->store(nr);
2972
 
}
2973
 
 
2974
 
 
2975
 
void Item_float::print(String *str, enum_query_type)
2976
 
{
2977
 
  if (presentation)
2978
 
  {
2979
 
    str->append(presentation);
2980
 
    return;
2981
 
  }
2982
 
  char buffer[20];
2983
 
  String num(buffer, sizeof(buffer), &my_charset_bin);
2984
 
  num.set_real(value, decimals, &my_charset_bin);
2985
 
  str->append(num);
2986
 
}
2987
 
 
2988
 
 
2989
 
/*
2990
 
  hex item
2991
 
  In string context this is a binary string.
2992
 
  In number context this is a int64_t value.
2993
 
*/
2994
 
 
2995
 
bool Item_float::eq(const Item *arg, bool) const
2996
 
{
2997
 
  if (arg->basic_const_item() && arg->type() == type())
2998
 
  {
2999
 
    /*
3000
 
      We need to cast off const to call val_int(). This should be OK for
3001
 
      a basic constant.
3002
 
    */
3003
 
    Item *item= (Item*) arg;
3004
 
    return item->val_real() == value;
3005
 
  }
3006
 
  return false;
3007
 
}
3008
 
 
3009
 
 
3010
1697
inline uint32_t char_val(char X)
3011
1698
{
3012
1699
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
3166
1853
 
3167
1854
 
3168
1855
/**
3169
 
  Pack data in buffer for sending.
3170
 
*/
3171
 
 
3172
 
bool Item_null::send(Protocol *protocol,
3173
 
                     String *)
3174
 
{
3175
 
  return protocol->store_null();
3176
 
}
3177
 
 
3178
 
/**
3179
1856
  This is only called from items that is not of type item_field.
3180
1857
*/
3181
1858