~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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"
 
20
using namespace std;
 
21
#include <drizzled/server_includes.h>
 
22
 
 
23
#include CSTDINT_H
 
24
#include <cassert>
21
25
 
22
26
#include <drizzled/sql_string.h>
23
27
#include <drizzled/sql_list.h>
24
28
 
25
29
#include <drizzled/function/math/int.h>
26
 
#include <drizzled/field/int32.h>
27
 
#include <drizzled/field/int64.h>
 
30
#include <drizzled/field/int64_t.h>
 
31
#include <drizzled/field/long.h>
28
32
#include <drizzled/field/double.h>
29
33
#include <drizzled/field/decimal.h>
 
34
#include CMATH_H
 
35
#include <drizzled/util/math.h>
30
36
#include <drizzled/session.h>
31
37
#include <drizzled/error.h>
32
38
#include <drizzled/check_stack_overrun.h>
33
 
#include <limits>
34
 
#include <algorithm>
35
 
 
36
 
using namespace std;
37
 
 
38
 
namespace drizzled
39
 
{
 
39
 
 
40
#if defined(CMATH_NAMESPACE)
 
41
using namespace CMATH_NAMESPACE;
 
42
#endif
 
43
 
40
44
 
41
45
 
42
46
void Item_func::set_arguments(List<Item> &list)
44
48
  allowed_arg_cols= 1;
45
49
  arg_count=list.elements;
46
50
  args= tmp_arg;                                // If 2 arguments
47
 
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
 
51
  if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
48
52
  {
49
53
    List_iterator_fast<Item> li(list);
50
54
    Item *item;
59
63
  list.empty();          // Fields are used
60
64
}
61
65
 
62
 
Item_func::Item_func(List<Item> &list) :
63
 
  _session(*current_session),
64
 
  allowed_arg_cols(1)
 
66
Item_func::Item_func(List<Item> &list)
 
67
  :allowed_arg_cols(1)
65
68
{
66
 
  collation.set(DERIVATION_SYSCONST);
67
69
  set_arguments(list);
68
70
}
69
71
 
70
 
Item_func::Item_func(Session *session, Item_func *item) :
71
 
  Item_result_field(session, item),
72
 
  _session(*current_session),
73
 
  allowed_arg_cols(item->allowed_arg_cols),
74
 
  arg_count(item->arg_count),
75
 
  used_tables_cache(item->used_tables_cache),
76
 
  not_null_tables_cache(item->not_null_tables_cache),
77
 
  const_item_cache(item->const_item_cache)
 
72
Item_func::Item_func(Session *session, Item_func *item)
 
73
  :Item_result_field(session, item),
 
74
   allowed_arg_cols(item->allowed_arg_cols),
 
75
   arg_count(item->arg_count),
 
76
   used_tables_cache(item->used_tables_cache),
 
77
   not_null_tables_cache(item->not_null_tables_cache),
 
78
   const_item_cache(item->const_item_cache)
78
79
{
79
80
  if (arg_count)
80
81
  {
87
88
    }
88
89
    memcpy(args, item->args, sizeof(Item*)*arg_count);
89
90
  }
90
 
  collation.set(DERIVATION_SYSCONST);
91
91
}
92
92
 
93
93
 
147
147
        We shouldn't call fix_fields() twice, so check 'fixed' field first
148
148
      */
149
149
      if ((!(*arg)->fixed && (*arg)->fix_fields(session, arg)))
150
 
        return true;
 
150
        return true;        /* purecov: inspected */
151
151
      item= *arg;
152
152
 
153
153
      if (allowed_arg_cols)
283
283
        change records at each execution.
284
284
      */
285
285
      if (*arg != new_item)
286
 
        getSession().change_item_tree(arg, new_item);
 
286
        current_session->change_item_tree(arg, new_item);
287
287
    }
288
288
  }
289
289
  return (this->*transformer)(argument);
454
454
 
455
455
Field *Item_func::tmp_table_field(Table *table)
456
456
{
457
 
  Field *field= NULL;
 
457
  Field *field;
458
458
 
459
459
  switch (result_type()) {
460
460
  case INT_RESULT:
461
461
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
462
 
      field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
462
      field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
463
463
    else
464
 
      field= new field::Int32(max_length, maybe_null, name, unsigned_flag);
 
464
      field= new Field_long(max_length, maybe_null, name, unsigned_flag);
465
465
    break;
466
 
 
467
466
  case REAL_RESULT:
468
467
    field= new Field_double(max_length, maybe_null, name, decimals);
469
468
    break;
470
 
 
471
469
  case STRING_RESULT:
472
470
    return make_string_field(table);
473
 
 
 
471
    break;
474
472
  case DECIMAL_RESULT:
475
 
    field= new Field_decimal(my_decimal_precision_to_length(decimal_precision(),
476
 
                                                            decimals,
477
 
                                                            unsigned_flag),
478
 
                             maybe_null,
479
 
                             name,
480
 
                             decimals,
481
 
                             unsigned_flag);
 
473
    field= new Field_new_decimal(
 
474
                       my_decimal_precision_to_length(decimal_precision(),
 
475
                                                      decimals,
 
476
                                                      unsigned_flag),
 
477
                       maybe_null, name, decimals, unsigned_flag);
482
478
    break;
483
479
  case ROW_RESULT:
 
480
  default:
484
481
    // This case should never be chosen
485
482
    assert(0);
 
483
    field= 0;
486
484
    break;
487
485
  }
488
 
 
489
486
  if (field)
490
487
    field->init(table);
491
 
 
492
488
  return field;
493
489
}
494
490
 
527
523
 
528
524
double Item_func::fix_result(double value)
529
525
{
530
 
  static double fix_infinity= numeric_limits<double>::infinity();
531
 
 
532
 
  if (value != fix_infinity && value != -fix_infinity)
 
526
  if (isfinite(value))
533
527
    return value;
534
528
  null_value=1;
535
529
  return 0.0;
563
557
  int max_int_part= 0;
564
558
  decimals= 0;
565
559
  unsigned_flag= 1;
566
 
  for (uint32_t i= 0 ; i < arg_count ; i++)
 
560
  for (uint32_t i=0 ; i < arg_count ; i++)
567
561
  {
568
562
    set_if_bigger(decimals, args[i]->decimals);
569
563
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
570
564
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
571
565
  }
572
 
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
566
  int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
573
567
  max_length= my_decimal_precision_to_length(precision, decimals,
574
568
                                             unsigned_flag);
575
569
}
625
619
 
626
620
void Item_func::signal_divide_by_null()
627
621
{
628
 
  my_error(ER_DIVISION_BY_ZERO, MYF(0));
629
 
  null_value= 0;
 
622
  Session *session= current_session;
 
623
  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
624
  null_value= 1;
630
625
}
631
626
 
632
627
 
638
633
}
639
634
 
640
635
 
641
 
} /* namespace drizzled */