mysqli_result_iterator.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: Georg Richter <georg@php.net> |
  14. | Andrey Hristov <andrey@php.net> |
  15. | Ulf Wendel <uw@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <signal.h>
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "php_mysqli_structs.h"
  25. #include "mysqli_priv.h"
  26. #include "zend_interfaces.h"
  27. extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
  28. typedef struct {
  29. zend_object_iterator intern;
  30. mysqli_object *result;
  31. zval current_row;
  32. my_longlong row_num;
  33. } php_mysqli_result_iterator;
  34. /* {{{ */
  35. zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
  36. {
  37. php_mysqli_result_iterator *iterator;
  38. if (by_ref) {
  39. zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
  40. return NULL;
  41. }
  42. iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
  43. zend_iterator_init(&iterator->intern);
  44. Z_ADDREF_P(object);
  45. ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
  46. iterator->intern.funcs = &php_mysqli_result_iterator_funcs;
  47. iterator->result = Z_MYSQLI_P(object);
  48. iterator->row_num = -1;
  49. return &iterator->intern;
  50. }
  51. /* }}} */
  52. /* {{{ */
  53. static void php_mysqli_result_iterator_dtor(zend_object_iterator *iter)
  54. {
  55. php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*)iter;
  56. /* cleanup handled in sxe_object_dtor as we don't always have an iterator wrapper */
  57. zval_ptr_dtor(&iterator->intern.data);
  58. zval_ptr_dtor(&iterator->current_row);
  59. }
  60. /* }}} */
  61. /* {{{ */
  62. static int php_mysqli_result_iterator_valid(zend_object_iterator *iter)
  63. {
  64. php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  65. return Z_TYPE(iterator->current_row) == IS_ARRAY ? SUCCESS : FAILURE;
  66. }
  67. /* }}} */
  68. /* {{{ */
  69. static zval *php_mysqli_result_iterator_current_data(zend_object_iterator *iter)
  70. {
  71. php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  72. return &iterator->current_row;
  73. }
  74. /* }}} */
  75. /* {{{ */
  76. static void php_mysqli_result_iterator_move_forward(zend_object_iterator *iter)
  77. {
  78. php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  79. mysqli_object *intern = iterator->result;
  80. MYSQL_RES *result;
  81. MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
  82. zval_ptr_dtor(&iterator->current_row);
  83. php_mysqli_fetch_into_hash_aux(&iterator->current_row, result, MYSQLI_ASSOC);
  84. if (Z_TYPE(iterator->current_row) == IS_ARRAY) {
  85. iterator->row_num++;
  86. }
  87. }
  88. /* }}} */
  89. /* {{{ */
  90. static void php_mysqli_result_iterator_rewind(zend_object_iterator *iter)
  91. {
  92. php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  93. mysqli_object *intern = iterator->result;
  94. MYSQL_RES *result;
  95. MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
  96. if (mysqli_result_is_unbuffered(result)) {
  97. #ifdef MYSQLI_USE_MYSQLND
  98. if (result->unbuf->eof_reached) {
  99. #else
  100. if (result->eof) {
  101. #endif
  102. php_error_docref(NULL, E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
  103. return;
  104. }
  105. } else {
  106. mysql_data_seek(result, 0);
  107. }
  108. iterator->row_num = -1;
  109. php_mysqli_result_iterator_move_forward(iter);
  110. }
  111. /* }}} */
  112. /* {{{ php_mysqli_result_iterator_current_key */
  113. static void php_mysqli_result_iterator_current_key(zend_object_iterator *iter, zval *key)
  114. {
  115. php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  116. ZVAL_LONG(key, iterator->row_num);
  117. }
  118. /* }}} */
  119. /* {{{ php_mysqli_result_iterator_funcs */
  120. const zend_object_iterator_funcs php_mysqli_result_iterator_funcs = {
  121. php_mysqli_result_iterator_dtor,
  122. php_mysqli_result_iterator_valid,
  123. php_mysqli_result_iterator_current_data,
  124. php_mysqli_result_iterator_current_key,
  125. php_mysqli_result_iterator_move_forward,
  126. php_mysqli_result_iterator_rewind,
  127. NULL,
  128. NULL, /* get_gc */
  129. };
  130. /* }}} */