mysqlnd_result_meta.c 10 KB

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