mysqlnd_driver.c 11 KB

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