~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 19:24:49 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425192449-0htujbt2r9jzupn5
Moved heap.

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
 
22
22
#include <drizzled/sql_string.h>
23
23
#include <drizzled/sql_list.h>
24
24
 
25
25
#include <drizzled/function/math/int.h>
26
 
#include <drizzled/field/int32.h>
27
 
#include <drizzled/field/int64.h>
 
26
#include <drizzled/field/int64_t.h>
 
27
#include <drizzled/field/long.h>
28
28
#include <drizzled/field/double.h>
29
29
#include <drizzled/field/decimal.h>
30
30
#include <drizzled/session.h>
31
31
#include <drizzled/error.h>
32
32
#include <drizzled/check_stack_overrun.h>
33
33
#include <limits>
34
 
#include <algorithm>
35
34
 
36
35
using namespace std;
37
36
 
38
 
namespace drizzled
39
 
{
40
 
 
41
 
 
42
37
void Item_func::set_arguments(List<Item> &list)
43
38
{
44
39
  allowed_arg_cols= 1;
45
40
  arg_count=list.elements;
46
41
  args= tmp_arg;                                // If 2 arguments
47
 
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
 
42
  if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
48
43
  {
49
44
    List_iterator_fast<Item> li(list);
50
45
    Item *item;
59
54
  list.empty();          // Fields are used
60
55
}
61
56
 
62
 
Item_func::Item_func(List<Item> &list) :
63
 
  _session(*current_session),
64
 
  allowed_arg_cols(1)
 
57
Item_func::Item_func(List<Item> &list)
 
58
  :allowed_arg_cols(1)
65
59
{
66
 
  collation.set(DERIVATION_SYSCONST);
67
60
  set_arguments(list);
68
61
}
69
62
 
70
 
Item_func::Item_func(Session *session, Item_func *item) :
71
 
  Item_result_field(session, item),
72
 
  _session(*current_session),
73
 
  allowed_arg_cols(item->allowed_arg_cols),
74
 
  arg_count(item->arg_count),
75
 
  used_tables_cache(item->used_tables_cache),
76
 
  not_null_tables_cache(item->not_null_tables_cache),
77
 
  const_item_cache(item->const_item_cache)
 
63
Item_func::Item_func(Session *session, Item_func *item)
 
64
  :Item_result_field(session, item),
 
65
   allowed_arg_cols(item->allowed_arg_cols),
 
66
   arg_count(item->arg_count),
 
67
   used_tables_cache(item->used_tables_cache),
 
68
   not_null_tables_cache(item->not_null_tables_cache),
 
69
   const_item_cache(item->const_item_cache)
78
70
{
79
71
  if (arg_count)
80
72
  {
87
79
    }
88
80
    memcpy(args, item->args, sizeof(Item*)*arg_count);
89
81
  }
90
 
  collation.set(DERIVATION_SYSCONST);
91
82
}
92
83
 
93
84
 
147
138
        We shouldn't call fix_fields() twice, so check 'fixed' field first
148
139
      */
149
140
      if ((!(*arg)->fixed && (*arg)->fix_fields(session, arg)))
150
 
        return true;
 
141
        return true;        /* purecov: inspected */
151
142
      item= *arg;
152
143
 
153
144
      if (allowed_arg_cols)
283
274
        change records at each execution.
284
275
      */
285
276
      if (*arg != new_item)
286
 
        getSession().change_item_tree(arg, new_item);
 
277
        current_session->change_item_tree(arg, new_item);
287
278
    }
288
279
  }
289
280
  return (this->*transformer)(argument);
454
445
 
455
446
Field *Item_func::tmp_table_field(Table *table)
456
447
{
457
 
  Field *field= NULL;
 
448
  Field *field;
458
449
 
459
450
  switch (result_type()) {
460
451
  case INT_RESULT:
461
452
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
462
 
      field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
453
      field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
463
454
    else
464
 
      field= new field::Int32(max_length, maybe_null, name, unsigned_flag);
 
455
      field= new Field_long(max_length, maybe_null, name, unsigned_flag);
465
456
    break;
466
 
 
467
457
  case REAL_RESULT:
468
458
    field= new Field_double(max_length, maybe_null, name, decimals);
469
459
    break;
470
 
 
471
460
  case STRING_RESULT:
472
461
    return make_string_field(table);
473
 
 
474
462
  case DECIMAL_RESULT:
475
 
    field= new Field_decimal(my_decimal_precision_to_length(decimal_precision(),
476
 
                                                            decimals,
477
 
                                                            unsigned_flag),
478
 
                             maybe_null,
479
 
                             name,
480
 
                             decimals,
481
 
                             unsigned_flag);
 
463
    field= new Field_new_decimal(
 
464
                       my_decimal_precision_to_length(decimal_precision(),
 
465
                                                      decimals,
 
466
                                                      unsigned_flag),
 
467
                       maybe_null, name, decimals, unsigned_flag);
482
468
    break;
483
469
  case ROW_RESULT:
 
470
  default:
484
471
    // This case should never be chosen
485
472
    assert(0);
 
473
    field= 0;
486
474
    break;
487
475
  }
488
 
 
489
476
  if (field)
490
477
    field->init(table);
491
 
 
492
478
  return field;
493
479
}
494
480
 
563
549
  int max_int_part= 0;
564
550
  decimals= 0;
565
551
  unsigned_flag= 1;
566
 
  for (uint32_t i= 0 ; i < arg_count ; i++)
 
552
  for (uint32_t i=0 ; i < arg_count ; i++)
567
553
  {
568
554
    set_if_bigger(decimals, args[i]->decimals);
569
555
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
570
556
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
571
557
  }
572
 
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
558
  int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
573
559
  max_length= my_decimal_precision_to_length(precision, decimals,
574
560
                                             unsigned_flag);
575
561
}
625
611
 
626
612
void Item_func::signal_divide_by_null()
627
613
{
628
 
  my_error(ER_DIVISION_BY_ZERO, MYF(0));
629
 
  null_value= 0;
 
614
  Session *session= current_session;
 
615
  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
616
  null_value= 1;
630
617
}
631
618
 
632
619
 
638
625
}
639
626
 
640
627
 
641
 
} /* namespace drizzled */