~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Stewart Smith
  • Date: 2011-02-10 00:46:02 UTC
  • mto: (2154.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2155.
  • Revision ID: stewart@flamingspork.com-20110210004602-9hev3ooklc9f2yti
switch get_sql_command to getSqlCommand

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
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>
34
 
 
35
 
#include <drizzled/field/str.h>
36
 
#include <drizzled/field/num.h>
37
 
 
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>
53
 
 
54
 
#include <drizzled/current_session.h>
55
 
#include <drizzled/session.h>
56
 
 
57
 
#include <drizzled/internal/m_string.h>
 
20
#include "config.h"
 
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"
 
34
 
 
35
#include "drizzled/field/str.h"
 
36
#include "drizzled/field/num.h"
 
37
 
 
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"
 
53
 
 
54
#include "drizzled/internal/m_string.h"
58
55
 
59
56
#include <cstdio>
60
57
#include <math.h>
186
183
                     res->length(), 
187
184
                     res->charset()) & E_DEC_BAD_NUM)
188
185
  {
189
 
    push_warning_printf(&getSession(), 
 
186
    push_warning_printf(current_session, 
190
187
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
191
188
                        ER_TRUNCATED_WRONG_VALUE,
192
189
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
308
305
 
309
306
  /*
310
307
    Item constructor can be called during execution other then SQL_COM
311
 
    command => we should check session->getLex()->current_select on zero (session->lex
 
308
    command => we should check session->lex->current_select on zero (session->lex
312
309
    can be uninitialised)
313
310
  */
314
 
  if (getSession().getLex()->current_select)
 
311
  if (getSession().lex->current_select)
315
312
  {
316
313
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
317
314
    if (place == SELECT_LIST || place == IN_HAVING)
439
436
    if (orig_len != length && ! is_autogenerated_name)
440
437
    {
441
438
      if (length == 0)
442
 
        push_warning_printf(&getSession(), 
 
439
        push_warning_printf(current_session, 
443
440
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
444
441
                            ER_NAME_BECOMES_EMPTY, 
445
442
                            ER(ER_NAME_BECOMES_EMPTY),
446
443
                            str + length - orig_len);
447
444
      else
448
 
        push_warning_printf(&getSession(),
 
445
        push_warning_printf(current_session, 
449
446
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
450
447
                            ER_REMOVED_SPACES, 
451
448
                            ER(ER_REMOVED_SPACES),
484
481
    }
485
482
    else if (result_type() == STRING_RESULT)
486
483
    {
487
 
      char buff[type::Time::MAX_STRING_LENGTH];
 
484
      char buff[40];
488
485
      String tmp(buff,sizeof(buff), &my_charset_bin),*res;
489
486
      if (!(res=val_str(&tmp)) ||
490
 
          str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
 
487
          str_to_datetime_with_warn(current_session, res->ptr(), res->length(),
491
488
                                    &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
492
489
      {
493
490
        break;
502
499
 
503
500
      if (not type::is_valid(date_value))
504
501
      {
505
 
        char buff[DECIMAL_LONGLONG_DIGITS], *end;
 
502
        char buff[22], *end;
506
503
        end= internal::int64_t10_to_str(value, buff, -10);
507
 
        make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
504
        make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
508
505
                                     buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
509
506
        break;
510
507
      }
520
517
 
521
518
bool Item::get_time(type::Time &ltime)
522
519
{
523
 
  char buff[type::Time::MAX_STRING_LENGTH];
 
520
  char buff[40];
524
521
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
525
522
  if (!(res=val_str(&tmp)) or
526
 
      str_to_time_with_warn(&getSession(), res->ptr(), res->length(), &ltime))
 
523
      str_to_time_with_warn(current_session, res->ptr(), res->length(), &ltime))
527
524
  {
528
525
    ltime.reset();
529
526
 
788
785
    Item *real_itm= real_item();
789
786
 
790
787
    ref_pointer_array[el]= real_itm;
791
 
    if (!(item_ref= new Item_aggregate_ref(&session->getLex()->current_select->context,
 
788
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
792
789
                                           ref_pointer_array + el, 0, name)))
793
790
      return; /* fatal_error is set */
794
791
    if (type() == SUM_FUNC_ITEM)
821
818
  if (mark_item)
822
819
    mark_item->depended_from= last;
823
820
  current->mark_as_dependent(last);
824
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
821
  if (session->lex->describe & DESCRIBE_EXTENDED)
825
822
  {
826
823
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
827
824
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
887
884
    - the found item on success
888
885
    - NULL if find_item is not in group_list
889
886
*/
890
 
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
 
887
static Item** find_field_in_group_list(Item *find_item, Order *group_list)
891
888
{
892
889
  const char *db_name;
893
890
  const char *table_name;
966
963
          best match, they must reference the same column, otherwise the field
967
964
          is ambiguous.
968
965
        */
969
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
 
966
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where());
970
967
        return NULL;
971
968
      }
972
969
    }
1002
999
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
1003
1000
  if (select->having_fix_field && !ref->with_sum_func && group_list)
1004
1001
  {
1005
 
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
1002
    group_by_ref= find_field_in_group_list(ref, group_list);
1006
1003
 
1007
1004
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
1008
1005
    if (group_by_ref && (select_ref != not_found_item) &&
1090
1087
 
1091
1088
bool Item::is_datetime()
1092
1089
{
1093
 
  return field::isDateTime(field_type());
 
1090
  switch (field_type())
 
1091
  {
 
1092
    case DRIZZLE_TYPE_TIME:
 
1093
    case DRIZZLE_TYPE_DATE:
 
1094
    case DRIZZLE_TYPE_DATETIME:
 
1095
    case DRIZZLE_TYPE_TIMESTAMP:
 
1096
    case DRIZZLE_TYPE_MICROTIME:
 
1097
      return true;
 
1098
    case DRIZZLE_TYPE_BLOB:
 
1099
    case DRIZZLE_TYPE_VARCHAR:
 
1100
    case DRIZZLE_TYPE_DOUBLE:
 
1101
    case DRIZZLE_TYPE_DECIMAL:
 
1102
    case DRIZZLE_TYPE_ENUM:
 
1103
    case DRIZZLE_TYPE_LONG:
 
1104
    case DRIZZLE_TYPE_LONGLONG:
 
1105
    case DRIZZLE_TYPE_NULL:
 
1106
    case DRIZZLE_TYPE_UUID:
 
1107
    case DRIZZLE_TYPE_BOOLEAN:
 
1108
      return false;
 
1109
  }
 
1110
 
 
1111
  assert(0);
 
1112
  abort();
1094
1113
}
1095
1114
 
1096
1115
String *Item::check_well_formed_result(String *str, bool send_error)
1103
1122
                                       str->length(), &well_formed_error);
1104
1123
  if (wlen < str->length())
1105
1124
  {
 
1125
    Session *session= current_session;
1106
1126
    char hexbuf[7];
1107
1127
    enum DRIZZLE_ERROR::enum_warning_level level;
1108
1128
    uint32_t diff= str->length() - wlen;
1119
1139
      null_value= 1;
1120
1140
      str= 0;
1121
1141
    }
1122
 
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
 
1142
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1123
1143
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1124
1144
  }
1125
1145
  return str;
1197
1217
                            name, decimals, 0, unsigned_flag);
1198
1218
    break;
1199
1219
  case DRIZZLE_TYPE_NULL:
1200
 
    field= new Field_null((unsigned char*) 0, max_length, name);
 
1220
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1201
1221
    break;
1202
1222
  case DRIZZLE_TYPE_DATE:
1203
 
    field= new Field_date(maybe_null, name);
 
1223
    field= new Field_date(maybe_null, name, &my_charset_bin);
1204
1224
    break;
1205
1225
 
1206
1226
  case DRIZZLE_TYPE_MICROTIME:
1211
1231
    field= new field::Epoch(maybe_null, name);
1212
1232
    break;
1213
1233
  case DRIZZLE_TYPE_DATETIME:
1214
 
    field= new Field_datetime(maybe_null, name);
 
1234
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1215
1235
    break;
1216
1236
  case DRIZZLE_TYPE_TIME:
1217
 
    field= new field::Time(maybe_null, name);
 
1237
    field= new field::Time(maybe_null, name, &my_charset_bin);
1218
1238
    break;
1219
1239
  case DRIZZLE_TYPE_BOOLEAN:
1220
1240
  case DRIZZLE_TYPE_UUID:
1428
1448
  return result;
1429
1449
}
1430
1450
 
1431
 
uint32_t Item::max_char_length() const
1432
 
{
1433
 
  return max_length / collation.collation->mbmaxlen;
1434
 
}
1435
 
 
1436
 
void Item::fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs)
1437
 
1438
 
  max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1439
 
  collation.collation= cs;
1440
 
}
1441
 
 
1442
 
void Item::fix_char_length(uint32_t max_char_length_arg)
1443
 
1444
 
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1445
 
}
1446
 
 
1447
 
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
1448
 
1449
 
  uint64_t max_result_length= max_char_length_arg *
1450
 
    collation.collation->mbmaxlen;
1451
 
 
1452
 
  if (max_result_length >= MAX_BLOB_WIDTH)
1453
 
  { 
1454
 
    max_length= MAX_BLOB_WIDTH;
1455
 
    maybe_null= false;
1456
 
  }
1457
 
  else
1458
 
  {
1459
 
    max_length= max_result_length;
1460
 
  }
1461
 
}
1462
 
 
1463
 
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
1464
 
1465
 
  collation.set(&my_charset_bin);
1466
 
  fix_char_length(max_char_length_arg);
1467
 
}
1468
 
 
1469
1451
Item_result item_cmp_type(Item_result a,Item_result b)
1470
1452
{
1471
1453
  if (a == STRING_RESULT && b == STRING_RESULT)
1681
1663
  case STRING_RESULT:
1682
1664
    assert(item->collation.collation);
1683
1665
 
 
1666
    enum enum_field_types type;
1684
1667
    /*
1685
1668
      DATE/TIME fields have STRING_RESULT result type.
1686
1669
      To preserve type they needed to be handled separately.
1687
1670
    */
1688
 
    if (field::isDateTime(item->field_type()))
 
1671
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
 
1672
        type == DRIZZLE_TYPE_TIME ||
 
1673
        type == DRIZZLE_TYPE_MICROTIME ||
 
1674
        type == DRIZZLE_TYPE_DATE ||
 
1675
        type == DRIZZLE_TYPE_TIMESTAMP)
1689
1676
    {
1690
1677
      new_field= item->tmp_table_field_from_field_type(table, 1);
1691
1678
      /*