~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Stewart Smith
  • Date: 2009-06-16 00:47:17 UTC
  • mto: (1119.2.6 merge)
  • mto: This revision was merged to the branch mainline in revision 1124.
  • Revision ID: stewart@flamingspork.com-20090616004717-po86r1tt9ch8eg0d
make show_create_table_utf8 test not leave tables behind

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"
 
20
#include "drizzled/server_includes.h"
21
21
#include "drizzled/sql_select.h"
22
22
#include "drizzled/error.h"
23
23
#include "drizzled/show.h"
29
29
#include "drizzled/function/str/conv_charset.h"
30
30
#include "drizzled/sql_base.h"
31
31
#include "drizzled/util/convert.h"
32
 
#include "drizzled/plugin/client.h"
33
 
#include "drizzled/time_functions.h"
34
32
 
35
33
#include "drizzled/field/str.h"
36
34
#include "drizzled/field/num.h"
47
45
#include "drizzled/field/timestamp.h"
48
46
#include "drizzled/field/datetime.h"
49
47
#include "drizzled/field/varstring.h"
50
 
#include "drizzled/internal/m_string.h"
51
48
 
52
49
#include <math.h>
53
50
#include <algorithm>
54
 
#include <float.h>
55
51
 
56
52
using namespace std;
57
 
 
58
 
namespace drizzled
59
 
{
 
53
using namespace drizzled;
60
54
 
61
55
const String my_null_string("NULL", 4, default_charset_info);
62
56
 
113
107
{
114
108
  double nr= val_real();
115
109
  if (null_value)
116
 
    return NULL;
 
110
    return NULL; /* purecov: inspected */
117
111
 
118
112
  str->set_real(nr, decimals, &my_charset_bin);
119
113
  return str;
321
315
  session->free_list= this;
322
316
}
323
317
 
324
 
uint32_t Item::float_length(uint32_t decimals_par) const
325
 
{
326
 
  return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
327
 
}
328
 
 
329
318
uint32_t Item::decimal_precision() const
330
319
{
331
320
  Item_result restype= result_type();
429
418
                            str + length - orig_len);
430
419
    }
431
420
  }
432
 
  name= memory::sql_strmake(str, length);
 
421
  name= sql_strmake(str, length);
433
422
}
434
423
 
435
424
bool Item::eq(const Item *item, bool) const
469
458
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
470
459
    {
471
460
      char buff[22], *end;
472
 
      end= internal::int64_t10_to_str(value, buff, -10);
 
461
      end= int64_t10_to_str(value, buff, -10);
473
462
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
474
463
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
475
464
                                   NULL);
614
603
  return false;
615
604
}
616
605
 
 
606
bool Item::reset_query_id_processor(unsigned char *)
 
607
{
 
608
  return false;
 
609
}
 
610
 
617
611
bool Item::register_field_in_read_map(unsigned char *)
618
612
{
619
613
  return false;
693
687
  return test(is_expensive_cache);
694
688
}
695
689
 
 
690
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
 
691
{
 
692
  int res;
 
693
  Table *table= field->table;
 
694
  Session *session= table->in_use;
 
695
  enum_check_fields tmp= session->count_cuted_fields;
 
696
  ulong sql_mode= session->variables.sql_mode;
 
697
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
 
698
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
699
  res= save_in_field(field, no_conversions);
 
700
  session->count_cuted_fields= tmp;
 
701
  session->variables.sql_mode= sql_mode;
 
702
  return res;
 
703
}
 
704
 
696
705
/*
697
706
 need a special class to adjust printing : references to aggregate functions
698
707
 must not be printed as refs because the aggregate functions that are added to
787
796
  if (session->lex->describe & DESCRIBE_EXTENDED)
788
797
  {
789
798
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
790
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
 
799
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
791
800
            db_name, (db_name[0] ? "." : ""),
792
801
            table_name, (table_name [0] ? "." : ""),
793
802
            resolved_item->field_name,
906
915
        if (cur_field->db_name && db_name)
907
916
        {
908
917
          /* If field_name is also qualified by a database name. */
909
 
          if (strcasecmp(cur_field->db_name, db_name))
 
918
          if (strcmp(cur_field->db_name, db_name))
910
919
            /* Same field names, different databases. */
911
920
            return NULL;
912
921
          ++cur_match_degree;
1038
1047
  case INT_RESULT:     
1039
1048
    return DRIZZLE_TYPE_LONGLONG;
1040
1049
  case DECIMAL_RESULT: 
1041
 
    return DRIZZLE_TYPE_DECIMAL;
 
1050
    return DRIZZLE_TYPE_NEWDECIMAL;
1042
1051
  case REAL_RESULT:    
1043
1052
    return DRIZZLE_TYPE_DOUBLE;
1044
1053
  case ROW_RESULT:
1142
1151
  Field *field;
1143
1152
 
1144
1153
  switch (field_type()) {
1145
 
  case DRIZZLE_TYPE_DECIMAL:
1146
 
    field= new Field_decimal((unsigned char*) 0,
1147
 
                                 max_length,
1148
 
                                 null_ptr,
1149
 
                                 0,
1150
 
                                 Field::NONE,
1151
 
                                 name,
1152
 
                                 decimals,
1153
 
                                 0,
 
1154
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1155
    field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
 
1156
                                 Field::NONE, name, decimals, 0,
1154
1157
                                 unsigned_flag);
1155
1158
    break;
1156
1159
  case DRIZZLE_TYPE_LONG:
1166
1169
                            name, decimals, 0, unsigned_flag);
1167
1170
    break;
1168
1171
  case DRIZZLE_TYPE_NULL:
1169
 
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
 
1172
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
 
1173
                          name, &my_charset_bin);
1170
1174
    break;
1171
1175
  case DRIZZLE_TYPE_DATE:
1172
1176
    field= new Field_date(maybe_null, name, &my_charset_bin);
1261
1265
  return error;
1262
1266
}
1263
1267
 
1264
 
/**
1265
 
  Check if an item is a constant one and can be cached.
1266
 
 
1267
 
  @param arg [out] TRUE <=> Cache this item.
1268
 
 
1269
 
  @return TRUE  Go deeper in item tree.
1270
 
  @return FALSE Don't go deeper in item tree.
1271
 
*/
1272
 
 
1273
 
bool Item::cache_const_expr_analyzer(unsigned char **arg)
1274
 
{
1275
 
  bool *cache_flag= (bool*)*arg;
1276
 
  if (!*cache_flag)
1277
 
  {
1278
 
    Item *item= real_item();
1279
 
    /*
1280
 
      Cache constant items unless it's a basic constant, constant field or
1281
 
      a subselect (they use their own cache).
1282
 
    */
1283
 
    if (const_item() &&
1284
 
        !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1285
 
          item->type() == SUBSELECT_ITEM ||
1286
 
           /*
1287
 
             Do not cache GET_USER_VAR() function as its const_item() may
1288
 
             return TRUE for the current thread but it still may change
1289
 
             during the execution.
1290
 
           */
1291
 
          (item->type() == Item::FUNC_ITEM &&
1292
 
           ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1293
 
      *cache_flag= true;
1294
 
    return true;
1295
 
  }
1296
 
  return false;
1297
 
}
1298
 
 
1299
 
/**
1300
 
  Cache item if needed.
1301
 
 
1302
 
  @param arg   TRUE <=> Cache this item.
1303
 
 
1304
 
  @return cache if cache needed.
1305
 
  @return this otherwise.
1306
 
*/
1307
 
 
1308
 
Item* Item::cache_const_expr_transformer(unsigned char *arg)
1309
 
{
1310
 
  if (*(bool*)arg)
1311
 
  {
1312
 
    *((bool*)arg)= false;
1313
 
    Item_cache *cache= Item_cache::get_cache(this);
1314
 
    if (!cache)
1315
 
      return NULL;
1316
 
    cache->setup(this);
1317
 
    cache->store(this);
1318
 
    return cache;
1319
 
  }
1320
 
  return this;
1321
 
}
1322
 
 
1323
 
bool Item::send(plugin::Client *client, String *buffer)
 
1268
bool Item::send(plugin::Protocol *protocol, String *buffer)
1324
1269
{
1325
1270
  bool result= false;
1326
1271
  enum_field_types f_type;
1331
1276
  case DRIZZLE_TYPE_ENUM:
1332
1277
  case DRIZZLE_TYPE_BLOB:
1333
1278
  case DRIZZLE_TYPE_VARCHAR:
1334
 
  case DRIZZLE_TYPE_DECIMAL:
 
1279
  case DRIZZLE_TYPE_NEWDECIMAL:
1335
1280
  {
1336
1281
    String *res;
1337
1282
    if ((res=val_str(buffer)))
1338
 
      result= client->store(res->ptr(),res->length());
 
1283
      result= protocol->store(res->ptr(),res->length());
1339
1284
    break;
1340
1285
  }
1341
1286
  case DRIZZLE_TYPE_LONG:
1343
1288
    int64_t nr;
1344
1289
    nr= val_int();
1345
1290
    if (!null_value)
1346
 
      result= client->store((int32_t)nr);
 
1291
      result= protocol->store((int32_t)nr);
1347
1292
    break;
1348
1293
  }
1349
1294
  case DRIZZLE_TYPE_LONGLONG:
1353
1298
    if (!null_value)
1354
1299
    {
1355
1300
      if (unsigned_flag)
1356
 
        result= client->store((uint64_t)nr);
 
1301
        result= protocol->store((uint64_t)nr);
1357
1302
      else
1358
 
        result= client->store((int64_t)nr);
 
1303
        result= protocol->store((int64_t)nr);
1359
1304
    }
1360
1305
    break;
1361
1306
  }
1363
1308
  {
1364
1309
    double nr= val_real();
1365
1310
    if (!null_value)
1366
 
      result= client->store(nr, decimals, buffer);
 
1311
      result= protocol->store(nr, decimals, buffer);
1367
1312
    break;
1368
1313
  }
1369
1314
  case DRIZZLE_TYPE_DATETIME:
1372
1317
    DRIZZLE_TIME tm;
1373
1318
    get_date(&tm, TIME_FUZZY_DATE);
1374
1319
    if (!null_value)
1375
 
      result= client->store(&tm);
 
1320
      result= protocol->store(&tm);
1376
1321
    break;
1377
1322
  }
1378
1323
  }
1379
1324
  if (null_value)
1380
 
    result= client->store();
 
1325
    result= protocol->store();
1381
1326
  return result;
1382
1327
}
1383
1328
 
1403
1348
    return; /* Can't be better */
1404
1349
  Item_result res_type=item_cmp_type(comp_item->result_type(),
1405
1350
                                     item->result_type());
1406
 
  char *name=item->name; /* Alloced by memory::sql_alloc */
 
1351
  char *name=item->name; /* Alloced by sql_alloc */
1407
1352
 
1408
1353
  switch (res_type) {
1409
1354
  case STRING_RESULT:
1416
1361
    else
1417
1362
    {
1418
1363
      uint32_t length= result->length();
1419
 
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
 
1364
      char *tmp_str= sql_strmake(result->ptr(), length);
1420
1365
      new_item= new Item_string(name, tmp_str, length, result->charset());
1421
1366
    }
1422
1367
    break;
1635
1580
        len-= item->decimals - dec;             // corrected value fits
1636
1581
    }
1637
1582
 
1638
 
    new_field= new Field_decimal(len,
1639
 
                                 maybe_null,
1640
 
                                 item->name,
1641
 
                                 dec,
1642
 
                                 item->unsigned_flag);
 
1583
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
1584
                                     dec, item->unsigned_flag);
1643
1585
    break;
1644
1586
  }
1645
1587
  case ROW_RESULT:
1664
1606
Field *create_tmp_field(Session *session,
1665
1607
                        Table *table,
1666
1608
                        Item *item,
1667
 
                        Item::Type type,
1668
 
                        Item ***copy_func,
 
1609
                        Item::Type type, 
 
1610
                        Item ***copy_func, 
1669
1611
                        Field **from_field,
1670
 
                        Field **default_field,
1671
 
                        bool group,
 
1612
                        Field **default_field, 
 
1613
                        bool group, 
1672
1614
                        bool modify_item,
 
1615
                        bool, 
1673
1616
                        bool make_copy_field,
1674
1617
                        uint32_t convert_blob_length)
1675
1618
{
1760
1703
  }
1761
1704
}
1762
1705
 
1763
 
} /* namespace drizzled */
 
1706
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
1707
template class List<Item>;
 
1708
template class List_iterator<Item>;
 
1709
template class List_iterator_fast<Item>;
 
1710
template class List_iterator_fast<Item_field>;
 
1711
template class List<List_item>;
 
1712
#endif