~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

mergeĀ lp:~linuxjedi/drizzle/trunk-remove-drizzleadmin

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
 
 
22
 
#include <drizzled/sql_string.h>
23
 
#include <drizzled/sql_list.h>
24
 
 
25
 
#include <drizzled/function/math/int.h>
 
20
#include <config.h>
 
21
 
 
22
#include <drizzled/check_stack_overrun.h>
 
23
#include <drizzled/current_session.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/field/decimal.h>
 
26
#include <drizzled/field/double.h>
26
27
#include <drizzled/field/int32.h>
27
28
#include <drizzled/field/int64.h>
28
 
#include <drizzled/field/decimal.h>
29
 
#include <drizzled/field/double.h>
30
29
#include <drizzled/field/size.h>
 
30
#include <drizzled/function/math/int.h>
 
31
#include <drizzled/item/field.h>
31
32
#include <drizzled/session.h>
32
 
#include <drizzled/error.h>
33
 
#include <drizzled/check_stack_overrun.h>
 
33
#include <drizzled/sql_list.h>
 
34
#include <drizzled/sql_string.h>
 
35
 
34
36
#include <limits>
35
37
#include <algorithm>
36
38
 
40
42
{
41
43
 
42
44
 
 
45
Item_func::Item_func(void):
 
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
  allowed_arg_cols(1), arg_count(1),
 
55
  const_item_cache(false)
 
56
  {
 
57
    args= tmp_arg;
 
58
    args[0]= a;
 
59
    with_sum_func= a->with_sum_func;
 
60
    collation.set(DERIVATION_SYSCONST);
 
61
  }
 
62
 
 
63
Item_func::Item_func(Item *a,Item *b):
 
64
  allowed_arg_cols(1), arg_count(2),
 
65
  const_item_cache(false)
 
66
  {
 
67
    args= tmp_arg;
 
68
    args[0]= a; args[1]= b;
 
69
    with_sum_func= a->with_sum_func || b->with_sum_func;
 
70
    collation.set(DERIVATION_SYSCONST);
 
71
  }
 
72
 
 
73
Item_func::Item_func(Item *a,Item *b,Item *c):
 
74
  allowed_arg_cols(1),
 
75
  const_item_cache(false)
 
76
  {
 
77
    arg_count= 0;
 
78
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*3)))
 
79
    {
 
80
      arg_count= 3;
 
81
      args[0]= a; args[1]= b; args[2]= c;
 
82
      with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
 
83
    }
 
84
    collation.set(DERIVATION_SYSCONST);
 
85
  }
 
86
 
 
87
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d):
 
88
  allowed_arg_cols(1),
 
89
  const_item_cache(false)
 
90
  {
 
91
    arg_count= 0;
 
92
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*4)))
 
93
    {
 
94
      arg_count= 4;
 
95
      args[0]= a; args[1]= b; args[2]= c; args[3]= d;
 
96
      with_sum_func= a->with_sum_func || b->with_sum_func ||
 
97
        c->with_sum_func || d->with_sum_func;
 
98
    }
 
99
    collation.set(DERIVATION_SYSCONST);
 
100
  }
 
101
 
 
102
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
 
103
  allowed_arg_cols(1),
 
104
  const_item_cache(false)
 
105
  {
 
106
    arg_count= 5;
 
107
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*5)))
 
108
    {
 
109
      args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
 
110
      with_sum_func= a->with_sum_func || b->with_sum_func ||
 
111
        c->with_sum_func || d->with_sum_func || e->with_sum_func ;
 
112
    }
 
113
    collation.set(DERIVATION_SYSCONST);
 
114
  }
 
115
 
 
116
 
43
117
void Item_func::set_arguments(List<Item> &list)
44
118
{
45
119
  allowed_arg_cols= 1;
46
 
  arg_count=list.elements;
 
120
  arg_count=list.size();
47
121
  args= tmp_arg;                                // If 2 arguments
48
122
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
49
123
  {
50
 
    List_iterator_fast<Item> li(list);
 
124
    List<Item>::iterator li(list.begin());
51
125
    Item *item;
52
126
    Item **save_args= args;
53
127
 
57
131
      with_sum_func|=item->with_sum_func;
58
132
    }
59
133
  }
60
 
  list.empty();          // Fields are used
 
134
  list.clear();          // Fields are used
61
135
}
62
136
 
63
137
Item_func::Item_func(List<Item> &list) :
64
 
  _session(*current_session),
65
138
  allowed_arg_cols(1),
66
139
  const_item_cache(false)
67
140
{
71
144
 
72
145
Item_func::Item_func(Session *session, Item_func *item) :
73
146
  Item_result_field(session, item),
74
 
  _session(*current_session),
75
147
  allowed_arg_cols(item->allowed_arg_cols),
76
148
  arg_count(item->arg_count),
77
149
  used_tables_cache(item->used_tables_cache),
84
156
      args= tmp_arg;
85
157
    else
86
158
    {
87
 
      if (!(args=(Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
159
      if (!(args=(Item**) getSession().getMemRoot()->allocate(sizeof(Item*)*arg_count)))
88
160
        return;
89
161
    }
90
162
    memcpy(args, item->args, sizeof(Item*)*arg_count);
277
349
      Item *new_item= (*arg)->transform(transformer, argument);
278
350
      if (!new_item)
279
351
        return 0;
280
 
 
281
 
      /*
282
 
        Session::change_item_tree() should be called only if the tree was
283
 
        really transformed, i.e. when a new item has been created.
284
 
        Otherwise we'll be allocating a lot of unnecessary memory for
285
 
        change records at each execution.
286
 
      */
287
 
      if (*arg != new_item)
288
 
        getSession().change_item_tree(arg, new_item);
 
352
      *arg= new_item;
289
353
    }
290
354
  }
291
355
  return (this->*transformer)(argument);
333
397
      unsigned char *arg_v= *arg_p;
334
398
      Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
335
399
      if (new_item && *arg != new_item)
336
 
        current_session->change_item_tree(arg, new_item);
 
400
        *arg= new_item;
337
401
    }
338
402
  }
339
403
  return (this->*transformer)(arg_t);
377
441
}
378
442
 
379
443
 
380
 
void Item_func::print(String *str, enum_query_type query_type)
 
444
void Item_func::print(String *str)
381
445
{
382
446
  str->append(func_name());
383
447
  str->append('(');
384
 
  print_args(str, 0, query_type);
 
448
  print_args(str, 0);
385
449
  str->append(')');
386
450
}
387
451
 
388
452
 
389
 
void Item_func::print_args(String *str, uint32_t from, enum_query_type query_type)
 
453
void Item_func::print_args(String *str, uint32_t from)
390
454
{
391
455
  for (uint32_t i=from ; i < arg_count ; i++)
392
456
  {
393
457
    if (i != from)
394
458
      str->append(',');
395
 
    args[i]->print(str, query_type);
 
459
    args[i]->print(str);
396
460
  }
397
461
}
398
462
 
399
463
 
400
 
void Item_func::print_op(String *str, enum_query_type query_type)
 
464
void Item_func::print_op(String *str)
401
465
{
402
466
  str->append('(');
403
467
  for (uint32_t i=0 ; i < arg_count-1 ; i++)
404
468
  {
405
 
    args[i]->print(str, query_type);
 
469
    args[i]->print(str);
406
470
    str->append(' ');
407
471
    str->append(func_name());
408
472
    str->append(' ');
409
473
  }
410
 
  args[arg_count-1]->print(str, query_type);
 
474
  args[arg_count-1]->print(str);
411
475
  str->append(')');
412
476
}
413
477