~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/field.cc

Merge Joe, plus I updated the tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
30
30
#include <drizzled/item/outer_ref.h>
31
31
#include <drizzled/plugin/client.h>
32
32
 
33
 
#include <boost/dynamic_bitset.hpp>
34
 
 
35
33
namespace drizzled
36
34
{
37
35
 
110
108
bool Item_field::register_field_in_read_map(unsigned char *arg)
111
109
{
112
110
  Table *table= (Table *) arg;
113
 
  if (field->getTable() == table || !table)
114
 
    field->getTable()->setReadSet(field->position());
 
111
  if (field->table == table || !table)
 
112
    field->table->setReadSet(field->field_index);
115
113
 
116
114
  return 0;
117
115
}
118
116
 
119
117
 
120
118
Item_field::Item_field(Field *f)
121
 
  :Item_ident(0, NULL, f->getTable()->getAlias(), f->field_name),
 
119
  :Item_ident(0, NULL, *f->table_name, f->field_name),
122
120
   item_equal(0), no_const_subst(0),
123
121
   have_privileges(0), any_privileges(0)
124
122
{
142
140
                       Name_resolution_context *context_arg,
143
141
                       Field *f) :
144
142
  Item_ident(context_arg,
145
 
             f->getTable()->getShare()->getSchemaName(),
146
 
             f->getTable()->getAlias(),
 
143
             f->table->getShare()->getSchemaName(),
 
144
             *f->table_name,
147
145
             f->field_name),
148
146
   item_equal(0),
149
147
   no_const_subst(0),
165
163
   have_privileges(0),
166
164
   any_privileges(0)
167
165
{
168
 
  Select_Lex *select= getSession().getLex()->current_select;
 
166
  Select_Lex *select= current_session->lex->current_select;
169
167
  collation.set(DERIVATION_IMPLICIT);
170
168
 
171
169
  if (select && select->parsing_place != IN_HAVING)
194
192
  maybe_null=field->maybe_null();
195
193
  decimals= field->decimals();
196
194
  max_length= field_par->max_display_length();
197
 
  table_name= field_par->getTable()->getAlias();
 
195
  table_name= *field_par->table_name;
198
196
  field_name= field_par->field_name;
199
 
  db_name= field_par->getTable()->getShare()->getSchemaName();
200
 
  alias_name_used= field_par->getTable()->alias_name_used;
 
197
  db_name= field_par->table->getShare()->getSchemaName();
 
198
  alias_name_used= field_par->table->alias_name_used;
201
199
  unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
202
200
  collation.set(field_par->charset(), field_par->derivation());
203
201
  fixed= 1;
246
244
}
247
245
 
248
246
 
249
 
type::Decimal *Item_field::val_decimal(type::Decimal *decimal_value)
 
247
my_decimal *Item_field::val_decimal(my_decimal *decimal_value)
250
248
{
251
249
  if ((null_value= field->is_null()))
252
250
    return 0;
262
260
  return result_field->val_str(str,&str_value);
263
261
}
264
262
 
265
 
bool Item_field::get_date(type::Time &ltime,uint32_t fuzzydate)
 
263
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
266
264
{
267
265
  if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
268
266
  {
269
 
    ltime.reset();
 
267
    memset(ltime, 0, sizeof(*ltime));
270
268
    return 1;
271
269
  }
272
270
  return 0;
273
271
}
274
272
 
275
 
bool Item_field::get_date_result(type::Time &ltime,uint32_t fuzzydate)
 
273
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
276
274
{
277
275
  if ((null_value=result_field->is_null()) ||
278
276
      result_field->get_date(ltime,fuzzydate))
279
277
  {
280
 
    ltime.reset();
 
278
    memset(ltime, 0, sizeof(*ltime));
281
279
    return 1;
282
280
  }
283
281
  return 0;
284
282
}
285
283
 
286
 
bool Item_field::get_time(type::Time &ltime)
 
284
bool Item_field::get_time(DRIZZLE_TIME *ltime)
287
285
{
288
286
  if ((null_value=field->is_null()) || field->get_time(ltime))
289
287
  {
290
 
    ltime.reset();
 
288
    memset(ltime, 0, sizeof(*ltime));
291
289
    return 1;
292
290
  }
293
291
  return 0;
308
306
}
309
307
 
310
308
 
311
 
type::Decimal *Item_field::val_decimal_result(type::Decimal *decimal_value)
 
309
my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value)
312
310
{
313
311
  if ((null_value= result_field->is_null()))
314
312
    return 0;
319
317
bool Item_field::val_bool_result()
320
318
{
321
319
  if ((null_value= result_field->is_null()))
322
 
  {
323
320
    return false;
324
 
  }
325
 
 
326
321
  switch (result_field->result_type()) {
327
322
  case INT_RESULT:
328
323
    return result_field->val_int() != 0;
329
 
 
330
324
  case DECIMAL_RESULT:
331
 
    {
332
 
      type::Decimal decimal_value;
333
 
      type::Decimal *val= result_field->val_decimal(&decimal_value);
334
 
      if (val)
335
 
        return not val->isZero();
336
 
      return 0;
337
 
    }
338
 
 
 
325
  {
 
326
    my_decimal decimal_value;
 
327
    my_decimal *val= result_field->val_decimal(&decimal_value);
 
328
    if (val)
 
329
      return !my_decimal_is_zero(val);
 
330
    return 0;
 
331
  }
339
332
  case REAL_RESULT:
340
333
  case STRING_RESULT:
341
334
    return result_field->val_real() != 0.0;
342
 
 
343
335
  case ROW_RESULT:
 
336
  default:
344
337
    assert(0);
345
338
    return 0;                                   // Shut up compiler
346
339
  }
347
 
 
348
 
  assert(0);
349
 
  return 0;                                   // Shut up compiler
350
340
}
351
341
 
352
342
 
369
359
    (In cases where we would choose wrong we would have to generate a
370
360
    ER_NON_UNIQ_ERROR).
371
361
  */
372
 
  return (not my_strcasecmp(system_charset_info, item_field->name, field_name) &&
373
 
          (not item_field->table_name || not table_name ||
374
 
           (not my_strcasecmp(table_alias_charset, item_field->table_name, table_name) &&
375
 
            (not item_field->db_name || not db_name ||
376
 
             (item_field->db_name && not my_strcasecmp(system_charset_info, item_field->db_name, db_name))))));
 
362
  return (!my_strcasecmp(system_charset_info, item_field->name,
 
363
                         field_name) &&
 
364
          (!item_field->table_name || !table_name ||
 
365
           (!my_strcasecmp(table_alias_charset, item_field->table_name,
 
366
                           table_name) &&
 
367
            (!item_field->db_name || !db_name ||
 
368
             (item_field->db_name && !strcasecmp(item_field->db_name,
 
369
                                             db_name))))));
377
370
}
378
371
 
379
372
 
380
373
table_map Item_field::used_tables() const
381
374
{
382
 
  if (field->getTable()->const_table)
383
 
  {
 
375
  if (field->table->const_table)
384
376
    return 0;                                   // const item
385
 
  }
386
 
 
387
 
  return (depended_from ? OUTER_REF_TABLE_BIT : field->getTable()->map);
 
377
  return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map);
388
378
}
389
379
 
390
380
enum Item_result Item_field::result_type () const
540
530
      {
541
531
        if (*from_field != view_ref_found)
542
532
        {
543
 
          prev_subselect_item->used_tables_cache|= (*from_field)->getTable()->map;
544
 
          prev_subselect_item->const_item_cache= false;
 
533
          prev_subselect_item->used_tables_cache|= (*from_field)->table->map;
 
534
          prev_subselect_item->const_item_cache= 0;
545
535
          set_field(*from_field);
546
536
          if (!last_checked_context->select_lex->having_fix_field &&
547
537
              select->group_list.elements &&
629
619
      case it does not matter which used tables bits we set)
630
620
    */
631
621
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
632
 
    prev_subselect_item->const_item_cache= false;
 
622
    prev_subselect_item->const_item_cache= 0;
633
623
  }
634
624
 
635
625
  assert(ref != 0);
640
630
    if (upward_lookup)
641
631
    {
642
632
      // We can't say exactly what absent table or field
643
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where());
 
633
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where);
644
634
    }
645
635
    else
646
636
    {
707
697
    {
708
698
      Item_ref *rf;
709
699
      rf= new Item_ref(context,
710
 
                       (cached_table->getSchemaName()[0] ? cached_table->getSchemaName() : 0),
 
700
                       (cached_table->db[0] ? cached_table->db : 0),
711
701
                       (char*) cached_table->alias, (char*) field_name);
712
702
      if (!rf)
713
703
        return -1;
824
814
            {
825
815
              /* The column to which we link isn't valid. */
826
816
              my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
827
 
                       session->where());
 
817
                       current_session->where);
828
818
              return(1);
829
819
            }
830
820
 
898
888
  }
899
889
  else if (session->mark_used_columns != MARK_COLUMNS_NONE)
900
890
  {
901
 
    Table *table= field->getTable();
902
 
    boost::dynamic_bitset<> *current_bitmap, *other_bitmap;
 
891
    Table *table= field->table;
 
892
    MyBitmap *current_bitmap, *other_bitmap;
903
893
    if (session->mark_used_columns == MARK_COLUMNS_READ)
904
894
    {
905
895
      current_bitmap= table->read_set;
910
900
      current_bitmap= table->write_set;
911
901
      other_bitmap=   table->read_set;
912
902
    }
913
 
    //if (! current_bitmap->testAndSet(field->position()))
914
 
    if (! current_bitmap->test(field->position()))
 
903
    if (! current_bitmap->testAndSet(field->field_index))
915
904
    {
916
 
      if (! other_bitmap->test(field->position()))
 
905
      if (! other_bitmap->isBitSet(field->field_index))
917
906
      {
918
907
        /* First usage of column */
919
908
        table->used_fields++;                     // Used to optimize loops
1211
1200
    need to set no_errors to prevent warnings about type conversion
1212
1201
    popping up.
1213
1202
  */
1214
 
  Session *session= field->getTable()->in_use;
 
1203
  Session *session= field->table->in_use;
1215
1204
  int no_errors;
1216
1205
 
1217
1206
  no_errors= session->no_errors;
1248
1237
  Select_Lex *select= (Select_Lex*)select_arg;
1249
1238
  assert(fixed);
1250
1239
 
1251
 
  if (field->getTable() != select->context.table_list->table)
 
1240
  if (field->table != select->context.table_list->table)
1252
1241
  {
1253
1242
    List<Item> *all_fields= &select->join->all_fields;
1254
1243
    Item **ref_pointer_array= select->ref_pointer_array;
1267
1256
 
1268
1257
void Item_field::print(String *str, enum_query_type query_type)
1269
1258
{
1270
 
  if (field && field->getTable()->const_table)
 
1259
  if (field && field->table->const_table)
1271
1260
  {
1272
1261
    char buff[MAX_FIELD_WIDTH];
1273
1262
    String tmp(buff,sizeof(buff),str->charset());
1274
 
    field->val_str_internal(&tmp);
1275
 
    if (field->is_null())  {
1276
 
      str->append("NULL");
1277
 
    }
1278
 
    else {
1279
 
      str->append('\'');
1280
 
      str->append(tmp);
1281
 
      str->append('\'');
1282
 
    }
 
1263
    field->val_str(&tmp);
 
1264
    str->append('\'');
 
1265
    str->append(tmp);
 
1266
    str->append('\'');
1283
1267
    return;
1284
1268
  }
1285
1269
  Item_ident::print(str, query_type);