mariadb_dyncol.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #ifndef LIBMARIADB
  30. #include <decimal.h>
  31. #include <my_decimal_limits.h>
  32. #endif
  33. #include <mysql.h>
  34. #ifndef longlong_defined
  35. #if defined(HAVE_LONG_LONG) && SIZEOF_LONG != 8
  36. typedef unsigned long long int ulonglong; /* ulong or unsigned long long */
  37. typedef long long int longlong;
  38. #else
  39. typedef unsigned long ulonglong; /* ulong or unsigned long long */
  40. typedef long longlong;
  41. #endif
  42. #define longlong_defined
  43. #endif
  44. #ifndef _my_sys_h
  45. typedef struct st_dynamic_string
  46. {
  47. char *str;
  48. size_t length,max_length,alloc_increment;
  49. } DYNAMIC_STRING;
  50. #endif
  51. struct st_mysql_lex_string
  52. {
  53. char *str;
  54. size_t length;
  55. };
  56. typedef struct st_mysql_lex_string MYSQL_LEX_STRING;
  57. typedef struct st_mysql_lex_string LEX_STRING;
  58. /*
  59. Limits of implementation
  60. */
  61. #define MAX_TOTAL_NAME_LENGTH 65535
  62. #define MAX_NAME_LENGTH (MAX_TOTAL_NAME_LENGTH/4)
  63. /* NO and OK is the same used just to show semantics */
  64. #define ER_DYNCOL_NO ER_DYNCOL_OK
  65. enum enum_dyncol_func_result
  66. {
  67. ER_DYNCOL_OK= 0,
  68. ER_DYNCOL_YES= 1, /* For functions returning 0/1 */
  69. ER_DYNCOL_FORMAT= -1, /* Wrong format of the encoded string */
  70. ER_DYNCOL_LIMIT= -2, /* Some limit reached */
  71. ER_DYNCOL_RESOURCE= -3, /* Out of resourses */
  72. ER_DYNCOL_DATA= -4, /* Incorrect input data */
  73. ER_DYNCOL_UNKNOWN_CHARSET= -5, /* Unknown character set */
  74. ER_DYNCOL_TRUNCATED= 2 /* OK, but data was truncated */
  75. };
  76. typedef DYNAMIC_STRING DYNAMIC_COLUMN;
  77. enum enum_dynamic_column_type
  78. {
  79. DYN_COL_NULL= 0,
  80. DYN_COL_INT,
  81. DYN_COL_UINT,
  82. DYN_COL_DOUBLE,
  83. DYN_COL_STRING,
  84. DYN_COL_DECIMAL,
  85. DYN_COL_DATETIME,
  86. DYN_COL_DATE,
  87. DYN_COL_TIME,
  88. DYN_COL_DYNCOL
  89. };
  90. typedef enum enum_dynamic_column_type DYNAMIC_COLUMN_TYPE;
  91. struct st_dynamic_column_value
  92. {
  93. DYNAMIC_COLUMN_TYPE type;
  94. union
  95. {
  96. long long long_value;
  97. unsigned long long ulong_value;
  98. double double_value;
  99. struct {
  100. MYSQL_LEX_STRING value;
  101. MARIADB_CHARSET_INFO *charset;
  102. } string;
  103. #ifndef LIBMARIADB
  104. struct {
  105. decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
  106. decimal_t value;
  107. } decimal;
  108. #endif
  109. MYSQL_TIME time_value;
  110. } x;
  111. };
  112. typedef struct st_dynamic_column_value DYNAMIC_COLUMN_VALUE;
  113. #ifdef MADYNCOL_DEPRECATED
  114. enum enum_dyncol_func_result
  115. dynamic_column_create(DYNAMIC_COLUMN *str,
  116. uint column_nr, DYNAMIC_COLUMN_VALUE *value);
  117. enum enum_dyncol_func_result
  118. dynamic_column_create_many(DYNAMIC_COLUMN *str,
  119. uint column_count,
  120. uint *column_numbers,
  121. DYNAMIC_COLUMN_VALUE *values);
  122. enum enum_dyncol_func_result
  123. dynamic_column_update(DYNAMIC_COLUMN *org, uint column_nr,
  124. DYNAMIC_COLUMN_VALUE *value);
  125. enum enum_dyncol_func_result
  126. dynamic_column_update_many(DYNAMIC_COLUMN *str,
  127. uint add_column_count,
  128. uint *column_numbers,
  129. DYNAMIC_COLUMN_VALUE *values);
  130. enum enum_dyncol_func_result
  131. dynamic_column_exists(DYNAMIC_COLUMN *org, uint column_nr);
  132. enum enum_dyncol_func_result
  133. dynamic_column_list(DYNAMIC_COLUMN *org, DYNAMIC_ARRAY *array_of_uint);
  134. enum enum_dyncol_func_result
  135. dynamic_column_get(DYNAMIC_COLUMN *org, uint column_nr,
  136. DYNAMIC_COLUMN_VALUE *store_it_here);
  137. #endif
  138. /* new functions */
  139. enum enum_dyncol_func_result
  140. mariadb_dyncol_create_many_num(DYNAMIC_COLUMN *str,
  141. uint column_count,
  142. uint *column_numbers,
  143. DYNAMIC_COLUMN_VALUE *values,
  144. my_bool new_string);
  145. enum enum_dyncol_func_result
  146. mariadb_dyncol_create_many_named(DYNAMIC_COLUMN *str,
  147. uint column_count,
  148. MYSQL_LEX_STRING *column_keys,
  149. DYNAMIC_COLUMN_VALUE *values,
  150. my_bool new_string);
  151. enum enum_dyncol_func_result
  152. mariadb_dyncol_update_many_num(DYNAMIC_COLUMN *str,
  153. uint add_column_count,
  154. uint *column_keys,
  155. DYNAMIC_COLUMN_VALUE *values);
  156. enum enum_dyncol_func_result
  157. mariadb_dyncol_update_many_named(DYNAMIC_COLUMN *str,
  158. uint add_column_count,
  159. MYSQL_LEX_STRING *column_keys,
  160. DYNAMIC_COLUMN_VALUE *values);
  161. enum enum_dyncol_func_result
  162. mariadb_dyncol_exists_num(DYNAMIC_COLUMN *org, uint column_nr);
  163. enum enum_dyncol_func_result
  164. mariadb_dyncol_exists_named(DYNAMIC_COLUMN *str, MYSQL_LEX_STRING *name);
  165. /* List of not NULL columns */
  166. enum enum_dyncol_func_result
  167. mariadb_dyncol_list_num(DYNAMIC_COLUMN *str, uint *count, uint **nums);
  168. enum enum_dyncol_func_result
  169. mariadb_dyncol_list_named(DYNAMIC_COLUMN *str, uint *count,
  170. MYSQL_LEX_STRING **names);
  171. /*
  172. if the column do not exists it is NULL
  173. */
  174. enum enum_dyncol_func_result
  175. mariadb_dyncol_get_num(DYNAMIC_COLUMN *org, uint column_nr,
  176. DYNAMIC_COLUMN_VALUE *store_it_here);
  177. enum enum_dyncol_func_result
  178. mariadb_dyncol_get_named(DYNAMIC_COLUMN *str, MYSQL_LEX_STRING *name,
  179. DYNAMIC_COLUMN_VALUE *store_it_here);
  180. my_bool mariadb_dyncol_has_names(DYNAMIC_COLUMN *str);
  181. enum enum_dyncol_func_result
  182. mariadb_dyncol_check(DYNAMIC_COLUMN *str);
  183. enum enum_dyncol_func_result
  184. mariadb_dyncol_json(DYNAMIC_COLUMN *str, DYNAMIC_STRING *json);
  185. void mariadb_dyncol_free(DYNAMIC_COLUMN *str);
  186. #define mariadb_dyncol_init(A) memset((A), 0, sizeof(DYNAMIC_COLUMN))
  187. #define dynamic_column_initialize(A) mariadb_dyncol_init((A))
  188. #define dynamic_column_column_free(A) mariadb_dyncol_free((A))
  189. /* conversion of values to 3 base types */
  190. enum enum_dyncol_func_result
  191. mariadb_dyncol_val_str(DYNAMIC_STRING *str, DYNAMIC_COLUMN_VALUE *val,
  192. MARIADB_CHARSET_INFO *cs, my_bool quote);
  193. enum enum_dyncol_func_result
  194. mariadb_dyncol_val_long(longlong *ll, DYNAMIC_COLUMN_VALUE *val);
  195. enum enum_dyncol_func_result
  196. mariadb_dyncol_val_double(double *dbl, DYNAMIC_COLUMN_VALUE *val);
  197. enum enum_dyncol_func_result
  198. mariadb_dyncol_unpack(DYNAMIC_COLUMN *str,
  199. uint *count,
  200. MYSQL_LEX_STRING **names, DYNAMIC_COLUMN_VALUE **vals);
  201. int mariadb_dyncol_column_cmp_named(const MYSQL_LEX_STRING *s1,
  202. const MYSQL_LEX_STRING *s2);
  203. enum enum_dyncol_func_result
  204. mariadb_dyncol_column_count(DYNAMIC_COLUMN *str, uint *column_count);
  205. #define mariadb_dyncol_value_init(V) (V)->type= DYN_COL_NULL
  206. /*
  207. Prepare value for using as decimal
  208. */
  209. void mariadb_dyncol_prepare_decimal(DYNAMIC_COLUMN_VALUE *value);
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif