added constructors for mysqli_stmt and mysqli_result classes

This commit is contained in:
Georg Richter 2004-12-26 13:16:40 +00:00
parent b44785e958
commit 683f6c879c
4 changed files with 164 additions and 4 deletions

View File

@ -281,23 +281,31 @@ static union _zend_function *php_mysqli_constructor_get(zval *object TSRMLS_DC)
{
mysqli_object *obj = (mysqli_object *)zend_objects_get_address(object TSRMLS_CC);
if (obj->zo.ce != mysqli_link_class_entry) {
if (obj->zo.ce != mysqli_link_class_entry && obj->zo.ce != mysqli_stmt_class_entry &&
obj->zo.ce != mysqli_result_class_entry) {
return obj->zo.ce->constructor;
} else {
static zend_internal_function f;
f.function_name = mysqli_link_class_entry->name;
f.scope = mysqli_link_class_entry;
f.function_name = obj->zo.ce->name;
f.scope = obj->zo.ce;
f.arg_info = NULL;
f.num_args = 0;
f.fn_flags = 0;
f.type = ZEND_INTERNAL_FUNCTION;
f.handler = ZEND_FN(mysqli_connect);
if (obj->zo.ce == mysqli_link_class_entry) {
f.handler = ZEND_FN(mysqli_connect);
} else if (obj->zo.ce == mysqli_stmt_class_entry) {
f.handler = ZEND_FN(mysqli_stmt_construct);
} else if (obj->zo.ce == mysqli_result_class_entry) {
f.handler = ZEND_FN(mysqli_result_construct);
}
return (union _zend_function*)&f;
}
}
/* {{{ mysqli_objects_new
*/
PHP_MYSQLI_EXPORT(zend_object_value) mysqli_objects_new(zend_class_entry *class_type TSRMLS_DC)
@ -578,6 +586,109 @@ PHP_MINFO_FUNCTION(mysqli)
}
/* }}} */
/* {{{ mixed mysqli_stmt_construct()
constructor for statement object.
Parameters:
object -> mysqli_init
object, query -> mysqli_prepare
*/
ZEND_FUNCTION(mysqli_stmt_construct)
{
MY_MYSQL *mysql;
zval **mysql_link, **statement;
MY_STMT *stmt;
MYSQLI_RESOURCE *mysqli_resource;
switch (ZEND_NUM_ARGS())
{
case 1: /* mysql_stmt_init */
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
return;
}
MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, mysql_link, "mysqli_link");
stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
stmt->stmt = mysql_stmt_init(mysql->mysql);
break;
case 2:
if (zend_get_parameters_ex(2, &mysql_link, &statement)==FAILURE) {
return;
}
MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, mysql_link, "mysqli_link");
convert_to_string_ex(statement);
stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
if ((stmt->stmt = mysql_stmt_init(mysql->mysql))) {
mysql_stmt_prepare(stmt->stmt, Z_STRVAL_PP(statement), strlen(Z_STRVAL_PP(statement)));
}
break;
default:
WRONG_PARAM_COUNT;
break;
}
if (!stmt->stmt) {
efree(stmt);
RETURN_FALSE;
}
mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
mysqli_resource->ptr = (void *)stmt;
((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;
}
/* }}} */
/* {{{ mixed mysqli_result_construct()
constructor for result object.
Parameters:
object [, mode] -> mysqli_store/use_result
*/
ZEND_FUNCTION(mysqli_result_construct)
{
MY_MYSQL *mysql;
MYSQL_RES *result;
zval **mysql_link, **mode;
MYSQLI_RESOURCE *mysqli_resource;
int resmode = MYSQLI_STORE_RESULT;
switch (ZEND_NUM_ARGS()) {
case 1:
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
return;
}
break;
case 2:
if (zend_get_parameters_ex(2, &mysql_link, &mode)==FAILURE) {
return;
}
resmode = Z_LVAL_PP(mode);
break;
default:
WRONG_PARAM_COUNT;
}
MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, mysql_link, "mysqli_link");
result = (resmode == MYSQLI_STORE_RESULT) ? mysql_store_result(mysql->mysql) :
mysql_use_result(mysql->mysql);
if (!result) {
RETURN_FALSE;
}
mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
mysqli_resource->ptr = (void *)result;
((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;
}
/* }}} */
/* {{{ php_mysqli_fetch_into_hash
*/
void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags, int into_object)

View File

@ -372,6 +372,9 @@ PHP_FUNCTION(mysqli_thread_safe);
PHP_FUNCTION(mysqli_use_result);
PHP_FUNCTION(mysqli_warning_count);
ZEND_FUNCTION(mysqli_stmt_construct);
ZEND_FUNCTION(mysqli_result_construct);
ZEND_BEGIN_MODULE_GLOBALS(mysqli)
long default_link;
long num_links;

25
ext/mysqli/tests/062.phpt Normal file
View File

@ -0,0 +1,25 @@
--TEST--
resultset constructor
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
include "connect.inc";
$mysql = new mysqli($host, $user, $passwd);
$mysql->real_query("SELECT 'foo' FROM DUAL");
$myresult = new mysqli_result($mysql);
$row = $myresult->fetch_row();
$myresult->close();
$mysql->close();
var_dump($row);
?>
--EXPECT--
array(1) {
[0]=>
string(3) "foo"
}

21
ext/mysqli/tests/063.phpt Normal file
View File

@ -0,0 +1,21 @@
--TEST--
resultset constructor
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
include "connect.inc";
$mysql = new mysqli($host, $user, $passwd);
$stmt = new mysqli_stmt($mysql, "SELECT 'foo' FROM DUAL");
$stmt->execute();
$stmt->bind_result($foo);
$stmt->fetch();
$stmt->close();
$mysql->close();
var_dump($foo);
?>
--EXPECT--
string(3) "foo"