~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

  • Committer: Jay Pipes
  • Date: 2010-03-09 20:02:29 UTC
  • mto: This revision was merged to the branch mainline in revision 1339.
  • Revision ID: jpipes@serialcoder-20100309200229-dfrliy4fads9vyf4
Fixes Bug #535296 by only incrementing ha_commit_count when its a normal transaction commit.

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
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
21
 
 
22
 
#include <drizzled/check_stack_overrun.h>
23
 
#include <drizzled/current_session.h>
24
 
#include <drizzled/error.h>
 
20
#include "config.h"
 
21
 
 
22
#include <drizzled/sql_string.h>
 
23
#include <drizzled/sql_list.h>
 
24
 
 
25
#include <drizzled/function/math/int.h>
 
26
#include <drizzled/field/int64_t.h>
 
27
#include <drizzled/field/long.h>
 
28
#include <drizzled/field/double.h>
25
29
#include <drizzled/field/decimal.h>
26
 
#include <drizzled/field/double.h>
27
 
#include <drizzled/field/int32.h>
28
 
#include <drizzled/field/int64.h>
29
 
#include <drizzled/field/size.h>
30
 
#include <drizzled/function/math/int.h>
31
30
#include <drizzled/session.h>
32
 
#include <drizzled/sql_list.h>
33
 
#include <drizzled/sql_string.h>
34
 
 
 
31
#include <drizzled/error.h>
 
32
#include <drizzled/check_stack_overrun.h>
35
33
#include <limits>
36
34
#include <algorithm>
37
35
 
41
39
{
42
40
 
43
41
 
44
 
Item_func::Item_func(void):
45
 
  _session(*current_session),
46
 
  allowed_arg_cols(1), arg_count(0),
47
 
  const_item_cache(false)
48
 
  {
49
 
    with_sum_func= 0;
50
 
    collation.set(DERIVATION_SYSCONST);
51
 
  }
52
 
 
53
 
Item_func::Item_func(Item *a):
54
 
  _session(*current_session),
55
 
  allowed_arg_cols(1), arg_count(1),
56
 
  const_item_cache(false)
57
 
  {
58
 
    args= tmp_arg;
59
 
    args[0]= a;
60
 
    with_sum_func= a->with_sum_func;
61
 
    collation.set(DERIVATION_SYSCONST);
62
 
  }
63
 
 
64
 
Item_func::Item_func(Item *a,Item *b):
65
 
  _session(*current_session),
66
 
  allowed_arg_cols(1), arg_count(2),
67
 
  const_item_cache(false)
68
 
  {
69
 
    args= tmp_arg;
70
 
    args[0]= a; args[1]= b;
71
 
    with_sum_func= a->with_sum_func || b->with_sum_func;
72
 
    collation.set(DERIVATION_SYSCONST);
73
 
  }
74
 
 
75
 
Item_func::Item_func(Item *a,Item *b,Item *c):
76
 
  _session(*current_session),
77
 
  allowed_arg_cols(1),
78
 
  const_item_cache(false)
79
 
  {
80
 
    arg_count= 0;
81
 
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*3)))
82
 
    {
83
 
      arg_count= 3;
84
 
      args[0]= a; args[1]= b; args[2]= c;
85
 
      with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
86
 
    }
87
 
    collation.set(DERIVATION_SYSCONST);
88
 
  }
89
 
 
90
 
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d):
91
 
  _session(*current_session),
92
 
  allowed_arg_cols(1),
93
 
  const_item_cache(false)
94
 
  {
95
 
    arg_count= 0;
96
 
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*4)))
97
 
    {
98
 
      arg_count= 4;
99
 
      args[0]= a; args[1]= b; args[2]= c; args[3]= d;
100
 
      with_sum_func= a->with_sum_func || b->with_sum_func ||
101
 
        c->with_sum_func || d->with_sum_func;
102
 
    }
103
 
    collation.set(DERIVATION_SYSCONST);
104
 
  }
105
 
 
106
 
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
107
 
  _session(*current_session),
108
 
  allowed_arg_cols(1),
109
 
  const_item_cache(false)
110
 
  {
111
 
    arg_count= 5;
112
 
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*5)))
113
 
    {
114
 
      args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
115
 
      with_sum_func= a->with_sum_func || b->with_sum_func ||
116
 
        c->with_sum_func || d->with_sum_func || e->with_sum_func ;
117
 
    }
118
 
    collation.set(DERIVATION_SYSCONST);
119
 
  }
120
 
 
121
 
 
122
42
void Item_func::set_arguments(List<Item> &list)
123
43
{
124
44
  allowed_arg_cols= 1;
126
46
  args= tmp_arg;                                // If 2 arguments
127
47
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
128
48
  {
129
 
    List<Item>::iterator li(list.begin());
 
49
    List_iterator_fast<Item> li(list);
130
50
    Item *item;
131
51
    Item **save_args= args;
132
52
 
136
56
      with_sum_func|=item->with_sum_func;
137
57
    }
138
58
  }
139
 
  list.clear();          // Fields are used
 
59
  list.empty();          // Fields are used
140
60
}
141
61
 
142
 
Item_func::Item_func(List<Item> &list) :
143
 
  _session(*current_session),
144
 
  allowed_arg_cols(1),
145
 
  const_item_cache(false)
 
62
Item_func::Item_func(List<Item> &list)
 
63
  :allowed_arg_cols(1)
146
64
{
147
 
  collation.set(DERIVATION_SYSCONST);
148
65
  set_arguments(list);
149
66
}
150
67
 
151
 
Item_func::Item_func(Session *session, Item_func *item) :
152
 
  Item_result_field(session, item),
153
 
  _session(*current_session),
154
 
  allowed_arg_cols(item->allowed_arg_cols),
155
 
  arg_count(item->arg_count),
156
 
  used_tables_cache(item->used_tables_cache),
157
 
  not_null_tables_cache(item->not_null_tables_cache),
158
 
  const_item_cache(item->const_item_cache)
 
68
Item_func::Item_func(Session *session, Item_func *item)
 
69
  :Item_result_field(session, item),
 
70
   allowed_arg_cols(item->allowed_arg_cols),
 
71
   arg_count(item->arg_count),
 
72
   used_tables_cache(item->used_tables_cache),
 
73
   not_null_tables_cache(item->not_null_tables_cache),
 
74
   const_item_cache(item->const_item_cache)
159
75
{
160
76
  if (arg_count)
161
77
  {
163
79
      args= tmp_arg;
164
80
    else
165
81
    {
166
 
      if (!(args=(Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
82
      if (!(args=(Item**) session->alloc(sizeof(Item*)*arg_count)))
167
83
        return;
168
84
    }
169
85
    memcpy(args, item->args, sizeof(Item*)*arg_count);
170
86
  }
171
 
  collation.set(DERIVATION_SYSCONST);
172
87
}
173
88
 
174
89
 
214
129
  unsigned char buff[STACK_BUFF_ALLOC];      // Max argument in function
215
130
  session->session_marker= 0;
216
131
  used_tables_cache= not_null_tables_cache= 0;
217
 
  const_item_cache= true;
 
132
  const_item_cache=1;
218
133
 
219
134
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
220
135
    return true;        // Fatal error if flag is set!
269
184
  Item **arg,**arg_end;
270
185
 
271
186
  used_tables_cache= not_null_tables_cache= 0;
272
 
  const_item_cache= false;
 
187
  const_item_cache=1;
273
188
 
274
189
  if (arg_count)
275
190
  {
364
279
        change records at each execution.
365
280
      */
366
281
      if (*arg != new_item)
367
 
        getSession().change_item_tree(arg, new_item);
 
282
        current_session->change_item_tree(arg, new_item);
368
283
    }
369
284
  }
370
285
  return (this->*transformer)(argument);
434
349
void Item_func::update_used_tables()
435
350
{
436
351
  used_tables_cache=0;
437
 
  const_item_cache= true;
 
352
  const_item_cache=1;
438
353
  for (uint32_t i=0 ; i < arg_count ; i++)
439
354
  {
440
355
    args[i]->update_used_tables();
514
429
}
515
430
 
516
431
 
517
 
bool Item_func::get_arg0_date(type::Time &ltime, uint32_t fuzzy_date)
 
432
bool Item_func::get_arg0_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
518
433
{
519
434
  return (null_value=args[0]->get_date(ltime, fuzzy_date));
520
435
}
521
436
 
522
437
 
523
 
bool Item_func::get_arg0_time(type::Time &ltime)
 
438
bool Item_func::get_arg0_time(DRIZZLE_TIME *ltime)
524
439
{
525
 
  return (null_value= args[0]->get_time(ltime));
 
440
  return (null_value=args[0]->get_time(ltime));
526
441
}
527
442
 
528
443
 
535
450
 
536
451
Field *Item_func::tmp_table_field(Table *table)
537
452
{
538
 
  Field *field= NULL;
 
453
  Field *field;
539
454
 
540
455
  switch (result_type()) {
541
456
  case INT_RESULT:
542
 
    if (unsigned_flag)
543
 
    {
544
 
      field= new field::Size(max_length, maybe_null, name, true);
545
 
    } 
546
 
    else if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
547
 
    {
548
 
      field= new field::Int64(max_length, maybe_null, name, false);
549
 
    }
 
457
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
 
458
      field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
550
459
    else
551
 
    {
552
 
      field= new field::Int32(max_length, maybe_null, name, false);
553
 
    }
554
 
 
 
460
      field= new Field_long(max_length, maybe_null, name, unsigned_flag);
555
461
    break;
556
 
 
557
462
  case REAL_RESULT:
558
463
    field= new Field_double(max_length, maybe_null, name, decimals);
559
464
    break;
560
 
 
561
465
  case STRING_RESULT:
562
466
    return make_string_field(table);
563
 
 
564
467
  case DECIMAL_RESULT:
565
 
    field= new Field_decimal(class_decimal_precision_to_length(decimal_precision(),
 
468
    field= new Field_decimal(my_decimal_precision_to_length(decimal_precision(),
566
469
                                                            decimals,
567
470
                                                            unsigned_flag),
568
471
                             maybe_null,
571
474
                             unsigned_flag);
572
475
    break;
573
476
  case ROW_RESULT:
 
477
  default:
574
478
    // This case should never be chosen
575
479
    assert(0);
 
480
    field= 0;
576
481
    break;
577
482
  }
578
 
 
579
483
  if (field)
580
484
    field->init(table);
581
 
 
582
485
  return field;
583
486
}
584
487
 
585
488
 
586
 
type::Decimal *Item_func::val_decimal(type::Decimal *decimal_value)
 
489
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
587
490
{
588
491
  assert(fixed);
589
 
  int2_class_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
 
492
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
590
493
  return decimal_value;
591
494
}
592
495
 
660
563
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
661
564
  }
662
565
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
663
 
  max_length= class_decimal_precision_to_length(precision, decimals,
 
566
  max_length= my_decimal_precision_to_length(precision, decimals,
664
567
                                             unsigned_flag);
665
568
}
666
569
 
715
618
 
716
619
void Item_func::signal_divide_by_null()
717
620
{
718
 
  my_error(ER_DIVISION_BY_ZERO, MYF(0));
719
 
  null_value= 0;
 
621
  Session *session= current_session;
 
622
  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
623
  null_value= 1;
720
624
}
721
625
 
722
626