PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/mysqli - mysqli_result_iterator.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 44 49 89.8 %
Date: 2014-10-16 Functions: 6 7 85.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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.c 299335 2010-05-13 11:05:09Z andrey $
      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           7 : 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           7 :         if (by_ref) {
      52           0 :                 zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
      53             :         }
      54           7 :         iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
      55             : 
      56             :         Z_ADDREF_P(object);
      57           7 :         iterator->intern.data = (void*)object;
      58           7 :         iterator->intern.funcs = &php_mysqli_result_iterator_funcs;
      59           7 :         iterator->result = (mysqli_object *) zend_object_store_get_object(object TSRMLS_CC);
      60           7 :         iterator->row_num = -1;
      61             : 
      62           7 :         return (zend_object_iterator*)iterator;
      63             : }
      64             : /* }}} */
      65             : 
      66             : 
      67             : /* {{{ */
      68           7 : static void php_mysqli_result_iterator_dtor(zend_object_iterator *iter TSRMLS_DC)
      69             : {
      70           7 :         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           7 :         if (iterator->intern.data) {
      74           7 :                 zval_ptr_dtor((zval**)&iterator->intern.data);
      75             :         }
      76           7 :         if (iterator->current_row) {
      77           5 :                 zval_ptr_dtor(&iterator->current_row);
      78             :         }
      79           7 :         efree(iterator);
      80           7 : }
      81             : /* }}} */
      82             : 
      83             : 
      84             : /* {{{ */
      85          37 : static int php_mysqli_result_iterator_valid(zend_object_iterator *iter TSRMLS_DC)
      86             : {
      87          37 :         php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
      88             : 
      89          37 :         return iterator->current_row && Z_TYPE_P(iterator->current_row) == IS_ARRAY ? SUCCESS : FAILURE;
      90             : }
      91             : /* }}} */
      92             : 
      93             : 
      94             : /* {{{ */
      95          30 : static void php_mysqli_result_iterator_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC)
      96             : {
      97          30 :         php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
      98             : 
      99          30 :         *data = &iterator->current_row;
     100          30 : }
     101             : /* }}} */
     102             : 
     103             : 
     104             : /* {{{ */
     105          35 : static void php_mysqli_result_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC)
     106             : {
     107             : 
     108          35 :         php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
     109          35 :         mysqli_object *intern = iterator->result;
     110             :         MYSQL_RES       *result;
     111             : 
     112          35 :         MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
     113          35 :         if (iterator->current_row) {
     114          30 :                 zval_ptr_dtor(&iterator->current_row);
     115             :         }
     116          35 :         MAKE_STD_ZVAL(iterator->current_row);
     117          35 :         php_mysqli_fetch_into_hash_aux(iterator->current_row, result, MYSQLI_ASSOC TSRMLS_CC);
     118          35 :         if (Z_TYPE_P(iterator->current_row) == IS_ARRAY) {
     119          30 :                 iterator->row_num++;
     120             :         }
     121             : }
     122             : /* }}} */
     123             : 
     124             : 
     125             : /* {{{ */
     126           7 : static void php_mysqli_result_iterator_rewind(zend_object_iterator *iter TSRMLS_DC)
     127             : {
     128           7 :         php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
     129           7 :         mysqli_object *intern = iterator->result;
     130             :         MYSQL_RES       *result;
     131             : 
     132           7 :         MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
     133             : 
     134           6 :         if (mysqli_result_is_unbuffered(result)) {
     135             : #if MYSQLI_USE_MYSQLND
     136           2 :                 if (result->unbuf->eof_reached) {
     137             : #else
     138             :                 if (result->eof) {
     139             : #endif
     140           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
     141           1 :                         return;
     142             :                 }
     143             :         } else {
     144           4 :                 mysql_data_seek(result, 0);
     145             :         }
     146           5 :         iterator->row_num = -1;
     147           5 :         php_mysqli_result_iterator_move_forward(iter TSRMLS_CC);
     148             : }
     149             : /* }}} */
     150             : 
     151             : 
     152             : /* {{{ php_mysqli_result_iterator_current_key */
     153           0 : 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           0 :         php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
     156             : 
     157           0 :         *int_key = (ulong) iterator->row_num;
     158           0 :         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             :  */

Generated by: LCOV version 1.10

Generated at Thu, 16 Oct 2014 05:27:04 +0000 (6 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.