~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/hybrid_type_traits_decimal.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
#include "drizzled/hybrid_type_traits_decimal.h"
 
22
#include "drizzled/hybrid_type.h"
 
23
#include "drizzled/definitions.h"
 
24
#include "drizzled/item.h"
 
25
 
 
26
#include <algorithm>
 
27
 
 
28
using namespace std;
 
29
namespace drizzled
 
30
{
 
31
 
 
32
/* Hybrid_type_traits_decimal */
 
33
static const Hybrid_type_traits_decimal decimal_traits_instance;
 
34
 
 
35
 
 
36
Item_result Hybrid_type_traits_decimal::type() const { return DECIMAL_RESULT; }
 
37
 
 
38
 
 
39
void
 
40
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
 
41
{
 
42
  item->decimals= arg->decimals;
 
43
  item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS,
 
44
                        (unsigned int)DECIMAL_MAX_STR_LENGTH);
 
45
}
 
46
 
 
47
 
 
48
void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
 
49
{
 
50
  my_decimal_set_zero(&val->dec_buf[0]);
 
51
  val->used_dec_buf_no= 0;
 
52
}
 
53
 
 
54
 
 
55
void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
 
56
{
 
57
  my_decimal_add(E_DEC_FATAL_ERROR,
 
58
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
 
59
                 &val->dec_buf[val->used_dec_buf_no],
 
60
                 f->val_decimal(&val->dec_buf[2]));
 
61
  val->used_dec_buf_no^= 1;
 
62
}
 
63
 
 
64
 
 
65
/**
 
66
  @todo
 
67
  what is '4' for scale?
 
68
*/
 
69
void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
 
70
{
 
71
  int2my_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
 
72
  /* XXX: what is '4' for scale? */
 
73
  my_decimal_div(E_DEC_FATAL_ERROR,
 
74
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
 
75
                 &val->dec_buf[val->used_dec_buf_no],
 
76
                 &val->dec_buf[2], 4);
 
77
  val->used_dec_buf_no^= 1;
 
78
}
 
79
 
 
80
 
 
81
int64_t
 
82
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
 
83
{
 
84
  int64_t result;
 
85
  my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
 
86
                 unsigned_flag, &result);
 
87
  return result;
 
88
}
 
89
 
 
90
 
 
91
double
 
92
Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
 
93
{
 
94
  my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
 
95
                    &val->real);
 
96
  return val->real;
 
97
}
 
98
 
 
99
 
 
100
my_decimal *Hybrid_type_traits_decimal::val_decimal(Hybrid_type *val,
 
101
                                                    my_decimal *) const
 
102
{ return &val->dec_buf[val->used_dec_buf_no]; }
 
103
 
 
104
 
 
105
String *
 
106
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
 
107
                                    uint8_t decimals) const
 
108
{
 
109
  my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
 
110
                   decimals, false, &val->dec_buf[2]);
 
111
  my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
 
112
  return to;
 
113
}
 
114
 
 
115
 
 
116
const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
 
117
{
 
118
  return &decimal_traits_instance;
 
119
}
 
120
 
 
121
 
 
122
Hybrid_type_traits_decimal::Hybrid_type_traits_decimal()
 
123
{}
 
124
 
 
125
} /* namespace drizzled */