~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Andrew Hutchings
  • Date: 2011-02-02 12:51:57 UTC
  • mto: (2157.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2158.
  • Revision ID: andrew@linuxjedi.co.uk-20110202125157-r6thh7ox4g9l90ec
Make transaction_message_threshold a read-only global variable instead of a per-session variable
Make replication_dictionary column lengths use transation_message_threshold

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>
105
102
      type::Decimal decimal_value;
106
103
      type::Decimal *val= val_decimal(&decimal_value);
107
104
      if (val)
108
 
        return not val->isZero();
 
105
        return not val->is_zero();
109
106
      return false;
110
107
    }
111
108
 
149
146
    return NULL;
150
147
 
151
148
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
152
 
  class_decimal2string(&dec_buf, 0, str);
 
149
  class_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
153
150
  return str;
154
151
}
155
152
 
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",
244
241
  return result;
245
242
}
246
243
 
247
 
bool Item::save_time_in_field(Field *field)
 
244
int Item::save_time_in_field(Field *field)
248
245
{
249
246
  type::Time ltime;
250
247
 
256
253
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
257
254
}
258
255
 
259
 
bool Item::save_date_in_field(Field *field)
 
256
int Item::save_date_in_field(Field *field)
260
257
{
261
258
  type::Time ltime;
262
259
 
297
294
  with_sum_func(false),
298
295
  is_autogenerated_name(true),
299
296
  with_subselect(false),
300
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
301
 
  _session(*current_session)
 
297
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
302
298
{
303
299
  cmp_context= (Item_result)-1;
304
300
 
305
301
  /* Put item in free list so that we can free all items at end */
306
 
  next= getSession().free_list;
307
 
  getSession().free_list= this;
 
302
  Session *session= current_session;
 
303
  next= session->free_list;
 
304
  session->free_list= this;
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 (session->lex->current_select)
315
312
  {
316
 
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
 
313
    enum_parsing_place place= session->lex->current_select->parsing_place;
317
314
    if (place == SELECT_LIST || place == IN_HAVING)
318
 
      getSession().getLex()->current_select->select_n_having_items++;
 
315
      session->lex->current_select->select_n_having_items++;
319
316
  }
320
317
}
321
318
 
336
333
  is_autogenerated_name(item->is_autogenerated_name),
337
334
  with_subselect(item->with_subselect),
338
335
  collation(item->collation),
339
 
  cmp_context(item->cmp_context),
340
 
  _session(*session)
 
336
  cmp_context(item->cmp_context)
341
337
{
342
338
  /* Put this item in the session's free list */
343
 
  next= getSession().free_list;
344
 
  getSession().free_list= this;
 
339
  next= session->free_list;
 
340
  session->free_list= this;
345
341
}
346
342
 
347
343
uint32_t Item::float_length(uint32_t decimals_par) const
439
435
    if (orig_len != length && ! is_autogenerated_name)
440
436
    {
441
437
      if (length == 0)
442
 
        push_warning_printf(&getSession(), 
 
438
        push_warning_printf(current_session, 
443
439
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
444
440
                            ER_NAME_BECOMES_EMPTY, 
445
441
                            ER(ER_NAME_BECOMES_EMPTY),
446
442
                            str + length - orig_len);
447
443
      else
448
 
        push_warning_printf(&getSession(),
 
444
        push_warning_printf(current_session, 
449
445
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
450
446
                            ER_REMOVED_SPACES, 
451
447
                            ER(ER_REMOVED_SPACES),
484
480
    }
485
481
    else if (result_type() == STRING_RESULT)
486
482
    {
487
 
      char buff[type::Time::MAX_STRING_LENGTH];
 
483
      char buff[40];
488
484
      String tmp(buff,sizeof(buff), &my_charset_bin),*res;
489
485
      if (!(res=val_str(&tmp)) ||
490
 
          str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
 
486
          str_to_datetime_with_warn(current_session, res->ptr(), res->length(),
491
487
                                    &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
492
488
      {
493
489
        break;
502
498
 
503
499
      if (not type::is_valid(date_value))
504
500
      {
505
 
        char buff[DECIMAL_LONGLONG_DIGITS], *end;
 
501
        char buff[22], *end;
506
502
        end= internal::int64_t10_to_str(value, buff, -10);
507
 
        make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
503
        make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
508
504
                                     buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
509
505
        break;
510
506
      }
520
516
 
521
517
bool Item::get_time(type::Time &ltime)
522
518
{
523
 
  char buff[type::Time::MAX_STRING_LENGTH];
 
519
  char buff[40];
524
520
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
525
 
  if (!(res=val_str(&tmp)) or
526
 
      str_to_time_with_warn(&getSession(), res->ptr(), res->length(), &ltime))
 
521
  if (!(res=val_str(&tmp)) || str_to_time_with_warn(res->ptr(), res->length(), &ltime))
527
522
  {
528
523
    ltime.reset();
529
524
 
788
783
    Item *real_itm= real_item();
789
784
 
790
785
    ref_pointer_array[el]= real_itm;
791
 
    if (!(item_ref= new Item_aggregate_ref(&session->getLex()->current_select->context,
 
786
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
792
787
                                           ref_pointer_array + el, 0, name)))
793
788
      return; /* fatal_error is set */
794
789
    if (type() == SUM_FUNC_ITEM)
821
816
  if (mark_item)
822
817
    mark_item->depended_from= last;
823
818
  current->mark_as_dependent(last);
824
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
819
  if (session->lex->describe & DESCRIBE_EXTENDED)
825
820
  {
826
821
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
827
822
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
887
882
    - the found item on success
888
883
    - NULL if find_item is not in group_list
889
884
*/
890
 
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
 
885
static Item** find_field_in_group_list(Item *find_item, Order *group_list)
891
886
{
892
887
  const char *db_name;
893
888
  const char *table_name;
966
961
          best match, they must reference the same column, otherwise the field
967
962
          is ambiguous.
968
963
        */
969
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
 
964
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where());
970
965
        return NULL;
971
966
      }
972
967
    }
1002
997
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
1003
998
  if (select->having_fix_field && !ref->with_sum_func && group_list)
1004
999
  {
1005
 
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
1000
    group_by_ref= find_field_in_group_list(ref, group_list);
1006
1001
 
1007
1002
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
1008
1003
    if (group_by_ref && (select_ref != not_found_item) &&
1090
1085
 
1091
1086
bool Item::is_datetime()
1092
1087
{
1093
 
  return field::isDateTime(field_type());
 
1088
  switch (field_type())
 
1089
  {
 
1090
    case DRIZZLE_TYPE_TIME:
 
1091
    case DRIZZLE_TYPE_DATE:
 
1092
    case DRIZZLE_TYPE_DATETIME:
 
1093
    case DRIZZLE_TYPE_TIMESTAMP:
 
1094
    case DRIZZLE_TYPE_MICROTIME:
 
1095
      return true;
 
1096
    case DRIZZLE_TYPE_BLOB:
 
1097
    case DRIZZLE_TYPE_VARCHAR:
 
1098
    case DRIZZLE_TYPE_DOUBLE:
 
1099
    case DRIZZLE_TYPE_DECIMAL:
 
1100
    case DRIZZLE_TYPE_ENUM:
 
1101
    case DRIZZLE_TYPE_LONG:
 
1102
    case DRIZZLE_TYPE_LONGLONG:
 
1103
    case DRIZZLE_TYPE_NULL:
 
1104
    case DRIZZLE_TYPE_UUID:
 
1105
    case DRIZZLE_TYPE_BOOLEAN:
 
1106
      return false;
 
1107
  }
 
1108
 
 
1109
  assert(0);
 
1110
  abort();
1094
1111
}
1095
1112
 
1096
1113
String *Item::check_well_formed_result(String *str, bool send_error)
1103
1120
                                       str->length(), &well_formed_error);
1104
1121
  if (wlen < str->length())
1105
1122
  {
 
1123
    Session *session= current_session;
1106
1124
    char hexbuf[7];
1107
1125
    enum DRIZZLE_ERROR::enum_warning_level level;
1108
1126
    uint32_t diff= str->length() - wlen;
1119
1137
      null_value= 1;
1120
1138
      str= 0;
1121
1139
    }
1122
 
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
 
1140
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1123
1141
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1124
1142
  }
1125
1143
  return str;
1184
1202
                                 0,
1185
1203
                                 Field::NONE,
1186
1204
                                 name,
1187
 
                                 decimals);
 
1205
                                 decimals,
 
1206
                                 0,
 
1207
                                 unsigned_flag);
1188
1208
    break;
1189
1209
  case DRIZZLE_TYPE_LONG:
1190
1210
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
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
      /*