~drizzle-trunk/drizzle/development

574.3.6 by Lee
moving functions from item_strfunc to functions/str directory
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1759.3.22 by Stewart Smith
move FORMAT() function to string_functions plugin
21
#include "format.h"
919.2.11 by Monty Taylor
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!
22
23
#include <limits>
574.3.6 by Lee
moving functions from item_strfunc to functions/str directory
24
919.2.11 by Monty Taylor
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!
25
using namespace std;
584.1.4 by Monty Taylor
Added namespace call.
26
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
27
namespace drizzled
28
{
29
574.3.6 by Lee
moving functions from item_strfunc to functions/str directory
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
void Item_func_format::fix_length_and_dec()
39
{
40
  collation.set(default_charset());
41
  uint32_t char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
42
  max_length= ((char_length + (char_length-args[0]->decimals)/3) *
43
               collation.collation->mbmaxlen);
44
}
45
46
47
/**
48
  @todo
49
  This needs to be fixed for multi-byte character set where numbers
50
  are stored in more than one byte
51
*/
52
53
String *Item_func_format::val_str(String *str)
54
{
55
  uint32_t length;
56
  uint32_t str_length;
57
  /* Number of decimal digits */
58
  int dec;
59
  /* Number of characters used to represent the decimals, including '.' */
60
  uint32_t dec_length;
61
  int diff;
62
  assert(fixed == 1);
63
64
  dec= (int) args[1]->val_int();
65
  if (args[1]->null_value)
66
  {
67
    null_value=1;
68
    return NULL;
69
  }
70
71
  dec= set_zone(dec, 0, FORMAT_MAX_DECIMALS);
72
  dec_length= dec ? dec+1 : 0;
73
  null_value=0;
74
75
  if (args[0]->result_type() == DECIMAL_RESULT ||
76
      args[0]->result_type() == INT_RESULT)
77
  {
78
    my_decimal dec_val, rnd_dec, *res;
79
    res= args[0]->val_decimal(&dec_val);
80
    if ((null_value=args[0]->null_value))
971.6.11 by Eric Day
Removed purecov messages.
81
      return 0;
574.3.6 by Lee
moving functions from item_strfunc to functions/str directory
82
    my_decimal_round(E_DEC_FATAL_ERROR, res, dec, false, &rnd_dec);
83
    my_decimal2string(E_DEC_FATAL_ERROR, &rnd_dec, 0, 0, 0, str);
84
    str_length= str->length();
85
    if (rnd_dec.sign())
86
      str_length--;
87
  }
88
  else
89
  {
90
    double nr= args[0]->val_real();
91
    if ((null_value=args[0]->null_value))
971.6.11 by Eric Day
Removed purecov messages.
92
      return 0;
574.3.6 by Lee
moving functions from item_strfunc to functions/str directory
93
    nr= my_double_round(nr, (int64_t) dec, false, false);
94
    /* Here default_charset() is right as this is not an automatic conversion */
95
    str->set_real(nr, dec, default_charset());
919.2.11 by Monty Taylor
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!
96
    if (nr == numeric_limits<double>::quiet_NaN())
574.3.6 by Lee
moving functions from item_strfunc to functions/str directory
97
      return str;
98
    str_length=str->length();
99
    if (nr < 0)
100
      str_length--;				// Don't count sign
101
  }
102
  /* We need this test to handle 'nan' values */
103
  if (str_length >= dec_length+4)
104
  {
105
    char *tmp,*pos;
106
    length= str->length()+(diff=((int)(str_length- dec_length-1))/3);
107
    str= copy_if_not_alloced(&tmp_str,str,length);
108
    str->length(length);
109
    tmp= (char*) str->ptr()+length - dec_length-1;
110
    for (pos= (char*) str->ptr()+length-1; pos != tmp; pos--)
111
      pos[0]= pos[-diff];
112
    while (diff)
113
    {
114
      *pos= *(pos - diff);
115
      pos--;
116
      *pos= *(pos - diff);
117
      pos--;
118
      *pos= *(pos - diff);
119
      pos--;
120
      pos[0]=',';
121
      pos--;
122
      diff--;
123
    }
124
  }
125
  return str;
126
}
127
128
129
void Item_func_format::print(String *str, enum_query_type query_type)
130
{
131
  str->append(STRING_WITH_LEN("format("));
132
  args[0]->print(str, query_type);
133
  str->append(',');
134
  args[1]->print(str, query_type);
135
  str->append(')');
136
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
137
138
} /* namespace drizzled */