diff options
author | Andrey Hristov <andrey@php.net> | 2007-10-05 21:23:56 +0000 |
---|---|---|
committer | Andrey Hristov <andrey@php.net> | 2007-10-05 21:23:56 +0000 |
commit | 8b9b553aa264d3c0afe611f49499e787004983c7 (patch) | |
tree | 840ea2b1127ec97a63f36ffda689a1a1fe5fb975 /ext/mysqli/mysqli_warning.c | |
parent | 9f9495a48483aac9f6bc92ceea5841a8d243e2aa (diff) | |
download | php-git-8b9b553aa264d3c0afe611f49499e787004983c7.tar.gz |
Import mysqlnd
Patch ext/mysql and ext/mysqli to support mysqlnd
Diffstat (limited to 'ext/mysqli/mysqli_warning.c')
-rw-r--r-- | ext/mysqli/mysqli_warning.c | 162 |
1 files changed, 133 insertions, 29 deletions
diff --git a/ext/mysqli/mysqli_warning.c b/ext/mysqli/mysqli_warning.c index 92dc8ce327..7b02d9117a 100644 --- a/ext/mysqli/mysqli_warning.c +++ b/ext/mysqli/mysqli_warning.c @@ -25,55 +25,144 @@ #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" -#include "php_mysqli.h" +#include "php_mysqli_structs.h" + +/* Define these in the PHP5 tree to make merging easy process */ +#define ZSTR_DUPLICATE (1<<0) +#define ZSTR_AUTOFREE (1<<1) + +#define ZVAL_UTF8_STRING(z, s, flags) ZVAL_STRING((z), (char*)(s), ((flags) & ZSTR_DUPLICATE)) +#define ZVAL_UTF8_STRINGL(z, s, l, flags) ZVAL_STRINGL((z), (char*)(s), (l), ((flags) & ZSTR_DUPLICATE)) + /* {{{ void php_clear_warnings() */ void php_clear_warnings(MYSQLI_WARNING *w) { - MYSQLI_WARNING *n; + MYSQLI_WARNING *n; while (w) { n = w; - efree(w->reason); + zval_dtor(&(w->reason)); + zval_dtor(&(w->sqlstate)); w = w->next; efree(n); } } /* }}} */ + +#ifndef HAVE_MYSQLND /* {{{ MYSQLI_WARNING *php_new_warning */ -MYSQLI_WARNING *php_new_warning(char *reason, char *sqlstate, int errorno) +static +MYSQLI_WARNING *php_new_warning(const char *reason, int errorno TSRMLS_DC) { - MYSQLI_WARNING *w; + MYSQLI_WARNING *w; w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING)); - w->reason = safe_estrdup(reason); - if (sqlstate) { - strcpy(w->sqlstate, sqlstate); - } else { - strcpy(w->sqlstate, "00000"); - } + ZVAL_UTF8_STRING(&(w->reason), reason, ZSTR_DUPLICATE); + + ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1, ZSTR_DUPLICATE); + w->errorno = errorno; return w; } /* }}} */ -/* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */ -MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) + +/* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) */ +MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) { - MYSQLI_WARNING *w, *first = NULL, *prev = NULL; + MYSQLI_WARNING *w, *first = NULL, *prev = NULL; MYSQL_RES *result; MYSQL_ROW row; - if (mysql_query(mysql, "SHOW WARNINGS")) { + if (mysql_real_query(mysql, "SHOW WARNINGS", 13)) { return NULL; } result = mysql_store_result(mysql); + while ((row = mysql_fetch_row(result))) { - w = php_new_warning(row[2], "HY000", atoi(row[1])); + w = php_new_warning(row[2], atoi(row[1]) TSRMLS_CC); + if (!first) { + first = w; + } + if (prev) { + prev->next = w; + } + prev = w; + } + mysql_free_result(result); + return first; +} +/* }}} */ +#else +/* {{{ MYSQLI_WARNING *php_new_warning */ +static +MYSQLI_WARNING *php_new_warning(const zval *reason, int errorno TSRMLS_DC) +{ + MYSQLI_WARNING *w; + + w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING)); + + w->reason = *reason; + zval_copy_ctor(&(w->reason)); + + ZVAL_UTF8_STRINGL(&(w->reason), Z_STRVAL(w->reason), Z_STRLEN(w->reason), ZSTR_AUTOFREE); + + ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1, ZSTR_DUPLICATE); + + w->errorno = errorno; + + return w; +} +/* }}} */ + + +/* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) */ +MYSQLI_WARNING *php_get_warnings(MYSQL *mysql TSRMLS_DC) +{ + MYSQLI_WARNING *w, *first = NULL, *prev = NULL; + MYSQL_RES *result; + zval *row; + + if (mysql_real_query(mysql, "SHOW WARNINGS", 13)) { + return NULL; + } + + result = mysql_use_result(mysql); + + for (;;) { + zval **entry; + int errno; + + MAKE_STD_ZVAL(row); + mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, row, MYSQLND_MYSQLI); + if (Z_TYPE_P(row) != IS_ARRAY) { + zval_ptr_dtor(&row); + break; + } + zend_hash_internal_pointer_reset(Z_ARRVAL_P(row)); + /* 0. we don't care about the first */ + zend_hash_move_forward(Z_ARRVAL_P(row)); + + /* 1. Here comes the error no */ + zend_hash_get_current_data(Z_ARRVAL_P(row), (void **)&entry); + convert_to_long_ex(entry); + errno = Z_LVAL_PP(entry); + zend_hash_move_forward(Z_ARRVAL_P(row)); + + /* 2. Here comes the reason */ + zend_hash_get_current_data(Z_ARRVAL_P(row), (void **)&entry); + + w = php_new_warning(*entry, errno TSRMLS_CC); + /* + Don't destroy entry, because the row destroy will decrease + the refcounter. Decreased twice then mysqlnd_free_result() + will crash, because it will try to access already freed memory. + */ if (!first) { first = w; } @@ -81,11 +170,16 @@ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) prev->next = (void *)w; } prev = w; + + zval_ptr_dtor(&row); } + mysql_free_result(result); return first; } /* }}} */ +#endif + /* {{{ bool mysqli_warning::next() */ PHP_METHOD(mysqli_warning, next) @@ -112,7 +206,9 @@ PHP_METHOD(mysqli_warning, next) } /* }}} */ + /* {{{ property mysqli_warning_message */ +static int mysqli_warning_message(mysqli_object *obj, zval **retval TSRMLS_DC) { MYSQLI_WARNING *w; @@ -122,17 +218,16 @@ int mysqli_warning_message(mysqli_object *obj, zval **retval TSRMLS_DC) } w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr; - ALLOC_ZVAL(*retval); - if (w->reason) { - ZVAL_STRING(*retval, w->reason, 1); - } else { - ZVAL_NULL(*retval); - } + MAKE_STD_ZVAL(*retval); + **retval = w->reason; + zval_copy_ctor(*retval); return SUCCESS; } /* }}} */ + /* {{{ property mysqli_warning_sqlstate */ +static int mysqli_warning_sqlstate(mysqli_object *obj, zval **retval TSRMLS_DC) { MYSQLI_WARNING *w; @@ -142,13 +237,16 @@ int mysqli_warning_sqlstate(mysqli_object *obj, zval **retval TSRMLS_DC) } w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr; - ALLOC_ZVAL(*retval); - ZVAL_STRING(*retval, w->sqlstate, 1); + MAKE_STD_ZVAL(*retval); + **retval = w->sqlstate; + zval_copy_ctor(*retval); return SUCCESS; } /* }}} */ + /* {{{ property mysqli_warning_error */ +static int mysqli_warning_errno(mysqli_object *obj, zval **retval TSRMLS_DC) { MYSQLI_WARNING *w; @@ -157,7 +255,7 @@ int mysqli_warning_errno(mysqli_object *obj, zval **retval TSRMLS_DC) return FAILURE; } w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr; - ALLOC_ZVAL(*retval); + MAKE_STD_ZVAL(*retval); ZVAL_LONG(*retval, w->errorno); return SUCCESS; } @@ -187,20 +285,22 @@ PHP_METHOD(mysqli_warning, __construct) } else if (obj->zo.ce == mysqli_stmt_class_entry) { MY_STMT *stmt; MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &z, "mysqli_stmt", MYSQLI_STATUS_VALID); - hdl = stmt->stmt->mysql; + hdl = mysqli_stmt_get_connection(stmt->stmt); } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid class argument"); RETURN_FALSE; } if (mysql_warning_count(hdl)) { - w = php_get_warnings(hdl); + w = php_get_warnings(hdl TSRMLS_CC); } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "No warnings found"); RETURN_FALSE; } mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE)); - mysqli_resource->status = MYSQLI_STATUS_VALID; mysqli_resource->ptr = mysqli_resource->info = (void *)w; + mysqli_resource->status = MYSQLI_STATUS_VALID; if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_warning_class_entry TSRMLS_CC)) { MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry); @@ -211,18 +311,22 @@ PHP_METHOD(mysqli_warning, __construct) } /* }}} */ +/* {{{ mysqli_warning_methods */ const zend_function_entry mysqli_warning_methods[] = { PHP_ME(mysqli_warning, __construct, NULL, ZEND_ACC_PROTECTED) PHP_ME(mysqli_warning, next, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; +/* }}} */ -mysqli_property_entry mysqli_warning_property_entries[] = { +/* {{{ mysqli_warning_property_entries */ +const mysqli_property_entry mysqli_warning_property_entries[] = { {"message", mysqli_warning_message, NULL}, {"sqlstate", mysqli_warning_sqlstate, NULL}, {"errno", mysqli_warning_errno, NULL}, {NULL, NULL, NULL} }; +/* }}} */ /* * Local variables: |