ma_dyncol.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (c) 2011, Monty Program Ab
  2. Copyright (c) 2011, Oleksandr Byelkin
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are
  5. met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must the following disclaimer in
  9. the documentation and/or other materials provided with the
  10. distribution.
  11. THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY
  12. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
  15. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  18. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  20. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  21. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. SUCH DAMAGE.
  23. */
  24. #ifndef ma_dyncol_h
  25. #define ma_dyncol_h
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <decimal.h>
  30. #include <my_decimal_limits.h>
  31. #include <mysql_time.h>
  32. #ifndef _my_sys_h
  33. typedef struct st_dynamic_string
  34. {
  35. char *str;
  36. size_t length,max_length,alloc_increment;
  37. } DYNAMIC_STRING;
  38. #endif
  39. #ifndef MY_GLOBAL_INCLUDED
  40. struct st_mysql_lex_string
  41. {
  42. char *str;
  43. size_t length;
  44. };
  45. typedef struct st_mysql_lex_string MYSQL_LEX_STRING;
  46. typedef struct st_mysql_lex_string LEX_STRING;
  47. #endif
  48. /*
  49. Limits of implementation
  50. */
  51. #define MAX_TOTAL_NAME_LENGTH 65535
  52. #define MAX_NAME_LENGTH (MAX_TOTAL_NAME_LENGTH/4)
  53. /* NO and OK is the same used just to show semantics */
  54. #define ER_DYNCOL_NO ER_DYNCOL_OK
  55. enum enum_dyncol_func_result
  56. {
  57. ER_DYNCOL_OK= 0,
  58. ER_DYNCOL_YES= 1, /* For functions returning 0/1 */
  59. ER_DYNCOL_FORMAT= -1, /* Wrong format of the encoded string */
  60. ER_DYNCOL_LIMIT= -2, /* Some limit reached */
  61. ER_DYNCOL_RESOURCE= -3, /* Out of resourses */
  62. ER_DYNCOL_DATA= -4, /* Incorrect input data */
  63. ER_DYNCOL_UNKNOWN_CHARSET= -5, /* Unknown character set */
  64. ER_DYNCOL_TRUNCATED= 2 /* OK, but data was truncated */
  65. };
  66. typedef DYNAMIC_STRING DYNAMIC_COLUMN;
  67. enum enum_dynamic_column_type
  68. {
  69. DYN_COL_NULL= 0,
  70. DYN_COL_INT,
  71. DYN_COL_UINT,
  72. DYN_COL_DOUBLE,
  73. DYN_COL_STRING,
  74. DYN_COL_DECIMAL,
  75. DYN_COL_DATETIME,
  76. DYN_COL_DATE,
  77. DYN_COL_TIME,
  78. DYN_COL_DYNCOL
  79. };
  80. typedef enum enum_dynamic_column_type DYNAMIC_COLUMN_TYPE;
  81. struct st_dynamic_column_value
  82. {
  83. DYNAMIC_COLUMN_TYPE type;
  84. union
  85. {
  86. long long long_value;
  87. unsigned long long ulong_value;
  88. double double_value;
  89. struct {
  90. MYSQL_LEX_STRING value;
  91. CHARSET_INFO *charset;
  92. } string;
  93. struct {
  94. decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
  95. decimal_t value;
  96. } decimal;
  97. MYSQL_TIME time_value;
  98. } x;
  99. };
  100. typedef struct st_dynamic_column_value DYNAMIC_COLUMN_VALUE;
  101. #ifdef MADYNCOL_DEPRECATED
  102. enum enum_dyncol_func_result
  103. dynamic_column_create(DYNAMIC_COLUMN *str,
  104. uint column_nr, DYNAMIC_COLUMN_VALUE *value);
  105. enum enum_dyncol_func_result
  106. dynamic_column_create_many(DYNAMIC_COLUMN *str,
  107. uint column_count,
  108. uint *column_numbers,
  109. DYNAMIC_COLUMN_VALUE *values);
  110. enum enum_dyncol_func_result
  111. dynamic_column_update(DYNAMIC_COLUMN *org, uint column_nr,
  112. DYNAMIC_COLUMN_VALUE *value);
  113. enum enum_dyncol_func_result
  114. dynamic_column_update_many(DYNAMIC_COLUMN *str,
  115. uint add_column_count,
  116. uint *column_numbers,
  117. DYNAMIC_COLUMN_VALUE *values);
  118. enum enum_dyncol_func_result
  119. dynamic_column_exists(DYNAMIC_COLUMN *org, uint column_nr);
  120. enum enum_dyncol_func_result
  121. dynamic_column_list(DYNAMIC_COLUMN *org, DYNAMIC_ARRAY *array_of_uint);
  122. enum enum_dyncol_func_result
  123. dynamic_column_get(DYNAMIC_COLUMN *org, uint column_nr,
  124. DYNAMIC_COLUMN_VALUE *store_it_here);
  125. #endif
  126. /* new functions */
  127. enum enum_dyncol_func_result
  128. mariadb_dyncol_create_many_num(DYNAMIC_COLUMN *str,
  129. uint column_count,
  130. uint *column_numbers,
  131. DYNAMIC_COLUMN_VALUE *values,
  132. my_bool new_string);
  133. enum enum_dyncol_func_result
  134. mariadb_dyncol_create_many_named(DYNAMIC_COLUMN *str,
  135. uint column_count,
  136. MYSQL_LEX_STRING *column_keys,
  137. DYNAMIC_COLUMN_VALUE *values,
  138. my_bool new_string);
  139. enum enum_dyncol_func_result
  140. mariadb_dyncol_update_many_num(DYNAMIC_COLUMN *str,
  141. uint add_column_count,
  142. uint *column_keys,
  143. DYNAMIC_COLUMN_VALUE *values);
  144. enum enum_dyncol_func_result
  145. mariadb_dyncol_update_many_named(DYNAMIC_COLUMN *str,
  146. uint add_column_count,
  147. MYSQL_LEX_STRING *column_keys,
  148. DYNAMIC_COLUMN_VALUE *values);
  149. enum enum_dyncol_func_result
  150. mariadb_dyncol_exists_num(DYNAMIC_COLUMN *org, uint column_nr);
  151. enum enum_dyncol_func_result
  152. mariadb_dyncol_exists_named(DYNAMIC_COLUMN *str, MYSQL_LEX_STRING *name);
  153. /* List of not NULL columns */
  154. enum enum_dyncol_func_result
  155. mariadb_dyncol_list_num(DYNAMIC_COLUMN *str, uint *count, uint **nums);
  156. enum enum_dyncol_func_result
  157. mariadb_dyncol_list_named(DYNAMIC_COLUMN *str, uint *count,
  158. MYSQL_LEX_STRING **names);
  159. /*
  160. if the column do not exists it is NULL
  161. */
  162. enum enum_dyncol_func_result
  163. mariadb_dyncol_get_num(DYNAMIC_COLUMN *org, uint column_nr,
  164. DYNAMIC_COLUMN_VALUE *store_it_here);
  165. enum enum_dyncol_func_result
  166. mariadb_dyncol_get_named(DYNAMIC_COLUMN *str, MYSQL_LEX_STRING *name,
  167. DYNAMIC_COLUMN_VALUE *store_it_here);
  168. my_bool mariadb_dyncol_has_names(DYNAMIC_COLUMN *str);
  169. enum enum_dyncol_func_result
  170. mariadb_dyncol_check(DYNAMIC_COLUMN *str);
  171. enum enum_dyncol_func_result
  172. mariadb_dyncol_json(DYNAMIC_COLUMN *str, DYNAMIC_STRING *json);
  173. #define mariadb_dyncol_init(A) memset((A), 0, sizeof(*(A)))
  174. void mariadb_dyncol_free(DYNAMIC_COLUMN *str);
  175. /* conversion of values to 3 base types */
  176. enum enum_dyncol_func_result
  177. mariadb_dyncol_val_str(DYNAMIC_STRING *str, DYNAMIC_COLUMN_VALUE *val,
  178. CHARSET_INFO *cs, my_bool quote);
  179. enum enum_dyncol_func_result
  180. mariadb_dyncol_val_long(longlong *ll, DYNAMIC_COLUMN_VALUE *val);
  181. enum enum_dyncol_func_result
  182. mariadb_dyncol_val_double(double *dbl, DYNAMIC_COLUMN_VALUE *val);
  183. enum enum_dyncol_func_result
  184. mariadb_dyncol_unpack(DYNAMIC_COLUMN *str,
  185. uint *count,
  186. MYSQL_LEX_STRING **names, DYNAMIC_COLUMN_VALUE **vals);
  187. void mariadb_dyncol_unpack_free(MYSQL_LEX_STRING *names,
  188. DYNAMIC_COLUMN_VALUE *vals);
  189. int mariadb_dyncol_column_cmp_named(const MYSQL_LEX_STRING *s1,
  190. const MYSQL_LEX_STRING *s2);
  191. enum enum_dyncol_func_result
  192. mariadb_dyncol_column_count(DYNAMIC_COLUMN *str, uint *column_count);
  193. #define mariadb_dyncol_value_init(V) (V)->type= DYN_COL_NULL
  194. /*
  195. Prepare value for using as decimal
  196. */
  197. void mariadb_dyncol_prepare_decimal(DYNAMIC_COLUMN_VALUE *value);
  198. #ifdef __cplusplus
  199. }
  200. #endif
  201. #endif