~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/type/decimal.h

  • Committer: Brian Aker
  • Date: 2011-01-19 18:03:32 UTC
  • mfrom: (2088.8.12 timestamp)
  • mto: This revision was merged to the branch mainline in revision 2098.
  • Revision ID: brian@tangent.org-20110119180332-acfk5i8oofp63s40
Merge in time code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
{
24
24
 
25
25
typedef enum
26
 
{TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
27
 
  decimal_round_mode;
 
26
{
 
27
  TRUNCATE= 0,
 
28
  HALF_EVEN,
 
29
  HALF_UP,
 
30
  CEILING,
 
31
  FLOOR
 
32
} decimal_round_mode;
28
33
typedef int32_t decimal_digit_t;
29
34
 
30
35
typedef struct st_decimal_t {
179
184
 
180
185
namespace type {
181
186
/**
182
 
  type::Decimal class limits 'decimal_t' type to what we need in MySQL.
 
187
  type Decimal class limits 'decimal_t' type to what we need in MySQL.
183
188
 
184
189
  It contains internally all necessary space needed by the instance so
185
190
  no extra memory is needed. One should call fix_buffer_pointer() function
186
191
  when he moves type::Decimal objects in memory.
187
192
*/
188
193
 
189
 
class Decimal :public decimal_t
 
194
class Decimal : public decimal_t
 
195
{
 
196
  decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
 
197
 
 
198
public:
 
199
 
 
200
  void init()
190
201
  {
191
 
    decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
192
 
 
193
 
  public:
194
 
 
195
 
    void init()
196
 
    {
197
 
      len= DECIMAL_BUFF_LENGTH;
198
 
      buf= buffer;
 
202
    len= DECIMAL_BUFF_LENGTH;
 
203
    buf= buffer;
199
204
#if !defined (HAVE_VALGRIND)
200
 
      /* Set buffer to 'random' value to find wrong buffer usage */
201
 
      for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
202
 
        buffer[i]= i;
 
205
    /* Set buffer to 'random' value to find wrong buffer usage */
 
206
    for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
 
207
      buffer[i]= i;
203
208
#endif
204
 
    }
205
 
 
206
 
    Decimal()
207
 
    {
208
 
      init();
209
 
    }
210
 
    void fix_buffer_pointer() { buf= buffer; }
211
 
    bool sign() const { return decimal_t::sign; }
212
 
    void sign(bool s) { decimal_t::sign= s; }
213
 
    uint32_t precision() const { return intg + frac; }
214
 
 
215
 
    int val_int32(uint32_t mask, bool unsigned_flag, int64_t *l) const
216
 
    {
217
 
      type::Decimal rounded;
218
 
      /* decimal_round can return only E_DEC_TRUNCATED */
219
 
      decimal_round(static_cast<const decimal_t*>(this), &rounded, 0, HALF_UP);
220
 
      return check_result(mask, (unsigned_flag ?
221
 
                                 decimal2uint64_t(&rounded, reinterpret_cast<uint64_t *>(l)) :
222
 
                                 decimal2int64_t(&rounded, l)));
223
 
    }
224
 
 
225
 
    int string_length() const
226
 
    {
227
 
      return decimal_string_size(this);
228
 
    }
229
 
 
230
 
    int val_binary(uint32_t mask, unsigned char *bin, int prec, int scale) const;
231
 
 
232
 
 
233
 
    int set_zero()
234
 
    {
235
 
      decimal_make_zero(static_cast<decimal_t*> (this));
236
 
      return 0;
237
 
    }
238
 
 
239
 
 
240
 
    bool is_zero() const
241
 
    {
242
 
      return decimal_is_zero(static_cast<const decimal_t*>(this));
243
 
    }
244
 
 
245
 
 
246
 
    int store(uint32_t mask, const char *from, uint32_t length, const CHARSET_INFO * charset);
247
 
 
248
 
    int store(uint32_t mask, char *str, char **end)
249
 
    {
250
 
      return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(this), end));
251
 
    }
252
 
 
253
 
    int store(uint32_t mask, const String *str)
254
 
    {
255
 
      return store(mask, str->ptr(), str->length(), str->charset());
256
 
    }
257
 
 
258
 
    int check_result_and_overflow(uint32_t mask, int result)
259
 
    {
260
 
      if (check_result(mask, result) & E_DEC_OVERFLOW)
261
 
      {
262
 
        bool _sign= sign();
263
 
        fix_buffer_pointer();
264
 
        max_internal_decimal(this);
265
 
        sign(_sign);
266
 
      }
267
 
      return result;
268
 
}
269
 
 
270
 
 
271
 
  };
 
209
  }
 
210
 
 
211
  Decimal()
 
212
  {
 
213
    init();
 
214
  }
 
215
  void fix_buffer_pointer() { buf= buffer; }
 
216
  bool sign() const { return decimal_t::sign; }
 
217
  void sign(bool s) { decimal_t::sign= s; }
 
218
  uint32_t precision() const { return intg + frac; }
 
219
 
 
220
  int val_int32(uint32_t mask, bool unsigned_flag, int64_t *l) const
 
221
  {
 
222
    type::Decimal rounded;
 
223
    /* decimal_round can return only E_DEC_TRUNCATED */
 
224
    decimal_round(static_cast<const decimal_t*>(this), &rounded, 0, HALF_UP);
 
225
    return check_result(mask, (unsigned_flag ?
 
226
                               decimal2uint64_t(&rounded, reinterpret_cast<uint64_t *>(l)) :
 
227
                               decimal2int64_t(&rounded, l)));
 
228
  }
 
229
 
 
230
  int string_length() const
 
231
  {
 
232
    return decimal_string_size(this);
 
233
  }
 
234
 
 
235
  int val_binary(uint32_t mask, unsigned char *bin, int prec, int scale) const;
 
236
 
 
237
 
 
238
  int set_zero()
 
239
  {
 
240
    decimal_make_zero(static_cast<decimal_t*> (this));
 
241
    return 0;
 
242
  }
 
243
 
 
244
 
 
245
  bool is_zero() const
 
246
  {
 
247
    return decimal_is_zero(static_cast<const decimal_t*>(this));
 
248
  }
 
249
 
 
250
 
 
251
  int store(uint32_t mask, const char *from, uint32_t length, const CHARSET_INFO * charset);
 
252
 
 
253
  int store(uint32_t mask, char *str, char **end)
 
254
  {
 
255
    return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(this), end));
 
256
  }
 
257
 
 
258
  int store(uint32_t mask, const String *str)
 
259
  {
 
260
    return store(mask, str->ptr(), str->length(), str->charset());
 
261
  }
 
262
 
 
263
  int check_result_and_overflow(uint32_t mask, int result)
 
264
  {
 
265
    if (check_result(mask, result) & E_DEC_OVERFLOW)
 
266
    {
 
267
      bool _sign= sign();
 
268
      fix_buffer_pointer();
 
269
      max_internal_decimal(this);
 
270
      sign(_sign);
 
271
    }
 
272
    return result;
 
273
  }
 
274
 
 
275
  void convert(double &value) const;
 
276
};
272
277
 
273
278
} // type
274
279