mysqli_result_iterator.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-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: Georg Richter <georg@php.net> |
  16. | Andrey Hristov <andrey@php.net> |
  17. | Ulf Wendel <uw@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <signal.h>
  24. #include "php.h"
  25. #include "php_ini.h"
  26. #include "php_mysqli_structs.h"
  27. #include "mysqli_priv.h"
  28. #include "zend_interfaces.h"
  29. extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
  30. typedef struct {
  31. zend_object_iterator intern;
  32. mysqli_object *result;
  33. zval current_row;
  34. my_longlong row_num;
  35. } php_mysqli_result_iterator;
  36. /* {{{ */
  37. zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
  38. {
  39. php_mysqli_result_iterator *iterator;
  40. if (by_ref) {
  41. zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
  42. }
  43. iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
  44. zend_iterator_init(&iterator->intern);
  45. ZVAL_COPY(&iterator->intern.data, 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. #if 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. };
  129. /* }}} */
  130. /*
  131. * Local variables:
  132. * tab-width: 4
  133. * c-basic-offset: 4
  134. * End:
  135. * vim600: noet sw=4 ts=4 fdm=marker
  136. * vim<600: noet sw=4 ts=4
  137. */