~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/format.cc

code clean move Item_func_abs, Item_func_int_exp, Item_func_ln, Item_func_log to functions directory

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/function/str/format.h>
22
 
 
23
 
#include <limits>
24
 
 
25
 
using namespace std;
26
 
 
27
 
namespace drizzled
28
 
{
29
 
 
30
 
/**
31
 
  Change a number to format '3,333,333,333.000'.
32
 
 
33
 
  This should be 'internationalized' sometimes.
34
 
*/
35
 
 
36
 
const int FORMAT_MAX_DECIMALS= 30;
37
 
 
38
 
Item_func_format::Item_func_format(Item *org, Item *dec)
39
 
: Item_str_func(org, dec)
40
 
{
41
 
}
42
 
 
43
 
void Item_func_format::fix_length_and_dec()
44
 
{
45
 
  collation.set(default_charset());
46
 
  uint32_t char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
47
 
  max_length= ((char_length + (char_length-args[0]->decimals)/3) *
48
 
               collation.collation->mbmaxlen);
49
 
}
50
 
 
51
 
 
52
 
/**
53
 
  @todo
54
 
  This needs to be fixed for multi-byte character set where numbers
55
 
  are stored in more than one byte
56
 
*/
57
 
 
58
 
String *Item_func_format::val_str(String *str)
59
 
{
60
 
  uint32_t length;
61
 
  uint32_t str_length;
62
 
  /* Number of decimal digits */
63
 
  int dec;
64
 
  /* Number of characters used to represent the decimals, including '.' */
65
 
  uint32_t dec_length;
66
 
  int diff;
67
 
  assert(fixed == 1);
68
 
 
69
 
  dec= (int) args[1]->val_int();
70
 
  if (args[1]->null_value)
71
 
  {
72
 
    null_value=1;
73
 
    return NULL;
74
 
  }
75
 
 
76
 
  dec= set_zone(dec, 0, FORMAT_MAX_DECIMALS);
77
 
  dec_length= dec ? dec+1 : 0;
78
 
  null_value=0;
79
 
 
80
 
  if (args[0]->result_type() == DECIMAL_RESULT ||
81
 
      args[0]->result_type() == INT_RESULT)
82
 
  {
83
 
    my_decimal dec_val, rnd_dec, *res;
84
 
    res= args[0]->val_decimal(&dec_val);
85
 
    if ((null_value=args[0]->null_value))
86
 
      return 0;
87
 
    my_decimal_round(E_DEC_FATAL_ERROR, res, dec, false, &rnd_dec);
88
 
    my_decimal2string(E_DEC_FATAL_ERROR, &rnd_dec, 0, 0, 0, str);
89
 
    str_length= str->length();
90
 
    if (rnd_dec.sign())
91
 
      str_length--;
92
 
  }
93
 
  else
94
 
  {
95
 
    double nr= args[0]->val_real();
96
 
    if ((null_value=args[0]->null_value))
97
 
      return 0;
98
 
    nr= my_double_round(nr, (int64_t) dec, false, false);
99
 
    /* Here default_charset() is right as this is not an automatic conversion */
100
 
    str->set_real(nr, dec, default_charset());
101
 
    if (nr == numeric_limits<double>::quiet_NaN())
102
 
      return str;
103
 
    str_length=str->length();
104
 
    if (nr < 0)
105
 
      str_length--;                             // Don't count sign
106
 
  }
107
 
  /* We need this test to handle 'nan' values */
108
 
  if (str_length >= dec_length+4)
109
 
  {
110
 
    char *tmp,*pos;
111
 
    length= str->length()+(diff=((int)(str_length- dec_length-1))/3);
112
 
    str= copy_if_not_alloced(&tmp_str,str,length);
113
 
    str->length(length);
114
 
    tmp= (char*) str->ptr()+length - dec_length-1;
115
 
    for (pos= (char*) str->ptr()+length-1; pos != tmp; pos--)
116
 
      pos[0]= pos[-diff];
117
 
    while (diff)
118
 
    {
119
 
      *pos= *(pos - diff);
120
 
      pos--;
121
 
      *pos= *(pos - diff);
122
 
      pos--;
123
 
      *pos= *(pos - diff);
124
 
      pos--;
125
 
      pos[0]=',';
126
 
      pos--;
127
 
      diff--;
128
 
    }
129
 
  }
130
 
  return str;
131
 
}
132
 
 
133
 
 
134
 
void Item_func_format::print(String *str, enum_query_type query_type)
135
 
{
136
 
  str->append(STRING_WITH_LEN("format("));
137
 
  args[0]->print(str, query_type);
138
 
  str->append(',');
139
 
  args[1]->print(str, query_type);
140
 
  str->append(')');
141
 
}
142
 
 
143
 
} /* namespace drizzled */