~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int64_t.cc

MergingĀ mainline

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 MySQL
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; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
 
22
 
#include <drizzled/server_includes.h>
23
 
#include <drizzled/field/int64_t.h>
24
 
 
25
 
/****************************************************************************
26
 
 Field type int64_t int (8 bytes)
27
 
****************************************************************************/
28
 
 
29
 
int Field_int64_t::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
30
 
{
31
 
  int error= 0;
32
 
  char *end;
33
 
  uint64_t tmp;
34
 
 
35
 
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
36
 
  if (error == MY_ERRNO_ERANGE)
37
 
  {
38
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
39
 
    error= 1;
40
 
  }
41
 
  else if (table->in_use->count_cuted_fields && 
42
 
           check_int(cs, from, len, end, error))
43
 
    error= 1;
44
 
  else
45
 
    error= 0;
46
 
#ifdef WORDS_BIGENDIAN
47
 
  if (table->s->db_low_byte_first)
48
 
  {
49
 
    int8store(ptr,tmp);
50
 
  }
51
 
  else
52
 
#endif
53
 
    int64_tstore(ptr,tmp);
54
 
  return error;
55
 
}
56
 
 
57
 
 
58
 
int Field_int64_t::store(double nr)
59
 
{
60
 
  int error= 0;
61
 
  int64_t res;
62
 
 
63
 
  nr= rint(nr);
64
 
 
65
 
  if (nr <= (double) INT64_MIN)
66
 
  {
67
 
    res= INT64_MIN;
68
 
    error= (nr < (double) INT64_MIN);
69
 
  }
70
 
  else if (nr >= (double) (uint64_t) INT64_MAX)
71
 
  {
72
 
    res= INT64_MAX;
73
 
    error= (nr > (double) INT64_MAX);
74
 
  }
75
 
  else
76
 
    res=(int64_t) nr;
77
 
 
78
 
  if (error)
79
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
80
 
 
81
 
#ifdef WORDS_BIGENDIAN
82
 
  if (table->s->db_low_byte_first)
83
 
  {
84
 
    int8store(ptr,res);
85
 
  }
86
 
  else
87
 
#endif
88
 
    int64_tstore(ptr,res);
89
 
  return error;
90
 
}
91
 
 
92
 
 
93
 
int Field_int64_t::store(int64_t nr, bool unsigned_val __attribute__((unused)))
94
 
{
95
 
  int error= 0;
96
 
 
97
 
#ifdef WORDS_BIGENDIAN
98
 
  if (table->s->db_low_byte_first)
99
 
  {
100
 
    int8store(ptr,nr);
101
 
  }
102
 
  else
103
 
#endif
104
 
    int64_tstore(ptr,nr);
105
 
  return error;
106
 
}
107
 
 
108
 
 
109
 
double Field_int64_t::val_real(void)
110
 
{
111
 
  int64_t j;
112
 
#ifdef WORDS_BIGENDIAN
113
 
  if (table->s->db_low_byte_first)
114
 
  {
115
 
    j=sint8korr(ptr);
116
 
  }
117
 
  else
118
 
#endif
119
 
    int64_tget(j,ptr);
120
 
  /* The following is open coded to avoid a bug in gcc 3.3 */
121
 
  return (double) j;
122
 
}
123
 
 
124
 
 
125
 
int64_t Field_int64_t::val_int(void)
126
 
{
127
 
  int64_t j;
128
 
#ifdef WORDS_BIGENDIAN
129
 
  if (table->s->db_low_byte_first)
130
 
    j=sint8korr(ptr);
131
 
  else
132
 
#endif
133
 
    int64_tget(j,ptr);
134
 
  return j;
135
 
}
136
 
 
137
 
 
138
 
String *Field_int64_t::val_str(String *val_buffer,
139
 
                                String *val_ptr __attribute__((unused)))
140
 
{
141
 
  const CHARSET_INFO * const cs= &my_charset_bin;
142
 
  uint32_t length;
143
 
  uint32_t mlength=cmax(field_length+1,22*cs->mbmaxlen);
144
 
  val_buffer->alloc(mlength);
145
 
  char *to=(char*) val_buffer->ptr();
146
 
  int64_t j;
147
 
#ifdef WORDS_BIGENDIAN
148
 
  if (table->s->db_low_byte_first)
149
 
    j=sint8korr(ptr);
150
 
  else
151
 
#endif
152
 
    int64_tget(j,ptr);
153
 
 
154
 
  length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
155
 
  val_buffer->length(length);
156
 
 
157
 
  return val_buffer;
158
 
}
159
 
 
160
 
 
161
 
bool Field_int64_t::send_binary(Protocol *protocol)
162
 
{
163
 
  return protocol->store_int64_t(Field_int64_t::val_int(), false);
164
 
}
165
 
 
166
 
 
167
 
int Field_int64_t::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
168
 
{
169
 
  int64_t a,b;
170
 
#ifdef WORDS_BIGENDIAN
171
 
  if (table->s->db_low_byte_first)
172
 
  {
173
 
    a=sint8korr(a_ptr);
174
 
    b=sint8korr(b_ptr);
175
 
  }
176
 
  else
177
 
#endif
178
 
  {
179
 
    int64_tget(a,a_ptr);
180
 
    int64_tget(b,b_ptr);
181
 
  }
182
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
183
 
}
184
 
 
185
 
void Field_int64_t::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
186
 
{
187
 
#ifdef WORDS_BIGENDIAN
188
 
  if (!table->s->db_low_byte_first)
189
 
  {
190
 
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
191
 
    to[1]   = ptr[1];
192
 
    to[2]   = ptr[2];
193
 
    to[3]   = ptr[3];
194
 
    to[4]   = ptr[4];
195
 
    to[5]   = ptr[5];
196
 
    to[6]   = ptr[6];
197
 
    to[7]   = ptr[7];
198
 
  }
199
 
  else
200
 
#endif
201
 
  {
202
 
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
203
 
    to[1]   = ptr[6];
204
 
    to[2]   = ptr[5];
205
 
    to[3]   = ptr[4];
206
 
    to[4]   = ptr[3];
207
 
    to[5]   = ptr[2];
208
 
    to[6]   = ptr[1];
209
 
    to[7]   = ptr[0];
210
 
  }
211
 
}
212
 
 
213
 
 
214
 
void Field_int64_t::sql_type(String &res) const
215
 
{
216
 
  const CHARSET_INFO * const cs=res.charset();
217
 
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
218
 
}