mysqlnd_result_meta.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_connection.h"
  22. #include "mysqlnd_ps.h"
  23. #include "mysqlnd_priv.h"
  24. #include "mysqlnd_result.h"
  25. #include "mysqlnd_wireprotocol.h"
  26. #include "mysqlnd_debug.h"
  27. #include "ext/standard/basic_functions.h"
  28. /* {{{ php_mysqlnd_free_field_metadata */
  29. static void
  30. php_mysqlnd_free_field_metadata(MYSQLND_FIELD *meta)
  31. {
  32. if (meta) {
  33. meta->root = NULL;
  34. meta->def = NULL;
  35. if (meta->sname) {
  36. zend_string_release_ex(meta->sname, 0);
  37. }
  38. }
  39. }
  40. /* }}} */
  41. /* {{{ mysqlnd_res_meta::read_metadata */
  42. static enum_func_status
  43. MYSQLND_METHOD(mysqlnd_res_meta, read_metadata)(MYSQLND_RES_METADATA * const meta, MYSQLND_CONN_DATA * conn, MYSQLND_RES * result)
  44. {
  45. unsigned int i = 0;
  46. MYSQLND_PACKET_RES_FIELD field_packet;
  47. DBG_ENTER("mysqlnd_res_meta::read_metadata");
  48. conn->payload_decoder_factory->m.init_result_field_packet(&field_packet);
  49. field_packet.memory_pool = result->memory_pool;
  50. for (;i < meta->field_count; i++) {
  51. zend_ulong idx;
  52. /* We re-read metadata for PS */
  53. ZEND_ASSERT(meta->fields[i].root == NULL);
  54. meta->fields[i].root = NULL;
  55. field_packet.metadata = &(meta->fields[i]);
  56. if (FAIL == PACKET_READ(conn, &field_packet)) {
  57. PACKET_FREE(&field_packet);
  58. DBG_RETURN(FAIL);
  59. }
  60. if (field_packet.error_info.error_no) {
  61. COPY_CLIENT_ERROR(conn->error_info, field_packet.error_info);
  62. /* Return back from CONN_QUERY_SENT */
  63. PACKET_FREE(&field_packet);
  64. DBG_RETURN(FAIL);
  65. }
  66. if (mysqlnd_ps_fetch_functions[meta->fields[i].type].func == NULL) {
  67. DBG_ERR_FMT("Unknown type %u sent by the server. Please send a report to the developers", meta->fields[i].type);
  68. php_error_docref(NULL, E_WARNING, "Unknown type %u sent by the server. Please send a report to the developers", meta->fields[i].type);
  69. PACKET_FREE(&field_packet);
  70. DBG_RETURN(FAIL);
  71. }
  72. /* For BC we have to check whether the key is numeric and use it like this */
  73. if ((meta->fields[i].is_numeric = ZEND_HANDLE_NUMERIC(field_packet.metadata->sname, idx))) {
  74. meta->fields[i].num_key = idx;
  75. }
  76. }
  77. PACKET_FREE(&field_packet);
  78. DBG_RETURN(PASS);
  79. }
  80. /* }}} */
  81. /* {{{ mysqlnd_res_meta::free */
  82. static void
  83. MYSQLND_METHOD(mysqlnd_res_meta, free)(MYSQLND_RES_METADATA * meta)
  84. {
  85. int i;
  86. MYSQLND_FIELD *fields;
  87. DBG_ENTER("mysqlnd_res_meta::free");
  88. if ((fields = meta->fields)) {
  89. DBG_INF("Freeing fields metadata");
  90. i = meta->field_count;
  91. while (i--) {
  92. php_mysqlnd_free_field_metadata(fields++);
  93. }
  94. meta->fields = NULL;
  95. }
  96. DBG_INF("Freeing metadata structure");
  97. DBG_VOID_RETURN;
  98. }
  99. /* }}} */
  100. /* {{{ mysqlnd_res::clone_metadata */
  101. static MYSQLND_RES_METADATA *
  102. MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata)(MYSQLND_RES * result, const MYSQLND_RES_METADATA * const meta)
  103. {
  104. unsigned int i;
  105. /* +1 is to have empty marker at the end */
  106. MYSQLND_RES_METADATA * new_meta = NULL;
  107. MYSQLND_FIELD * new_fields;
  108. MYSQLND_FIELD * orig_fields = meta->fields;
  109. DBG_ENTER("mysqlnd_res_meta::clone_metadata");
  110. new_meta = result->memory_pool->get_chunk(result->memory_pool, sizeof(MYSQLND_RES_METADATA));
  111. if (!new_meta) {
  112. goto oom;
  113. }
  114. memset(new_meta, 0, sizeof(MYSQLND_RES_METADATA));
  115. new_meta->m = meta->m;
  116. new_fields = result->memory_pool->get_chunk(result->memory_pool, (meta->field_count + 1) * sizeof(MYSQLND_FIELD));
  117. if (!new_fields) {
  118. goto oom;
  119. }
  120. memset(new_fields, 0, (meta->field_count + 1) * sizeof(MYSQLND_FIELD));
  121. /*
  122. This will copy also the strings and the root, which we will have
  123. to adjust in the loop
  124. */
  125. memcpy(new_fields, orig_fields, (meta->field_count) * sizeof(MYSQLND_FIELD));
  126. for (i = 0; i < meta->field_count; i++) {
  127. /* First copy the root, then field by field adjust the pointers */
  128. new_fields[i].root = result->memory_pool->get_chunk(result->memory_pool, orig_fields[i].root_len);
  129. if (!new_fields[i].root) {
  130. goto oom;
  131. }
  132. memcpy(new_fields[i].root, orig_fields[i].root, new_fields[i].root_len);
  133. if (orig_fields[i].sname) {
  134. new_fields[i].sname = zend_string_copy(orig_fields[i].sname);
  135. new_fields[i].name = ZSTR_VAL(new_fields[i].sname);
  136. new_fields[i].name_length = ZSTR_LEN(new_fields[i].sname);
  137. }
  138. new_fields[i].is_numeric = orig_fields[i].is_numeric;
  139. new_fields[i].num_key = orig_fields[i].num_key;
  140. if (orig_fields[i].org_name && orig_fields[i].org_name != mysqlnd_empty_string) {
  141. new_fields[i].org_name = new_fields[i].root +
  142. (orig_fields[i].org_name - orig_fields[i].root);
  143. }
  144. if (orig_fields[i].table && orig_fields[i].table != mysqlnd_empty_string) {
  145. new_fields[i].table = new_fields[i].root +
  146. (orig_fields[i].table - orig_fields[i].root);
  147. }
  148. if (orig_fields[i].org_table && orig_fields[i].org_table != mysqlnd_empty_string) {
  149. new_fields[i].org_table = new_fields[i].root +
  150. (orig_fields[i].org_table - orig_fields[i].root);
  151. }
  152. if (orig_fields[i].db && orig_fields[i].db != mysqlnd_empty_string) {
  153. new_fields[i].db = new_fields[i].root + (orig_fields[i].db - orig_fields[i].root);
  154. }
  155. if (orig_fields[i].catalog && orig_fields[i].catalog != mysqlnd_empty_string) {
  156. new_fields[i].catalog = new_fields[i].root + (orig_fields[i].catalog - orig_fields[i].root);
  157. }
  158. /* def is not on the root, if allocated at all */
  159. if (orig_fields[i].def) {
  160. new_fields[i].def = result->memory_pool->get_chunk(result->memory_pool, orig_fields[i].def_length + 1);
  161. if (!new_fields[i].def) {
  162. goto oom;
  163. }
  164. /* copy the trailing \0 too */
  165. memcpy(new_fields[i].def, orig_fields[i].def, orig_fields[i].def_length + 1);
  166. }
  167. }
  168. new_meta->current_field = 0;
  169. new_meta->field_count = meta->field_count;
  170. new_meta->fields = new_fields;
  171. DBG_RETURN(new_meta);
  172. oom:
  173. if (new_meta) {
  174. new_meta->m->free_metadata(new_meta);
  175. new_meta = NULL;
  176. }
  177. DBG_RETURN(NULL);
  178. }
  179. /* }}} */
  180. /* {{{ mysqlnd_res_meta::fetch_field */
  181. static const MYSQLND_FIELD *
  182. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field)(MYSQLND_RES_METADATA * const meta)
  183. {
  184. DBG_ENTER("mysqlnd_res_meta::fetch_field");
  185. if (meta->current_field >= meta->field_count) {
  186. DBG_INF("no more fields");
  187. DBG_RETURN(NULL);
  188. }
  189. DBG_INF_FMT("name=%s max_length=%u",
  190. meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
  191. meta->fields[meta->current_field].max_length);
  192. DBG_RETURN(&meta->fields[meta->current_field++]);
  193. }
  194. /* }}} */
  195. /* {{{ mysqlnd_res_meta::fetch_field_direct */
  196. static const MYSQLND_FIELD *
  197. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct)(const MYSQLND_RES_METADATA * const meta, const MYSQLND_FIELD_OFFSET fieldnr)
  198. {
  199. DBG_ENTER("mysqlnd_res_meta::fetch_field_direct");
  200. DBG_INF_FMT("fieldnr=%u", fieldnr);
  201. DBG_INF_FMT("name=%s max_length=%u",
  202. meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
  203. meta->fields[meta->current_field].max_length);
  204. DBG_RETURN(&meta->fields[fieldnr]);
  205. }
  206. /* }}} */
  207. /* {{{ mysqlnd_res_meta::fetch_fields */
  208. static const MYSQLND_FIELD *
  209. MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields)(MYSQLND_RES_METADATA * const meta)
  210. {
  211. DBG_ENTER("mysqlnd_res_meta::fetch_fields");
  212. DBG_RETURN(meta->fields);
  213. }
  214. /* }}} */
  215. /* {{{ mysqlnd_res_meta::field_tell */
  216. static MYSQLND_FIELD_OFFSET
  217. MYSQLND_METHOD(mysqlnd_res_meta, field_tell)(const MYSQLND_RES_METADATA * const meta)
  218. {
  219. return meta->current_field;
  220. }
  221. /* }}} */
  222. /* {{{ mysqlnd_res_meta::field_seek */
  223. static MYSQLND_FIELD_OFFSET
  224. MYSQLND_METHOD(mysqlnd_res_meta, field_seek)(MYSQLND_RES_METADATA * const meta, const MYSQLND_FIELD_OFFSET field_offset)
  225. {
  226. MYSQLND_FIELD_OFFSET return_value = 0;
  227. DBG_ENTER("mysqlnd_res_meta::fetch_fields");
  228. return_value = meta->current_field;
  229. meta->current_field = field_offset;
  230. DBG_RETURN(return_value);
  231. }
  232. /* }}} */
  233. static
  234. MYSQLND_CLASS_METHODS_START(mysqlnd_res_meta)
  235. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field),
  236. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct),
  237. MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields),
  238. MYSQLND_METHOD(mysqlnd_res_meta, field_tell),
  239. MYSQLND_METHOD(mysqlnd_res_meta, field_seek),
  240. MYSQLND_METHOD(mysqlnd_res_meta, read_metadata),
  241. MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata),
  242. MYSQLND_METHOD(mysqlnd_res_meta, free),
  243. MYSQLND_CLASS_METHODS_END;
  244. /* {{{ mysqlnd_result_meta_init */
  245. PHPAPI MYSQLND_RES_METADATA *
  246. mysqlnd_result_meta_init(MYSQLND_RES *result, unsigned int field_count)
  247. {
  248. size_t alloc_size = sizeof(MYSQLND_RES_METADATA) + mysqlnd_plugin_count() * sizeof(void *);
  249. MYSQLND_RES_METADATA *ret;
  250. DBG_ENTER("mysqlnd_result_meta_init");
  251. do {
  252. ret = result->memory_pool->get_chunk(result->memory_pool, alloc_size);
  253. memset(ret, 0, alloc_size);
  254. ret->m = & mysqlnd_mysqlnd_res_meta_methods;
  255. ret->field_count = field_count;
  256. /* +1 is to have empty marker at the end */
  257. alloc_size = (field_count + 1) * sizeof(MYSQLND_FIELD);
  258. ret->fields = result->memory_pool->get_chunk(result->memory_pool, alloc_size);
  259. memset(ret->fields, 0, alloc_size);
  260. DBG_INF_FMT("meta=%p", ret);
  261. DBG_RETURN(ret);
  262. } while (0);
  263. DBG_RETURN(NULL);
  264. }
  265. /* }}} */
  266. /* {{{ mysqlnd_res_meta_get_methods */
  267. PHPAPI struct st_mysqlnd_res_meta_methods *
  268. mysqlnd_result_metadata_get_methods()
  269. {
  270. return &mysqlnd_mysqlnd_res_meta_methods;
  271. }
  272. /* }}} */
  273. /* {{{ _mysqlnd_plugin_get_plugin_result_metadata_data */
  274. PHPAPI void **
  275. _mysqlnd_plugin_get_plugin_result_metadata_data(const MYSQLND_RES_METADATA * meta, unsigned int plugin_id)
  276. {
  277. DBG_ENTER("_mysqlnd_plugin_get_plugin_result_metadata_data");
  278. DBG_INF_FMT("plugin_id=%u", plugin_id);
  279. if (!meta || plugin_id >= mysqlnd_plugin_count()) {
  280. return NULL;
  281. }
  282. DBG_RETURN((void *)((char *)meta + sizeof(MYSQLND_RES_METADATA) + plugin_id * sizeof(void *)));
  283. }
  284. /* }}} */
  285. /*
  286. * Local variables:
  287. * tab-width: 4
  288. * c-basic-offset: 4
  289. * End:
  290. * vim600: noet sw=4 ts=4 fdm=marker
  291. * vim<600: noet sw=4 ts=4
  292. */