diff options
author | Georg Richter <georg@php.net> | 2005-06-22 09:12:43 +0000 |
---|---|---|
committer | Georg Richter <georg@php.net> | 2005-06-22 09:12:43 +0000 |
commit | 6fba73e8e26322f9214b225e6ca55a2a56435dd5 (patch) | |
tree | 065528b15a0c68e7223ce6a5b58a7f29bf840b2f /ext/mysqli | |
parent | 372c38a53a050f19c6ebf1f5696f1ee78f0388a3 (diff) | |
download | php-git-6fba73e8e26322f9214b225e6ca55a2a56435dd5.tar.gz |
changed/fixed mysqli_warning class
added prefetch support for cursors
Diffstat (limited to 'ext/mysqli')
-rw-r--r-- | ext/mysqli/mysqli.c | 13 | ||||
-rw-r--r-- | ext/mysqli/mysqli_warning.c | 199 | ||||
-rw-r--r-- | ext/mysqli/php_mysqli.h | 18 |
3 files changed, 146 insertions, 84 deletions
diff --git a/ext/mysqli/mysqli.c b/ext/mysqli/mysqli.c index 635bebe282..bc2f596cb7 100644 --- a/ext/mysqli/mysqli.c +++ b/ext/mysqli/mysqli.c @@ -152,7 +152,7 @@ static void mysqli_objects_free_storage(zend_object *object TSRMLS_DC) } } else if (intern->zo.ce == mysqli_warning_class_entry) { /* warning object */ if (my_res && my_res->ptr) { - php_clear_warnings((MYSQLI_WARNING *)my_res->ptr); + php_clear_warnings((MYSQLI_WARNING *)my_res->info); } } my_efree(my_res); @@ -316,7 +316,7 @@ static union _zend_function *php_mysqli_constructor_get(zval *object TSRMLS_DC) } else if (obj->zo.ce == mysqli_driver_class_entry) { f.handler = ZEND_FN(mysqli_driver_construct); } else if (obj->zo.ce == mysqli_warning_class_entry) { - f.handler = ZEND_FN(mysqli_driver_construct); + f.handler = ZEND_FN(mysqli_warning___construct); } return (union _zend_function*)&f; @@ -451,7 +451,7 @@ PHP_MINIT_FUNCTION(mysqli) zend_hash_init(&mysqli_driver_properties, 0, NULL, NULL, 1); MYSQLI_ADD_PROPERTIES(&mysqli_driver_properties, mysqli_driver_property_entries); zend_hash_add(&classes, ce->name, ce->name_length+1, &mysqli_driver_properties, sizeof(mysqli_driver_properties), NULL); - ce->ce_flags |= ZEND_ACC_FINAL; + ce->ce_flags |= ZEND_ACC_FINAL_CLASS; REGISTER_MYSQLI_CLASS_ENTRY("mysqli", mysqli_link_class_entry, mysqli_link_methods); ce = mysqli_link_class_entry; @@ -461,11 +461,10 @@ PHP_MINIT_FUNCTION(mysqli) REGISTER_MYSQLI_CLASS_ENTRY("mysqli_warning", mysqli_warning_class_entry, mysqli_warning_methods); ce = mysqli_warning_class_entry; - ce->ce_flags |= ZEND_ACC_FINAL; + ce->ce_flags |= ZEND_ACC_FINAL_CLASS | ZEND_ACC_PROTECTED; zend_hash_init(&mysqli_warning_properties, 0, NULL, NULL, 1); MYSQLI_ADD_PROPERTIES(&mysqli_warning_properties, mysqli_warning_property_entries); zend_hash_add(&classes, ce->name, ce->name_length+1, &mysqli_warning_properties, sizeof(mysqli_warning_properties), NULL); - ce->ce_flags |= ZEND_ACC_FINAL; REGISTER_MYSQLI_CLASS_ENTRY("mysqli_result", mysqli_result_class_entry, mysqli_result_methods); ce = mysqli_result_class_entry; @@ -513,6 +512,10 @@ PHP_MINIT_FUNCTION(mysqli) REGISTER_LONG_CONSTANT("MYSQLI_CURSOR_TYPE_FOR_UPDATE", CURSOR_TYPE_FOR_UPDATE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("MYSQLI_CURSOR_TYPE_SCROLLABLE", CURSOR_TYPE_SCROLLABLE, CONST_CS | CONST_PERSISTENT); #endif + +#if MYSQL_VERSION_ID > 50007 + REGISTER_LONG_CONSTANT("MYSQLI_STMT_ATTR_PREFETCH_ROWS", STMT_ATTR_PREFETCH_ROWS, CONST_CS | CONST_PERSISTENT); +#endif /* column information */ REGISTER_LONG_CONSTANT("MYSQLI_NOT_NULL_FLAG", NOT_NULL_FLAG, CONST_CS | CONST_PERSISTENT); diff --git a/ext/mysqli/mysqli_warning.c b/ext/mysqli/mysqli_warning.c index 9016857e14..f9215583d4 100644 --- a/ext/mysqli/mysqli_warning.c +++ b/ext/mysqli/mysqli_warning.c @@ -30,88 +30,81 @@ /* {{{ void php_clear_warnings() */ void php_clear_warnings(MYSQLI_WARNING *w) { - if (w->result) { - mysql_free_result(w->result); - } - efree(w); + MYSQLI_WARNING *n; + + while (w) { + n = w; + efree(w->reason); + w = w->next; + efree(n); + } } /* }}} */ -/* {{{ void php_get_warnings(MYSQL *mysql) */ -MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) +/* {{{ MYSQLI_WARNING *php_new_warning */ +MYSQLI_WARNING *php_new_warning(char *reason, char *sqlstate, int errorno) { - MYSQLI_WARNING *w; - int cwarnings; + MYSQLI_WARNING *w; - if (!(cwarnings = mysql_warning_count(mysql))) { - return NULL; - } + w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING)); - if (mysql_query(mysql, "SHOW WARNINGS")) { - return NULL; - } - - w = (MYSQLI_WARNING *)ecalloc(sizeof(MYSQLI_WARNING), 1); - - w->warning_count = cwarnings; - w->result = mysql_store_result(mysql); - if (!(w->row = mysql_fetch_row(w->result))) { - mysql_free_result(w->result); - efree(w); - return NULL; + w->reason = safe_estrdup(reason); + if (sqlstate) { + strcpy(w->sqlstate, sqlstate); + } else { + strcpy(w->sqlstate, "00000"); } + w->errorno = errorno; return w; } /* }}} */ -/* {{{ mysqli_warning::__construct */ -ZEND_FUNCTION(mysqli_warning_construct) +/* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */ +MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) { - MYSQL *mysql = NULL; - MYSQLI_WARNING *w; - MYSQLI_RESOURCE *mysqli_resource; - mysqli_object *obj; + MYSQLI_WARNING *w, *first = NULL, *prev = NULL; + MYSQL_RES *result; + MYSQL_ROW row; - if (!getThis()) { - RETURN_FALSE; - } - - obj = (mysqli_object *)zend_objects_get_address(getThis() TSRMLS_CC); - - if (obj->zo.ce == mysqli_link_class_entry) { - mysql = (MYSQL *)((MY_MYSQL *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr)->mysql; - } else if (obj->zo.ce == mysqli_stmt_class_entry) { - mysql = (MYSQL *)((MY_STMT *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr)->stmt->mysql; + if (mysql_query(mysql, "SHOW WARNINGS")) { + return NULL; } - if ((w = php_get_warnings(mysql))) { - mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE)); - mysqli_resource->ptr = (void *)w; - obj->valid = 1; - - MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry); - } else { - RETURN_FALSE; + result = mysql_store_result(mysql); + while ((row = mysql_fetch_row(result))) { + w = php_new_warning(row[2], "HY000", atoi(row[1])); + if (!first) { + first = w; + } + if (prev) { + prev->next = (void *)w; + } + prev = w; } + mysql_free_result(result); + return first; } /* }}} */ -/* {{{ bool mysqli_warning::next */ -ZEND_FUNCTION(mysqli_warning_next) { +/* {{{ bool mysqli_warning::next() */ +PHP_METHOD(mysqli_warning, next) +{ MYSQLI_WARNING *w; - zval *mysql_warning; + zval *mysqli_warning; mysqli_object *obj = (mysqli_object *)zend_objects_get_address(getThis() TSRMLS_CC); - if (obj->valid) { + if (obj->ptr) { if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", - &mysql_warning, mysqli_warning_class_entry) == FAILURE) { + &mysqli_warning, mysqli_warning_class_entry) == FAILURE) { return; } - MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, &mysql_warning, "mysqli_warning"); + MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, &mysqli_warning, "mysqli_warning"); - if (w->warning_count && (w->row = mysql_fetch_row(w->result))) { + if (w->next) { + w = w->next; + ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w; RETURN_TRUE; } } @@ -119,16 +112,19 @@ ZEND_FUNCTION(mysqli_warning_next) { } /* }}} */ -/* {{{ property mysqli_warning_error */ -int mysqli_warning_error(mysqli_object *obj, zval **retval TSRMLS_DC) +/* {{{ property mysqli_warning_message */ +int mysqli_warning_message(mysqli_object *obj, zval **retval TSRMLS_DC) { MYSQLI_WARNING *w; - ALLOC_ZVAL(*retval); - w = obj->valid ? (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr : NULL; + if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) { + return FAILURE; + } - if (w && w->row && w->row[2]) { - ZVAL_STRING(*retval, w->row[2], 1); + w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr; + ALLOC_ZVAL(*retval); + if (w->reason) { + ZVAL_STRING(*retval, w->reason, 1); } else { ZVAL_NULL(*retval); } @@ -136,35 +132,94 @@ int mysqli_warning_error(mysqli_object *obj, zval **retval TSRMLS_DC) } /* }}} */ +/* {{{ property mysqli_warning_sqlstate */ +int mysqli_warning_sqlstate(mysqli_object *obj, zval **retval TSRMLS_DC) +{ + MYSQLI_WARNING *w; + + if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) { + return FAILURE; + } + + w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr; + ALLOC_ZVAL(*retval); + ZVAL_STRING(*retval, w->sqlstate, 1); + return SUCCESS; +} +/* }}} */ + /* {{{ property mysqli_warning_error */ int mysqli_warning_errno(mysqli_object *obj, zval **retval TSRMLS_DC) { MYSQLI_WARNING *w; + if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) { + return FAILURE; + } + w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr; ALLOC_ZVAL(*retval); - w = obj->valid ? (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr : NULL; + ZVAL_LONG(*retval, w->errorno); + return SUCCESS; +} +/* }}} */ + +/* {{{ mysqli_warning_construct(object obj) */ +PHP_METHOD(mysqli_warning, __construct) +{ + zval **z; + mysqli_object *obj; + MYSQL *hdl; + MYSQLI_WARNING *w; + MYSQLI_RESOURCE *mysqli_resource; + + if (ZEND_NUM_ARGS() != 1) { + WRONG_PARAM_COUNT; + } + if (zend_get_parameters_ex(1, &z)==FAILURE) { + return; + } + obj = (mysqli_object *)zend_object_store_get_object(*(z) TSRMLS_CC);\ - if (w && w->row && w->row[1]) { - ZVAL_LONG(*retval, atoi(w->row[1])); + if (obj->zo.ce == mysqli_link_class_entry) { + MY_MYSQL *mysql; + MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, z, "mysqli_link"); + hdl = mysql->mysql; + } else if (obj->zo.ce == mysqli_stmt_class_entry) { + MY_STMT *stmt; + MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, z, "mysqli_stmt"); + hdl = stmt->stmt->mysql; } else { - ZVAL_NULL(*retval); + RETURN_FALSE; } - return SUCCESS; + + if (mysql_warning_count(hdl)) { + w = php_get_warnings(hdl); + } else { + RETURN_FALSE; + } + + mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE)); + mysqli_resource->ptr = mysqli_resource->info = (void *)w; + + if (!getThis()) { + MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry); + } else { + ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr = mysqli_resource; + ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->valid = 1; + } + } /* }}} */ -/* {{{ mysqli_warning_methods[] - */ function_entry mysqli_warning_methods[] = { - PHP_FALIAS(__construct,mysqli_warning_construct, NULL) - PHP_FALIAS(next,mysqli_warning_next,NULL) + 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[] = { - {"error", mysqli_warning_error, NULL}, + {"message", mysqli_warning_message, NULL}, + {"sqlstate", mysqli_warning_sqlstate, NULL}, {"errno", mysqli_warning_errno, NULL}, {NULL, NULL, NULL} }; diff --git a/ext/mysqli/php_mysqli.h b/ext/mysqli/php_mysqli.h index b37af76add..94dbd5dd81 100644 --- a/ext/mysqli/php_mysqli.h +++ b/ext/mysqli/php_mysqli.h @@ -71,12 +71,6 @@ typedef struct { void *info; /* additional buffer */ } MYSQLI_RESOURCE; -typedef struct { - MYSQL_RES *result; /* stored result set from SHOW WARNINGS */ - MYSQL_ROW row; - int warning_count; /* number of warnings */ -} MYSQLI_WARNING; - typedef struct _mysqli_object { zend_object zo; void *ptr; @@ -84,6 +78,13 @@ typedef struct _mysqli_object { HashTable *prop_handler; } mysqli_object; /* extends zend_object */ +typedef struct { + char *reason; + char sqlstate[6]; + int errorno; + void *next; +} MYSQLI_WARNING; + typedef struct _mysqli_property_entry { char *pname; int (*r_func)(mysqli_object *obj, zval **retval TSRMLS_DC); @@ -157,6 +158,7 @@ extern mysqli_property_entry mysqli_warning_property_entries[]; extern void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flag, int into_object); extern void php_clear_stmt_bind(MY_STMT *stmt); extern void php_clear_mysql(MY_MYSQL *); +extern MYSQLI_WARNING *php_get_warnings(MYSQL *mysql); extern void php_clear_warnings(MYSQLI_WARNING *w); extern void php_free_stmt_bind_buffer(BIND_BUFFER bbuf, int type); extern void php_mysqli_report_error(char *sqlstate, int errorno, char *error TSRMLS_DC); @@ -359,6 +361,7 @@ PHP_FUNCTION(mysqli_get_host_info); PHP_FUNCTION(mysqli_get_proto_info); PHP_FUNCTION(mysqli_get_server_info); PHP_FUNCTION(mysqli_get_server_version); +PHP_FUNCTION(mysqli_get_warnings); PHP_FUNCTION(mysqli_info); PHP_FUNCTION(mysqli_insert_id); PHP_FUNCTION(mysqli_init); @@ -411,6 +414,7 @@ PHP_FUNCTION(mysqli_stmt_data_seek); PHP_FUNCTION(mysqli_stmt_errno); PHP_FUNCTION(mysqli_stmt_error); PHP_FUNCTION(mysqli_stmt_free_result); +PHP_FUNCTION(mysqli_stmt_get_warnings); PHP_FUNCTION(mysqli_stmt_reset); PHP_FUNCTION(mysqli_stmt_insert_id); PHP_FUNCTION(mysqli_stmt_num_rows); @@ -425,7 +429,7 @@ PHP_FUNCTION(mysqli_warning_count); ZEND_FUNCTION(mysqli_stmt_construct); ZEND_FUNCTION(mysqli_result_construct); ZEND_FUNCTION(mysqli_driver_construct); -ZEND_FUNCTION(mysqli_warning_construct); +ZEND_METHOD(mysqli_warning,__construct); ZEND_BEGIN_MODULE_GLOBALS(mysqli) long default_link; |