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 
00022 namespace drizzled {
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 inline int class_decimal_int_part(uint32_t precision, uint32_t decimals)
00149 {
00150   return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
00151 }
00152 
00153 int decimal_operation_results(int result);
00154 
00155 inline void max_Decimal(type::Decimal *to, int precision, int frac)
00156 {
00157   assert((precision <= DECIMAL_MAX_PRECISION)&&
00158               (frac <= DECIMAL_MAX_SCALE));
00159   max_decimal(precision, frac, (decimal_t*) to);
00160 }
00161 
00162 inline void max_internal_decimal(type::Decimal *to)
00163 {
00164   max_Decimal(to, DECIMAL_MAX_PRECISION, 0);
00165 }
00166 
00167 inline int check_result(uint32_t mask, int result)
00168 {
00169   if (result & mask)
00170     decimal_operation_results(result);
00171   return result;
00172 }
00173 
00174 namespace type {
00183 class Decimal : public decimal_t
00184 {
00185   decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
00186 
00187 public:
00188 
00189   void init()
00190   {
00191     len= DECIMAL_BUFF_LENGTH;
00192     buf= buffer;
00193 #if !defined (HAVE_VALGRIND)
00194     /* Set buffer to 'random' value to find wrong buffer usage */
00195     for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
00196       buffer[i]= i;
00197 #endif
00198   }
00199 
00200   Decimal()
00201   {
00202     init();
00203   }
00204 
00205   void fix_buffer_pointer() { buf= buffer; }
00206   bool sign() const { return decimal_t::sign; }
00207   void sign(bool s) { decimal_t::sign= s; }
00208   uint32_t precision() const { return intg + frac; }
00209 
00210   int val_int32(uint32_t mask, bool unsigned_flag, int64_t *l) const
00211   {
00212     type::Decimal rounded;
00213     /* decimal_round can return only E_DEC_TRUNCATED */
00214     decimal_round(static_cast<const decimal_t*>(this), &rounded, 0, HALF_UP);
00215     return check_result(mask, (unsigned_flag ?
00216                                decimal2uint64_t(&rounded, reinterpret_cast<uint64_t *>(l)) :
00217                                decimal2int64_t(&rounded, l)));
00218   }
00219 
00220   int string_length() const
00221   {
00222     return decimal_string_size(this);
00223   }
00224 
00225   int val_binary(uint32_t mask, unsigned char *bin, int prec, int scale) const;
00226 
00227   int store(uint32_t mask, const char *from, uint32_t length, const charset_info_st * charset);
00228 
00229   int store(uint32_t mask, char *str, char **end)
00230   {
00231     return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(this), end));
00232   }
00233 
00234   int store(uint32_t mask, const String *str)
00235   {
00236     return store(mask, str->ptr(), str->length(), str->charset());
00237   }
00238 
00239   int check_result_and_overflow(uint32_t mask, int result)
00240   {
00241     if (check_result(mask, result) & E_DEC_OVERFLOW)
00242     {
00243       bool _sign= sign();
00244       fix_buffer_pointer();
00245       max_internal_decimal(this);
00246       sign(_sign);
00247     }
00248     return result;
00249   }
00250 
00251   void convert(double &value) const;
00252 };
00253 
00254 } // type
00255 
00256 std::ostream& operator<<(std::ostream& output, const type::Decimal &dec);
00257 
00258 inline uint32_t class_decimal_length_to_precision(uint32_t length, uint32_t scale,
00259                                                   bool unsigned_flag)
00260 {
00261   return (uint32_t) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
00262 }
00263 
00264 inline uint32_t class_decimal_precision_to_length(uint32_t precision, uint8_t scale,
00265                                                   bool unsigned_flag)
00266 {
00267   set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
00268   return static_cast<uint32_t>(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
00269 }
00270 
00271 
00272 inline
00273 int class_decimal_max_length(const type::Decimal *d)
00274 {
00275   /* -1 because we do not count \0 */
00276   return decimal_string_size(d) - 1;
00277 }
00278 
00279 
00280 inline
00281 int class_decimal_get_binary_size(uint32_t precision, uint32_t scale)
00282 {
00283   return decimal_bin_size(static_cast<int>(precision), static_cast<int>(scale));
00284 }
00285 
00286 
00287 inline
00288 void class_decimal2decimal(const type::Decimal *from, type::Decimal *to)
00289 {
00290   *to= *from;
00291   to->fix_buffer_pointer();
00292 }
00293 
00294 
00295 inline
00296 int binary2_class_decimal(uint32_t mask, const unsigned char *bin, type::Decimal *d, int prec,
00297           int scale)
00298 {
00299   return check_result(mask, bin2decimal(bin, static_cast<decimal_t*>(d), prec, scale));
00300 }
00301 
00302 
00303 inline
00304 int class_decimal_round(uint32_t mask, const type::Decimal *from, int scale,
00305                      bool truncate, type::Decimal *to)
00306 {
00307   return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, scale,
00308                                           (truncate ? TRUNCATE : HALF_UP)));
00309 }
00310 
00311 
00312 inline
00313 int class_decimal_floor(uint32_t mask, const type::Decimal *from, type::Decimal *to)
00314 {
00315   return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, FLOOR));
00316 }
00317 
00318 
00319 inline
00320 int class_decimal_ceiling(uint32_t mask, const type::Decimal *from, type::Decimal *to)
00321 {
00322   return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, CEILING));
00323 }
00324 
00325 
00326 int class_decimal2string(const type::Decimal *d,
00327                          uint32_t fixed_dec, String *str);
00328 
00329 
00330 inline
00331 int class_decimal2double(uint32_t, const type::Decimal *d, double *result)
00332 {
00333   /* No need to call check_result as this will always succeed */
00334   return decimal2double(static_cast<const decimal_t*>(d), result);
00335 }
00336 
00337 
00338 type::Decimal *date2_class_decimal(type::Time *ltime, type::Decimal *dec);
00339 
00340 
00341 inline
00342 int double2_class_decimal(uint32_t mask, double val, type::Decimal *d)
00343 {
00344   return d->check_result_and_overflow(mask, double2decimal(val, static_cast<decimal_t*>(d)));
00345 }
00346 
00347 
00348 inline
00349 int int2_class_decimal(uint32_t mask, int64_t i, bool unsigned_flag, type::Decimal *d)
00350 {
00351   return check_result(mask, (unsigned_flag ?
00352            uint64_t2decimal(static_cast<uint64_t>(i), d) :
00353            int64_t2decimal(i, d)));
00354 }
00355 
00356 
00357 inline
00358 void class_decimal_neg(decimal_t *arg)
00359 {
00360   if (arg->isZero())
00361   {
00362     arg->sign= 0;
00363     return;
00364   }
00365   arg->negate();
00366 }
00367 
00368 
00369 inline
00370 int class_decimal_add(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00371        const type::Decimal *b)
00372 {
00373   return res->check_result_and_overflow(mask,
00374                                         decimal_add(static_cast<const decimal_t*>(a),
00375                                                     static_cast<const decimal_t*>(b), res));
00376 }
00377 
00378 
00379 inline
00380 int class_decimal_sub(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00381        const type::Decimal *b)
00382 {
00383   return res->check_result_and_overflow(mask,
00384                                         decimal_sub(static_cast<const decimal_t*>(a),
00385                                                     static_cast<const decimal_t*>(b), res));
00386 }
00387 
00388 
00389 inline
00390 int class_decimal_mul(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00391        const type::Decimal *b)
00392 {
00393   return res->check_result_and_overflow(mask,
00394                                         decimal_mul(static_cast<const decimal_t*>(a),
00395                                                     static_cast<const decimal_t*>(b),res));
00396 }
00397 
00398 
00399 inline
00400 int class_decimal_div(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00401        const type::Decimal *b, int div_scale_inc)
00402 {
00403   return res->check_result_and_overflow(mask,
00404                                         decimal_div(static_cast<const decimal_t*>(a),
00405                                                     static_cast<const decimal_t*>(b),res,
00406                                                     div_scale_inc));
00407 }
00408 
00409 
00410 inline
00411 int class_decimal_mod(uint32_t mask, type::Decimal *res, const type::Decimal *a,
00412        const type::Decimal *b)
00413 {
00414   return res->check_result_and_overflow(mask,
00415                                         decimal_mod(static_cast<const decimal_t*>(a),
00416                                                     static_cast<const decimal_t*>(b),res));
00417 }
00418 
00419 
00424 inline
00425 int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)
00426 {
00427   return decimal_cmp(static_cast<const decimal_t*>(a),
00428                      static_cast<const decimal_t*>(b));
00429 }
00430 
00431 
00432 inline
00433 int class_decimal_intg(const type::Decimal *a)
00434 {
00435   return decimal_intg(static_cast<const decimal_t*>(a));
00436 }
00437 
00438 
00439 void class_decimal_trim(uint32_t *precision, uint32_t *scale);
00440 
00441 inline type::Decimal &decimal_zero_const()
00442 {
00443   static type::Decimal _decimal_zero;
00444   return _decimal_zero;
00445 }
00446 
00447 double my_double_round(double value, int64_t dec, bool dec_unsigned,
00448                        bool truncate);
00449 
00450 
00451 #define decimal_zero decimal_zero_const()
00452 
00453 } /* namespace drizzled */
00454 
00455