PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/mysqlnd/mysqlnd_result_meta.c

http://github.com/infusion/PHP
C | 532 lines | 403 code | 54 blank | 75 comment | 93 complexity | 2452f79c63798064a3a3244b9e34fa93 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2006-2011 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: Georg Richter <georg@mysql.com> |
  16. | Andrey Hristov <andrey@mysql.com> |
  17. | Ulf Wendel <uwendel@mysql.com> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id: mysqlnd_result_meta.c 306939 2011-01-01 02:19:59Z felipe $ */
  21. #include "php.h"
  22. #include "mysqlnd.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, zend_bool persistent TSRMLS_DC)
  31. {
  32. if (meta) {
  33. if (meta->root) {
  34. mnd_pefree(meta->root, persistent);
  35. meta->root = NULL;
  36. }
  37. if (meta->def) {
  38. mnd_pefree(meta->def, persistent);
  39. meta->def = NULL;
  40. }
  41. }
  42. }
  43. /* }}} */
  44. /* {{{ mysqlnd_handle_numeric */
  45. /*
  46. The following code is stolen from ZE - HANDLE_NUMERIC() macro from zend_hash.c
  47. and modified for the needs of mysqlnd.
  48. */
  49. static zend_bool
  50. mysqlnd_is_key_numeric(const char * key, size_t length, long *idx)
  51. {
  52. register const char * tmp = key;
  53. if (*tmp=='-') {
  54. tmp++;
  55. }
  56. if ((*tmp>='0' && *tmp<='9')) {
  57. do { /* possibly a numeric index */
  58. const char *end=key+length-1;
  59. if (*tmp++=='0' && length>2) { /* don't accept numbers with leading zeros */
  60. break;
  61. }
  62. while (tmp<end) {
  63. if (!(*tmp>='0' && *tmp<='9')) {
  64. break;
  65. }
  66. tmp++;
  67. }
  68. if (tmp==end && *tmp=='\0') { /* a numeric index */
  69. if (*key=='-') {
  70. *idx = strtol(key, NULL, 10);
  71. if (*idx!=LONG_MIN) {
  72. return TRUE;
  73. }
  74. } else {
  75. *idx = strtol(key, NULL, 10);
  76. if (*idx!=LONG_MAX) {
  77. return TRUE;
  78. }
  79. }
  80. }
  81. } while (0);
  82. }
  83. return FALSE;
  84. }
  85. /* }}} */
  86. #if MYSQLND_UNICODE
  87. /* {{{ mysqlnd_unicode_is_key_numeric */
  88. static zend_bool
  89. mysqlnd_unicode_is_key_numeric(UChar *key, size_t length, long *idx)
  90. {
  91. register UChar * tmp=key;
  92. if (*tmp==0x2D /*'-'*/) {
  93. tmp++;
  94. }
  95. if ((*tmp>=0x30 /*'0'*/ && *tmp<=0x39 /*'9'*/)) { /* possibly a numeric index */
  96. do {
  97. UChar *end=key+length-1;
  98. if (*tmp++==0x30 && length>2) { /* don't accept numbers with leading zeros */
  99. break;
  100. }
  101. while (tmp<end) {
  102. if (!(*tmp>=0x30 /*'0'*/ && *tmp<=0x39 /*'9'*/)) {
  103. break;
  104. }
  105. tmp++;
  106. }
  107. if (tmp==end && *tmp==0) { /* a numeric index */
  108. if (*key==0x2D /*'-'*/) {
  109. *idx = zend_u_strtol(key, NULL, 10);
  110. if (*idx!=LONG_MIN) {
  111. return TRUE;
  112. }
  113. } else {
  114. *idx = zend_u_strtol(key, NULL, 10);
  115. if (*idx!=LONG_MAX) {
  116. return TRUE;
  117. }
  118. }
  119. }
  120. } while (0);
  121. }
  122. return FALSE;
  123. }
  124. /* }}} */
  125. #endif
  126. /* {{{ mysqlnd_res_meta::read_metadata */
  127. static enum_func_status
  128. MYSQLND_METHOD(mysqlnd_res_meta, read_metadata)(MYSQLND_RES_METADATA * const meta, MYSQLND *conn TSRMLS_DC)
  129. {
  130. unsigned int i = 0;
  131. MYSQLND_PACKET_RES_FIELD * field_packet;
  132. #if MYSQLND_UNICODE
  133. UChar *ustr;
  134. int ulen;
  135. #endif
  136. DBG_ENTER("mysqlnd_res_meta::read_metadata");
  137. field_packet = conn->protocol->m.get_result_field_packet(conn->protocol, FALSE TSRMLS_CC);
  138. if (!field_packet) {
  139. SET_OOM_ERROR(conn->error_info);
  140. DBG_RETURN(FAIL);
  141. }
  142. field_packet->persistent_alloc = meta->persistent;
  143. for (;i < meta->field_count; i++) {
  144. long idx;
  145. if (meta->fields[i].root) {
  146. /* We re-read metadata for PS */
  147. mnd_pefree(meta->fields[i].root, meta->persistent);
  148. meta->fields[i].root = NULL;
  149. }
  150. field_packet->metadata = &(meta->fields[i]);
  151. if (FAIL == PACKET_READ(field_packet, conn)) {
  152. PACKET_FREE(field_packet);
  153. DBG_RETURN(FAIL);
  154. }
  155. if (field_packet->error_info.error_no) {
  156. conn->error_info = field_packet->error_info;
  157. /* Return back from CONN_QUERY_SENT */
  158. PACKET_FREE(field_packet);
  159. DBG_RETURN(FAIL);
  160. }
  161. if (field_packet->stupid_list_fields_eof == TRUE) {
  162. meta->field_count = i;
  163. break;
  164. }
  165. if (mysqlnd_ps_fetch_functions[meta->fields[i].type].func == NULL) {
  166. DBG_ERR_FMT("Unknown type %u sent by the server. Please send a report to the developers",
  167. meta->fields[i].type);
  168. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  169. "Unknown type %u sent by the server. "
  170. "Please send a report to the developers",
  171. meta->fields[i].type);
  172. PACKET_FREE(field_packet);
  173. DBG_RETURN(FAIL);
  174. }
  175. if (meta->fields[i].type == MYSQL_TYPE_BIT) {
  176. size_t field_len;
  177. DBG_INF("BIT");
  178. ++meta->bit_fields_count;
  179. /* .length is in bits */
  180. field_len = meta->fields[i].length / 8;
  181. /*
  182. If there is rest, add one byte :
  183. 8 bits = 1 byte but 9 bits = 2 bytes
  184. */
  185. if (meta->fields[i].length % 8) {
  186. ++field_len;
  187. }
  188. switch (field_len) {
  189. case 8:
  190. case 7:
  191. case 6:
  192. case 5:
  193. meta->bit_fields_total_len += 20;/* 21 digis, no sign*/
  194. break;
  195. case 4:
  196. meta->bit_fields_total_len += 10;/* 2 000 000 000*/
  197. break;
  198. case 3:
  199. meta->bit_fields_total_len += 8;/* 12 000 000*/
  200. break;
  201. case 2:
  202. meta->bit_fields_total_len += 5;/* 32 500 */
  203. break;
  204. case 1:
  205. meta->bit_fields_total_len += 3;/* 120 */
  206. break;
  207. }
  208. }
  209. #if MYSQLND_UNICODE
  210. zend_string_to_unicode(UG(utf8_conv), &ustr, &ulen,
  211. meta->fields[i].name,
  212. meta->fields[i].name_length TSRMLS_CC);
  213. if ((meta->zend_hash_keys[i].is_numeric =
  214. mysqlnd_unicode_is_key_numeric(ustr, ulen + 1, &idx)))
  215. {
  216. meta->zend_hash_keys[i].key = idx;
  217. mnd_efree(ustr);
  218. } else {
  219. meta->zend_hash_keys[i].ustr.u = ustr;
  220. meta->zend_hash_keys[i].ulen = ulen;
  221. meta->zend_hash_keys[i].key = zend_u_get_hash_value(IS_UNICODE, ZSTR(ustr), ulen + 1);
  222. }
  223. #else
  224. /* For BC we have to check whether the key is numeric and use it like this */
  225. if ((meta->zend_hash_keys[i].is_numeric =
  226. mysqlnd_is_key_numeric(field_packet->metadata->name,
  227. field_packet->metadata->name_length + 1,
  228. &idx)))
  229. {
  230. meta->zend_hash_keys[i].key = idx;
  231. } else {
  232. meta->zend_hash_keys[i].key =
  233. zend_get_hash_value(field_packet->metadata->name,
  234. field_packet->metadata->name_length + 1);
  235. }
  236. #endif
  237. }
  238. PACKET_FREE(field_packet);
  239. DBG_RETURN(PASS);
  240. }
  241. /* }}} */
  242. /* {{{ mysqlnd_res_meta::free */
  243. static void
  244. MYSQLND_METHOD(mysqlnd_res_meta, free)(MYSQLND_RES_METADATA * meta TSRMLS_DC)
  245. {
  246. int i;
  247. MYSQLND_FIELD *fields;
  248. DBG_ENTER("mysqlnd_res_meta::free");
  249. DBG_INF_FMT("persistent=%u", meta->persistent);
  250. if ((fields = meta->fields)) {
  251. DBG_INF("Freeing fields metadata");
  252. i = meta->field_count;
  253. while (i--) {
  254. php_mysqlnd_free_field_metadata(fields++, meta->persistent TSRMLS_CC);
  255. }
  256. mnd_pefree(meta->fields, meta->persistent);
  257. meta->fields = NULL;
  258. }
  259. if (meta->zend_hash_keys) {
  260. DBG_INF("Freeing zend_hash_keys");
  261. #if MYSQLND_UNICODE
  262. if (UG(unicode)) {
  263. for (i = 0; i < meta->field_count; i++) {
  264. if (meta->zend_hash_keys[i].ustr.v) {
  265. mnd_pefree(meta->zend_hash_keys[i].ustr.v, meta->persistent);
  266. }
  267. }
  268. }
  269. #endif
  270. mnd_pefree(meta->zend_hash_keys, meta->persistent);
  271. meta->zend_hash_keys = NULL;
  272. }
  273. DBG_INF("Freeing metadata structure");
  274. mnd_pefree(meta, meta->persistent);
  275. DBG_VOID_RETURN;
  276. }
  277. /* }}} */
  278. /* {{{ mysqlnd_res::clone_metadata */
  279. static MYSQLND_RES_METADATA *
  280. MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata)(const MYSQLND_RES_METADATA * const meta, zend_bool persistent TSRMLS_DC)
  281. {
  282. unsigned int i;
  283. /* +1 is to have empty marker at the end */
  284. MYSQLND_RES_METADATA * new_meta = NULL;
  285. MYSQLND_FIELD * new_fields;
  286. MYSQLND_FIELD * orig_fields = meta->fields;
  287. size_t len = meta->field_count * sizeof(struct mysqlnd_field_hash_key);
  288. DBG_ENTER("mysqlnd_res_meta::clone_metadata");
  289. DBG_INF_FMT("persistent=%u", persistent);
  290. new_meta = mnd_pecalloc(1, sizeof(MYSQLND_RES_METADATA), persistent);
  291. if (!new_meta) {
  292. goto oom;
  293. }
  294. new_meta->persistent = persistent;
  295. new_meta->m = meta->m;
  296. new_fields = mnd_pecalloc(meta->field_count + 1, sizeof(MYSQLND_FIELD), persistent);
  297. if (!new_fields) {
  298. goto oom;
  299. }
  300. new_meta->zend_hash_keys = mnd_pemalloc(len, persistent);
  301. if (!new_meta->zend_hash_keys) {
  302. goto oom;
  303. }
  304. memcpy(new_meta->zend_hash_keys, meta->zend_hash_keys, len);
  305. /*
  306. This will copy also the strings and the root, which we will have
  307. to adjust in the loop
  308. */
  309. memcpy(new_fields, orig_fields, (meta->field_count) * sizeof(MYSQLND_FIELD));
  310. for (i = 0; i < meta->field_count; i++) {
  311. /* First copy the root, then field by field adjust the pointers */
  312. new_fields[i].root = mnd_pemalloc(orig_fields[i].root_len, persistent);
  313. if (!new_fields[i].root) {
  314. goto oom;
  315. }
  316. memcpy(new_fields[i].root, orig_fields[i].root, new_fields[i].root_len);
  317. if (orig_fields[i].name && orig_fields[i].name != mysqlnd_empty_string) {
  318. new_fields[i].name = new_fields[i].root +
  319. (orig_fields[i].name - orig_fields[i].root);
  320. }
  321. if (orig_fields[i].org_name && orig_fields[i].org_name != mysqlnd_empty_string) {
  322. new_fields[i].org_name = new_fields[i].root +
  323. (orig_fields[i].org_name - orig_fields[i].root);
  324. }
  325. if (orig_fields[i].table && orig_fields[i].table != mysqlnd_empty_string) {
  326. new_fields[i].table = new_fields[i].root +
  327. (orig_fields[i].table - orig_fields[i].root);
  328. }
  329. if (orig_fields[i].org_table && orig_fields[i].org_table != mysqlnd_empty_string) {
  330. new_fields[i].org_table = new_fields[i].root +
  331. (orig_fields[i].org_table - orig_fields[i].root);
  332. }
  333. if (orig_fields[i].db && orig_fields[i].db != mysqlnd_empty_string) {
  334. new_fields[i].db = new_fields[i].root + (orig_fields[i].db - orig_fields[i].root);
  335. }
  336. if (orig_fields[i].catalog && orig_fields[i].catalog != mysqlnd_empty_string) {
  337. new_fields[i].catalog = new_fields[i].root + (orig_fields[i].catalog - orig_fields[i].root);
  338. }
  339. /* def is not on the root, if allocated at all */
  340. if (orig_fields[i].def) {
  341. new_fields[i].def = mnd_pemalloc(orig_fields[i].def_length + 1, persistent);
  342. if (!new_fields[i].def) {
  343. goto oom;
  344. }
  345. /* copy the trailing \0 too */
  346. memcpy(new_fields[i].def, orig_fields[i].def, orig_fields[i].def_length + 1);
  347. }
  348. #if MYSQLND_UNICODE
  349. if (new_meta->zend_hash_keys[i].ustr.u) {
  350. new_meta->zend_hash_keys[i].ustr.u =
  351. eustrndup(new_meta->zend_hash_keys[i].ustr.u, new_meta->zend_hash_keys[i].ulen);
  352. if (!new_meta->zend_hash_keys[i].ustr.u) {
  353. goto oom;
  354. }
  355. }
  356. #endif
  357. }
  358. new_meta->current_field = 0;
  359. new_meta->field_count = meta->field_count;
  360. new_meta->fields = new_fields;
  361. DBG_RETURN(new_meta);
  362. oom:
  363. if (new_meta) {
  364. new_meta->m->free_metadata(new_meta TSRMLS_CC);
  365. new_meta = NULL;
  366. }
  367. DBG_RETURN(NULL);
  368. }
  369. /* }}} */
  370. /* {{{ mysqlnd_res_meta::fetch_field */
  371. static const MYSQLND_FIELD *
  372. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field)(MYSQLND_RES_METADATA * const meta TSRMLS_DC)
  373. {
  374. DBG_ENTER("mysqlnd_res_meta::fetch_field");
  375. if (meta->current_field >= meta->field_count) {
  376. DBG_INF("no more fields");
  377. DBG_RETURN(NULL);
  378. }
  379. DBG_INF_FMT("name=%s max_length=%u",
  380. meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
  381. meta->fields[meta->current_field].max_length);
  382. DBG_RETURN(&meta->fields[meta->current_field++]);
  383. }
  384. /* }}} */
  385. /* {{{ mysqlnd_res_meta::fetch_field_direct */
  386. static const MYSQLND_FIELD *
  387. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct)(const MYSQLND_RES_METADATA * const meta, MYSQLND_FIELD_OFFSET fieldnr TSRMLS_DC)
  388. {
  389. DBG_ENTER("mysqlnd_res_meta::fetch_field_direct");
  390. DBG_INF_FMT("fieldnr=%u", fieldnr);
  391. DBG_INF_FMT("name=%s max_length=%u",
  392. meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
  393. meta->fields[meta->current_field].max_length);
  394. DBG_RETURN(&meta->fields[fieldnr]);
  395. }
  396. /* }}} */
  397. /* {{{ mysqlnd_res_meta::fetch_fields */
  398. static const MYSQLND_FIELD *
  399. MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields)(MYSQLND_RES_METADATA * const meta TSRMLS_DC)
  400. {
  401. DBG_ENTER("mysqlnd_res_meta::fetch_fields");
  402. DBG_RETURN(meta->fields);
  403. }
  404. /* }}} */
  405. /* {{{ mysqlnd_res_meta::field_tell */
  406. static MYSQLND_FIELD_OFFSET
  407. MYSQLND_METHOD(mysqlnd_res_meta, field_tell)(const MYSQLND_RES_METADATA * const meta TSRMLS_DC)
  408. {
  409. return meta->current_field;
  410. }
  411. /* }}} */
  412. static
  413. MYSQLND_CLASS_METHODS_START(mysqlnd_res_meta)
  414. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field),
  415. MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct),
  416. MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields),
  417. MYSQLND_METHOD(mysqlnd_res_meta, field_tell),
  418. MYSQLND_METHOD(mysqlnd_res_meta, read_metadata),
  419. MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata),
  420. MYSQLND_METHOD(mysqlnd_res_meta, free),
  421. MYSQLND_CLASS_METHODS_END;
  422. /* {{{ mysqlnd_result_meta_init */
  423. PHPAPI MYSQLND_RES_METADATA *
  424. mysqlnd_result_meta_init(unsigned int field_count, zend_bool persistent TSRMLS_DC)
  425. {
  426. size_t alloc_size = sizeof(MYSQLND_RES_METADATA) + mysqlnd_plugin_count() * sizeof(void *);
  427. MYSQLND_RES_METADATA *ret = mnd_pecalloc(1, alloc_size, persistent);
  428. DBG_ENTER("mysqlnd_result_meta_init");
  429. DBG_INF_FMT("persistent=%u", persistent);
  430. do {
  431. if (!ret) {
  432. break;
  433. }
  434. ret->m = & mysqlnd_mysqlnd_res_meta_methods;
  435. ret->persistent = persistent;
  436. ret->field_count = field_count;
  437. /* +1 is to have empty marker at the end */
  438. ret->fields = mnd_pecalloc(field_count + 1, sizeof(MYSQLND_FIELD), ret->persistent);
  439. ret->zend_hash_keys = mnd_pecalloc(field_count, sizeof(struct mysqlnd_field_hash_key), ret->persistent);
  440. if (!ret->fields || !ret->zend_hash_keys) {
  441. break;
  442. }
  443. DBG_INF_FMT("meta=%p", ret);
  444. DBG_RETURN(ret);
  445. } while (0);
  446. if (ret) {
  447. ret->m->free_metadata(ret TSRMLS_CC);
  448. }
  449. DBG_RETURN(NULL);
  450. }
  451. /* }}} */
  452. /* {{{ mysqlnd_res_meta_get_methods */
  453. PHPAPI struct st_mysqlnd_res_meta_methods *
  454. mysqlnd_result_metadata_get_methods()
  455. {
  456. return &mysqlnd_mysqlnd_res_meta_methods;
  457. }
  458. /* }}} */
  459. /* {{{ _mysqlnd_plugin_get_plugin_result_metadata_data */
  460. PHPAPI void **
  461. _mysqlnd_plugin_get_plugin_result_metadata_data(const MYSQLND_RES_METADATA * meta, unsigned int plugin_id TSRMLS_DC)
  462. {
  463. DBG_ENTER("_mysqlnd_plugin_get_plugin_result_metadata_data");
  464. DBG_INF_FMT("plugin_id=%u", plugin_id);
  465. if (!meta || plugin_id >= mysqlnd_plugin_count()) {
  466. return NULL;
  467. }
  468. DBG_RETURN((void *)((char *)meta + sizeof(MYSQLND_RES_METADATA) + plugin_id * sizeof(void *)));
  469. }
  470. /* }}} */
  471. /*
  472. * Local variables:
  473. * tab-width: 4
  474. * c-basic-offset: 4
  475. * End:
  476. * vim600: noet sw=4 ts=4 fdm=marker
  477. * vim<600: noet sw=4 ts=4
  478. */