mariadb_stmt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /************************************************************************
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Library General Public
  4. License as published by the Free Software Foundation; either
  5. version 2 of the License, or (at your option) any later version.
  6. This library 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 GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with this library; if not, write to the Free
  12. Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  13. MA 02111-1301, USA
  14. Part of this code includes code from PHP's mysqlnd extension
  15. (written by Andrey Hristov, Georg Richter and Ulf Wendel), freely
  16. available from http://www.php.net/software
  17. *************************************************************************/
  18. #define MYSQL_NO_DATA 100
  19. #define MYSQL_DATA_TRUNCATED 101
  20. #define MYSQL_DEFAULT_PREFETCH_ROWS (unsigned long) 1
  21. /* Bind flags */
  22. #define MADB_BIND_DUMMY 1
  23. #define MARIADB_STMT_BULK_SUPPORTED(stmt)\
  24. ((stmt)->mysql && \
  25. (!((stmt)->mysql->server_capabilities & CLIENT_MYSQL) &&\
  26. ((stmt)->mysql->extension->mariadb_server_capabilities & \
  27. (MARIADB_CLIENT_STMT_BULK_OPERATIONS >> 32))))
  28. #define SET_CLIENT_STMT_ERROR(a, b, c, d) \
  29. { \
  30. (a)->last_errno= (b);\
  31. strncpy((a)->sqlstate, (c), sizeof((a)->sqlstate));\
  32. strncpy((a)->last_error, (d) ? (d) : ER((b)), sizeof((a)->last_error));\
  33. }
  34. #define CLEAR_CLIENT_STMT_ERROR(a) \
  35. { \
  36. (a)->last_errno= 0;\
  37. strcpy((a)->sqlstate, "00000");\
  38. (a)->last_error[0]= 0;\
  39. }
  40. #define MYSQL_PS_SKIP_RESULT_W_LEN -1
  41. #define MYSQL_PS_SKIP_RESULT_STR -2
  42. #define STMT_ID_LENGTH 4
  43. typedef struct st_mysql_stmt MYSQL_STMT;
  44. typedef MYSQL_RES* (*mysql_stmt_use_or_store_func)(MYSQL_STMT *);
  45. enum enum_stmt_attr_type
  46. {
  47. STMT_ATTR_UPDATE_MAX_LENGTH,
  48. STMT_ATTR_CURSOR_TYPE,
  49. STMT_ATTR_PREFETCH_ROWS,
  50. STMT_ATTR_PREBIND_PARAMS=200,
  51. STMT_ATTR_ARRAY_SIZE,
  52. STMT_ATTR_ROW_SIZE
  53. };
  54. enum enum_cursor_type
  55. {
  56. CURSOR_TYPE_NO_CURSOR= 0,
  57. CURSOR_TYPE_READ_ONLY= 1,
  58. CURSOR_TYPE_FOR_UPDATE= 2,
  59. CURSOR_TYPE_SCROLLABLE= 4
  60. };
  61. enum enum_indicator_type
  62. {
  63. STMT_INDICATOR_NTS=-1,
  64. STMT_INDICATOR_NONE=0,
  65. STMT_INDICATOR_NULL=1,
  66. STMT_INDICATOR_DEFAULT=2,
  67. STMT_INDICATOR_IGNORE=3
  68. };
  69. /*
  70. bulk PS flags
  71. */
  72. #define STMT_BULK_FLAG_CLIENT_SEND_TYPES 128
  73. #define STMT_BULK_FLAG_INSERT_ID_REQUEST 64
  74. typedef enum mysql_stmt_state
  75. {
  76. MYSQL_STMT_INITTED = 0,
  77. MYSQL_STMT_PREPARED,
  78. MYSQL_STMT_EXECUTED,
  79. MYSQL_STMT_WAITING_USE_OR_STORE,
  80. MYSQL_STMT_USE_OR_STORE_CALLED,
  81. MYSQL_STMT_USER_FETCHING, /* fetch_row_buff or fetch_row_unbuf */
  82. MYSQL_STMT_FETCH_DONE
  83. } enum_mysqlnd_stmt_state;
  84. typedef struct st_mysql_bind
  85. {
  86. unsigned long *length; /* output length pointer */
  87. my_bool *is_null; /* Pointer to null indicator */
  88. void *buffer; /* buffer to get/put data */
  89. /* set this if you want to track data truncations happened during fetch */
  90. my_bool *error;
  91. union {
  92. unsigned char *row_ptr; /* for the current data position */
  93. char *indicator; /* indicator variable */
  94. } u;
  95. void (*store_param_func)(NET *net, struct st_mysql_bind *param);
  96. void (*fetch_result)(struct st_mysql_bind *, MYSQL_FIELD *,
  97. unsigned char **row);
  98. void (*skip_result)(struct st_mysql_bind *, MYSQL_FIELD *,
  99. unsigned char **row);
  100. /* output buffer length, must be set when fetching str/binary */
  101. unsigned long buffer_length;
  102. unsigned long offset; /* offset position for char/binary fetch */
  103. unsigned long length_value; /* Used if length is 0 */
  104. unsigned int flags; /* special flags, e.g. for dummy bind */
  105. unsigned int pack_length; /* Internal length for packed data */
  106. enum enum_field_types buffer_type; /* buffer type */
  107. my_bool error_value; /* used if error is 0 */
  108. my_bool is_unsigned; /* set if integer type is unsigned */
  109. my_bool long_data_used; /* If used with mysql_send_long_data */
  110. my_bool is_null_value; /* Used if is_null is 0 */
  111. void *extension;
  112. } MYSQL_BIND;
  113. typedef struct st_mysqlnd_upsert_result
  114. {
  115. unsigned int warning_count;
  116. unsigned int server_status;
  117. unsigned long long affected_rows;
  118. unsigned long long last_insert_id;
  119. } mysql_upsert_status;
  120. typedef struct st_mysql_cmd_buffer
  121. {
  122. unsigned char *buffer;
  123. size_t length;
  124. } MYSQL_CMD_BUFFER;
  125. typedef struct st_mysql_error_info
  126. {
  127. unsigned int error_no;
  128. char error[MYSQL_ERRMSG_SIZE+1];
  129. char sqlstate[SQLSTATE_LENGTH + 1];
  130. } mysql_error_info;
  131. struct st_mysqlnd_stmt_methods
  132. {
  133. my_bool (*prepare)(const MYSQL_STMT * stmt, const char * const query, size_t query_len);
  134. my_bool (*execute)(const MYSQL_STMT * stmt);
  135. MYSQL_RES * (*use_result)(const MYSQL_STMT * stmt);
  136. MYSQL_RES * (*store_result)(const MYSQL_STMT * stmt);
  137. MYSQL_RES * (*get_result)(const MYSQL_STMT * stmt);
  138. my_bool (*free_result)(const MYSQL_STMT * stmt);
  139. my_bool (*seek_data)(const MYSQL_STMT * stmt, unsigned long long row);
  140. my_bool (*reset)(const MYSQL_STMT * stmt);
  141. my_bool (*close)(const MYSQL_STMT * stmt); /* private */
  142. my_bool (*dtor)(const MYSQL_STMT * stmt); /* use this for mysqlnd_stmt_close */
  143. my_bool (*fetch)(const MYSQL_STMT * stmt, my_bool * const fetched_anything);
  144. my_bool (*bind_param)(const MYSQL_STMT * stmt, const MYSQL_BIND bind);
  145. my_bool (*refresh_bind_param)(const MYSQL_STMT * stmt);
  146. my_bool (*bind_result)(const MYSQL_STMT * stmt, const MYSQL_BIND *bind);
  147. my_bool (*send_long_data)(const MYSQL_STMT * stmt, unsigned int param_num,
  148. const char * const data, size_t length);
  149. MYSQL_RES *(*get_parameter_metadata)(const MYSQL_STMT * stmt);
  150. MYSQL_RES *(*get_result_metadata)(const MYSQL_STMT * stmt);
  151. unsigned long long (*get_last_insert_id)(const MYSQL_STMT * stmt);
  152. unsigned long long (*get_affected_rows)(const MYSQL_STMT * stmt);
  153. unsigned long long (*get_num_rows)(const MYSQL_STMT * stmt);
  154. unsigned int (*get_param_count)(const MYSQL_STMT * stmt);
  155. unsigned int (*get_field_count)(const MYSQL_STMT * stmt);
  156. unsigned int (*get_warning_count)(const MYSQL_STMT * stmt);
  157. unsigned int (*get_error_no)(const MYSQL_STMT * stmt);
  158. const char * (*get_error_str)(const MYSQL_STMT * stmt);
  159. const char * (*get_sqlstate)(const MYSQL_STMT * stmt);
  160. my_bool (*get_attribute)(const MYSQL_STMT * stmt, enum enum_stmt_attr_type attr_type, const void * value);
  161. my_bool (*set_attribute)(const MYSQL_STMT * stmt, enum enum_stmt_attr_type attr_type, const void * value);
  162. void (*set_error)(MYSQL_STMT *stmt, unsigned int error_nr, const char *sqlstate, const char *format, ...);
  163. };
  164. typedef int (*mysql_stmt_fetch_row_func)(MYSQL_STMT *stmt, unsigned char **row);
  165. struct st_mysql_stmt
  166. {
  167. MA_MEM_ROOT mem_root;
  168. MYSQL *mysql;
  169. unsigned long stmt_id;
  170. unsigned long flags;/* cursor is set here */
  171. enum_mysqlnd_stmt_state state;
  172. MYSQL_FIELD *fields;
  173. unsigned int field_count;
  174. unsigned int param_count;
  175. unsigned char send_types_to_server;
  176. MYSQL_BIND *params;
  177. MYSQL_BIND *bind;
  178. MYSQL_DATA result; /* we don't use mysqlnd's result set logic */
  179. MYSQL_ROWS *result_cursor;
  180. my_bool bind_result_done;
  181. my_bool bind_param_done;
  182. mysql_upsert_status upsert_status;
  183. unsigned int last_errno;
  184. char last_error[MYSQL_ERRMSG_SIZE+1];
  185. char sqlstate[SQLSTATE_LENGTH + 1];
  186. my_bool update_max_length;
  187. unsigned long prefetch_rows;
  188. LIST list;
  189. my_bool cursor_exists;
  190. void *extension;
  191. mysql_stmt_fetch_row_func fetch_row_func;
  192. unsigned int execute_count;/* count how many times the stmt was executed */
  193. mysql_stmt_use_or_store_func default_rset_handler;
  194. struct st_mysqlnd_stmt_methods *m;
  195. unsigned int array_size;
  196. size_t row_size;
  197. unsigned int prebind_params;
  198. };
  199. typedef void (*ps_field_fetch_func)(MYSQL_BIND *r_param, const MYSQL_FIELD * field, unsigned char **row);
  200. typedef struct st_mysql_perm_bind {
  201. ps_field_fetch_func func;
  202. /* should be signed int */
  203. int pack_len;
  204. unsigned long max_len;
  205. } MYSQL_PS_CONVERSION;
  206. extern MYSQL_PS_CONVERSION mysql_ps_fetch_functions[MYSQL_TYPE_GEOMETRY + 1];
  207. unsigned long ma_net_safe_read(MYSQL *mysql);
  208. void mysql_init_ps_subsystem(void);
  209. unsigned long net_field_length(unsigned char **packet);
  210. int ma_simple_command(MYSQL *mysql,enum enum_server_command command, const char *arg,
  211. size_t length, my_bool skipp_check, void *opt_arg);
  212. /*
  213. * function prototypes
  214. */
  215. MYSQL_STMT * STDCALL mysql_stmt_init(MYSQL *mysql);
  216. int STDCALL mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, unsigned long length);
  217. int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt);
  218. int STDCALL mysql_stmt_fetch(MYSQL_STMT *stmt);
  219. int STDCALL mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind_arg, unsigned int column, unsigned long offset);
  220. int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt);
  221. unsigned long STDCALL mysql_stmt_param_count(MYSQL_STMT * stmt);
  222. my_bool STDCALL mysql_stmt_attr_set(MYSQL_STMT *stmt, enum enum_stmt_attr_type attr_type, const void *attr);
  223. my_bool STDCALL mysql_stmt_attr_get(MYSQL_STMT *stmt, enum enum_stmt_attr_type attr_type, void *attr);
  224. my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
  225. my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
  226. my_bool STDCALL mysql_stmt_close(MYSQL_STMT * stmt);
  227. my_bool STDCALL mysql_stmt_reset(MYSQL_STMT * stmt);
  228. my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt);
  229. my_bool STDCALL mysql_stmt_send_long_data(MYSQL_STMT *stmt, unsigned int param_number, const char *data, unsigned long length);
  230. MYSQL_RES *STDCALL mysql_stmt_result_metadata(MYSQL_STMT *stmt);
  231. MYSQL_RES *STDCALL mysql_stmt_param_metadata(MYSQL_STMT *stmt);
  232. unsigned int STDCALL mysql_stmt_errno(MYSQL_STMT * stmt);
  233. const char *STDCALL mysql_stmt_error(MYSQL_STMT * stmt);
  234. const char *STDCALL mysql_stmt_sqlstate(MYSQL_STMT * stmt);
  235. MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_seek(MYSQL_STMT *stmt, MYSQL_ROW_OFFSET offset);
  236. MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_tell(MYSQL_STMT *stmt);
  237. void STDCALL mysql_stmt_data_seek(MYSQL_STMT *stmt, unsigned long long offset);
  238. unsigned long long STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt);
  239. unsigned long long STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt);
  240. unsigned long long STDCALL mysql_stmt_insert_id(MYSQL_STMT *stmt);
  241. unsigned int STDCALL mysql_stmt_field_count(MYSQL_STMT *stmt);
  242. int STDCALL mysql_stmt_next_result(MYSQL_STMT *stmt);
  243. my_bool STDCALL mysql_stmt_more_results(MYSQL_STMT *stmt);
  244. int STDCALL mariadb_stmt_execute_direct(MYSQL_STMT *stmt, const char *stmt_str, size_t length);