Skip to content

Commit a109767

Browse files
committed
Use ZPP callable for PDO Sqlite
1 parent 7cfb141 commit a109767

File tree

3 files changed

+37
-51
lines changed

3 files changed

+37
-51
lines changed

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,13 @@ static int php_sqlite3_collation_callback(void *context,
514514
return ret;
515515
}
516516

517-
/* {{{ bool SQLite::sqliteCreateFunction(string name, mixed callback [, int argcount, int flags])
517+
/* {{{ bool SQLite::sqliteCreateFunction(string name, callable callback [, int argcount, int flags])
518518
Registers a UDF with the sqlite db handle */
519519
static PHP_METHOD(SQLite, sqliteCreateFunction)
520520
{
521521
struct pdo_sqlite_func *func;
522-
zval *callback;
522+
zend_fcall_info fci;
523+
zend_fcall_info_cache fcc;
523524
char *func_name;
524525
size_t func_name_len;
525526
zend_long argc = -1;
@@ -530,7 +531,7 @@ static PHP_METHOD(SQLite, sqliteCreateFunction)
530531

531532
ZEND_PARSE_PARAMETERS_START(2, 4)
532533
Z_PARAM_STRING(func_name, func_name_len)
533-
Z_PARAM_ZVAL(callback)
534+
Z_PARAM_FUNC(fci, fcc)
534535
Z_PARAM_OPTIONAL
535536
Z_PARAM_LONG(argc)
536537
Z_PARAM_LONG(flags)
@@ -539,13 +540,6 @@ static PHP_METHOD(SQLite, sqliteCreateFunction)
539540
dbh = Z_PDO_DBH_P(ZEND_THIS);
540541
PDO_CONSTRUCT_CHECK;
541542

542-
if (!zend_is_callable(callback, 0, NULL)) {
543-
zend_string *cbname = zend_get_callable_name(callback);
544-
php_error_docref(NULL, E_WARNING, "Function '%s' is not callable", ZSTR_VAL(cbname));
545-
zend_string_release_ex(cbname, 0);
546-
RETURN_FALSE;
547-
}
548-
549543
H = (pdo_sqlite_db_handle *)dbh->driver_data;
550544

551545
func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func));
@@ -555,7 +549,7 @@ static PHP_METHOD(SQLite, sqliteCreateFunction)
555549
if (ret == SQLITE_OK) {
556550
func->funcname = estrdup(func_name);
557551

558-
ZVAL_COPY(&func->func, callback);
552+
ZVAL_COPY(&func->func, &fci.function_name);
559553

560554
func->argc = argc;
561555

@@ -570,7 +564,7 @@ static PHP_METHOD(SQLite, sqliteCreateFunction)
570564
}
571565
/* }}} */
572566

573-
/* {{{ bool SQLite::sqliteCreateAggregate(string name, mixed step, mixed fini [, int argcount])
567+
/* {{{ bool SQLite::sqliteCreateAggregate(string name, callable step, callable fini [, int argcount])
574568
Registers a UDF with the sqlite db handle */
575569

576570
/* The step function should have the prototype:
@@ -592,7 +586,8 @@ static PHP_METHOD(SQLite, sqliteCreateFunction)
592586
static PHP_METHOD(SQLite, sqliteCreateAggregate)
593587
{
594588
struct pdo_sqlite_func *func;
595-
zval *step_callback, *fini_callback;
589+
zend_fcall_info step_fci, fini_fci;
590+
zend_fcall_info_cache step_fcc, fini_fcc;
596591
char *func_name;
597592
size_t func_name_len;
598593
zend_long argc = -1;
@@ -602,29 +597,15 @@ static PHP_METHOD(SQLite, sqliteCreateAggregate)
602597

603598
ZEND_PARSE_PARAMETERS_START(3, 4)
604599
Z_PARAM_STRING(func_name, func_name_len)
605-
Z_PARAM_ZVAL(step_callback)
606-
Z_PARAM_ZVAL(fini_callback)
600+
Z_PARAM_FUNC(step_fci, step_fcc)
601+
Z_PARAM_FUNC(fini_fci, fini_fcc)
607602
Z_PARAM_OPTIONAL
608603
Z_PARAM_LONG(argc)
609604
ZEND_PARSE_PARAMETERS_END();
610605

611606
dbh = Z_PDO_DBH_P(ZEND_THIS);
612607
PDO_CONSTRUCT_CHECK;
613608

614-
if (!zend_is_callable(step_callback, 0, NULL)) {
615-
zend_string *cbname = zend_get_callable_name(step_callback);
616-
php_error_docref(NULL, E_WARNING, "Function '%s' is not callable", ZSTR_VAL(cbname));
617-
zend_string_release_ex(cbname, 0);
618-
RETURN_FALSE;
619-
}
620-
621-
if (!zend_is_callable(fini_callback, 0, NULL)) {
622-
zend_string *cbname = zend_get_callable_name(fini_callback);
623-
php_error_docref(NULL, E_WARNING, "Function '%s' is not callable", ZSTR_VAL(cbname));
624-
zend_string_release_ex(cbname, 0);
625-
RETURN_FALSE;
626-
}
627-
628609
H = (pdo_sqlite_db_handle *)dbh->driver_data;
629610

630611
func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func));
@@ -634,9 +615,9 @@ static PHP_METHOD(SQLite, sqliteCreateAggregate)
634615
if (ret == SQLITE_OK) {
635616
func->funcname = estrdup(func_name);
636617

637-
ZVAL_COPY(&func->step, step_callback);
618+
ZVAL_COPY(&func->step, &step_fci.function_name);
638619

639-
ZVAL_COPY(&func->fini, fini_callback);
620+
ZVAL_COPY(&func->fini, &fini_fci.function_name);
640621

641622
func->argc = argc;
642623

@@ -651,12 +632,13 @@ static PHP_METHOD(SQLite, sqliteCreateAggregate)
651632
}
652633
/* }}} */
653634

654-
/* {{{ bool SQLite::sqliteCreateCollation(string name, mixed callback)
635+
/* {{{ bool SQLite::sqliteCreateCollation(string name, callable callback)
655636
Registers a collation with the sqlite db handle */
656637
static PHP_METHOD(SQLite, sqliteCreateCollation)
657638
{
658639
struct pdo_sqlite_collation *collation;
659-
zval *callback;
640+
zend_fcall_info fci;
641+
zend_fcall_info_cache fcc;
660642
char *collation_name;
661643
size_t collation_name_len;
662644
pdo_dbh_t *dbh;
@@ -665,19 +647,12 @@ static PHP_METHOD(SQLite, sqliteCreateCollation)
665647

666648
ZEND_PARSE_PARAMETERS_START(2, 2)
667649
Z_PARAM_STRING(collation_name, collation_name_len)
668-
Z_PARAM_ZVAL(callback)
650+
Z_PARAM_FUNC(fci, fcc)
669651
ZEND_PARSE_PARAMETERS_END();
670652

671653
dbh = Z_PDO_DBH_P(ZEND_THIS);
672654
PDO_CONSTRUCT_CHECK;
673655

674-
if (!zend_is_callable(callback, 0, NULL)) {
675-
zend_string *cbname = zend_get_callable_name(callback);
676-
php_error_docref(NULL, E_WARNING, "Function '%s' is not callable", ZSTR_VAL(cbname));
677-
zend_string_release_ex(cbname, 0);
678-
RETURN_FALSE;
679-
}
680-
681656
H = (pdo_sqlite_db_handle *)dbh->driver_data;
682657

683658
collation = (struct pdo_sqlite_collation*)ecalloc(1, sizeof(*collation));
@@ -686,7 +661,7 @@ static PHP_METHOD(SQLite, sqliteCreateCollation)
686661
if (ret == SQLITE_OK) {
687662
collation->name = estrdup(collation_name);
688663

689-
ZVAL_COPY(&collation->callback, callback);
664+
ZVAL_COPY(&collation->callback, &fci.function_name);
690665

691666
collation->next = H->collations;
692667
H->collations = collation;

ext/pdo_sqlite/tests/pdo_sqlite_createaggregate_002.phpt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ PDO_sqlite: Testing invalid callback for sqliteCreateAggregate()
77

88
$pdo = new PDO('sqlite::memory:');
99

10-
$pdo->sqliteCreateAggregate('foo', 'a', '');
11-
$pdo->sqliteCreateAggregate('foo', 'strlen', '');
10+
try {
11+
$pdo->sqliteCreateAggregate('foo', 'a', '');
12+
} catch (\TypeError $e) {
13+
echo $e->getMessage() . \PHP_EOL;
14+
}
15+
try {
16+
$pdo->sqliteCreateAggregate('foo', 'strlen', '');
17+
} catch (\TypeError $e) {
18+
echo $e->getMessage() . \PHP_EOL;
19+
}
1220

1321
?>
14-
--EXPECTF--
15-
Warning: PDO::sqliteCreateAggregate(): Function 'a' is not callable in %s on line %d
16-
17-
Warning: PDO::sqliteCreateAggregate(): Function '' is not callable in %s on line %d
22+
--EXPECT--
23+
PDO::sqliteCreateAggregate(): Argument #2 must be a valid callback, function "a" not found or invalid function name
24+
PDO::sqliteCreateAggregate(): Argument #3 must be a valid callback, function "" not found or invalid function name

ext/pdo_sqlite/tests/pdo_sqlite_createfunction_002.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ Chris MacPherson [email protected]
1010

1111
$db = new PDO( 'sqlite::memory:');
1212

13-
$db->sqliteCreateFunction('bar-alias', 'bar');
13+
try {
14+
$db->sqliteCreateFunction('bar-alias', 'bar');
15+
} catch (\TypeError $e) {
16+
echo $e->getMessage() . \PHP_EOL;
17+
}
1418

1519
?>
16-
--EXPECTF--
17-
Warning: PDO::sqliteCreateFunction(): Function 'bar' is not callable in %s on line %d
20+
--EXPECT--
21+
PDO::sqliteCreateFunction(): Argument #2 must be a valid callback, function "bar" not found or invalid function name

0 commit comments

Comments
 (0)