~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int64_t.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:59 UTC
  • mfrom: (518 drizzle)
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063259-fwbqogq7lnezct0l
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include <config.h>
23
 
#include <drizzled/field/int64.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/table.h>
26
 
#include <drizzled/session.h>
27
 
#include <drizzled/internal/my_sys.h>
28
 
 
29
 
#include <math.h>
30
 
 
31
 
#include <algorithm>
32
 
 
33
 
using namespace std;
34
 
 
35
 
namespace drizzled
36
 
{
37
 
 
38
 
namespace field
39
 
{
 
22
#include <drizzled/server_includes.h>
 
23
#include <drizzled/field/int64_t.h>
40
24
 
41
25
/****************************************************************************
42
 
  Field type Int64 int (8 bytes)
43
 
 ****************************************************************************/
 
26
 Field type int64_t int (8 bytes)
 
27
****************************************************************************/
44
28
 
45
 
int Int64::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
29
int Field_int64_t::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
46
30
{
47
31
  int error= 0;
48
32
  char *end;
49
33
  uint64_t tmp;
50
34
 
51
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
52
 
 
53
35
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
54
36
  if (error == MY_ERRNO_ERANGE)
55
37
  {
56
38
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
57
39
    error= 1;
58
40
  }
59
 
  else if (getTable()->in_use->count_cuted_fields && check_int(cs, from, len, end, error))
60
 
  {
 
41
  else if (table->in_use->count_cuted_fields && 
 
42
           check_int(cs, from, len, end, error))
61
43
    error= 1;
62
 
  }
63
44
  else
64
 
  {
65
45
    error= 0;
 
46
#ifdef WORDS_BIGENDIAN
 
47
  if (table->s->db_low_byte_first)
 
48
  {
 
49
    int8store(ptr,tmp);
66
50
  }
67
 
 
68
 
  int64_tstore(ptr,tmp);
69
 
 
 
51
  else
 
52
#endif
 
53
    int64_tstore(ptr,tmp);
70
54
  return error;
71
55
}
72
56
 
73
57
 
74
 
int Int64::store(double nr)
 
58
int Field_int64_t::store(double nr)
75
59
{
76
60
  int error= 0;
77
61
  int64_t res;
78
62
 
79
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
80
 
 
81
63
  nr= rint(nr);
82
64
 
83
65
  if (nr <= (double) INT64_MIN)
91
73
    error= (nr > (double) INT64_MAX);
92
74
  }
93
75
  else
94
 
  {
95
76
    res=(int64_t) nr;
96
 
  }
97
77
 
98
78
  if (error)
99
79
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
100
80
 
101
 
  int64_tstore(ptr, res);
102
 
 
 
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);
103
89
  return error;
104
90
}
105
91
 
106
92
 
107
 
int Int64::store(int64_t nr, bool arg)
 
93
int Field_int64_t::store(int64_t nr, bool unsigned_val __attribute__((unused)))
108
94
{
109
95
  int error= 0;
110
96
 
111
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
112
 
  if (arg and (nr < 0)) // Only a partial fix for overflow
 
97
#ifdef WORDS_BIGENDIAN
 
98
  if (table->s->db_low_byte_first)
113
99
  {
114
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
115
 
    error= 1;
 
100
    int8store(ptr,nr);
116
101
  }
117
 
 
118
 
  int64_tstore(ptr,nr);
119
 
 
 
102
  else
 
103
#endif
 
104
    int64_tstore(ptr,nr);
120
105
  return error;
121
106
}
122
107
 
123
108
 
124
 
double Int64::val_real(void) const
 
109
double Field_int64_t::val_real(void)
125
110
{
126
111
  int64_t j;
127
 
 
128
 
  ASSERT_COLUMN_MARKED_FOR_READ;
129
 
 
130
 
  int64_tget(j,ptr);
 
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);
131
120
  /* The following is open coded to avoid a bug in gcc 3.3 */
132
 
 
133
121
  return (double) j;
134
122
}
135
123
 
136
124
 
137
 
int64_t Int64::val_int(void) const
 
125
int64_t Field_int64_t::val_int(void)
138
126
{
139
127
  int64_t j;
140
 
 
141
 
  ASSERT_COLUMN_MARKED_FOR_READ;
142
 
 
143
 
  int64_tget(j,ptr);
144
 
 
 
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);
145
134
  return j;
146
135
}
147
136
 
148
137
 
149
 
String *Int64::val_str(String *val_buffer, String *) const
 
138
String *Field_int64_t::val_str(String *val_buffer,
 
139
                                String *val_ptr __attribute__((unused)))
150
140
{
151
141
  const CHARSET_INFO * const cs= &my_charset_bin;
152
142
  uint32_t length;
153
 
  uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
 
143
  uint32_t mlength=cmax(field_length+1,22*cs->mbmaxlen);
154
144
  val_buffer->alloc(mlength);
155
145
  char *to=(char*) val_buffer->ptr();
156
146
  int64_t j;
157
 
 
158
 
  ASSERT_COLUMN_MARKED_FOR_READ;
159
 
 
160
 
  int64_tget(j,ptr);
161
 
 
162
 
  length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, 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);
163
155
  val_buffer->length(length);
164
156
 
165
157
  return val_buffer;
166
158
}
167
159
 
168
 
int Int64::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
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)
169
168
{
170
169
  int64_t a,b;
171
 
 
172
 
  int64_tget(a,a_ptr);
173
 
  int64_tget(b,b_ptr);
174
 
 
 
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
  }
175
182
  return (a < b) ? -1 : (a > b) ? 1 : 0;
176
183
}
177
184
 
178
 
void Int64::sort_string(unsigned char *to,uint32_t )
 
185
void Field_int64_t::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
179
186
{
180
187
#ifdef WORDS_BIGENDIAN
 
188
  if (!table->s->db_low_byte_first)
181
189
  {
182
190
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
183
191
    to[1]   = ptr[1];
188
196
    to[6]   = ptr[6];
189
197
    to[7]   = ptr[7];
190
198
  }
191
 
#else
 
199
  else
 
200
#endif
192
201
  {
193
202
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
194
203
    to[1]   = ptr[6];
199
208
    to[6]   = ptr[1];
200
209
    to[7]   = ptr[0];
201
210
  }
202
 
#endif
203
211
}
204
212
 
205
213
 
206
 
void Int64::sql_type(String &res) const
 
214
void Field_int64_t::sql_type(String &res) const
207
215
{
208
216
  const CHARSET_INFO * const cs=res.charset();
209
217
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
210
218
}
211
 
 
212
 
 
213
 
unsigned char *Int64::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
214
 
{
215
 
  int64_t val;
216
 
 
217
 
  int64_tget(val, from);
218
 
  int64_tstore(to, val);
219
 
 
220
 
  return to + sizeof(val);
221
 
}
222
 
 
223
 
 
224
 
const unsigned char *Int64::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
225
 
{
226
 
  int64_t val;
227
 
 
228
 
  int64_tget(val, from);
229
 
  int64_tstore(to, val);
230
 
 
231
 
  return from + sizeof(val);
232
 
}
233
 
 
234
 
} /* namespace field */
235
 
} /* namespace drizzled */