~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/field/long.cc

  • Committer: Monty Taylor
  • Date: 2008-07-26 16:22:28 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080726162228-atatk41l6w4np70m
Added gettext calls in to my_getopt.c and drizzle.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008 MySQL
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
 
22
 
#include "config.h"
23
 
#include <drizzled/field/int32.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/table.h>
26
 
#include <drizzled/session.h>
27
 
 
28
 
#include <math.h>
29
 
 
30
 
#include <algorithm>
31
 
 
32
 
using namespace std;
33
 
 
34
 
namespace drizzled
35
 
{
36
 
 
37
 
namespace field
38
 
{
 
21
#ifdef USE_PRAGMA_IMPLEMENTATION
 
22
#pragma implementation                          // gcc: Class implementation
 
23
#endif
 
24
 
 
25
#include "drizzle/server/field/long.h"
39
26
 
40
27
/****************************************************************************
41
 
 ** Int32
42
 
 ****************************************************************************/
 
28
** long int
 
29
****************************************************************************/
43
30
 
44
 
  int Int32::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
31
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
 
32
{
 
33
  long store_tmp;
 
34
  int error;
 
35
  int64_t rnd;
 
36
  
 
37
  error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
 
38
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
 
39
#ifdef WORDS_BIGENDIAN
 
40
  if (table->s->db_low_byte_first)
45
41
  {
46
 
    long store_tmp;
47
 
    int error;
48
 
    int64_t rnd;
49
 
 
50
 
    ASSERT_COLUMN_MARKED_FOR_WRITE;
51
 
 
52
 
    error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
53
 
    store_tmp= (long) rnd;
 
42
    int4store(ptr, store_tmp);
 
43
  }
 
44
  else
 
45
#endif
54
46
    longstore(ptr, store_tmp);
55
 
 
56
 
    return error;
 
47
  return error;
 
48
}
 
49
 
 
50
 
 
51
int Field_long::store(double nr)
 
52
{
 
53
  int error= 0;
 
54
  int32_t res;
 
55
  nr=rint(nr);
 
56
  if (unsigned_flag)
 
57
  {
 
58
    if (nr < 0)
 
59
    {
 
60
      res=0;
 
61
      error= 1;
 
62
    }
 
63
    else if (nr > (double) UINT32_MAX)
 
64
    {
 
65
      res= INT32_MAX;
 
66
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
67
      error= 1;
 
68
    }
 
69
    else
 
70
      res=(int32_t) (ulong) nr;
57
71
  }
58
 
 
59
 
 
60
 
  int Int32::store(double nr)
 
72
  else
61
73
  {
62
 
    int error= 0;
63
 
    int32_t res;
64
 
    nr=rint(nr);
65
 
 
66
 
    ASSERT_COLUMN_MARKED_FOR_WRITE;
67
 
 
68
74
    if (nr < (double) INT32_MIN)
69
75
    {
70
76
      res=(int32_t) INT32_MIN;
77
83
    }
78
84
    else
79
85
      res=(int32_t) (int64_t) nr;
80
 
 
81
 
    if (error)
82
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
83
 
 
 
86
  }
 
87
  if (error)
 
88
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
89
 
 
90
#ifdef WORDS_BIGENDIAN
 
91
  if (table->s->db_low_byte_first)
 
92
  {
 
93
    int4store(ptr,res);
 
94
  }
 
95
  else
 
96
#endif
84
97
    longstore(ptr,res);
85
 
 
86
 
    return error;
 
98
  return error;
 
99
}
 
100
 
 
101
 
 
102
int Field_long::store(int64_t nr, bool unsigned_val)
 
103
{
 
104
  int error= 0;
 
105
  int32_t res;
 
106
 
 
107
  if (unsigned_flag)
 
108
  {
 
109
    if (nr < 0 && !unsigned_val)
 
110
    {
 
111
      res=0;
 
112
      error= 1;
 
113
    }
 
114
    else if ((uint64_t) nr >= (1LL << 32))
 
115
    {
 
116
      res=(int32_t) (uint32_t) ~0L;
 
117
      error= 1;
 
118
    }
 
119
    else
 
120
      res=(int32_t) (uint32_t) nr;
87
121
  }
88
 
 
89
 
 
90
 
  int Int32::store(int64_t nr, bool unsigned_val)
 
122
  else
91
123
  {
92
 
    int error= 0;
93
 
    int32_t res;
94
 
 
95
 
    ASSERT_COLUMN_MARKED_FOR_WRITE;
96
 
 
97
124
    if (nr < 0 && unsigned_val)
98
125
      nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
99
 
 
100
 
    if (nr < (int64_t) INT32_MIN)
 
126
    if (nr < (int64_t) INT32_MIN) 
101
127
    {
102
128
      res=(int32_t) INT32_MIN;
103
129
      error= 1;
108
134
      error= 1;
109
135
    }
110
136
    else
111
 
    {
112
137
      res=(int32_t) nr;
113
 
    }
114
 
 
115
 
    if (error)
116
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
117
 
 
 
138
  }
 
139
  if (error)
 
140
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
141
 
 
142
#ifdef WORDS_BIGENDIAN
 
143
  if (table->s->db_low_byte_first)
 
144
  {
 
145
    int4store(ptr,res);
 
146
  }
 
147
  else
 
148
#endif
118
149
    longstore(ptr,res);
119
 
 
120
 
    return error;
121
 
  }
122
 
 
123
 
 
124
 
  double Int32::val_real(void)
125
 
  {
126
 
    int32_t j;
127
 
 
128
 
    ASSERT_COLUMN_MARKED_FOR_READ;
129
 
 
130
 
    longget(j,ptr);
131
 
 
132
 
    return (double) j;
133
 
  }
134
 
 
135
 
  int64_t Int32::val_int(void)
136
 
  {
137
 
    int32_t j;
138
 
 
139
 
    ASSERT_COLUMN_MARKED_FOR_READ;
140
 
 
141
 
    longget(j,ptr);
142
 
 
143
 
    return (int64_t) j;
144
 
  }
145
 
 
146
 
  String *Int32::val_str(String *val_buffer, String *)
147
 
  {
148
 
    const CHARSET_INFO * const cs= &my_charset_bin;
149
 
    uint32_t length;
150
 
    uint32_t mlength= max(field_length+1,12*cs->mbmaxlen);
151
 
    val_buffer->alloc(mlength);
152
 
    char *to=(char*) val_buffer->ptr();
153
 
    int32_t j;
154
 
 
155
 
    ASSERT_COLUMN_MARKED_FOR_READ;
156
 
 
157
 
    longget(j,ptr);
158
 
 
 
150
  return error;
 
151
}
 
152
 
 
153
 
 
154
double Field_long::val_real(void)
 
155
{
 
156
  int32_t j;
 
157
#ifdef WORDS_BIGENDIAN
 
158
  if (table->s->db_low_byte_first)
 
159
    j=sint4korr(ptr);
 
160
  else
 
161
#endif
 
162
    longget(j,ptr);
 
163
  return unsigned_flag ? (double) (uint32_t) j : (double) j;
 
164
}
 
165
 
 
166
int64_t Field_long::val_int(void)
 
167
{
 
168
  int32_t j;
 
169
  /* See the comment in Field_long::store(long long) */
 
170
  assert(table->in_use == current_thd);
 
171
#ifdef WORDS_BIGENDIAN
 
172
  if (table->s->db_low_byte_first)
 
173
    j=sint4korr(ptr);
 
174
  else
 
175
#endif
 
176
    longget(j,ptr);
 
177
  return unsigned_flag ? (int64_t) (uint32_t) j : (int64_t) j;
 
178
}
 
179
 
 
180
String *Field_long::val_str(String *val_buffer,
 
181
                            String *val_ptr __attribute__((unused)))
 
182
{
 
183
  CHARSET_INFO *cs= &my_charset_bin;
 
184
  uint length;
 
185
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
 
186
  val_buffer->alloc(mlength);
 
187
  char *to=(char*) val_buffer->ptr();
 
188
  int32_t j;
 
189
#ifdef WORDS_BIGENDIAN
 
190
  if (table->s->db_low_byte_first)
 
191
    j=sint4korr(ptr);
 
192
  else
 
193
#endif
 
194
    longget(j,ptr);
 
195
 
 
196
  if (unsigned_flag)
 
197
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32_t)j);
 
198
  else
159
199
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
160
 
    val_buffer->length(length);
161
 
 
162
 
    return val_buffer;
 
200
  val_buffer->length(length);
 
201
  if (zerofill)
 
202
    prepend_zeros(val_buffer);
 
203
  return val_buffer;
 
204
}
 
205
 
 
206
 
 
207
bool Field_long::send_binary(Protocol *protocol)
 
208
{
 
209
  return protocol->store_long(Field_long::val_int());
 
210
}
 
211
 
 
212
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
213
{
 
214
  int32_t a,b;
 
215
#ifdef WORDS_BIGENDIAN
 
216
  if (table->s->db_low_byte_first)
 
217
  {
 
218
    a=sint4korr(a_ptr);
 
219
    b=sint4korr(b_ptr);
163
220
  }
164
 
 
165
 
  int Int32::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
221
  else
 
222
#endif
166
223
  {
167
 
    int32_t a,b;
168
 
 
169
224
    longget(a,a_ptr);
170
225
    longget(b,b_ptr);
171
 
 
172
 
    return (a < b) ? -1 : (a > b) ? 1 : 0;
173
226
  }
 
227
  if (unsigned_flag)
 
228
    return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
 
229
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
230
}
174
231
 
175
 
  void Int32::sort_string(unsigned char *to,uint32_t )
 
232
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
 
233
{
 
234
#ifdef WORDS_BIGENDIAN
 
235
  if (!table->s->db_low_byte_first)
176
236
  {
177
 
#ifdef WORDS_BIGENDIAN
178
 
    {
 
237
    if (unsigned_flag)
 
238
      to[0] = ptr[0];
 
239
    else
179
240
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
180
 
      to[1]   = ptr[1];
181
 
      to[2]   = ptr[2];
182
 
      to[3]   = ptr[3];
183
 
    }
184
 
#else
185
 
    {
 
241
    to[1]   = ptr[1];
 
242
    to[2]   = ptr[2];
 
243
    to[3]   = ptr[3];
 
244
  }
 
245
  else
 
246
#endif
 
247
  {
 
248
    if (unsigned_flag)
 
249
      to[0] = ptr[3];
 
250
    else
186
251
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
187
 
      to[1]   = ptr[2];
188
 
      to[2]   = ptr[1];
189
 
      to[3]   = ptr[0];
190
 
    }
191
 
#endif
192
 
  }
193
 
 
194
 
 
195
 
  void Int32::sql_type(String &res) const
196
 
  {
197
 
    const CHARSET_INFO * const cs=res.charset();
198
 
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
199
 
  }
200
 
 
201
 
  unsigned char *Int32::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
202
 
  {
203
 
    int32_t val;
204
 
    longget(val, from);
205
 
 
206
 
    longstore(to, val);
207
 
    return to + sizeof(val);
208
 
  }
209
 
 
210
 
 
211
 
  const unsigned char *Int32::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
212
 
  {
213
 
    int32_t val;
214
 
    longget(val, from);
215
 
 
216
 
    longstore(to, val);
217
 
 
218
 
    return from + sizeof(val);
219
 
  }
220
 
 
221
 
} /* namespace field */
222
 
} /* namespace drizzled */
 
252
    to[1]   = ptr[2];
 
253
    to[2]   = ptr[1];
 
254
    to[3]   = ptr[0];
 
255
  }
 
256
}
 
257
 
 
258
 
 
259
void Field_long::sql_type(String &res) const
 
260
{
 
261
  CHARSET_INFO *cs=res.charset();
 
262
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
263
                          "int(%d)",(int) field_length));
 
264
  add_zerofill_and_unsigned(res);
 
265
}
 
266