~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Fixed the clock_gettime test.

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