~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

few updates and modifications to admin commands

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
62
Item_func::Item_func(List<Item> &list) :
143
63
  _session(*current_session),
144
 
  allowed_arg_cols(1),
145
 
  const_item_cache(false)
 
64
  allowed_arg_cols(1)
146
65
{
147
66
  collation.set(DERIVATION_SYSCONST);
148
67
  set_arguments(list);
163
82
      args= tmp_arg;
164
83
    else
165
84
    {
166
 
      if (!(args=(Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
85
      if (!(args=(Item**) session->alloc(sizeof(Item*)*arg_count)))
167
86
        return;
168
87
    }
169
88
    memcpy(args, item->args, sizeof(Item*)*arg_count);
214
133
  unsigned char buff[STACK_BUFF_ALLOC];      // Max argument in function
215
134
  session->session_marker= 0;
216
135
  used_tables_cache= not_null_tables_cache= 0;
217
 
  const_item_cache= true;
 
136
  const_item_cache=1;
218
137
 
219
138
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
220
139
    return true;        // Fatal error if flag is set!
269
188
  Item **arg,**arg_end;
270
189
 
271
190
  used_tables_cache= not_null_tables_cache= 0;
272
 
  const_item_cache= false;
 
191
  const_item_cache=1;
273
192
 
274
193
  if (arg_count)
275
194
  {
434
353
void Item_func::update_used_tables()
435
354
{
436
355
  used_tables_cache=0;
437
 
  const_item_cache= true;
 
356
  const_item_cache=1;
438
357
  for (uint32_t i=0 ; i < arg_count ; i++)
439
358
  {
440
359
    args[i]->update_used_tables();
514
433
}
515
434
 
516
435
 
517
 
bool Item_func::get_arg0_date(type::Time &ltime, uint32_t fuzzy_date)
 
436
bool Item_func::get_arg0_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
518
437
{
519
438
  return (null_value=args[0]->get_date(ltime, fuzzy_date));
520
439
}
521
440
 
522
441
 
523
 
bool Item_func::get_arg0_time(type::Time &ltime)
 
442
bool Item_func::get_arg0_time(DRIZZLE_TIME *ltime)
524
443
{
525
 
  return (null_value= args[0]->get_time(ltime));
 
444
  return (null_value=args[0]->get_time(ltime));
526
445
}
527
446
 
528
447
 
535
454
 
536
455
Field *Item_func::tmp_table_field(Table *table)
537
456
{
538
 
  Field *field= NULL;
 
457
  Field *field;
539
458
 
540
459
  switch (result_type()) {
541
460
  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
 
    }
 
461
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
 
462
      field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
550
463
    else
551
 
    {
552
 
      field= new field::Int32(max_length, maybe_null, name, false);
553
 
    }
554
 
 
 
464
      field= new Field_long(max_length, maybe_null, name, unsigned_flag);
555
465
    break;
556
 
 
557
466
  case REAL_RESULT:
558
467
    field= new Field_double(max_length, maybe_null, name, decimals);
559
468
    break;
560
 
 
561
469
  case STRING_RESULT:
562
470
    return make_string_field(table);
563
 
 
564
471
  case DECIMAL_RESULT:
565
 
    field= new Field_decimal(class_decimal_precision_to_length(decimal_precision(),
 
472
    field= new Field_decimal(my_decimal_precision_to_length(decimal_precision(),
566
473
                                                            decimals,
567
474
                                                            unsigned_flag),
568
475
                             maybe_null,
571
478
                             unsigned_flag);
572
479
    break;
573
480
  case ROW_RESULT:
 
481
  default:
574
482
    // This case should never be chosen
575
483
    assert(0);
 
484
    field= 0;
576
485
    break;
577
486
  }
578
 
 
579
487
  if (field)
580
488
    field->init(table);
581
 
 
582
489
  return field;
583
490
}
584
491
 
585
492
 
586
 
type::Decimal *Item_func::val_decimal(type::Decimal *decimal_value)
 
493
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
587
494
{
588
495
  assert(fixed);
589
 
  int2_class_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
 
496
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
590
497
  return decimal_value;
591
498
}
592
499
 
660
567
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
661
568
  }
662
569
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
663
 
  max_length= class_decimal_precision_to_length(precision, decimals,
 
570
  max_length= my_decimal_precision_to_length(precision, decimals,
664
571
                                             unsigned_flag);
665
572
}
666
573