Skip to content

Commit 196671a

Browse files
committed
Simplify mysqli_stmt_bind_param() implementation
By using zpp.
1 parent 179bc21 commit 196671a

File tree

1 file changed

+14
-44
lines changed

1 file changed

+14
-44
lines changed

ext/mysqli/mysqli_api.c

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ PHP_FUNCTION(mysqli_autocommit)
176176
/* {{{ mysqli_stmt_bind_param_do_bind */
177177
#ifndef MYSQLI_USE_MYSQLND
178178
static
179-
int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars,
180-
zval *args, unsigned int start, const char * const types)
179+
int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int num_vars, zval *args, const char * const types, unsigned int num_extra_args)
181180
{
182181
int i, ofs;
183182
MYSQL_BIND *bind;
@@ -192,7 +191,7 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in
192191
bind = (MYSQL_BIND *) ecalloc(num_vars, sizeof(MYSQL_BIND));
193192

194193
ofs = 0;
195-
for (i = start; i < argc; i++) {
194+
for (i = 0; i < num_vars; i++) {
196195
zval *param;
197196
if (Z_ISREF(args[i])) {
198197
param = Z_REFVAL(args[i]);
@@ -229,7 +228,7 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in
229228
break;
230229

231230
default:
232-
php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i+1);
231+
php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i + num_extra_args + 1);
233232
rc = 1;
234233
goto end_1;
235234
}
@@ -245,7 +244,7 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in
245244
stmt->param.vars = safe_emalloc(num_vars, sizeof(zval), 0);
246245
for (i = 0; i < num_vars; i++) {
247246
if (bind[i].buffer_type != MYSQL_TYPE_LONG_BLOB) {
248-
ZVAL_COPY(&stmt->param.vars[i], &args[i+start]);
247+
ZVAL_COPY(&stmt->param.vars[i], &args[i]);
249248
} else {
250249
ZVAL_UNDEF(&stmt->param.vars[i]);
251250
}
@@ -257,22 +256,21 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in
257256
}
258257
#else
259258
static
260-
int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars,
261-
zval *args, unsigned int start, const char * const types)
259+
int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int num_vars, zval *args, const char * const types, unsigned int num_extra_args)
262260
{
263261
unsigned int i;
264262
MYSQLND_PARAM_BIND *params;
265263
enum_func_status ret = FAIL;
266264

267265
/* If no params -> skip binding and return directly */
268-
if (argc == start) {
266+
if (num_vars == 0) {
269267
return PASS;
270268
}
271269
params = mysqlnd_stmt_alloc_param_bind(stmt->stmt);
272270
if (!params) {
273271
goto end;
274272
}
275-
for (i = 0; i < (argc - start); i++) {
273+
for (i = 0; i < num_vars; i++) {
276274
zend_uchar type;
277275
switch (types[i]) {
278276
case 'd': /* Double */
@@ -293,12 +291,12 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in
293291
break;
294292
default:
295293
/* We count parameters from 1 */
296-
php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[i], i + start + 1);
294+
php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[i], i + num_extra_args + 1);
297295
ret = FAIL;
298296
mysqlnd_stmt_free_param_bind(stmt->stmt, params);
299297
goto end;
300298
}
301-
ZVAL_COPY_VALUE(&params[i].zv, &args[i + start]);
299+
ZVAL_COPY_VALUE(&params[i].zv, &args[i]);
302300
params[i].type = type;
303301
}
304302
ret = mysqlnd_stmt_bind_param(stmt->stmt, params);
@@ -313,41 +311,24 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in
313311
PHP_FUNCTION(mysqli_stmt_bind_param)
314312
{
315313
zval *args;
316-
int argc = ZEND_NUM_ARGS();
317-
int num_vars;
318-
int start = 2;
314+
int argc;
319315
MY_STMT *stmt;
320316
zval *mysql_stmt;
321317
char *types;
322318
size_t types_len;
323-
zend_ulong rc;
324319

325-
/* calculate and check number of parameters */
326-
if (argc < 2) {
327-
/* there has to be at least one pair */
328-
WRONG_PARAM_COUNT;
329-
}
330-
331-
if (zend_parse_method_parameters((getThis()) ? 1:2, getThis(), "Os", &mysql_stmt, mysqli_stmt_class_entry,
332-
&types, &types_len) == FAILURE) {
320+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os*", &mysql_stmt, mysqli_stmt_class_entry, &types, &types_len, &args, &argc) == FAILURE) {
333321
RETURN_THROWS();
334322
}
335323

336324
MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
337325

338-
num_vars = argc - 1;
339-
if (getThis()) {
340-
start = 1;
341-
} else {
342-
/* ignore handle parameter in procedural interface*/
343-
--num_vars;
344-
}
345326
if (!types_len) {
346327
php_error_docref(NULL, E_WARNING, "Invalid type or no types specified");
347328
RETURN_FALSE;
348329
}
349330

350-
if (types_len != (size_t)(argc - start)) {
331+
if (types_len != (size_t) argc) {
351332
/* number of bind variables doesn't match number of elements in type definition string */
352333
php_error_docref(NULL, E_WARNING, "Number of elements in type definition string doesn't match number of bind variables");
353334
RETURN_FALSE;
@@ -358,19 +339,8 @@ PHP_FUNCTION(mysqli_stmt_bind_param)
358339
RETURN_FALSE;
359340
}
360341

361-
args = safe_emalloc(argc, sizeof(zval), 0);
362-
363-
if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
364-
zend_wrong_param_count();
365-
rc = 1;
366-
} else {
367-
rc = mysqli_stmt_bind_param_do_bind(stmt, argc, num_vars, args, start, types);
368-
MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
369-
}
370-
371-
efree(args);
372-
373-
RETURN_BOOL(!rc);
342+
RETVAL_BOOL(!mysqli_stmt_bind_param_do_bind(stmt, argc, args, types, getThis() ? 1 : 2));
343+
MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
374344
}
375345
/* }}} */
376346

0 commit comments

Comments
 (0)