mysqlnd_driver.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Andrey Hristov <andrey@php.net> |
  14. | Ulf Wendel <uw@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php.h"
  18. #include "mysqlnd.h"
  19. #include "mysqlnd_vio.h"
  20. #include "mysqlnd_protocol_frame_codec.h"
  21. #include "mysqlnd_wireprotocol.h"
  22. #include "mysqlnd_connection.h"
  23. #include "mysqlnd_ps.h"
  24. #include "mysqlnd_plugin.h"
  25. #include "mysqlnd_priv.h"
  26. #include "mysqlnd_statistics.h"
  27. #include "mysqlnd_debug.h"
  28. #include "mysqlnd_reverse_api.h"
  29. #include "mysqlnd_ext_plugin.h"
  30. static bool mysqlnd_library_initted = FALSE;
  31. static struct st_mysqlnd_plugin_core mysqlnd_plugin_core =
  32. {
  33. {
  34. MYSQLND_PLUGIN_API_VERSION,
  35. "mysqlnd",
  36. MYSQLND_VERSION_ID,
  37. PHP_MYSQLND_VERSION,
  38. "PHP License 3.01",
  39. "Andrey Hristov <andrey@php.net>, Ulf Wendel <uw@php.net>, Georg Richter <georg@php.net>",
  40. {
  41. NULL, /* will be filled later */
  42. mysqlnd_stats_values_names,
  43. },
  44. {
  45. NULL /* plugin shutdown */
  46. }
  47. }
  48. };
  49. /* {{{ mysqlnd_library_end */
  50. PHPAPI void mysqlnd_library_end(void)
  51. {
  52. if (mysqlnd_library_initted == TRUE) {
  53. mysqlnd_plugin_subsystem_end();
  54. mysqlnd_stats_end(mysqlnd_global_stats, 1);
  55. mysqlnd_global_stats = NULL;
  56. mysqlnd_library_initted = FALSE;
  57. mysqlnd_reverse_api_end();
  58. }
  59. }
  60. /* }}} */
  61. /* {{{ mysqlnd_library_init */
  62. PHPAPI void mysqlnd_library_init(void)
  63. {
  64. if (mysqlnd_library_initted == FALSE) {
  65. mysqlnd_library_initted = TRUE;
  66. mysqlnd_conn_set_methods(&MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_conn));
  67. mysqlnd_conn_data_set_methods(&MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_conn_data));
  68. _mysqlnd_init_ps_subsystem();
  69. /* Should be calloc, as mnd_calloc will reference LOCK_access*/
  70. mysqlnd_stats_init(&mysqlnd_global_stats, STAT_LAST, 1);
  71. mysqlnd_plugin_subsystem_init();
  72. {
  73. mysqlnd_plugin_core.plugin_header.plugin_stats.values = mysqlnd_global_stats;
  74. mysqlnd_plugin_register_ex((struct st_mysqlnd_plugin_header *) &mysqlnd_plugin_core);
  75. }
  76. #if MYSQLND_DBG_ENABLED == 1
  77. mysqlnd_example_plugin_register();
  78. #endif
  79. mysqlnd_debug_trace_plugin_register();
  80. mysqlnd_register_builtin_authentication_plugins();
  81. mysqlnd_reverse_api_init();
  82. }
  83. }
  84. /* }}} */
  85. /* {{{ mysqlnd_object_factory::get_connection */
  86. static MYSQLND *
  87. MYSQLND_METHOD(mysqlnd_object_factory, get_connection)(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory, const bool persistent)
  88. {
  89. const size_t alloc_size_ret = sizeof(MYSQLND) + mysqlnd_plugin_count() * sizeof(void *);
  90. const size_t alloc_size_ret_data = sizeof(MYSQLND_CONN_DATA) + mysqlnd_plugin_count() * sizeof(void *);
  91. MYSQLND * new_object;
  92. MYSQLND_CONN_DATA * data;
  93. DBG_ENTER("mysqlnd_driver::get_connection");
  94. DBG_INF_FMT("persistent=%u", persistent);
  95. new_object = mnd_pecalloc(1, alloc_size_ret, persistent);
  96. if (!new_object) {
  97. DBG_RETURN(NULL);
  98. }
  99. new_object->data = mnd_pecalloc(1, alloc_size_ret_data, persistent);
  100. if (!new_object->data) {
  101. mnd_pefree(new_object, persistent);
  102. DBG_RETURN(NULL);
  103. }
  104. new_object->persistent = persistent;
  105. new_object->m = mysqlnd_conn_get_methods();
  106. data = new_object->data;
  107. mysqlnd_error_info_init(&data->error_info_impl, persistent);
  108. data->error_info = &data->error_info_impl;
  109. data->options = &(data->options_impl);
  110. mysqlnd_upsert_status_init(&data->upsert_status_impl);
  111. data->upsert_status = &(data->upsert_status_impl);
  112. UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(data->upsert_status);
  113. data->persistent = persistent;
  114. data->m = mysqlnd_conn_data_get_methods();
  115. data->object_factory = *factory;
  116. mysqlnd_connection_state_init(&data->state);
  117. data->m->get_reference(data);
  118. mysqlnd_stats_init(&data->stats, STAT_LAST, persistent);
  119. data->protocol_frame_codec = mysqlnd_pfc_init(persistent, factory, data->stats, data->error_info);
  120. data->vio = mysqlnd_vio_init(persistent, factory, data->stats, data->error_info);
  121. data->payload_decoder_factory = mysqlnd_protocol_payload_decoder_factory_init(data, persistent);
  122. data->command = mysqlnd_command_get_methods();
  123. if (!data->protocol_frame_codec || !data->vio || !data->payload_decoder_factory || !data->command) {
  124. new_object->m->dtor(new_object);
  125. DBG_RETURN(NULL);
  126. }
  127. DBG_RETURN(new_object);
  128. }
  129. /* }}} */
  130. /* {{{ mysqlnd_object_factory::clone_connection_object */
  131. static MYSQLND *
  132. MYSQLND_METHOD(mysqlnd_object_factory, clone_connection_object)(MYSQLND * to_be_cloned)
  133. {
  134. const size_t alloc_size_ret = sizeof(MYSQLND) + mysqlnd_plugin_count() * sizeof(void *);
  135. MYSQLND * new_object;
  136. DBG_ENTER("mysqlnd_driver::clone_connection_object");
  137. DBG_INF_FMT("persistent=%u", to_be_cloned->persistent);
  138. if (!to_be_cloned || !to_be_cloned->data) {
  139. DBG_RETURN(NULL);
  140. }
  141. new_object = mnd_pecalloc(1, alloc_size_ret, to_be_cloned->persistent);
  142. if (!new_object) {
  143. DBG_RETURN(NULL);
  144. }
  145. new_object->persistent = to_be_cloned->persistent;
  146. new_object->m = to_be_cloned->m;
  147. new_object->data = to_be_cloned->data->m->get_reference(to_be_cloned->data);
  148. if (!new_object->data) {
  149. new_object->m->dtor(new_object);
  150. new_object = NULL;
  151. }
  152. DBG_RETURN(new_object);
  153. }
  154. /* }}} */
  155. /* {{{ mysqlnd_object_factory::get_prepared_statement */
  156. static MYSQLND_STMT *
  157. MYSQLND_METHOD(mysqlnd_object_factory, get_prepared_statement)(MYSQLND_CONN_DATA * const conn)
  158. {
  159. const size_t alloc_size = sizeof(MYSQLND_STMT) + mysqlnd_plugin_count() * sizeof(void *);
  160. MYSQLND_STMT * ret = mnd_ecalloc(1, alloc_size);
  161. MYSQLND_STMT_DATA * stmt = NULL;
  162. DBG_ENTER("mysqlnd_object_factory::get_prepared_statement");
  163. ret->m = mysqlnd_stmt_get_methods();
  164. stmt = ret->data = mnd_ecalloc(1, sizeof(MYSQLND_STMT_DATA));
  165. DBG_INF_FMT("stmt=%p", stmt);
  166. mysqlnd_error_info_init(&stmt->error_info_impl, 0);
  167. stmt->error_info = &stmt->error_info_impl;
  168. mysqlnd_upsert_status_init(&stmt->upsert_status_impl);
  169. stmt->upsert_status = &(stmt->upsert_status_impl);
  170. stmt->state = MYSQLND_STMT_INITTED;
  171. stmt->execute_cmd_buffer.length = 4096;
  172. stmt->execute_cmd_buffer.buffer = mnd_emalloc(stmt->execute_cmd_buffer.length);
  173. stmt->prefetch_rows = MYSQLND_DEFAULT_PREFETCH_ROWS;
  174. /*
  175. Mark that we reference the connection, thus it won't be
  176. be destructed till there is open statements. The last statement
  177. or normal query result will close it then.
  178. */
  179. stmt->conn = conn->m->get_reference(conn);
  180. DBG_RETURN(ret);
  181. }
  182. /* }}} */
  183. /* {{{ mysqlnd_object_factory::get_pfc */
  184. static MYSQLND_PFC *
  185. MYSQLND_METHOD(mysqlnd_object_factory, get_pfc)(const bool persistent, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
  186. {
  187. const size_t pfc_alloc_size = ZEND_MM_ALIGNED_SIZE(sizeof(MYSQLND_PFC) + mysqlnd_plugin_count() * sizeof(void *));
  188. const size_t pfc_data_alloc_size = sizeof(MYSQLND_PFC_DATA) + mysqlnd_plugin_count() * sizeof(void *);
  189. MYSQLND_PFC * pfc = mnd_pecalloc(1, pfc_alloc_size + pfc_data_alloc_size, persistent);
  190. DBG_ENTER("mysqlnd_object_factory::get_pfc");
  191. DBG_INF_FMT("persistent=%u", persistent);
  192. if (pfc) {
  193. pfc->data = (MYSQLND_PFC_DATA*)((char*)pfc + pfc_alloc_size);
  194. pfc->persistent = pfc->data->persistent = persistent;
  195. pfc->data->m = *mysqlnd_pfc_get_methods();
  196. if (PASS != pfc->data->m.init(pfc, stats, error_info)) {
  197. pfc->data->m.dtor(pfc, stats, error_info);
  198. pfc = NULL;
  199. }
  200. }
  201. DBG_RETURN(pfc);
  202. }
  203. /* }}} */
  204. /* {{{ mysqlnd_object_factory::get_vio */
  205. static MYSQLND_VIO *
  206. MYSQLND_METHOD(mysqlnd_object_factory, get_vio)(const bool persistent, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
  207. {
  208. const size_t vio_alloc_size = ZEND_MM_ALIGNED_SIZE(sizeof(MYSQLND_VIO) + mysqlnd_plugin_count() * sizeof(void *));
  209. const size_t vio_data_alloc_size = sizeof(MYSQLND_VIO_DATA) + mysqlnd_plugin_count() * sizeof(void *);
  210. MYSQLND_VIO * vio = mnd_pecalloc(1, vio_alloc_size + vio_data_alloc_size, persistent);
  211. DBG_ENTER("mysqlnd_object_factory::get_vio");
  212. DBG_INF_FMT("persistent=%u", persistent);
  213. if (vio) {
  214. vio->data = (MYSQLND_VIO_DATA*)((char*)vio + vio_alloc_size);
  215. vio->persistent = vio->data->persistent = persistent;
  216. vio->data->m = *mysqlnd_vio_get_methods();
  217. if (PASS != vio->data->m.init(vio, stats, error_info)) {
  218. vio->data->m.dtor(vio, stats, error_info);
  219. vio = NULL;
  220. }
  221. }
  222. DBG_RETURN(vio);
  223. }
  224. /* }}} */
  225. /* {{{ mysqlnd_object_factory::get_protocol_payload_decoder_factory */
  226. static MYSQLND_PROTOCOL_PAYLOAD_DECODER_FACTORY *
  227. MYSQLND_METHOD(mysqlnd_object_factory, get_protocol_payload_decoder_factory)(MYSQLND_CONN_DATA * conn, const bool persistent)
  228. {
  229. const size_t alloc_size = sizeof(MYSQLND_PROTOCOL_PAYLOAD_DECODER_FACTORY) + mysqlnd_plugin_count() * sizeof(void *);
  230. MYSQLND_PROTOCOL_PAYLOAD_DECODER_FACTORY *ret = mnd_pecalloc(1, alloc_size, persistent);
  231. DBG_ENTER("mysqlnd_object_factory::get_protocol_payload_decoder_factory");
  232. DBG_INF_FMT("persistent=%u", persistent);
  233. if (ret) {
  234. ret->persistent = persistent;
  235. ret->conn = conn;
  236. ret->m = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_protocol_payload_decoder_factory);
  237. }
  238. DBG_RETURN(ret);
  239. }
  240. /* }}} */
  241. PHPAPI MYSQLND_CLASS_METHODS_START(mysqlnd_object_factory)
  242. MYSQLND_METHOD(mysqlnd_object_factory, get_connection),
  243. MYSQLND_METHOD(mysqlnd_object_factory, clone_connection_object),
  244. MYSQLND_METHOD(mysqlnd_object_factory, get_prepared_statement),
  245. MYSQLND_METHOD(mysqlnd_object_factory, get_pfc),
  246. MYSQLND_METHOD(mysqlnd_object_factory, get_vio),
  247. MYSQLND_METHOD(mysqlnd_object_factory, get_protocol_payload_decoder_factory)
  248. MYSQLND_CLASS_METHODS_END;