You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
6.1 KiB

  1. /* Copyright (C) 2004 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. #include "mysql_priv.h"
  14. #ifndef MYSQL_CLIENT
  15. /*
  16. report result of decimal operation
  17. SYNOPSIS
  18. decimal_operation_results()
  19. result decimal library return code (E_DEC_* see include/decimal.h)
  20. TODO
  21. Fix error messages
  22. RETURN
  23. result
  24. */
  25. int decimal_operation_results(int result)
  26. {
  27. switch (result) {
  28. case E_DEC_OK:
  29. break;
  30. case E_DEC_TRUNCATED:
  31. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  32. WARN_DATA_TRUNCATED, ER(WARN_DATA_TRUNCATED),
  33. "", (long)-1);
  34. break;
  35. case E_DEC_OVERFLOW:
  36. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
  37. ER_TRUNCATED_WRONG_VALUE,
  38. ER(ER_TRUNCATED_WRONG_VALUE),
  39. "DECIMAL", "");
  40. break;
  41. case E_DEC_DIV_ZERO:
  42. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
  43. ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
  44. break;
  45. case E_DEC_BAD_NUM:
  46. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
  47. ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
  48. ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
  49. "decimal", "", "", (long)-1);
  50. break;
  51. case E_DEC_OOM:
  52. my_error(ER_OUT_OF_RESOURCES, MYF(0));
  53. break;
  54. default:
  55. DBUG_ASSERT(0);
  56. }
  57. return result;
  58. }
  59. /*
  60. Converting decimal to string
  61. SYNOPSIS
  62. my_decimal2string()
  63. return
  64. E_DEC_OK
  65. E_DEC_TRUNCATED
  66. E_DEC_OVERFLOW
  67. E_DEC_OOM
  68. */
  69. int my_decimal2string(uint mask, const my_decimal *d,
  70. uint fixed_prec, uint fixed_dec,
  71. char filler, String *str)
  72. {
  73. int length= (fixed_prec ? (fixed_prec + 1) : my_decimal_string_length(d));
  74. int result;
  75. if (str->alloc(length))
  76. return check_result(mask, E_DEC_OOM);
  77. result= decimal2string((decimal_t*) d, (char*) str->ptr(),
  78. &length, (int)fixed_prec, fixed_dec,
  79. filler);
  80. str->length(length);
  81. return check_result(mask, result);
  82. }
  83. /*
  84. Convert from decimal to binary representation
  85. SYNOPSIS
  86. my_decimal2binary()
  87. mask error processing mask
  88. d number for conversion
  89. bin pointer to buffer where to write result
  90. prec overall number of decimal digits
  91. scale number of decimal digits after decimal point
  92. NOTE
  93. Before conversion we round number if it need but produce truncation
  94. error in this case
  95. RETURN
  96. E_DEC_OK
  97. E_DEC_TRUNCATED
  98. E_DEC_OVERFLOW
  99. */
  100. int my_decimal2binary(uint mask, const my_decimal *d, char *bin, int prec,
  101. int scale)
  102. {
  103. int err1= E_DEC_OK, err2;
  104. my_decimal rounded;
  105. my_decimal2decimal(d, &rounded);
  106. rounded.frac= decimal_actual_fraction(&rounded);
  107. if (scale < rounded.frac)
  108. {
  109. err1= E_DEC_TRUNCATED;
  110. /* decimal_round can return only E_DEC_TRUNCATED */
  111. decimal_round(&rounded, &rounded, scale, HALF_UP);
  112. }
  113. err2= decimal2bin(&rounded, bin, prec, scale);
  114. if (!err2)
  115. err2= err1;
  116. return check_result(mask, err2);
  117. }
  118. /*
  119. Convert string for decimal when string can be in some multibyte charset
  120. SYNOPSIS
  121. str2my_decimal()
  122. mask error processing mask
  123. from string to process
  124. length length of given string
  125. charset charset of given string
  126. decimal_value buffer for result storing
  127. RESULT
  128. E_DEC_OK
  129. E_DEC_TRUNCATED
  130. E_DEC_OVERFLOW
  131. E_DEC_BAD_NUM
  132. E_DEC_OOM
  133. */
  134. int str2my_decimal(uint mask, const char *from, uint length,
  135. CHARSET_INFO *charset, my_decimal *decimal_value)
  136. {
  137. char *end, *from_end;
  138. int err;
  139. char buff[STRING_BUFFER_USUAL_SIZE];
  140. String tmp(buff, sizeof(buff), &my_charset_bin);
  141. if (charset->mbminlen > 1)
  142. {
  143. uint dummy_errors;
  144. tmp.copy(from, length, charset, &my_charset_latin1, &dummy_errors);
  145. from= tmp.ptr();
  146. length= tmp.length();
  147. charset= &my_charset_bin;
  148. }
  149. from_end= end= (char*) from+length;
  150. err= string2decimal((char *)from, (decimal_t*) decimal_value, &end);
  151. if (end != from_end && !err)
  152. {
  153. /* Give warning if there is something other than end space */
  154. for ( ; end < from_end; end++)
  155. {
  156. if (!my_isspace(&my_charset_latin1, *end))
  157. {
  158. err= E_DEC_TRUNCATED;
  159. break;
  160. }
  161. }
  162. }
  163. check_result_and_overflow(mask, err, decimal_value);
  164. return err;
  165. }
  166. #ifndef DBUG_OFF
  167. /* routines for debugging print */
  168. #define DIG_PER_DEC1 9
  169. #define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
  170. /* print decimal */
  171. void
  172. print_decimal(const my_decimal *dec)
  173. {
  174. int i, end;
  175. char buff[512], *pos;
  176. pos= buff;
  177. pos+= my_sprintf(buff, (buff, "Decimal: sign: %d intg: %d frac: %d { ",
  178. dec->sign(), dec->intg, dec->frac));
  179. end= ROUND_UP(dec->frac)+ROUND_UP(dec->intg)-1;
  180. for (i=0; i < end; i++)
  181. pos+= my_sprintf(pos, (pos, "%09d, ", dec->buf[i]));
  182. pos+= my_sprintf(pos, (pos, "%09d }\n", dec->buf[i]));
  183. fputs(buff, DBUG_FILE);
  184. }
  185. /* print decimal with its binary representation */
  186. void
  187. print_decimal_buff(const my_decimal *dec, const byte* ptr, int length)
  188. {
  189. print_decimal(dec);
  190. fprintf(DBUG_FILE, "Record: ");
  191. for (int i= 0; i < length; i++)
  192. {
  193. fprintf(DBUG_FILE, "%02X ", (uint)((uchar *)ptr)[i]);
  194. }
  195. fprintf(DBUG_FILE, "\n");
  196. }
  197. const char *dbug_decimal_as_string(char *buff, const my_decimal *val)
  198. {
  199. int length= DECIMAL_MAX_STR_LENGTH;
  200. if (!val)
  201. return "NULL";
  202. (void)decimal2string((decimal_t*) val, buff, &length, 0,0,0);
  203. return buff;
  204. }
  205. #endif /*DBUG_OFF*/
  206. #endif /*MYSQL_CLIENT*/