Drizzled Public API Documentation

hybrid_type_traits_decimal.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include <drizzled/definitions.h>
23 #include <drizzled/field.h>
24 #include <drizzled/hybrid_type.h>
25 #include <drizzled/hybrid_type_traits_decimal.h>
26 #include <drizzled/item.h>
27 
28 #include <algorithm>
29 
30 using namespace std;
31 namespace drizzled
32 {
33 
34 /* Hybrid_type_traits_decimal */
35 static const Hybrid_type_traits_decimal decimal_traits_instance;
36 
37 
38 Item_result Hybrid_type_traits_decimal::type() const { return DECIMAL_RESULT; }
39 
40 
41 void
42 Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
43 {
44  item->decimals= arg->decimals;
45  item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS,
46  (unsigned int)DECIMAL_MAX_STR_LENGTH);
47 }
48 
49 
50 void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
51 {
52  val->dec_buf[0].set_zero();
53  val->used_dec_buf_no= 0;
54 }
55 
56 
57 void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
58 {
59  class_decimal_add(E_DEC_FATAL_ERROR,
60  &val->dec_buf[val->used_dec_buf_no ^ 1],
61  &val->dec_buf[val->used_dec_buf_no],
62  f->val_decimal(&val->dec_buf[2]));
63  val->used_dec_buf_no^= 1;
64 }
65 
66 
71 void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
72 {
73  int2_class_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
74  /* XXX: what is '4' for scale? */
75  class_decimal_div(E_DEC_FATAL_ERROR,
76  &val->dec_buf[val->used_dec_buf_no ^ 1],
77  &val->dec_buf[val->used_dec_buf_no],
78  &val->dec_buf[2], 4);
79  val->used_dec_buf_no^= 1;
80 }
81 
82 
83 int64_t
84 Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
85 {
86  int64_t result;
87  val->dec_buf[val->used_dec_buf_no].val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
88 
89  return result;
90 }
91 
92 
93 double
94 Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
95 {
96  class_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
97  &val->real);
98  return val->real;
99 }
100 
101 
102 type::Decimal *Hybrid_type_traits_decimal::val_decimal(Hybrid_type *val,
103  type::Decimal *) const
104 { return &val->dec_buf[val->used_dec_buf_no]; }
105 
106 
107 String *
108 Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
109  uint8_t decimals) const
110 {
111  class_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
112  decimals, false, &val->dec_buf[2]);
113  class_decimal2string(&val->dec_buf[2], 0, to);
114  return to;
115 }
116 
117 
118 const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
119 {
120  return &decimal_traits_instance;
121 }
122 
123 
124 Hybrid_type_traits_decimal::Hybrid_type_traits_decimal()
125 {}
126 
127 } /* namespace drizzled */