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.

344 lines
9.6 KiB

WL#2649 Number-to-string conversions added: include/ctype_numconv.inc mysql-test/include/ctype_numconv.inc mysql-test/r/ctype_binary.result mysql-test/t/ctype_binary.test Adding tests modified: mysql-test/r/bigint.result mysql-test/r/case.result mysql-test/r/create.result mysql-test/r/ctype_cp1251.result mysql-test/r/ctype_latin1.result mysql-test/r/ctype_ucs.result mysql-test/r/func_gconcat.result mysql-test/r/func_str.result mysql-test/r/metadata.result mysql-test/r/ps_1general.result mysql-test/r/ps_2myisam.result mysql-test/r/ps_3innodb.result mysql-test/r/ps_4heap.result mysql-test/r/ps_5merge.result mysql-test/r/show_check.result mysql-test/r/type_datetime.result mysql-test/r/type_ranges.result mysql-test/r/union.result mysql-test/suite/ndb/r/ps_7ndb.result mysql-test/t/ctype_cp1251.test mysql-test/t/ctype_latin1.test mysql-test/t/ctype_ucs.test mysql-test/t/func_str.test Fixing tests @ sql/field.cc - Return str result using my_charset_numeric. - Using real multi-byte aware str_to_XXX functions to handle tricky charset values propely (e.g. UCS2) @ sql/field.h - Changing derivation of non-string field types to DERIVATION_NUMERIC. - Changing binary() for numeric/datetime fields to always return TRUE even if charset is not my_charset_bin. We need this to keep ha_base_keytype() return HA_KEYTYPE_BINARY. - Adding BINARY_FLAG into some fields, because it's not being set automatically anymore with "my_charset_bin to my_charset_numeric" change. - Changing derivation for numeric/datetime datatypes to a weaker value, to make "SELECT concat('string', field)" use character set of the string literal for the result of the function. @ sql/item.cc - Implementing generic val_str_ascii(). - Using max_char_length() instead of direct read of max_length to make "tricky" charsets like UCS2 work. NOTE: in the future we'll possibly remove all direct reads of max_length - Fixing Item_num::safe_charset_converter(). Previously it alligned binary string to character string (for example by adding leading 0x00 when doing binary->UCS2 conversion). Now it just converts from my_charset_numbner to "tocs". - Using val_str_ascii() in Item::get_time() to make UCS2 arguments work. - Other misc changes @ sql/item.h - Changing MY_COLL_CMP_CONV and MY_COLL_ALLOW_CONV to bit operations instead of hard-coded bit masks. - Addding new method DTCollation.set_numeric(). - Adding new methods to Item. - Adding helper functions to make code look nicer: agg_item_charsets_for_string_result() agg_item_charsets_for_comparison() - Changing charset for Item_num-derived items from my_charset_bin to my_charset_numeric (which is an alias for latin1). @ sql/item_cmpfunc.cc - Using new helper functions - Other misc changes @ sql/item_cmpfunc.h - Fixing strcmp() to return max_length=2. Previously it returned 1, which was wrong, because it did not fit '-1'. @ sql/item_func.cc - Using new helper functions - Other minor changes @ sql/item_func.h - Removing unused functions - Adding helper functions agg_arg_charsets_for_string_result() agg_arg_charsets_for_comparison() - Adding set_numeric() into constructors of numeric items. - Using fix_length_and_charset() and fix_char_length() instead of direct write to max_length. @ sql/item_geofunc.cc - Changing class for Item_func_geometry_type and Item_func_as_wkt from Item_str_func to Item_str_ascii_func, to make them return UCS2 result properly (when character_set_connection=ucs2). @ sql/item_geofunc.h - Changing class for Item_func_geometry_type and Item_func_as_wkt from Item_str_func to Item_str_ascii_func, to make them return UCS2 result properly (when @@character_set_connection=ucs2). @ sql/item_strfunc.cc - Implementing Item_str_func::val_str(). - Renaming val_str to val_str_ascii for some items, to make them work with UCS2 properly. - Using new helper functions - All single-argument functions that expect string result now call this method: agg_arg_charsets_for_string_result(collation, args, 1); This enables character set conversion to @@character_set_connection in case of pure numeric input. @ sql/item_strfunc.h - Introducing Item_str_ascii_func - for functions which return pure ASCII data, for performance purposes, as well as for the cases when the old implementation of val_str() was heavily 8-bit oriented and implementing a UCS2-aware version is tricky. @ sql/item_sum.cc - Using new helper functions. @ sql/item_timefunc.cc - Using my_charset_numeric instead of my_charset_bin. - Using fix_char_length(), fix_length_and_charset() and fix_length_and_charset_datetime() instead of direct write to max_length. - Using tricky-charset aware function str_to_time_with_warn() @ sql/item_timefunc.h - Using new helper functions for charset and length initialization. - Changing base class for Item_func_get_format() to make it return UCS2 properly (when character_set_connection=ucs2). @ sql/item_xmlfunc.cc - Using new helper function @ sql/my_decimal.cc - Adding a new DECIMAL to CHAR converter with real multibyte support (e.g. UCS2) @ sql/mysql_priv.h - Introducing a new derivation level for numeric/datetime data types. - Adding macros for my_charset_numeric and MY_REPERTOIRE_NUMERIC. - Adding prototypes for str_set_decimal() - Adding prototypes for character-set aware str_to_xxx() functions. @ sql/protocol.cc - Changing charsetnr to "binary" client-side metadata for numeric/datetime data types. @ sql/time.cc - Adding to_ascii() helper function, to convert a string in any character set to ascii representation. In the future can be extended to understand digits written in various non-Latin word scripts. - Adding real multy-byte character set aware versions for str_to_XXXX, to make these these type of queries work correct: INSERT INTO t1 SET datetime_column=ucs2_expression; @ strings/ctype-ucs2.c - endptr was not calculated correctly. INSERTing of UCS2 values into numeric columns returned warnings about truncated wrong data.
16 years ago
WL#2649 Number-to-string conversions added: include/ctype_numconv.inc mysql-test/include/ctype_numconv.inc mysql-test/r/ctype_binary.result mysql-test/t/ctype_binary.test Adding tests modified: mysql-test/r/bigint.result mysql-test/r/case.result mysql-test/r/create.result mysql-test/r/ctype_cp1251.result mysql-test/r/ctype_latin1.result mysql-test/r/ctype_ucs.result mysql-test/r/func_gconcat.result mysql-test/r/func_str.result mysql-test/r/metadata.result mysql-test/r/ps_1general.result mysql-test/r/ps_2myisam.result mysql-test/r/ps_3innodb.result mysql-test/r/ps_4heap.result mysql-test/r/ps_5merge.result mysql-test/r/show_check.result mysql-test/r/type_datetime.result mysql-test/r/type_ranges.result mysql-test/r/union.result mysql-test/suite/ndb/r/ps_7ndb.result mysql-test/t/ctype_cp1251.test mysql-test/t/ctype_latin1.test mysql-test/t/ctype_ucs.test mysql-test/t/func_str.test Fixing tests @ sql/field.cc - Return str result using my_charset_numeric. - Using real multi-byte aware str_to_XXX functions to handle tricky charset values propely (e.g. UCS2) @ sql/field.h - Changing derivation of non-string field types to DERIVATION_NUMERIC. - Changing binary() for numeric/datetime fields to always return TRUE even if charset is not my_charset_bin. We need this to keep ha_base_keytype() return HA_KEYTYPE_BINARY. - Adding BINARY_FLAG into some fields, because it's not being set automatically anymore with "my_charset_bin to my_charset_numeric" change. - Changing derivation for numeric/datetime datatypes to a weaker value, to make "SELECT concat('string', field)" use character set of the string literal for the result of the function. @ sql/item.cc - Implementing generic val_str_ascii(). - Using max_char_length() instead of direct read of max_length to make "tricky" charsets like UCS2 work. NOTE: in the future we'll possibly remove all direct reads of max_length - Fixing Item_num::safe_charset_converter(). Previously it alligned binary string to character string (for example by adding leading 0x00 when doing binary->UCS2 conversion). Now it just converts from my_charset_numbner to "tocs". - Using val_str_ascii() in Item::get_time() to make UCS2 arguments work. - Other misc changes @ sql/item.h - Changing MY_COLL_CMP_CONV and MY_COLL_ALLOW_CONV to bit operations instead of hard-coded bit masks. - Addding new method DTCollation.set_numeric(). - Adding new methods to Item. - Adding helper functions to make code look nicer: agg_item_charsets_for_string_result() agg_item_charsets_for_comparison() - Changing charset for Item_num-derived items from my_charset_bin to my_charset_numeric (which is an alias for latin1). @ sql/item_cmpfunc.cc - Using new helper functions - Other misc changes @ sql/item_cmpfunc.h - Fixing strcmp() to return max_length=2. Previously it returned 1, which was wrong, because it did not fit '-1'. @ sql/item_func.cc - Using new helper functions - Other minor changes @ sql/item_func.h - Removing unused functions - Adding helper functions agg_arg_charsets_for_string_result() agg_arg_charsets_for_comparison() - Adding set_numeric() into constructors of numeric items. - Using fix_length_and_charset() and fix_char_length() instead of direct write to max_length. @ sql/item_geofunc.cc - Changing class for Item_func_geometry_type and Item_func_as_wkt from Item_str_func to Item_str_ascii_func, to make them return UCS2 result properly (when character_set_connection=ucs2). @ sql/item_geofunc.h - Changing class for Item_func_geometry_type and Item_func_as_wkt from Item_str_func to Item_str_ascii_func, to make them return UCS2 result properly (when @@character_set_connection=ucs2). @ sql/item_strfunc.cc - Implementing Item_str_func::val_str(). - Renaming val_str to val_str_ascii for some items, to make them work with UCS2 properly. - Using new helper functions - All single-argument functions that expect string result now call this method: agg_arg_charsets_for_string_result(collation, args, 1); This enables character set conversion to @@character_set_connection in case of pure numeric input. @ sql/item_strfunc.h - Introducing Item_str_ascii_func - for functions which return pure ASCII data, for performance purposes, as well as for the cases when the old implementation of val_str() was heavily 8-bit oriented and implementing a UCS2-aware version is tricky. @ sql/item_sum.cc - Using new helper functions. @ sql/item_timefunc.cc - Using my_charset_numeric instead of my_charset_bin. - Using fix_char_length(), fix_length_and_charset() and fix_length_and_charset_datetime() instead of direct write to max_length. - Using tricky-charset aware function str_to_time_with_warn() @ sql/item_timefunc.h - Using new helper functions for charset and length initialization. - Changing base class for Item_func_get_format() to make it return UCS2 properly (when character_set_connection=ucs2). @ sql/item_xmlfunc.cc - Using new helper function @ sql/my_decimal.cc - Adding a new DECIMAL to CHAR converter with real multibyte support (e.g. UCS2) @ sql/mysql_priv.h - Introducing a new derivation level for numeric/datetime data types. - Adding macros for my_charset_numeric and MY_REPERTOIRE_NUMERIC. - Adding prototypes for str_set_decimal() - Adding prototypes for character-set aware str_to_xxx() functions. @ sql/protocol.cc - Changing charsetnr to "binary" client-side metadata for numeric/datetime data types. @ sql/time.cc - Adding to_ascii() helper function, to convert a string in any character set to ascii representation. In the future can be extended to understand digits written in various non-Latin word scripts. - Adding real multy-byte character set aware versions for str_to_XXXX, to make these these type of queries work correct: INSERT INTO t1 SET datetime_column=ucs2_expression; @ strings/ctype-ucs2.c - endptr was not calculated correctly. INSERTing of UCS2 values into numeric columns returned warnings about truncated wrong data.
16 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
  1. /* Copyright (C) 2005-2006 MySQL 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; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #include "sql_priv.h"
  13. #include <time.h>
  14. #ifndef MYSQL_CLIENT
  15. #include "sql_class.h" // THD
  16. #endif
  17. #ifndef MYSQL_CLIENT
  18. /**
  19. report result of decimal operation.
  20. @param result decimal library return code (E_DEC_* see include/decimal.h)
  21. @todo
  22. Fix error messages
  23. @return
  24. result
  25. */
  26. int decimal_operation_results(int result)
  27. {
  28. switch (result) {
  29. case E_DEC_OK:
  30. break;
  31. case E_DEC_TRUNCATED:
  32. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  33. WARN_DATA_TRUNCATED, ER(WARN_DATA_TRUNCATED),
  34. "", (long)-1);
  35. break;
  36. case E_DEC_OVERFLOW:
  37. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  38. ER_TRUNCATED_WRONG_VALUE,
  39. ER(ER_TRUNCATED_WRONG_VALUE),
  40. "DECIMAL", "");
  41. break;
  42. case E_DEC_DIV_ZERO:
  43. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  44. ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
  45. break;
  46. case E_DEC_BAD_NUM:
  47. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  48. ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
  49. ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
  50. "decimal", "", "", (long)-1);
  51. break;
  52. case E_DEC_OOM:
  53. my_error(ER_OUT_OF_RESOURCES, MYF(0));
  54. break;
  55. default:
  56. DBUG_ASSERT(0);
  57. }
  58. return result;
  59. }
  60. /**
  61. @brief Converting decimal to string
  62. @details Convert given my_decimal to String; allocate buffer as needed.
  63. @param[in] mask what problems to warn on (mask of E_DEC_* values)
  64. @param[in] d the decimal to print
  65. @param[in] fixed_prec overall number of digits if ZEROFILL, 0 otherwise
  66. @param[in] fixed_dec number of decimal places (if fixed_prec != 0)
  67. @param[in] filler what char to pad with (ZEROFILL et al.)
  68. @param[out] *str where to store the resulting string
  69. @return error coce
  70. @retval E_DEC_OK
  71. @retval E_DEC_TRUNCATED
  72. @retval E_DEC_OVERFLOW
  73. @retval E_DEC_OOM
  74. */
  75. int my_decimal2string(uint mask, const my_decimal *d,
  76. uint fixed_prec, uint fixed_dec,
  77. char filler, String *str)
  78. {
  79. /*
  80. Calculate the size of the string: For DECIMAL(a,b), fixed_prec==a
  81. holds true iff the type is also ZEROFILL, which in turn implies
  82. UNSIGNED. Hence the buffer for a ZEROFILLed value is the length
  83. the user requested, plus one for a possible decimal point, plus
  84. one if the user only wanted decimal places, but we force a leading
  85. zero on them. Because the type is implicitly UNSIGNED, we do not
  86. need to reserve a character for the sign. For all other cases,
  87. fixed_prec will be 0, and my_decimal_string_length() will be called
  88. instead to calculate the required size of the buffer.
  89. */
  90. int length= (fixed_prec
  91. ? (fixed_prec + ((fixed_prec == fixed_dec) ? 1 : 0) + 1)
  92. : my_decimal_string_length(d));
  93. int result;
  94. if (str->alloc(length))
  95. return check_result(mask, E_DEC_OOM);
  96. result= decimal2string((decimal_t*) d, (char*) str->ptr(),
  97. &length, (int)fixed_prec, fixed_dec,
  98. filler);
  99. str->length(length);
  100. str->set_charset(&my_charset_numeric);
  101. return check_result(mask, result);
  102. }
  103. /**
  104. @brief Converting decimal to string with character set conversion
  105. @details Convert given my_decimal to String; allocate buffer as needed.
  106. @param[in] mask what problems to warn on (mask of E_DEC_* values)
  107. @param[in] val the decimal to print
  108. @param[in] fixed_prec overall number of digits if ZEROFILL, 0 otherwise
  109. @param[in] fixed_dec number of decimal places (if fixed_prec != 0)
  110. @param[in] filler what char to pad with (ZEROFILL et al.)
  111. @param[out] *str where to store the resulting string
  112. @param[in] cs character set
  113. @return error coce
  114. @retval E_DEC_OK
  115. @retval E_DEC_TRUNCATED
  116. @retval E_DEC_OVERFLOW
  117. @retval E_DEC_OOM
  118. Would be great to make it a method of the String class,
  119. but this would need to include
  120. my_decimal.h from sql_string.h and sql_string.cc, which is not desirable.
  121. */
  122. bool
  123. str_set_decimal(uint mask, const my_decimal *val,
  124. uint fixed_prec, uint fixed_dec, char filler,
  125. String *str, CHARSET_INFO *cs)
  126. {
  127. if (!(cs->state & MY_CS_NONASCII))
  128. {
  129. /* For ASCII-compatible character sets we can use my_decimal2string */
  130. my_decimal2string(mask, val, fixed_prec, fixed_dec, filler, str);
  131. str->set_charset(cs);
  132. return FALSE;
  133. }
  134. else
  135. {
  136. /*
  137. For ASCII-incompatible character sets (like UCS2) we
  138. call my_decimal2string() on a temporary buffer first,
  139. and then convert the result to the target character
  140. with help of str->copy().
  141. */
  142. uint errors;
  143. char buf[DECIMAL_MAX_STR_LENGTH];
  144. String tmp(buf, sizeof(buf), &my_charset_latin1);
  145. my_decimal2string(mask, val, fixed_prec, fixed_dec, filler, &tmp);
  146. return str->copy(tmp.ptr(), tmp.length(), &my_charset_latin1, cs, &errors);
  147. }
  148. }
  149. /*
  150. Convert from decimal to binary representation
  151. SYNOPSIS
  152. my_decimal2binary()
  153. mask error processing mask
  154. d number for conversion
  155. bin pointer to buffer where to write result
  156. prec overall number of decimal digits
  157. scale number of decimal digits after decimal point
  158. NOTE
  159. Before conversion we round number if it need but produce truncation
  160. error in this case
  161. RETURN
  162. E_DEC_OK
  163. E_DEC_TRUNCATED
  164. E_DEC_OVERFLOW
  165. */
  166. int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
  167. int scale)
  168. {
  169. int err1= E_DEC_OK, err2;
  170. my_decimal rounded;
  171. my_decimal2decimal(d, &rounded);
  172. rounded.frac= decimal_actual_fraction(&rounded);
  173. if (scale < rounded.frac)
  174. {
  175. err1= E_DEC_TRUNCATED;
  176. /* decimal_round can return only E_DEC_TRUNCATED */
  177. decimal_round(&rounded, &rounded, scale, HALF_UP);
  178. }
  179. err2= decimal2bin(&rounded, bin, prec, scale);
  180. if (!err2)
  181. err2= err1;
  182. return check_result(mask, err2);
  183. }
  184. /*
  185. Convert string for decimal when string can be in some multibyte charset
  186. SYNOPSIS
  187. str2my_decimal()
  188. mask error processing mask
  189. from string to process
  190. length length of given string
  191. charset charset of given string
  192. decimal_value buffer for result storing
  193. RESULT
  194. E_DEC_OK
  195. E_DEC_TRUNCATED
  196. E_DEC_OVERFLOW
  197. E_DEC_BAD_NUM
  198. E_DEC_OOM
  199. */
  200. int str2my_decimal(uint mask, const char *from, uint length,
  201. CHARSET_INFO *charset, my_decimal *decimal_value)
  202. {
  203. char *end, *from_end;
  204. int err;
  205. char buff[STRING_BUFFER_USUAL_SIZE];
  206. String tmp(buff, sizeof(buff), &my_charset_bin);
  207. if (charset->mbminlen > 1)
  208. {
  209. uint dummy_errors;
  210. tmp.copy(from, length, charset, &my_charset_latin1, &dummy_errors);
  211. from= tmp.ptr();
  212. length= tmp.length();
  213. charset= &my_charset_bin;
  214. }
  215. from_end= end= (char*) from+length;
  216. err= string2decimal((char *)from, (decimal_t*) decimal_value, &end);
  217. if (end != from_end && !err)
  218. {
  219. /* Give warning if there is something other than end space */
  220. for ( ; end < from_end; end++)
  221. {
  222. if (!my_isspace(&my_charset_latin1, *end))
  223. {
  224. err= E_DEC_TRUNCATED;
  225. break;
  226. }
  227. }
  228. }
  229. check_result_and_overflow(mask, err, decimal_value);
  230. return err;
  231. }
  232. my_decimal *date2my_decimal(MYSQL_TIME *ltime, my_decimal *dec)
  233. {
  234. longlong date;
  235. date = (ltime->year*100L + ltime->month)*100L + ltime->day;
  236. if (ltime->time_type > MYSQL_TIMESTAMP_DATE)
  237. date= ((date*100L + ltime->hour)*100L+ ltime->minute)*100L + ltime->second;
  238. if (int2my_decimal(E_DEC_FATAL_ERROR, ltime->neg ? -date : date, FALSE, dec))
  239. return dec;
  240. if (ltime->second_part)
  241. {
  242. dec->buf[(dec->intg-1) / 9 + 1]= ltime->second_part * 1000;
  243. dec->frac= 6;
  244. }
  245. return dec;
  246. }
  247. void my_decimal_trim(ulong *precision, uint *scale)
  248. {
  249. if (!(*precision) && !(*scale))
  250. {
  251. *precision= 10;
  252. *scale= 0;
  253. return;
  254. }
  255. }
  256. #ifndef DBUG_OFF
  257. /* routines for debugging print */
  258. #define DIG_PER_DEC1 9
  259. #define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
  260. /* print decimal */
  261. void
  262. print_decimal(const my_decimal *dec)
  263. {
  264. int i, end;
  265. char buff[512], *pos;
  266. pos= buff;
  267. pos+= sprintf(buff, "Decimal: sign: %d intg: %d frac: %d { ",
  268. dec->sign(), dec->intg, dec->frac);
  269. end= ROUND_UP(dec->frac)+ROUND_UP(dec->intg)-1;
  270. for (i=0; i < end; i++)
  271. pos+= sprintf(pos, "%09d, ", dec->buf[i]);
  272. pos+= sprintf(pos, "%09d }\n", dec->buf[i]);
  273. fputs(buff, DBUG_FILE);
  274. }
  275. /* print decimal with its binary representation */
  276. void
  277. print_decimal_buff(const my_decimal *dec, const uchar* ptr, int length)
  278. {
  279. print_decimal(dec);
  280. fprintf(DBUG_FILE, "Record: ");
  281. for (int i= 0; i < length; i++)
  282. {
  283. fprintf(DBUG_FILE, "%02X ", (uint)((uchar *)ptr)[i]);
  284. }
  285. fprintf(DBUG_FILE, "\n");
  286. }
  287. const char *dbug_decimal_as_string(char *buff, const my_decimal *val)
  288. {
  289. int length= DECIMAL_MAX_STR_LENGTH;
  290. if (!val)
  291. return "NULL";
  292. (void)decimal2string((decimal_t*) val, buff, &length, 0,0,0);
  293. return buff;
  294. }
  295. #endif /*DBUG_OFF*/
  296. #endif /*MYSQL_CLIENT*/