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_warning.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 75 103 72.8 %
Date: 2014-08-04 Functions: 7 8 87.5 %
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             :   | Author: Georg Richter <georg@php.net>                                |
      16             :   +----------------------------------------------------------------------+
      17             : 
      18             : */
      19             : #ifdef HAVE_CONFIG_H
      20             : #include "config.h"
      21             : #endif
      22             : 
      23             : #include <signal.h>
      24             : 
      25             : #include "php.h"
      26             : #include "php_ini.h"
      27             : #include "ext/standard/info.h"
      28             : #include "php_mysqli_structs.h"
      29             : #include "mysqli_priv.h"
      30             : 
      31             : /* Define these in the PHP5 tree to make merging easy process */
      32             : #define ZSTR_DUPLICATE (1<<0)
      33             : #define ZSTR_AUTOFREE  (1<<1)
      34             : 
      35             : #define ZVAL_UTF8_STRING(z, s, flags)          ZVAL_STRING((z), (char*)(s), ((flags) & ZSTR_DUPLICATE))
      36             : #define ZVAL_UTF8_STRINGL(z, s, l, flags)      ZVAL_STRINGL((z), (char*)(s), (l), ((flags) & ZSTR_DUPLICATE))
      37             : 
      38             : 
      39             : /* {{{ void php_clear_warnings() */
      40           5 : void php_clear_warnings(MYSQLI_WARNING *w)
      41             : {
      42             :         MYSQLI_WARNING *n;
      43             : 
      44          16 :         while (w) {
      45           6 :                 n = w;
      46           6 :                 zval_dtor(&(w->reason));
      47           6 :                 zval_dtor(&(w->sqlstate));
      48           6 :                 w = w->next;
      49           6 :                 efree(n);
      50             :         }
      51           5 : }
      52             : /* }}} */
      53             : 
      54             : 
      55             : #ifndef MYSQLI_USE_MYSQLND
      56             : /* {{{ MYSQLI_WARNING *php_new_warning */
      57             : static
      58             : MYSQLI_WARNING *php_new_warning(const char *reason, int errorno TSRMLS_DC)
      59             : {
      60             :         MYSQLI_WARNING *w;
      61             : 
      62             :         w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
      63             : 
      64             :         ZVAL_UTF8_STRING(&(w->reason), reason, ZSTR_DUPLICATE);
      65             : 
      66             :         ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
      67             : 
      68             :         w->errorno = errorno;
      69             : 
      70             :         return w;
      71             : }
      72             : /* }}} */
      73             : 
      74             : 
      75             : /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) */
      76             : MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC)
      77             : {
      78             :         MYSQLI_WARNING *w, *first = NULL, *prev = NULL;
      79             :         MYSQL_RES               *result;
      80             :         MYSQL_ROW               row;
      81             : 
      82             :         if (mysql_real_query(mysql, "SHOW WARNINGS", 13)) {
      83             :                 return NULL;
      84             :         }
      85             : 
      86             :         result = mysql_store_result(mysql);
      87             : 
      88             :         while ((row = mysql_fetch_row(result))) {
      89             :                 w = php_new_warning(row[2], atoi(row[1]) TSRMLS_CC);
      90             :                 if (!first) {
      91             :                         first = w;
      92             :                 }
      93             :                 if (prev) {
      94             :                         prev->next = w;
      95             :                 }
      96             :                 prev = w;
      97             :         }
      98             :         mysql_free_result(result);
      99             :         return first;
     100             : }
     101             : /* }}} */
     102             : #else
     103             : /* {{{ MYSQLI_WARNING *php_new_warning */
     104             : static
     105           6 : MYSQLI_WARNING *php_new_warning(const zval * reason, int errorno TSRMLS_DC)
     106             : {
     107             :         MYSQLI_WARNING *w;
     108             : 
     109           6 :         w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
     110             : 
     111           6 :         w->reason = *reason;
     112           6 :         zval_copy_ctor(&(w->reason));
     113             : 
     114           6 :         ZVAL_UTF8_STRINGL(&(w->reason),  Z_STRVAL(w->reason), Z_STRLEN(w->reason),  ZSTR_AUTOFREE);
     115             : 
     116           6 :         ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
     117             : 
     118           6 :         w->errorno = errorno;
     119             : 
     120           6 :         return w;
     121             : }
     122             : /* }}} */
     123             : 
     124             : 
     125             : /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) */
     126           6 : MYSQLI_WARNING * php_get_warnings(MYSQLND_CONN_DATA * mysql TSRMLS_DC)
     127             : {
     128           6 :         MYSQLI_WARNING  *w, *first = NULL, *prev = NULL;
     129             :         MYSQL_RES               *result;
     130             :         zval                    *row;
     131             : 
     132           6 :         if (mysql->m->query(mysql, "SHOW WARNINGS", 13 TSRMLS_CC)) {
     133           1 :                 return NULL;
     134             :         }
     135             : 
     136           5 :         result = mysql->m->use_result(mysql, 0 TSRMLS_CC);
     137             : 
     138             :         for (;;) {
     139             :                 zval **entry;
     140             :                 int errno;
     141             : 
     142          11 :                 MAKE_STD_ZVAL(row);
     143          11 :                 mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, row, MYSQLND_MYSQLI);
     144          11 :                 if (Z_TYPE_P(row) != IS_ARRAY) {
     145           5 :                         zval_ptr_dtor(&row);
     146           5 :                         break;
     147             :                 }
     148           6 :                 zend_hash_internal_pointer_reset(Z_ARRVAL_P(row));
     149             :                 /* 0. we don't care about the first */
     150           6 :                 zend_hash_move_forward(Z_ARRVAL_P(row));
     151             : 
     152             :                 /* 1. Here comes the error no */
     153           6 :                 zend_hash_get_current_data(Z_ARRVAL_P(row), (void **)&entry);
     154          36 :                 convert_to_long_ex(entry);
     155           6 :                 errno = Z_LVAL_PP(entry);
     156           6 :                 zend_hash_move_forward(Z_ARRVAL_P(row));
     157             : 
     158             :                 /* 2. Here comes the reason */
     159           6 :                 zend_hash_get_current_data(Z_ARRVAL_P(row), (void **)&entry);
     160             : 
     161           6 :                 w = php_new_warning(*entry, errno TSRMLS_CC);
     162             :                 /*
     163             :                   Don't destroy entry, because the row destroy will decrease
     164             :                   the refcounter. Decreased twice then mysqlnd_free_result()
     165             :                   will crash, because it will try to access already freed memory.
     166             :                 */
     167           6 :                 if (!first) {
     168           5 :                         first = w;
     169             :                 }
     170           6 :                 if (prev) {
     171           1 :                         prev->next = (void *)w;
     172             :                 }
     173           6 :                 prev = w;
     174             : 
     175           6 :                 zval_ptr_dtor(&row);
     176           6 :         }
     177             : 
     178           5 :         mysql_free_result(result);
     179           5 :         return first;
     180             : }
     181             : /* }}} */
     182             : #endif
     183             : 
     184             : 
     185             : /* {{{ bool mysqli_warning::next() */
     186           5 : PHP_METHOD(mysqli_warning, next)
     187             : {
     188             :         MYSQLI_WARNING  *w;
     189             :         zval                    *mysqli_warning;
     190           5 :         mysqli_object *obj = (mysqli_object *)zend_objects_get_address(getThis() TSRMLS_CC);
     191             : 
     192           5 :         if (obj->ptr) {
     193           5 :                 if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
     194             :                                                                                  &mysqli_warning, mysqli_warning_class_entry) == FAILURE) {
     195           0 :                         return;
     196             :                 }
     197             : 
     198           5 :                 MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, &mysqli_warning, "mysqli_warning", MYSQLI_STATUS_VALID);
     199             : 
     200           5 :                 if (w && w->next) {
     201           1 :                         w = w->next;
     202           1 :                 ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
     203           1 :                         RETURN_TRUE;
     204             :                 }
     205             :         }
     206           4 :         RETURN_FALSE;
     207             : }
     208             : /* }}} */
     209             : 
     210             : 
     211             : /* {{{ property mysqli_warning_message */
     212             : static
     213           6 : int mysqli_warning_message(mysqli_object *obj, zval **retval TSRMLS_DC)
     214             : {
     215             :         MYSQLI_WARNING *w;
     216             : 
     217           6 :         if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
     218           1 :                 return FAILURE;
     219             :         }
     220             : 
     221           5 :         w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
     222           5 :         MAKE_STD_ZVAL(*retval);
     223           5 :         **retval = w->reason;
     224           5 :         zval_copy_ctor(*retval);
     225           5 :         return SUCCESS;
     226             : }
     227             : /* }}} */
     228             : 
     229             : 
     230             : /* {{{ property mysqli_warning_sqlstate */
     231             : static
     232           4 : int mysqli_warning_sqlstate(mysqli_object *obj, zval **retval TSRMLS_DC)
     233             : {
     234             :         MYSQLI_WARNING *w;
     235             : 
     236           4 :         if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
     237           0 :                 return FAILURE;
     238             :         }
     239             : 
     240           4 :         w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
     241           4 :         MAKE_STD_ZVAL(*retval);
     242           4 :         **retval = w->sqlstate;
     243           4 :         zval_copy_ctor(*retval);
     244           4 :         return SUCCESS;
     245             : }
     246             : /* }}} */
     247             : 
     248             : 
     249             : /* {{{ property mysqli_warning_error */
     250             : static
     251           6 : int mysqli_warning_errno(mysqli_object *obj, zval **retval TSRMLS_DC)
     252             : {
     253             :         MYSQLI_WARNING *w;
     254             : 
     255           6 :         if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
     256           1 :                 return FAILURE;
     257             :         }
     258           5 :         w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
     259           5 :         MAKE_STD_ZVAL(*retval);
     260           5 :         ZVAL_LONG(*retval, w->errorno);
     261           5 :         return SUCCESS;
     262             : }
     263             : /* }}} */
     264             : 
     265             : /* {{{ mysqli_warning_construct(object obj) */
     266           0 : PHP_METHOD(mysqli_warning, __construct)
     267             : {
     268             :         zval                    *z;
     269             :         mysqli_object   *obj;
     270             : #ifndef MYSQLI_USE_MYSQLND
     271             :         MYSQL                   *hdl;
     272             : #endif
     273             :         MYSQLI_WARNING  *w;
     274             :         MYSQLI_RESOURCE *mysqli_resource;
     275             : 
     276           0 :         if (ZEND_NUM_ARGS() != 1) {
     277           0 :                 WRONG_PARAM_COUNT;
     278             :         }
     279           0 :         if (zend_parse_parameters(1 TSRMLS_CC, "o", &z)==FAILURE) {
     280           0 :                 return;
     281             :         }
     282           0 :         obj = (mysqli_object *)zend_object_store_get_object(z TSRMLS_CC);\
     283             : 
     284           0 :         if (obj->zo.ce == mysqli_link_class_entry) {
     285             :                 MY_MYSQL *mysql;
     286           0 :                 MYSQLI_FETCH_RESOURCE_CONN(mysql, &z, MYSQLI_STATUS_VALID);
     287           0 :                 if (mysql_warning_count(mysql->mysql)) {
     288             : #ifndef MYSQLI_USE_MYSQLND
     289             :                         w = php_get_warnings(mysql->mysql TSRMLS_CC);
     290             : #else
     291           0 :                         w = php_get_warnings(mysql->mysql->data TSRMLS_CC);
     292             : #endif
     293             :                 } else {
     294           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "No warnings found");
     295           0 :                         RETURN_FALSE;
     296             :                 }
     297           0 :         } else if (obj->zo.ce == mysqli_stmt_class_entry) {
     298             :                 MY_STMT *stmt;
     299           0 :                 MYSQLI_FETCH_RESOURCE_STMT(stmt, &z, MYSQLI_STATUS_VALID);
     300             : #ifndef MYSQLI_USE_MYSQLND
     301             :                 hdl = mysqli_stmt_get_connection(stmt->stmt);
     302             :                 if (mysql_warning_count(hdl)) {
     303             :                         w = php_get_warnings(hdl TSRMLS_CC);
     304             : #else
     305           0 :                 if (mysqlnd_stmt_warning_count(stmt->stmt)) {
     306           0 :                         w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt) TSRMLS_CC);
     307             : #endif
     308             :                 } else {
     309           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "No warnings found");
     310           0 :                         RETURN_FALSE;
     311             :                 }
     312             :         } else {
     313           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid class argument");
     314           0 :                 RETURN_FALSE;
     315             :         }
     316             : 
     317           0 :         mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
     318           0 :         mysqli_resource->ptr = mysqli_resource->info = (void *)w;
     319           0 :         mysqli_resource->status = MYSQLI_STATUS_VALID;
     320             : 
     321           0 :         if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_warning_class_entry TSRMLS_CC)) {
     322           0 :                 MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
     323             :         } else {
     324           0 :                 ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr = mysqli_resource;
     325             :         }
     326             : 
     327             : }
     328             : /* }}} */
     329             : 
     330             : /* {{{ mysqli_warning_methods */
     331             : const zend_function_entry mysqli_warning_methods[] = {
     332             :         PHP_ME(mysqli_warning, __construct,             NULL, ZEND_ACC_PROTECTED)
     333             :         PHP_ME(mysqli_warning, next,                    NULL, ZEND_ACC_PUBLIC)
     334             :         {NULL, NULL, NULL}
     335             : };
     336             : /* }}} */
     337             : 
     338             : /* {{{ mysqli_warning_property_entries */
     339             : const mysqli_property_entry mysqli_warning_property_entries[] = {
     340             :         {"message", sizeof("message") - 1, mysqli_warning_message, NULL},
     341             :         {"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
     342             :         {"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
     343             :         {NULL, 0, NULL, NULL}
     344             : };
     345             : /* }}} */
     346             : 
     347             : /* {{{ mysqli_warning_property_info_entries */
     348             : const zend_property_info mysqli_warning_property_info_entries[] = {
     349             :         {ZEND_ACC_PUBLIC, "message",  sizeof("message") - 1,        -1, 0, NULL, 0, NULL},
     350             :         {ZEND_ACC_PUBLIC, "sqlstate", sizeof("sqlstate") - 1,       -1, 0, NULL, 0, NULL},
     351             :         {ZEND_ACC_PUBLIC, "errno",            sizeof("errno") - 1,  -1, 0, NULL, 0, NULL},
     352             :         {0,                                     NULL,                   0,                                      -1, 0, NULL, 0, NULL}
     353             : };
     354             : /* }}} */
     355             : 
     356             : 
     357             : /*
     358             :  * Local variables:
     359             :  * tab-width: 4
     360             :  * c-basic-offset: 4
     361             :  * indent-tabs-mode: t
     362             :  * End:
     363             :  * vim600: noet sw=4 ts=4 fdm=marker
     364             :  * vim<600: noet sw=4 ts=4
     365             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:07 +0000 (16 days ago)

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