~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Lee Bieber
  • Date: 2011-01-08 03:39:25 UTC
  • mfrom: (2062.1.2 build)
  • Revision ID: kalebral@gmail.com-20110108033925-koobar399kwrm4ba
Merge Brian - parser clean up
Merge Marisa - latest doc updates, fix sphinx warnings

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include "drizzled/field/epoch.h"
45
45
#include "drizzled/field/int32.h"
46
46
#include "drizzled/field/int64.h"
47
 
#include "drizzled/field/microtime.h"
48
47
#include "drizzled/field/null.h"
49
48
#include "drizzled/field/real.h"
50
49
#include "drizzled/field/size.h"
102
101
      type::Decimal decimal_value;
103
102
      type::Decimal *val= val_decimal(&decimal_value);
104
103
      if (val)
105
 
        return not val->isZero();
 
104
        return not val->is_zero();
106
105
      return false;
107
106
    }
108
107
 
146
145
    return NULL;
147
146
 
148
147
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
149
 
  class_decimal2string(&dec_buf, 0, str);
 
148
  class_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
150
149
  return str;
151
150
}
152
151
 
183
182
                     res->length(), 
184
183
                     res->charset()) & E_DEC_BAD_NUM)
185
184
  {
186
 
    push_warning_printf(&getSession(), 
 
185
    push_warning_printf(current_session, 
187
186
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
188
187
                        ER_TRUNCATED_WRONG_VALUE,
189
188
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
196
195
{
197
196
  assert(fixed);
198
197
  type::Time ltime;
199
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
198
  if (get_date(&ltime, TIME_FUZZY_DATE))
200
199
  {
201
200
    decimal_value->set_zero();
202
201
    null_value= 1;                               // set NULL, stop processing
209
208
{
210
209
  assert(fixed);
211
210
  type::Time ltime;
212
 
  if (get_time(ltime))
 
211
  if (get_time(&ltime))
213
212
  {
214
213
    decimal_value->set_zero();
215
214
    return NULL;
233
232
  /* Note that fix_fields may not be called for Item_avg_field items */
234
233
  int64_t result;
235
234
  type::Decimal value, *dec_val= val_decimal(&value);
236
 
 
237
235
  if (null_value)
238
236
    return 0;
239
237
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
240
 
 
241
238
  return result;
242
239
}
243
240
 
244
 
bool Item::save_time_in_field(Field *field)
 
241
int Item::save_time_in_field(Field *field)
245
242
{
246
243
  type::Time ltime;
247
 
 
248
 
  if (get_time(ltime))
 
244
  if (get_time(&ltime))
249
245
    return set_field_to_null(field);
250
 
 
251
246
  field->set_notnull();
252
 
 
253
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
 
247
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
254
248
}
255
249
 
256
 
bool Item::save_date_in_field(Field *field)
 
250
int Item::save_date_in_field(Field *field)
257
251
{
258
252
  type::Time ltime;
259
 
 
260
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
253
  if (get_date(&ltime, TIME_FUZZY_DATE))
261
254
    return set_field_to_null(field);
262
 
 
263
255
  field->set_notnull();
264
 
 
265
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
 
256
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
266
257
}
267
258
 
268
259
/**
273
264
{
274
265
  if (null_value)
275
266
    return set_field_to_null(field);
276
 
 
277
267
  field->set_notnull();
278
 
 
279
268
  return field->store(result->ptr(), result->length(), collation.collation);
280
269
}
281
270
 
294
283
  with_sum_func(false),
295
284
  is_autogenerated_name(true),
296
285
  with_subselect(false),
297
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
298
 
  _session(*current_session)
 
286
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
299
287
{
300
288
  cmp_context= (Item_result)-1;
301
289
 
302
290
  /* Put item in free list so that we can free all items at end */
303
 
  next= getSession().free_list;
304
 
  getSession().free_list= this;
 
291
  Session *session= current_session;
 
292
  next= session->free_list;
 
293
  session->free_list= this;
305
294
 
306
295
  /*
307
296
    Item constructor can be called during execution other then SQL_COM
308
297
    command => we should check session->lex->current_select on zero (session->lex
309
298
    can be uninitialised)
310
299
  */
311
 
  if (getSession().lex->current_select)
 
300
  if (session->lex->current_select)
312
301
  {
313
 
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
 
302
    enum_parsing_place place= session->lex->current_select->parsing_place;
314
303
    if (place == SELECT_LIST || place == IN_HAVING)
315
 
      getSession().getLex()->current_select->select_n_having_items++;
 
304
      session->lex->current_select->select_n_having_items++;
316
305
  }
317
306
}
318
307
 
333
322
  is_autogenerated_name(item->is_autogenerated_name),
334
323
  with_subselect(item->with_subselect),
335
324
  collation(item->collation),
336
 
  cmp_context(item->cmp_context),
337
 
  _session(*session)
 
325
  cmp_context(item->cmp_context)
338
326
{
339
327
  /* Put this item in the session's free list */
340
 
  next= getSession().free_list;
341
 
  getSession().free_list= this;
 
328
  next= session->free_list;
 
329
  session->free_list= this;
342
330
}
343
331
 
344
332
uint32_t Item::float_length(uint32_t decimals_par) const
436
424
    if (orig_len != length && ! is_autogenerated_name)
437
425
    {
438
426
      if (length == 0)
439
 
        push_warning_printf(&getSession(), 
 
427
        push_warning_printf(current_session, 
440
428
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
441
429
                            ER_NAME_BECOMES_EMPTY, 
442
430
                            ER(ER_NAME_BECOMES_EMPTY),
443
431
                            str + length - orig_len);
444
432
      else
445
 
        push_warning_printf(&getSession(),
 
433
        push_warning_printf(current_session, 
446
434
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
447
435
                            ER_REMOVED_SPACES, 
448
436
                            ER(ER_REMOVED_SPACES),
471
459
  return conv->safe ? conv : NULL;
472
460
}
473
461
 
474
 
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
 
462
bool Item::get_date(type::Time *ltime,uint32_t fuzzydate)
475
463
{
476
 
  do
477
 
  {
478
 
    if (is_null())
479
 
    {
480
 
      break;
481
 
    }
482
 
    else if (result_type() == STRING_RESULT)
483
 
    {
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
 
                                    &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
489
 
      {
490
 
        break;
491
 
      }
492
 
    }
493
 
    else
494
 
    {
495
 
      int64_t value= val_int();
496
 
      type::datetime_t date_value;
497
 
 
498
 
      ltime.convert(date_value, value, fuzzydate);
499
 
 
500
 
      if (not type::is_valid(date_value))
501
 
      {
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);
506
 
        break;
507
 
      }
508
 
    }
509
 
 
510
 
    return false;
511
 
  } while (0);
512
 
 
513
 
  ltime.reset();
514
 
 
 
464
  if (result_type() == STRING_RESULT)
 
465
  {
 
466
    char buff[40];
 
467
    String tmp(buff,sizeof(buff), &my_charset_bin),*res;
 
468
    if (!(res=val_str(&tmp)) ||
 
469
        str_to_datetime_with_warn(res->ptr(), res->length(),
 
470
                                  ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
471
      goto err;
 
472
  }
 
473
  else
 
474
  {
 
475
    int64_t value= val_int();
 
476
    int was_cut;
 
477
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
 
478
    {
 
479
      char buff[22], *end;
 
480
      end= internal::int64_t10_to_str(value, buff, -10);
 
481
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
482
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
 
483
                                   NULL);
 
484
      goto err;
 
485
    }
 
486
  }
 
487
  return false;
 
488
 
 
489
err:
 
490
  memset(ltime, 0, sizeof(*ltime));
515
491
  return true;
516
492
}
517
493
 
518
 
bool Item::get_time(type::Time &ltime)
 
494
bool Item::get_time(type::Time *ltime)
519
495
{
520
 
  char buff[type::Time::MAX_STRING_LENGTH];
 
496
  char buff[40];
521
497
  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(), &ltime))
 
498
  if (!(res=val_str(&tmp)) ||
 
499
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
524
500
  {
525
 
    ltime.reset();
526
 
 
 
501
    memset(ltime, 0, sizeof(*ltime));
527
502
    return true;
528
503
  }
529
 
 
530
504
  return false;
531
505
}
532
506
 
533
 
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
 
507
bool Item::get_date_result(type::Time *ltime,uint32_t fuzzydate)
534
508
{
535
 
  return get_date(ltime, fuzzydate);
 
509
  return get_date(ltime,fuzzydate);
536
510
}
537
511
 
538
512
bool Item::is_null()
884
858
    - the found item on success
885
859
    - NULL if find_item is not in group_list
886
860
*/
887
 
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
 
861
static Item** find_field_in_group_list(Item *find_item, Order *group_list)
888
862
{
889
863
  const char *db_name;
890
864
  const char *table_name;
940
914
        if (cur_field->db_name && db_name)
941
915
        {
942
916
          /* If field_name is also qualified by a database name. */
943
 
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
944
 
          {
 
917
          if (strcasecmp(cur_field->db_name, db_name))
945
918
            /* Same field names, different databases. */
946
919
            return NULL;
947
 
          }
948
920
          ++cur_match_degree;
949
921
        }
950
922
      }
963
935
          best match, they must reference the same column, otherwise the field
964
936
          is ambiguous.
965
937
        */
966
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
 
938
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where);
967
939
        return NULL;
968
940
      }
969
941
    }
999
971
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
1000
972
  if (select->having_fix_field && !ref->with_sum_func && group_list)
1001
973
  {
1002
 
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
974
    group_by_ref= find_field_in_group_list(ref, group_list);
1003
975
 
1004
976
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
1005
977
    if (group_by_ref && (select_ref != not_found_item) &&
1008
980
      ambiguous_fields= true;
1009
981
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1010
982
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
1011
 
                          session->where());
 
983
                          current_session->where);
1012
984
 
1013
985
    }
1014
986
  }
1093
1065
    case DRIZZLE_TYPE_DATE:
1094
1066
    case DRIZZLE_TYPE_DATETIME:
1095
1067
    case DRIZZLE_TYPE_TIMESTAMP:
1096
 
    case DRIZZLE_TYPE_MICROTIME:
1097
1068
      return true;
1098
1069
    case DRIZZLE_TYPE_BLOB:
1099
1070
    case DRIZZLE_TYPE_VARCHAR:
1122
1093
                                       str->length(), &well_formed_error);
1123
1094
  if (wlen < str->length())
1124
1095
  {
 
1096
    Session *session= current_session;
1125
1097
    char hexbuf[7];
1126
1098
    enum DRIZZLE_ERROR::enum_warning_level level;
1127
1099
    uint32_t diff= str->length() - wlen;
1138
1110
      null_value= 1;
1139
1111
      str= 0;
1140
1112
    }
1141
 
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
 
1113
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1142
1114
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1143
1115
  }
1144
1116
  return str;
1203
1175
                                 0,
1204
1176
                                 Field::NONE,
1205
1177
                                 name,
1206
 
                                 decimals);
 
1178
                                 decimals,
 
1179
                                 0,
 
1180
                                 unsigned_flag);
1207
1181
    break;
1208
1182
  case DRIZZLE_TYPE_LONG:
1209
1183
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1221
1195
  case DRIZZLE_TYPE_DATE:
1222
1196
    field= new Field_date(maybe_null, name, &my_charset_bin);
1223
1197
    break;
1224
 
 
1225
 
  case DRIZZLE_TYPE_MICROTIME:
1226
 
    field= new field::Microtime(maybe_null, name);
1227
 
    break;
1228
 
 
1229
1198
  case DRIZZLE_TYPE_TIMESTAMP:
1230
 
    field= new field::Epoch(maybe_null, name);
 
1199
    field= new field::Epoch(maybe_null, name, &my_charset_bin);
1231
1200
    break;
1232
1201
  case DRIZZLE_TYPE_DATETIME:
1233
1202
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1425
1394
  case DRIZZLE_TYPE_TIME:
1426
1395
    {
1427
1396
      type::Time tm;
1428
 
      get_time(tm);
 
1397
      get_time(&tm);
1429
1398
      if (not null_value)
1430
1399
        result= client->store(&tm);
1431
1400
      break;
1432
1401
    }
1433
1402
  case DRIZZLE_TYPE_DATETIME:
1434
 
  case DRIZZLE_TYPE_MICROTIME:
1435
1403
  case DRIZZLE_TYPE_TIMESTAMP:
1436
1404
    {
1437
1405
      type::Time tm;
1438
 
      get_date(tm, TIME_FUZZY_DATE);
 
1406
      get_date(&tm, TIME_FUZZY_DATE);
1439
1407
      if (!null_value)
1440
1408
        result= client->store(&tm);
1441
1409
      break;
1447
1415
  return result;
1448
1416
}
1449
1417
 
1450
 
uint32_t Item::max_char_length() const
1451
 
{
1452
 
  return max_length / collation.collation->mbmaxlen;
1453
 
}
1454
 
 
1455
 
void Item::fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs)
1456
 
1457
 
  max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1458
 
  collation.collation= cs;
1459
 
}
1460
 
 
1461
 
void Item::fix_char_length(uint32_t max_char_length_arg)
1462
 
1463
 
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1464
 
}
1465
 
 
1466
 
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
1467
 
1468
 
  uint64_t max_result_length= max_char_length_arg *
1469
 
    collation.collation->mbmaxlen;
1470
 
 
1471
 
  if (max_result_length >= MAX_BLOB_WIDTH)
1472
 
  { 
1473
 
    max_length= MAX_BLOB_WIDTH;
1474
 
    maybe_null= false;
1475
 
  }
1476
 
  else
1477
 
  {
1478
 
    max_length= max_result_length;
1479
 
  }
1480
 
}
1481
 
 
1482
 
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
1483
 
1484
 
  collation.set(&my_charset_bin);
1485
 
  fix_char_length(max_char_length_arg);
1486
 
}
1487
 
 
1488
1418
Item_result item_cmp_type(Item_result a,Item_result b)
1489
1419
{
1490
1420
  if (a == STRING_RESULT && b == STRING_RESULT)
1707
1637
    */
1708
1638
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1709
1639
        type == DRIZZLE_TYPE_TIME ||
1710
 
        type == DRIZZLE_TYPE_MICROTIME ||
1711
1640
        type == DRIZZLE_TYPE_DATE ||
1712
1641
        type == DRIZZLE_TYPE_TIMESTAMP)
1713
1642
    {