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

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:17 +0000 (44 hours ago)

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