~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Merged in latest plugin-slot-reorg.

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