File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / mysqli / mysqli_result_iterator.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 20:03:51 2014 UTC (10 years ago) by misho
Branches: php, MAIN
CVS tags: v5_4_29, HEAD
php 5.4.29

    1: /*
    2:   +----------------------------------------------------------------------+
    3:   | PHP Version 5                                                        |
    4:   +----------------------------------------------------------------------+
    5:   | Copyright (c) 1997-2014 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:   $Id: mysqli_result_iterator.c,v 1.1.1.3 2014/06/15 20:03:51 misho Exp $
   21: */
   22: 
   23: #ifdef HAVE_CONFIG_H
   24: #include "config.h"
   25: #endif
   26: 
   27: #include <signal.h>
   28: 
   29: #include "php.h"
   30: #include "php_ini.h"
   31: #include "php_mysqli_structs.h"
   32: #include "mysqli_priv.h"
   33: #include "zend_interfaces.h"
   34: 
   35: 
   36: extern zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
   37: 
   38: typedef struct {
   39: 	zend_object_iterator  intern;
   40: 	mysqli_object *result;
   41: 	zval *current_row;
   42: 	my_longlong row_num;
   43: } php_mysqli_result_iterator;
   44: 
   45: 
   46: /* {{{ */
   47: zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
   48: {
   49: 	php_mysqli_result_iterator *iterator;
   50: 
   51: 	if (by_ref) {
   52: 		zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
   53: 	}
   54: 	iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
   55: 
   56: 	Z_ADDREF_P(object);
   57: 	iterator->intern.data = (void*)object;
   58: 	iterator->intern.funcs = &php_mysqli_result_iterator_funcs;
   59: 	iterator->result = (mysqli_object *) zend_object_store_get_object(object TSRMLS_CC);
   60: 	iterator->row_num = -1;
   61: 
   62: 	return (zend_object_iterator*)iterator;
   63: }
   64: /* }}} */
   65: 
   66: 
   67: /* {{{ */
   68: static void php_mysqli_result_iterator_dtor(zend_object_iterator *iter TSRMLS_DC)
   69: {
   70: 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
   71: 
   72: 	/* cleanup handled in sxe_object_dtor as we dont always have an iterator wrapper */
   73: 	if (iterator->intern.data) {
   74: 		zval_ptr_dtor((zval**)&iterator->intern.data);
   75: 	}
   76: 	if (iterator->current_row) {
   77: 		zval_ptr_dtor(&iterator->current_row);
   78: 	}
   79: 	efree(iterator);
   80: }
   81: /* }}} */
   82: 
   83: 
   84: /* {{{ */
   85: static int php_mysqli_result_iterator_valid(zend_object_iterator *iter TSRMLS_DC)
   86: {
   87: 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
   88: 
   89: 	return iterator->current_row && Z_TYPE_P(iterator->current_row) == IS_ARRAY ? SUCCESS : FAILURE;
   90: }
   91: /* }}} */
   92: 
   93: 
   94: /* {{{ */
   95: static void php_mysqli_result_iterator_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC)
   96: {
   97: 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
   98: 
   99: 	*data = &iterator->current_row;
  100: }
  101: /* }}} */
  102: 
  103: 
  104: /* {{{ */
  105: static void php_mysqli_result_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC)
  106: {
  107: 
  108: 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  109: 	mysqli_object *intern = iterator->result;
  110: 	MYSQL_RES	*result;
  111: 
  112: 	MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
  113: 	if (iterator->current_row) {
  114: 		zval_ptr_dtor(&iterator->current_row);
  115: 	}
  116: 	MAKE_STD_ZVAL(iterator->current_row);
  117: 	php_mysqli_fetch_into_hash_aux(iterator->current_row, result, MYSQLI_ASSOC TSRMLS_CC);
  118: 	if (Z_TYPE_P(iterator->current_row) == IS_ARRAY) {
  119: 		iterator->row_num++;
  120: 	}
  121: }
  122: /* }}} */
  123: 
  124: 
  125: /* {{{ */
  126: static void php_mysqli_result_iterator_rewind(zend_object_iterator *iter TSRMLS_DC)
  127: {
  128: 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  129: 	mysqli_object *intern = iterator->result;
  130: 	MYSQL_RES	*result;
  131: 
  132: 	MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
  133: 
  134: 	if (mysqli_result_is_unbuffered(result)) {
  135: #if MYSQLI_USE_MYSQLND
  136: 		if (result->unbuf->eof_reached) {
  137: #else
  138: 		if (result->eof) {
  139: #endif
  140: 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
  141: 			return;
  142: 		}
  143: 	} else {
  144: 		mysql_data_seek(result, 0);
  145: 	}
  146: 	iterator->row_num = -1;
  147: 	php_mysqli_result_iterator_move_forward(iter TSRMLS_CC);
  148: }
  149: /* }}} */
  150: 
  151: 
  152: /* {{{ php_mysqli_result_iterator_current_key */
  153: static int php_mysqli_result_iterator_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
  154: {
  155: 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
  156: 
  157: 	*int_key = (ulong) iterator->row_num;
  158: 	return HASH_KEY_IS_LONG;
  159: }
  160: /* }}} */
  161: 
  162: 
  163: /* {{{ php_mysqli_result_iterator_funcs */
  164: zend_object_iterator_funcs php_mysqli_result_iterator_funcs = {
  165: 	php_mysqli_result_iterator_dtor,
  166: 	php_mysqli_result_iterator_valid,
  167: 	php_mysqli_result_iterator_current_data,
  168: 	php_mysqli_result_iterator_current_key,
  169: 	php_mysqli_result_iterator_move_forward,
  170: 	php_mysqli_result_iterator_rewind,
  171: };
  172: /* }}} */
  173: 
  174: 
  175: /*
  176:  * Local variables:
  177:  * tab-width: 4
  178:  * c-basic-offset: 4
  179:  * End:
  180:  * vim600: noet sw=4 ts=4 fdm=marker
  181:  * vim<600: noet sw=4 ts=4
  182:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>