~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 18:10:38 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705181038-0ih0nnamu5qrut0y
Fixed prototypes. Cleaned define a little bit.

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/long.h>
24
 
 
25
 
/****************************************************************************
26
 
** long int
27
 
****************************************************************************/
28
 
 
29
 
int Field_long::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
30
 
{
31
 
  long store_tmp;
32
 
  int error;
33
 
  int64_t rnd;
34
 
  
35
 
  error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
36
 
  store_tmp= (long) rnd;
37
 
#ifdef WORDS_BIGENDIAN
38
 
  if (table->s->db_low_byte_first)
39
 
  {
40
 
    int4store(ptr, store_tmp);
41
 
  }
42
 
  else
43
 
#endif
44
 
    longstore(ptr, store_tmp);
45
 
  return error;
46
 
}
47
 
 
48
 
 
49
 
int Field_long::store(double nr)
50
 
{
51
 
  int error= 0;
52
 
  int32_t res;
53
 
  nr=rint(nr);
54
 
 
55
 
  if (nr < (double) INT32_MIN)
56
 
  {
57
 
    res=(int32_t) INT32_MIN;
58
 
    error= 1;
59
 
  }
60
 
  else if (nr > (double) INT32_MAX)
61
 
  {
62
 
    res=(int32_t) INT32_MAX;
63
 
    error= 1;
64
 
  }
65
 
  else
66
 
    res=(int32_t) (int64_t) nr;
67
 
 
68
 
  if (error)
69
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
70
 
 
71
 
#ifdef WORDS_BIGENDIAN
72
 
  if (table->s->db_low_byte_first)
73
 
  {
74
 
    int4store(ptr,res);
75
 
  }
76
 
  else
77
 
#endif
78
 
    longstore(ptr,res);
79
 
  return error;
80
 
}
81
 
 
82
 
 
83
 
int Field_long::store(int64_t nr, bool unsigned_val)
84
 
{
85
 
  int error= 0;
86
 
  int32_t res;
87
 
 
88
 
  if (nr < 0 && unsigned_val)
89
 
    nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
90
 
  if (nr < (int64_t) INT32_MIN) 
91
 
  {
92
 
    res=(int32_t) INT32_MIN;
93
 
    error= 1;
94
 
  }
95
 
  else if (nr > (int64_t) INT32_MAX)
96
 
  {
97
 
    res=(int32_t) INT32_MAX;
98
 
    error= 1;
99
 
  }
100
 
  else
101
 
    res=(int32_t) nr;
102
 
 
103
 
  if (error)
104
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
105
 
 
106
 
#ifdef WORDS_BIGENDIAN
107
 
  if (table->s->db_low_byte_first)
108
 
  {
109
 
    int4store(ptr,res);
110
 
  }
111
 
  else
112
 
#endif
113
 
    longstore(ptr,res);
114
 
  return error;
115
 
}
116
 
 
117
 
 
118
 
double Field_long::val_real(void)
119
 
{
120
 
  int32_t j;
121
 
#ifdef WORDS_BIGENDIAN
122
 
  if (table->s->db_low_byte_first)
123
 
    j=sint4korr(ptr);
124
 
  else
125
 
#endif
126
 
    longget(j,ptr);
127
 
  return (double) j;
128
 
}
129
 
 
130
 
int64_t Field_long::val_int(void)
131
 
{
132
 
  int32_t j;
133
 
  /* See the comment in Field_long::store(int64_t) */
134
 
  assert(table->in_use == current_thd);
135
 
#ifdef WORDS_BIGENDIAN
136
 
  if (table->s->db_low_byte_first)
137
 
    j=sint4korr(ptr);
138
 
  else
139
 
#endif
140
 
    longget(j,ptr);
141
 
  return (int64_t) j;
142
 
}
143
 
 
144
 
String *Field_long::val_str(String *val_buffer,
145
 
                            String *val_ptr __attribute__((unused)))
146
 
{
147
 
  const CHARSET_INFO * const cs= &my_charset_bin;
148
 
  uint32_t length;
149
 
  uint32_t mlength=cmax(field_length+1,12*cs->mbmaxlen);
150
 
  val_buffer->alloc(mlength);
151
 
  char *to=(char*) val_buffer->ptr();
152
 
  int32_t j;
153
 
#ifdef WORDS_BIGENDIAN
154
 
  if (table->s->db_low_byte_first)
155
 
    j=sint4korr(ptr);
156
 
  else
157
 
#endif
158
 
    longget(j,ptr);
159
 
 
160
 
  length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
161
 
  val_buffer->length(length);
162
 
 
163
 
  return val_buffer;
164
 
}
165
 
 
166
 
 
167
 
bool Field_long::send_binary(Protocol *protocol)
168
 
{
169
 
  return protocol->store_long(Field_long::val_int());
170
 
}
171
 
 
172
 
int Field_long::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
173
 
{
174
 
  int32_t a,b;
175
 
#ifdef WORDS_BIGENDIAN
176
 
  if (table->s->db_low_byte_first)
177
 
  {
178
 
    a=sint4korr(a_ptr);
179
 
    b=sint4korr(b_ptr);
180
 
  }
181
 
  else
182
 
#endif
183
 
  {
184
 
    longget(a,a_ptr);
185
 
    longget(b,b_ptr);
186
 
  }
187
 
 
188
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
189
 
}
190
 
 
191
 
void Field_long::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
192
 
{
193
 
#ifdef WORDS_BIGENDIAN
194
 
  if (!table->s->db_low_byte_first)
195
 
  {
196
 
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
197
 
    to[1]   = ptr[1];
198
 
    to[2]   = ptr[2];
199
 
    to[3]   = ptr[3];
200
 
  }
201
 
  else
202
 
#endif
203
 
  {
204
 
    to[0] = (char) (ptr[3] ^ 128);              /* Revers signbit */
205
 
    to[1]   = ptr[2];
206
 
    to[2]   = ptr[1];
207
 
    to[3]   = ptr[0];
208
 
  }
209
 
}
210
 
 
211
 
 
212
 
void Field_long::sql_type(String &res) const
213
 
{
214
 
  const CHARSET_INFO * const cs=res.charset();
215
 
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
216
 
}
217