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: 68 96 70.8 %
Date: 2014-12-13 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:15 +0000 (6 days ago)

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