Drizzled Public API Documentation

decimal.h

00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #pragma once
00017 #include <assert.h>
00018 #include <drizzled/sql_string.h>
00019 #include <drizzled/definitions.h>
00020 #include <drizzled/type/time.h>
00021 namespace drizzled
00022 {
00023 
00024 typedef enum
00025 {
00026   TRUNCATE= 0,
00027   HALF_EVEN,
00028   HALF_UP,
00029   CEILING,
00030   FLOOR
00031 } decimal_round_mode;
00032 typedef int32_t decimal_digit_t;
00033 
00034 struct decimal_t {
00035   int    intg, frac, len;
00036   bool sign;
00037   decimal_digit_t *buf;
00038 
00039   /* set a decimal_t to zero */
00040   inline void set_zero()
00041   {                 
00042     buf[0]= 0;
00043     intg= 1;
00044     frac= 0;
00045     sign= 0; 
00046   }
00047 
00048   /* negate a decimal */
00049   inline void negate()
00050   {
00051     sign^=1;
00052   }
00053 
00054   int isZero() const;
00055 
00056 };
00057 
00058 int internal_str2dec(char *from, decimal_t *to, char **end,
00059                      bool fixed);
00060 int decimal2string(const decimal_t *from, char *to, int *to_len,
00061                    int fixed_precision, int fixed_decimals,
00062                    char filler);
00063 int decimal2uint64_t(const decimal_t *from, uint64_t *to);
00064 int uint64_t2decimal(const uint64_t from, decimal_t *to);
00065 int decimal2int64_t(const decimal_t *from, int64_t *to);
00066 int int64_t2decimal(const int64_t from, decimal_t *to);
00067 int decimal2double(const decimal_t *from, double *to);
00068 int double2decimal(const double from, decimal_t *to);
00069 int decimal_actual_fraction(decimal_t *from);
00070 int decimal2bin(const decimal_t *from, unsigned char *to, int precision, int scale);
00071 int bin2decimal(const unsigned char *from, decimal_t *to, int precision, int scale);
00072 
00073 int decimal_bin_size(int precision, int scale);
00074 
00075 int decimal_intg(const decimal_t *from);
00076 int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
00077 int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
00078 int decimal_cmp(const decimal_t *from1, const decimal_t *from2);
00079 int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
00080 int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
00081                 int scale_incr);
00082 int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
00083 int decimal_round(const decimal_t *from, decimal_t *to, int new_scale,
00084                   decimal_round_mode mode);
00085 void max_decimal(int precision, int frac, decimal_t *to);
00086 
00087 inline int string2decimal(char *from, decimal_t *to, char **end)
00088 {
00089   return internal_str2dec(from, to, end, false);
00090 }
00091 
00092 /*
00093   returns the length of the buffer to hold string representation
00094   of the decimal (including decimal dot, possible sign and \0)
00095 */
00096 
00097 inline int decimal_string_size(const decimal_t *dec)
00098 {
00099   return (dec->intg ? dec->intg : 1) + dec->frac + (dec->frac > 0) + 2;
00100 }
00101 
00102 /*
00103   conventions:
00104 
00105     decimal_smth() == 0     -- everything's ok
00106     decimal_smth() <= 1     -- result is usable, but precision loss is possible
00107     decimal_smth() <= 2     -- result can be unusable, most significant digits
00108                                could've been lost
00109     decimal_smth() >  2     -- no result was generated
00110 */
00111 
00112 #define E_DEC_OK                0
00113 #define E_DEC_TRUNCATED         1
00114 #define E_DEC_OVERFLOW          2
00115 #define E_DEC_DIV_ZERO          4
00116 #define E_DEC_BAD_NUM           8
00117 #define E_DEC_OOM              16
00118 
00119 #define E_DEC_ERROR            31
00120 #define E_DEC_FATAL_ERROR      30
00121 
00122 
00123 #define DECIMAL_LONGLONG_DIGITS 22
00124 
00126 #define DECIMAL_BUFF_LENGTH 9
00127 
00128 /* the number of digits that type::Decimal can possibly contain */
00129 #define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
00130 
00131 
00138 #define DECIMAL_MAX_PRECISION (DECIMAL_MAX_POSSIBLE_PRECISION - 8*2)
00139 #define DECIMAL_MAX_SCALE 30
00140 #define DECIMAL_NOT_SPECIFIED 31
00141 
00146 #define DECIMAL_MAX_STR_LENGTH (DECIMAL_MAX_POSSIBLE_PRECISION + 2)
00147 
00148 namespace type {
00149 class Decimal;
00150 }
00151 
00152 inline int class_decimal_int_part(uint32_t precision, uint32_t decimals)
00153 {
00154   return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
00155 }
00156 
00157 int decimal_operation_results(int result);
00158 
00159 inline void max_Decimal(type::Decimal *to, int precision, int frac)
00160 {
00161   assert((precision <= DECIMAL_MAX_PRECISION)&&
00162               (frac <= DECIMAL_MAX_SCALE));
00163   max_decimal(precision, frac, (decimal_t*) to);
00164 }
00165 
00166 inline void max_internal_decimal(type::Decimal *to)
00167 {
00168   max_Decimal(to, DECIMAL_MAX_PRECISION, 0);
00169 }
00170 
00171 inline int check_result(uint32_t mask, int result)
00172 {
00173   if (result & mask)
00174     decimal_operation_results(result);
00175   return result;
00176 }
00177 
00178 namespace type {
00187 class Decimal : public decimal_t
00188 {
00189   decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
00190 
00191 public:
00192 
00193   void init()
00194   {
00195     len= DECIMAL_BUFF_LENGTH;
00196     buf= buffer;
00197 #if !defined (HAVE_VALGRIND)
00198     /* Set buffer to 'random' value to find wrong buffer usage */
00199     for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
00200       buffer[i]= i;
00201 #endif
00202   }
00203 
00204   Decimal()
00205   {
00206     init();
00207   }
00208 
00209   void fix_buffer_pointer() { buf= buffer; }
00210   bool sign() const { return decimal_t::sign; }
00211   void sign(bool s) { decimal_t::sign= s; }
00212   uint32_t precision() const { return intg + frac; }
00213 
00214   int val_int32(uint32_t mask, bool unsigned_flag, int64_t *l) const
00215   {
00216     type::Decimal rounded;
00217     /* decimal_round can return only E_DEC_TRUNCATED */
00218     decimal_round(static_cast<const decimal_t*>(this), &rounded, 0, HALF_UP);
00219     return check_result(mask, (unsigned_flag ?
00220                                decimal2uint64_t(&rounded, reinterpret_cast<uint64_t *>(l)) :
00221                                decimal2int64_t(&rounded, l)));
00222   }
00223 
00224   int string_length() const
00225   {
00226     return decimal_string_size(this);
00227   }
00228 
00229   int val_binary(uint32_t mask, unsigned char *bin, int prec, int scale) const;
00230 
00231   int store(uint32_t mask, const char *from, uint32_t length, const CHARSET_INFO * charset);
00232 
00233   int store(uint32_t mask, char *str, char **end)
00234   {
00235     return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(this), end));
00236   }
00237 
00238   int store(uint32_t mask, const String *str)
00239   {
00240     return store(mask, str->ptr(), str->length(), str->charset());
00241   }
00242 
00243   int check_result_and_overflow(uint32_t mask, int result)
00244   {
00245     if (check_result(mask, result) & E_DEC_OVERFLOW)
00246     {
00247       bool _sign= sign();
00248       fix_buffer_pointer();
00249       max_internal_decimal(this);
00250       sign(_sign);
00251     }
00252     return result;
00253   }
00254 
00255   void convert(double &value) const;
00256 };
00257 
00258 } // type
00259 
00260 std::ostream& operator<<(std::ostream& output, const type::Decimal &dec);
00261 
00262 inline uint32_t class_decimal_length_to_precision(uint32_t length, uint32_t scale,
00263                                                   bool unsigned_flag)
00264 {
00265   return (uint32_t) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
00266 }
00267 
00268 inline uint32_t class_decimal_precision_to_length(uint32_t precision, uint8_t scale,
00269                                                   bool unsigned_flag)
00270 {
00271   set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
00272   return static_cast<uint32_t>(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
00273 }
00274 
00275 
00276 inline
00277 int class_decimal_max_length(const type::Decimal *d)
00278 {
00279   /* -1 because we do not count \0 */
00280   return decimal_string_size(d) - 1;
00281 }
00282 
00283 
00284 inline
00285 int class_decimal_get_binary_size(uint32_t precision, uint32_t scale)
00286 {
00287   return decimal_bin_size(static_cast<int>(precision), static_cast<int>(scale));
00288 }
00289 
00290 
00291 inline
00292 void class_decimal2decimal(const type::Decimal *from, type::Decimal *to)
00293 {
00294   *to= *from;
00295   to->fix_buffer_pointer();
00296 }
00297 
00298 
00299 inline
00300 int binary2_class_decimal(uint32_t mask, const unsigned char *bin, type::Decimal *d, int prec,
00301           int scale)
00302 {
00303   return check_result(mask, bin2decimal(bin, static_cast<decimal_t*>(d), prec, scale));
00304 }
00305 
00306 
00307 inline
00308 int class_decimal_round(uint32_t mask, const type::Decimal *from, int scale,
00309                      bool truncate, type::Decimal *to)
00310 {
00311   return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, scale,
00312                                           (truncate ? TRUNCATE : HALF_UP)));
00313 }
00314 
00315 
00316 inline
00317 int class_decimal_floor(uint32_t mask, const type::Decimal *from, type::Decimal *to)
00318 {
00319   return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, FLOOR));
00320 }
00321 
00322 
00323 inline
00324 int class_decimal_ceiling(uint32_t mask, const type::Decimal *from, type::Decimal *to)
00325 {
00326   return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, CEILING));
00327 }
00328 
00329 
00330 int class_decimal2string(const type::Decimal *d,
00331                          uint32_t fixed_dec, String *str);
00332 
00333 
00334 inline
00335 int class_decimal2double(uint32_t, const type::Decimal *d, double *result)
00336 {
00337   /* No need to call check_result as this will always succeed */
00338   return decimal2double(static_cast<const decimal_t*>(d), result);
00339 }
00340 
00341 
00342 type::Decimal *date2_class_decimal(type::Time *ltime, type::Decimal *dec);
00343 
00344 
00345 inline
00346 int double2_class_decimal(uint32_t mask, double val, type::Decimal *d)
00347 {
00348   return d->check_result_and_overflow(mask, double2decimal(val, static_cast<decimal_t*>(d)));
00349 }
00350 
00351 
00352 inline
00353 int int2_class_decimal(uint32_t mask, int64_t i, bool unsigned_flag, type::Decimal *d)
00354 {
00355   return check_result(mask, (unsigned_flag ?
00356            uint64_t2decimal(static_cast<uint64_t>(i), d) :
00357            int64_t2decimal(i, d)));
00358 }
00359 
00360 
00361 inline
00362 void class_decimal_neg(decimal_t *arg)
00363 {
00364   if (arg->isZero())
00365   {
00366     arg->sign= 0;
00367     return;
00368   }
00369   arg->negate();
00370 }
00371 
00372 
00373 inline
00374 int class_decimal_add(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00375        const type::Decimal *b)
00376 {
00377   return res->check_result_and_overflow(mask,
00378                                         decimal_add(static_cast<const decimal_t*>(a),
00379                                                     static_cast<const decimal_t*>(b), res));
00380 }
00381 
00382 
00383 inline
00384 int class_decimal_sub(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00385        const type::Decimal *b)
00386 {
00387   return res->check_result_and_overflow(mask,
00388                                         decimal_sub(static_cast<const decimal_t*>(a),
00389                                                     static_cast<const decimal_t*>(b), res));
00390 }
00391 
00392 
00393 inline
00394 int class_decimal_mul(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00395        const type::Decimal *b)
00396 {
00397   return res->check_result_and_overflow(mask,
00398                                         decimal_mul(static_cast<const decimal_t*>(a),
00399                                                     static_cast<const decimal_t*>(b),res));
00400 }
00401 
00402 
00403 inline
00404 int class_decimal_div(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00405        const type::Decimal *b, int div_scale_inc)
00406 {
00407   return res->check_result_and_overflow(mask,
00408                                         decimal_div(static_cast<const decimal_t*>(a),
00409                                                     static_cast<const decimal_t*>(b),res,
00410                                                     div_scale_inc));
00411 }
00412 
00413 
00414 inline
00415 int class_decimal_mod(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00416        const type::Decimal *b)
00417 {
00418   return res->check_result_and_overflow(mask,
00419                                         decimal_mod(static_cast<const decimal_t*>(a),
00420                                                     static_cast<const decimal_t*>(b),res));
00421 }
00422 
00423 
00428 inline
00429 int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)
00430 {
00431   return decimal_cmp(static_cast<const decimal_t*>(a),
00432                      static_cast<const decimal_t*>(b));
00433 }
00434 
00435 
00436 inline
00437 int class_decimal_intg(const type::Decimal *a)
00438 {
00439   return decimal_intg(static_cast<const decimal_t*>(a));
00440 }
00441 
00442 
00443 void class_decimal_trim(uint32_t *precision, uint32_t *scale);
00444 
00445 inline type::Decimal &decimal_zero_const()
00446 {
00447   static type::Decimal _decimal_zero;
00448   return _decimal_zero;
00449 }
00450 
00451 double my_double_round(double value, int64_t dec, bool dec_unsigned,
00452                        bool truncate);
00453 
00454 
00455 #define decimal_zero decimal_zero_const()
00456 
00457 } /* namespace drizzled */
00458 
00459