Back to index

php5  5.3.10
mysqli_warning.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author: Georg Richter <georg@php.net>                                |
00016   +----------------------------------------------------------------------+
00017 
00018 */
00019 #ifdef HAVE_CONFIG_H
00020 #include "config.h"
00021 #endif
00022 
00023 #include <signal.h>
00024 
00025 #include "php.h"
00026 #include "php_ini.h"
00027 #include "ext/standard/info.h"
00028 #include "php_mysqli_structs.h"
00029 #include "mysqli_priv.h"
00030 
00031 /* Define these in the PHP5 tree to make merging easy process */
00032 #define ZSTR_DUPLICATE (1<<0)
00033 #define ZSTR_AUTOFREE  (1<<1)
00034 
00035 #define ZVAL_UTF8_STRING(z, s, flags)          ZVAL_STRING((z), (char*)(s), ((flags) & ZSTR_DUPLICATE))
00036 #define ZVAL_UTF8_STRINGL(z, s, l, flags)      ZVAL_STRINGL((z), (char*)(s), (l), ((flags) & ZSTR_DUPLICATE))
00037 
00038 
00039 /* {{{ void php_clear_warnings() */
00040 void php_clear_warnings(MYSQLI_WARNING *w)
00041 {
00042        MYSQLI_WARNING *n;
00043 
00044        while (w) {
00045               n = w;
00046               zval_dtor(&(w->reason));
00047               zval_dtor(&(w->sqlstate));
00048               w = w->next;
00049               efree(n);
00050        }
00051 }
00052 /* }}} */
00053 
00054 
00055 #ifndef MYSQLI_USE_MYSQLND
00056 /* {{{ MYSQLI_WARNING *php_new_warning */
00057 static
00058 MYSQLI_WARNING *php_new_warning(const char *reason, int errorno TSRMLS_DC)
00059 {
00060        MYSQLI_WARNING *w;
00061 
00062        w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
00063 
00064        ZVAL_UTF8_STRING(&(w->reason), reason, ZSTR_DUPLICATE);
00065 
00066        ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
00067 
00068        w->errorno = errorno;
00069 
00070        return w;
00071 }
00072 /* }}} */
00073 
00074 
00075 /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) */
00076 MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC)
00077 {
00078        MYSQLI_WARNING *w, *first = NULL, *prev = NULL;
00079        MYSQL_RES            *result;
00080        MYSQL_ROW            row;
00081 
00082        if (mysql_real_query(mysql, "SHOW WARNINGS", 13)) {
00083               return NULL;
00084        }
00085 
00086        result = mysql_store_result(mysql);
00087 
00088        while ((row = mysql_fetch_row(result))) {
00089               w = php_new_warning(row[2], atoi(row[1]) TSRMLS_CC);
00090               if (!first) {
00091                      first = w;
00092               }
00093               if (prev) {
00094                      prev->next = w;
00095               }
00096               prev = w;
00097        }
00098        mysql_free_result(result);
00099        return first;
00100 }
00101 /* }}} */
00102 #else
00103 /* {{{ MYSQLI_WARNING *php_new_warning */
00104 static
00105 MYSQLI_WARNING *php_new_warning(const zval *reason, int errorno TSRMLS_DC)
00106 {
00107        MYSQLI_WARNING *w;
00108 
00109        w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
00110 
00111        w->reason = *reason;
00112        zval_copy_ctor(&(w->reason));
00113 
00114        ZVAL_UTF8_STRINGL(&(w->reason),  Z_STRVAL(w->reason), Z_STRLEN(w->reason),  ZSTR_AUTOFREE);
00115 
00116        ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
00117 
00118        w->errorno = errorno;
00119 
00120        return w;
00121 }
00122 /* }}} */
00123 
00124 
00125 /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) */
00126 MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC)
00127 {
00128        MYSQLI_WARNING       *w, *first = NULL, *prev = NULL;
00129        MYSQL_RES            *result;
00130        zval                 *row;
00131 
00132        if (mysql_real_query(mysql, "SHOW WARNINGS", 13)) {
00133               return NULL;
00134        }
00135 
00136        result = mysql_use_result(mysql);
00137 
00138        for (;;) {
00139               zval **entry;
00140               int errno;
00141 
00142               MAKE_STD_ZVAL(row);
00143               mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, row, MYSQLND_MYSQLI);
00144               if (Z_TYPE_P(row) != IS_ARRAY) {
00145                      zval_ptr_dtor(&row);
00146                      break;
00147               }
00148               zend_hash_internal_pointer_reset(Z_ARRVAL_P(row));
00149               /* 0. we don't care about the first */
00150               zend_hash_move_forward(Z_ARRVAL_P(row));
00151 
00152               /* 1. Here comes the error no */
00153               zend_hash_get_current_data(Z_ARRVAL_P(row), (void **)&entry);
00154               convert_to_long_ex(entry);
00155               errno = Z_LVAL_PP(entry);
00156               zend_hash_move_forward(Z_ARRVAL_P(row));
00157 
00158               /* 2. Here comes the reason */
00159               zend_hash_get_current_data(Z_ARRVAL_P(row), (void **)&entry);
00160 
00161               w = php_new_warning(*entry, errno TSRMLS_CC);
00162               /*
00163                 Don't destroy entry, because the row destroy will decrease
00164                 the refcounter. Decreased twice then mysqlnd_free_result()
00165                 will crash, because it will try to access already freed memory.
00166               */
00167               if (!first) {
00168                      first = w;
00169               }
00170               if (prev) {
00171                      prev->next = (void *)w;
00172               }
00173               prev = w;
00174 
00175               zval_ptr_dtor(&row);
00176        }
00177 
00178        mysql_free_result(result);
00179        return first;
00180 }
00181 /* }}} */
00182 #endif
00183 
00184 
00185 /* {{{ bool mysqli_warning::next() */
00186 PHP_METHOD(mysqli_warning, next)
00187 {
00188        MYSQLI_WARNING       *w;
00189        zval                 *mysqli_warning;
00190        mysqli_object *obj = (mysqli_object *)zend_objects_get_address(getThis() TSRMLS_CC);
00191 
00192        if (obj->ptr) {
00193               if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00194                                                                        &mysqli_warning, mysqli_warning_class_entry) == FAILURE) {
00195                      return;
00196               }
00197 
00198               MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, &mysqli_warning, "mysqli_warning", MYSQLI_STATUS_VALID);
00199 
00200               if (w && w->next) {
00201                      w = w->next;
00202                ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
00203                      RETURN_TRUE;
00204               }
00205        }
00206        RETURN_FALSE;
00207 }
00208 /* }}} */
00209 
00210 
00211 /* {{{ property mysqli_warning_message */
00212 static
00213 int mysqli_warning_message(mysqli_object *obj, zval **retval TSRMLS_DC)
00214 {
00215        MYSQLI_WARNING *w;
00216 
00217        if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
00218               return FAILURE;
00219        }
00220 
00221        w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
00222        MAKE_STD_ZVAL(*retval);
00223        **retval = w->reason;
00224        zval_copy_ctor(*retval);
00225        return SUCCESS;
00226 }
00227 /* }}} */
00228 
00229 
00230 /* {{{ property mysqli_warning_sqlstate */
00231 static
00232 int mysqli_warning_sqlstate(mysqli_object *obj, zval **retval TSRMLS_DC)
00233 {
00234        MYSQLI_WARNING *w;
00235 
00236        if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
00237               return FAILURE;
00238        }
00239 
00240        w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
00241        MAKE_STD_ZVAL(*retval);
00242        **retval = w->sqlstate;
00243        zval_copy_ctor(*retval);
00244        return SUCCESS;
00245 }
00246 /* }}} */
00247 
00248 
00249 /* {{{ property mysqli_warning_error */
00250 static
00251 int mysqli_warning_errno(mysqli_object *obj, zval **retval TSRMLS_DC)
00252 {
00253        MYSQLI_WARNING *w;
00254 
00255        if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
00256               return FAILURE;
00257        }
00258        w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
00259        MAKE_STD_ZVAL(*retval);
00260        ZVAL_LONG(*retval, w->errorno);
00261        return SUCCESS;
00262 }
00263 /* }}} */
00264 
00265 /* {{{ mysqli_warning_construct(object obj) */
00266 PHP_METHOD(mysqli_warning, __construct)
00267 {
00268        zval                 *z;
00269        mysqli_object *obj;
00270        MYSQL                *hdl;
00271        MYSQLI_WARNING  *w;
00272        MYSQLI_RESOURCE *mysqli_resource;
00273 
00274        if (ZEND_NUM_ARGS() != 1) {
00275               WRONG_PARAM_COUNT;
00276        }
00277        if (zend_parse_parameters(1 TSRMLS_CC, "o", &z)==FAILURE) {
00278               return;
00279        }
00280        obj = (mysqli_object *)zend_object_store_get_object(z TSRMLS_CC);\
00281 
00282        if (obj->zo.ce == mysqli_link_class_entry) {
00283               MY_MYSQL *mysql;
00284               MYSQLI_FETCH_RESOURCE_CONN(mysql, &z, MYSQLI_STATUS_VALID);
00285               hdl = mysql->mysql;
00286        } else if (obj->zo.ce == mysqli_stmt_class_entry) {
00287               MY_STMT *stmt;
00288               MYSQLI_FETCH_RESOURCE_STMT(stmt, &z, MYSQLI_STATUS_VALID);
00289               hdl = mysqli_stmt_get_connection(stmt->stmt);
00290        } else {
00291               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid class argument");
00292               RETURN_FALSE;
00293        }
00294 
00295        if (mysql_warning_count(hdl)) {
00296               w = php_get_warnings(hdl TSRMLS_CC);
00297        } else {
00298               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No warnings found");
00299               RETURN_FALSE;
00300        }
00301 
00302        mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
00303        mysqli_resource->ptr = mysqli_resource->info = (void *)w;
00304        mysqli_resource->status = MYSQLI_STATUS_VALID;
00305 
00306        if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_warning_class_entry TSRMLS_CC)) {
00307               MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
00308        } else {
00309               ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr = mysqli_resource;
00310        }
00311 
00312 }
00313 /* }}} */
00314 
00315 /* {{{ mysqli_warning_methods */
00316 const zend_function_entry mysqli_warning_methods[] = {
00317        PHP_ME(mysqli_warning, __construct,              NULL, ZEND_ACC_PROTECTED)
00318        PHP_ME(mysqli_warning, next,                     NULL, ZEND_ACC_PUBLIC)
00319        {NULL, NULL, NULL}
00320 };
00321 /* }}} */
00322 
00323 /* {{{ mysqli_warning_property_entries */
00324 const mysqli_property_entry mysqli_warning_property_entries[] = {
00325        {"message", sizeof("message") - 1, mysqli_warning_message, NULL},
00326        {"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
00327        {"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
00328        {NULL, 0, NULL, NULL}
00329 };
00330 /* }}} */
00331 
00332 /* {{{ mysqli_warning_property_info_entries */
00333 zend_property_info mysqli_warning_property_info_entries[] = {
00334        {ZEND_ACC_PUBLIC, "message",       sizeof("message") - 1,      0, NULL, 0, NULL},
00335        {ZEND_ACC_PUBLIC, "sqlstate",      sizeof("sqlstate") - 1,     0, NULL, 0, NULL},
00336        {ZEND_ACC_PUBLIC, "errno",         sizeof("errno") - 1,        0, NULL, 0, NULL},
00337        {0,                                NULL,                0,                                 0, NULL, 0, NULL}
00338 };
00339 /* }}} */
00340 
00341 
00342 /*
00343  * Local variables:
00344  * tab-width: 4
00345  * c-basic-offset: 4
00346  * indent-tabs-mode: t
00347  * End:
00348  * vim600: noet sw=4 ts=4 fdm=marker
00349  * vim<600: noet sw=4 ts=4
00350  */