~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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/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/int32.h>
 
27
#include <drizzled/field/int64.h>
25
28
#include <drizzled/field/decimal.h>
26
29
#include <drizzled/field/double.h>
27
 
#include <drizzled/field/int32.h>
28
 
#include <drizzled/field/int64.h>
29
30
#include <drizzled/field/size.h>
30
 
#include <drizzled/function/math/int.h>
31
 
#include <drizzled/item/field.h>
32
31
#include <drizzled/session.h>
33
 
#include <drizzled/sql_list.h>
34
 
#include <drizzled/sql_string.h>
35
 
 
 
32
#include <drizzled/error.h>
 
33
#include <drizzled/check_stack_overrun.h>
36
34
#include <limits>
37
35
#include <algorithm>
38
36
 
42
40
{
43
41
 
44
42
 
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
 
 
117
43
void Item_func::set_arguments(List<Item> &list)
118
44
{
119
45
  allowed_arg_cols= 1;
120
 
  arg_count=list.size();
 
46
  arg_count=list.elements;
121
47
  args= tmp_arg;                                // If 2 arguments
122
48
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
123
49
  {
124
 
    List<Item>::iterator li(list.begin());
 
50
    List_iterator_fast<Item> li(list);
125
51
    Item *item;
126
52
    Item **save_args= args;
127
53
 
131
57
      with_sum_func|=item->with_sum_func;
132
58
    }
133
59
  }
134
 
  list.clear();          // Fields are used
 
60
  list.empty();          // Fields are used
135
61
}
136
62
 
137
63
Item_func::Item_func(List<Item> &list) :
 
64
  _session(*current_session),
138
65
  allowed_arg_cols(1),
139
66
  const_item_cache(false)
140
67
{
144
71
 
145
72
Item_func::Item_func(Session *session, Item_func *item) :
146
73
  Item_result_field(session, item),
 
74
  _session(*current_session),
147
75
  allowed_arg_cols(item->allowed_arg_cols),
148
76
  arg_count(item->arg_count),
149
77
  used_tables_cache(item->used_tables_cache),
156
84
      args= tmp_arg;
157
85
    else
158
86
    {
159
 
      if (!(args=(Item**) getSession().getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
87
      if (!(args=(Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
160
88
        return;
161
89
    }
162
90
    memcpy(args, item->args, sizeof(Item*)*arg_count);
349
277
      Item *new_item= (*arg)->transform(transformer, argument);
350
278
      if (!new_item)
351
279
        return 0;
352
 
      *arg= new_item;
 
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);
353
289
    }
354
290
  }
355
291
  return (this->*transformer)(argument);
397
333
      unsigned char *arg_v= *arg_p;
398
334
      Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
399
335
      if (new_item && *arg != new_item)
400
 
        *arg= new_item;
 
336
        current_session->change_item_tree(arg, new_item);
401
337
    }
402
338
  }
403
339
  return (this->*transformer)(arg_t);
441
377
}
442
378
 
443
379
 
444
 
void Item_func::print(String *str)
 
380
void Item_func::print(String *str, enum_query_type query_type)
445
381
{
446
382
  str->append(func_name());
447
383
  str->append('(');
448
 
  print_args(str, 0);
 
384
  print_args(str, 0, query_type);
449
385
  str->append(')');
450
386
}
451
387
 
452
388
 
453
 
void Item_func::print_args(String *str, uint32_t from)
 
389
void Item_func::print_args(String *str, uint32_t from, enum_query_type query_type)
454
390
{
455
391
  for (uint32_t i=from ; i < arg_count ; i++)
456
392
  {
457
393
    if (i != from)
458
394
      str->append(',');
459
 
    args[i]->print(str);
 
395
    args[i]->print(str, query_type);
460
396
  }
461
397
}
462
398
 
463
399
 
464
 
void Item_func::print_op(String *str)
 
400
void Item_func::print_op(String *str, enum_query_type query_type)
465
401
{
466
402
  str->append('(');
467
403
  for (uint32_t i=0 ; i < arg_count-1 ; i++)
468
404
  {
469
 
    args[i]->print(str);
 
405
    args[i]->print(str, query_type);
470
406
    str->append(' ');
471
407
    str->append(func_name());
472
408
    str->append(' ');
473
409
  }
474
 
  args[arg_count-1]->print(str);
 
410
  args[arg_count-1]->print(str, query_type);
475
411
  str->append(')');
476
412
}
477
413