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.

472 lines
12 KiB

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
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
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
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. /*
  2. Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. */
  14. /**
  15. @file
  16. It is interface module to fixed precision decimals library.
  17. Most functions use 'uint mask' as parameter, if during operation error
  18. which fit in this mask is detected then it will be processed automatically
  19. here. (errors are E_DEC_* constants, see include/decimal.h)
  20. Most function are just inline wrappers around library calls
  21. */
  22. #ifndef my_decimal_h
  23. #define my_decimal_h
  24. C_MODE_START
  25. #include <decimal.h>
  26. C_MODE_END
  27. #define DECIMAL_LONGLONG_DIGITS 22
  28. #define DECIMAL_LONG_DIGITS 10
  29. #define DECIMAL_LONG3_DIGITS 8
  30. /** maximum length of buffer in our big digits (uint32). */
  31. #define DECIMAL_BUFF_LENGTH 9
  32. /* the number of digits that my_decimal can possibly contain */
  33. #define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
  34. /**
  35. maximum guaranteed precision of number in decimal digits (number of our
  36. digits * number of decimal digits in one our big digit - number of decimal
  37. digits in one our big digit decreased by 1 (because we always put decimal
  38. point on the border of our big digits))
  39. */
  40. #define DECIMAL_MAX_PRECISION (DECIMAL_MAX_POSSIBLE_PRECISION - 8*2)
  41. #define DECIMAL_MAX_SCALE 30
  42. #define DECIMAL_NOT_SPECIFIED 31
  43. /**
  44. maximum length of string representation (number of maximum decimal
  45. digits + 1 position for sign + 1 position for decimal point, no terminator)
  46. */
  47. #define DECIMAL_MAX_STR_LENGTH (DECIMAL_MAX_POSSIBLE_PRECISION + 2)
  48. /**
  49. maximum size of packet length.
  50. */
  51. #define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
  52. inline uint my_decimal_size(uint precision, uint scale)
  53. {
  54. /*
  55. Always allocate more space to allow library to put decimal point
  56. where it want
  57. */
  58. return decimal_size(precision, scale) + 1;
  59. }
  60. inline int my_decimal_int_part(uint precision, uint decimals)
  61. {
  62. return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
  63. }
  64. /**
  65. my_decimal class limits 'decimal_t' type to what we need in MySQL.
  66. It contains internally all necessary space needed by the instance so
  67. no extra memory is needed. One should call fix_buffer_pointer() function
  68. when he moves my_decimal objects in memory.
  69. */
  70. class my_decimal :public decimal_t
  71. {
  72. /*
  73. Several of the routines in strings/decimal.c have had buffer
  74. overrun/underrun problems. These are *not* caught by valgrind.
  75. To catch them, we allocate dummy fields around the buffer,
  76. and test that their values do not change.
  77. */
  78. #if !defined(DBUG_OFF)
  79. int foo1;
  80. #endif
  81. decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
  82. #if !defined(DBUG_OFF)
  83. int foo2;
  84. static const int test_value= 123;
  85. #endif
  86. public:
  87. void init()
  88. {
  89. #if !defined(DBUG_OFF)
  90. foo1= test_value;
  91. foo2= test_value;
  92. #endif
  93. len= DECIMAL_BUFF_LENGTH;
  94. buf= buffer;
  95. }
  96. my_decimal()
  97. {
  98. init();
  99. }
  100. ~my_decimal()
  101. {
  102. sanity_check();
  103. }
  104. void sanity_check()
  105. {
  106. DBUG_ASSERT(foo1 == test_value);
  107. DBUG_ASSERT(foo2 == test_value);
  108. }
  109. void fix_buffer_pointer() { buf= buffer; }
  110. bool sign() const { return decimal_t::sign; }
  111. void sign(bool s) { decimal_t::sign= s; }
  112. uint precision() const { return intg + frac; }
  113. /** Swap two my_decimal values */
  114. void swap(my_decimal &rhs)
  115. {
  116. swap_variables(my_decimal, *this, rhs);
  117. /* Swap the buffer pointers back */
  118. swap_variables(decimal_digit_t *, buf, rhs.buf);
  119. }
  120. };
  121. #ifndef DBUG_OFF
  122. void print_decimal(const my_decimal *dec);
  123. void print_decimal_buff(const my_decimal *dec, const uchar* ptr, int length);
  124. const char *dbug_decimal_as_string(char *buff, const my_decimal *val);
  125. #else
  126. #define dbug_decimal_as_string(A) NULL
  127. #endif
  128. #ifndef MYSQL_CLIENT
  129. int decimal_operation_results(int result);
  130. #else
  131. inline int decimal_operation_results(int result)
  132. {
  133. return result;
  134. }
  135. #endif /*MYSQL_CLIENT*/
  136. inline
  137. void max_my_decimal(my_decimal *to, int precision, int frac)
  138. {
  139. DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION)&&
  140. (frac <= DECIMAL_MAX_SCALE));
  141. max_decimal(precision, frac, (decimal_t*) to);
  142. }
  143. inline void max_internal_decimal(my_decimal *to)
  144. {
  145. max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
  146. }
  147. inline int check_result(uint mask, int result)
  148. {
  149. if (result & mask)
  150. decimal_operation_results(result);
  151. return result;
  152. }
  153. inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
  154. {
  155. if (check_result(mask, result) & E_DEC_OVERFLOW)
  156. {
  157. bool sign= val->sign();
  158. val->fix_buffer_pointer();
  159. max_internal_decimal(val);
  160. val->sign(sign);
  161. }
  162. return result;
  163. }
  164. inline uint my_decimal_length_to_precision(uint length, uint scale,
  165. bool unsigned_flag)
  166. {
  167. /* Precision can't be negative thus ignore unsigned_flag when length is 0. */
  168. DBUG_ASSERT(length || !scale);
  169. return (uint) (length - (scale>0 ? 1:0) -
  170. (unsigned_flag || !length ? 0:1));
  171. }
  172. inline uint32 my_decimal_precision_to_length_no_truncation(uint precision,
  173. uint8 scale,
  174. bool unsigned_flag)
  175. {
  176. /*
  177. When precision is 0 it means that original length was also 0. Thus
  178. unsigned_flag is ignored in this case.
  179. */
  180. DBUG_ASSERT(precision || !scale);
  181. return (uint32)(precision + (scale > 0 ? 1 : 0) +
  182. (unsigned_flag || !precision ? 0 : 1));
  183. }
  184. inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale,
  185. bool unsigned_flag)
  186. {
  187. /*
  188. When precision is 0 it means that original length was also 0. Thus
  189. unsigned_flag is ignored in this case.
  190. */
  191. DBUG_ASSERT(precision || !scale);
  192. set_if_smaller(precision, DECIMAL_MAX_PRECISION);
  193. return my_decimal_precision_to_length_no_truncation(precision, scale,
  194. unsigned_flag);
  195. }
  196. inline
  197. int my_decimal_string_length(const my_decimal *d)
  198. {
  199. /* length of string representation including terminating '\0' */
  200. return decimal_string_size(d);
  201. }
  202. inline
  203. int my_decimal_max_length(const my_decimal *d)
  204. {
  205. /* -1 because we do not count \0 */
  206. return decimal_string_size(d) - 1;
  207. }
  208. inline
  209. int my_decimal_get_binary_size(uint precision, uint scale)
  210. {
  211. return decimal_bin_size((int)precision, (int)scale);
  212. }
  213. inline
  214. void my_decimal2decimal(const my_decimal *from, my_decimal *to)
  215. {
  216. *to= *from;
  217. to->fix_buffer_pointer();
  218. }
  219. int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
  220. int scale);
  221. inline
  222. int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
  223. int scale)
  224. {
  225. return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
  226. }
  227. inline
  228. int my_decimal_set_zero(my_decimal *d)
  229. {
  230. decimal_make_zero(((decimal_t*) d));
  231. return 0;
  232. }
  233. inline
  234. bool my_decimal_is_zero(const my_decimal *decimal_value)
  235. {
  236. return decimal_is_zero((decimal_t*) decimal_value);
  237. }
  238. inline
  239. int my_decimal_round(uint mask, const my_decimal *from, int scale,
  240. bool truncate, my_decimal *to)
  241. {
  242. return check_result(mask, decimal_round((decimal_t*) from, to, scale,
  243. (truncate ? TRUNCATE : HALF_UP)));
  244. }
  245. inline
  246. int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
  247. {
  248. return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
  249. }
  250. inline
  251. int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
  252. {
  253. return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
  254. }
  255. #ifndef MYSQL_CLIENT
  256. int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
  257. uint fixed_dec, char filler, String *str);
  258. #endif
  259. inline
  260. int my_decimal2int(uint mask, const my_decimal *d, my_bool unsigned_flag,
  261. longlong *l)
  262. {
  263. my_decimal rounded;
  264. /* decimal_round can return only E_DEC_TRUNCATED */
  265. decimal_round((decimal_t*)d, &rounded, 0, HALF_UP);
  266. return check_result(mask, (unsigned_flag ?
  267. decimal2ulonglong(&rounded, (ulonglong *)l) :
  268. decimal2longlong(&rounded, l)));
  269. }
  270. inline
  271. int my_decimal2double(uint, const my_decimal *d, double *result)
  272. {
  273. /* No need to call check_result as this will always succeed */
  274. return decimal2double((decimal_t*) d, result);
  275. }
  276. inline
  277. int str2my_decimal(uint mask, const char *str, my_decimal *d, char **end)
  278. {
  279. return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
  280. d);
  281. }
  282. int str2my_decimal(uint mask, const char *from, uint length,
  283. CHARSET_INFO *charset, my_decimal *decimal_value);
  284. #if defined(MYSQL_SERVER) || defined(EMBEDDED_LIBRARY)
  285. inline
  286. int string2my_decimal(uint mask, const String *str, my_decimal *d)
  287. {
  288. return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
  289. }
  290. my_decimal *date2my_decimal(MYSQL_TIME *ltime, my_decimal *dec);
  291. #endif /*defined(MYSQL_SERVER) || defined(EMBEDDED_LIBRARY) */
  292. inline
  293. int double2my_decimal(uint mask, double val, my_decimal *d)
  294. {
  295. return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
  296. }
  297. inline
  298. int int2my_decimal(uint mask, longlong i, my_bool unsigned_flag, my_decimal *d)
  299. {
  300. return check_result(mask, (unsigned_flag ?
  301. ulonglong2decimal((ulonglong)i, d) :
  302. longlong2decimal(i, d)));
  303. }
  304. inline
  305. void my_decimal_neg(decimal_t *arg)
  306. {
  307. if (decimal_is_zero(arg))
  308. {
  309. arg->sign= 0;
  310. return;
  311. }
  312. decimal_neg(arg);
  313. }
  314. inline
  315. int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
  316. const my_decimal *b)
  317. {
  318. return check_result_and_overflow(mask,
  319. decimal_add((decimal_t*)a,(decimal_t*)b,res),
  320. res);
  321. }
  322. inline
  323. int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
  324. const my_decimal *b)
  325. {
  326. return check_result_and_overflow(mask,
  327. decimal_sub((decimal_t*)a,(decimal_t*)b,res),
  328. res);
  329. }
  330. inline
  331. int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
  332. const my_decimal *b)
  333. {
  334. return check_result_and_overflow(mask,
  335. decimal_mul((decimal_t*)a,(decimal_t*)b,res),
  336. res);
  337. }
  338. inline
  339. int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
  340. const my_decimal *b, int div_scale_inc)
  341. {
  342. return check_result_and_overflow(mask,
  343. decimal_div((decimal_t*)a,(decimal_t*)b,res,
  344. div_scale_inc),
  345. res);
  346. }
  347. inline
  348. int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
  349. const my_decimal *b)
  350. {
  351. return check_result_and_overflow(mask,
  352. decimal_mod((decimal_t*)a,(decimal_t*)b,res),
  353. res);
  354. }
  355. /**
  356. @return
  357. -1 if a<b, 1 if a>b and 0 if a==b
  358. */
  359. inline
  360. int my_decimal_cmp(const my_decimal *a, const my_decimal *b)
  361. {
  362. return decimal_cmp((decimal_t*) a, (decimal_t*) b);
  363. }
  364. inline
  365. int my_decimal_intg(const my_decimal *a)
  366. {
  367. return decimal_intg((decimal_t*) a);
  368. }
  369. void my_decimal_trim(ulong *precision, uint *scale);
  370. #endif /*my_decimal_h*/