~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

  • Committer: Jay Pipes
  • Date: 2009-09-15 21:01:42 UTC
  • mto: (1126.2.5 merge)
  • mto: This revision was merged to the branch mainline in revision 1128.
  • Revision ID: jpipes@serialcoder-20090915210142-x8mwiqn1q0vzjspp
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.

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"
 
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
#include <drizzled/field/double.h>
28
29
#include <drizzled/field/decimal.h>
29
 
#include <drizzled/field/double.h>
30
 
#include <drizzled/field/size.h>
31
30
#include <drizzled/session.h>
32
31
#include <drizzled/error.h>
33
32
#include <drizzled/check_stack_overrun.h>
36
35
 
37
36
using namespace std;
38
37
 
39
 
namespace drizzled
40
 
{
41
 
 
42
 
 
43
38
void Item_func::set_arguments(List<Item> &list)
44
39
{
45
40
  allowed_arg_cols= 1;
46
41
  arg_count=list.elements;
47
42
  args= tmp_arg;                                // If 2 arguments
48
 
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
 
43
  if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
49
44
  {
50
45
    List_iterator_fast<Item> li(list);
51
46
    Item *item;
60
55
  list.empty();          // Fields are used
61
56
}
62
57
 
63
 
Item_func::Item_func(List<Item> &list) :
64
 
  _session(*current_session),
65
 
  allowed_arg_cols(1),
66
 
  const_item_cache(false)
 
58
Item_func::Item_func(List<Item> &list)
 
59
  :allowed_arg_cols(1)
67
60
{
68
 
  collation.set(DERIVATION_SYSCONST);
69
61
  set_arguments(list);
70
62
}
71
63
 
72
 
Item_func::Item_func(Session *session, Item_func *item) :
73
 
  Item_result_field(session, item),
74
 
  _session(*current_session),
75
 
  allowed_arg_cols(item->allowed_arg_cols),
76
 
  arg_count(item->arg_count),
77
 
  used_tables_cache(item->used_tables_cache),
78
 
  not_null_tables_cache(item->not_null_tables_cache),
79
 
  const_item_cache(item->const_item_cache)
 
64
Item_func::Item_func(Session *session, Item_func *item)
 
65
  :Item_result_field(session, item),
 
66
   allowed_arg_cols(item->allowed_arg_cols),
 
67
   arg_count(item->arg_count),
 
68
   used_tables_cache(item->used_tables_cache),
 
69
   not_null_tables_cache(item->not_null_tables_cache),
 
70
   const_item_cache(item->const_item_cache)
80
71
{
81
72
  if (arg_count)
82
73
  {
84
75
      args= tmp_arg;
85
76
    else
86
77
    {
87
 
      if (!(args=(Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
78
      if (!(args=(Item**) session->alloc(sizeof(Item*)*arg_count)))
88
79
        return;
89
80
    }
90
81
    memcpy(args, item->args, sizeof(Item*)*arg_count);
91
82
  }
92
 
  collation.set(DERIVATION_SYSCONST);
93
83
}
94
84
 
95
85
 
135
125
  unsigned char buff[STACK_BUFF_ALLOC];      // Max argument in function
136
126
  session->session_marker= 0;
137
127
  used_tables_cache= not_null_tables_cache= 0;
138
 
  const_item_cache= true;
 
128
  const_item_cache=1;
139
129
 
140
130
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
141
131
    return true;        // Fatal error if flag is set!
149
139
        We shouldn't call fix_fields() twice, so check 'fixed' field first
150
140
      */
151
141
      if ((!(*arg)->fixed && (*arg)->fix_fields(session, arg)))
152
 
        return true;
 
142
        return true;        /* purecov: inspected */
153
143
      item= *arg;
154
144
 
155
145
      if (allowed_arg_cols)
190
180
  Item **arg,**arg_end;
191
181
 
192
182
  used_tables_cache= not_null_tables_cache= 0;
193
 
  const_item_cache= false;
 
183
  const_item_cache=1;
194
184
 
195
185
  if (arg_count)
196
186
  {
285
275
        change records at each execution.
286
276
      */
287
277
      if (*arg != new_item)
288
 
        getSession().change_item_tree(arg, new_item);
 
278
        current_session->change_item_tree(arg, new_item);
289
279
    }
290
280
  }
291
281
  return (this->*transformer)(argument);
355
345
void Item_func::update_used_tables()
356
346
{
357
347
  used_tables_cache=0;
358
 
  const_item_cache= true;
 
348
  const_item_cache=1;
359
349
  for (uint32_t i=0 ; i < arg_count ; i++)
360
350
  {
361
351
    args[i]->update_used_tables();
435
425
}
436
426
 
437
427
 
438
 
bool Item_func::get_arg0_date(type::Time &ltime, uint32_t fuzzy_date)
 
428
bool Item_func::get_arg0_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
439
429
{
440
430
  return (null_value=args[0]->get_date(ltime, fuzzy_date));
441
431
}
442
432
 
443
433
 
444
 
bool Item_func::get_arg0_time(type::Time &ltime)
 
434
bool Item_func::get_arg0_time(DRIZZLE_TIME *ltime)
445
435
{
446
 
  return (null_value= args[0]->get_time(ltime));
 
436
  return (null_value=args[0]->get_time(ltime));
447
437
}
448
438
 
449
439
 
456
446
 
457
447
Field *Item_func::tmp_table_field(Table *table)
458
448
{
459
 
  Field *field= NULL;
 
449
  Field *field;
460
450
 
461
451
  switch (result_type()) {
462
452
  case INT_RESULT:
463
 
    if (unsigned_flag)
464
 
    {
465
 
      field= new field::Size(max_length, maybe_null, name, true);
466
 
    } 
467
 
    else if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
468
 
    {
469
 
      field= new field::Int64(max_length, maybe_null, name, false);
470
 
    }
 
453
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
 
454
      field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
471
455
    else
472
 
    {
473
 
      field= new field::Int32(max_length, maybe_null, name, false);
474
 
    }
475
 
 
 
456
      field= new Field_long(max_length, maybe_null, name, unsigned_flag);
476
457
    break;
477
 
 
478
458
  case REAL_RESULT:
479
459
    field= new Field_double(max_length, maybe_null, name, decimals);
480
460
    break;
481
 
 
482
461
  case STRING_RESULT:
483
462
    return make_string_field(table);
484
 
 
485
463
  case DECIMAL_RESULT:
486
 
    field= new Field_decimal(class_decimal_precision_to_length(decimal_precision(),
487
 
                                                            decimals,
488
 
                                                            unsigned_flag),
489
 
                             maybe_null,
490
 
                             name,
491
 
                             decimals,
492
 
                             unsigned_flag);
 
464
    field= new Field_new_decimal(
 
465
                       my_decimal_precision_to_length(decimal_precision(),
 
466
                                                      decimals,
 
467
                                                      unsigned_flag),
 
468
                       maybe_null, name, decimals, unsigned_flag);
493
469
    break;
494
470
  case ROW_RESULT:
 
471
  default:
495
472
    // This case should never be chosen
496
473
    assert(0);
 
474
    field= 0;
497
475
    break;
498
476
  }
499
 
 
500
477
  if (field)
501
478
    field->init(table);
502
 
 
503
479
  return field;
504
480
}
505
481
 
506
482
 
507
 
type::Decimal *Item_func::val_decimal(type::Decimal *decimal_value)
 
483
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
508
484
{
509
485
  assert(fixed);
510
 
  int2_class_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
 
486
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
511
487
  return decimal_value;
512
488
}
513
489
 
581
557
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
582
558
  }
583
559
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
584
 
  max_length= class_decimal_precision_to_length(precision, decimals,
 
560
  max_length= my_decimal_precision_to_length(precision, decimals,
585
561
                                             unsigned_flag);
586
562
}
587
563
 
636
612
 
637
613
void Item_func::signal_divide_by_null()
638
614
{
639
 
  my_error(ER_DIVISION_BY_ZERO, MYF(0));
640
 
  null_value= 0;
 
615
  Session *session= current_session;
 
616
  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
617
  null_value= 1;
641
618
}
642
619
 
643
620
 
649
626
}
650
627
 
651
628
 
652
 
} /* namespace drizzled */