Skip to content

Commit 6d48f18

Browse files
committed
Add an optional array parameter to SQLite3Stmt::execute() method, like in PDOStatement
1 parent 1ea8c82 commit 6d48f18

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

ext/sqlite3/sqlite3.c

+41-1
Original file line numberDiff line numberDiff line change
@@ -1762,18 +1762,58 @@ PHP_METHOD(SQLite3Stmt, execute)
17621762
php_sqlite3_stmt *stmt_obj;
17631763
php_sqlite3_result *result;
17641764
zval *object = ZEND_THIS;
1765+
zval *input_params = NULL;
17651766
int return_code = 0;
17661767
int bind_rc = 0;
17671768

17681769
stmt_obj = Z_SQLITE3_STMT_P(object);
17691770

1770-
ZEND_PARSE_PARAMETERS_NONE();
1771+
ZEND_PARSE_PARAMETERS_START(0, 1)
1772+
Z_PARAM_OPTIONAL
1773+
Z_PARAM_ARRAY_OR_NULL(input_params)
1774+
ZEND_PARSE_PARAMETERS_END();
17711775

17721776
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
17731777

17741778
/* Always reset statement before execution, see bug #77051 */
17751779
sqlite3_reset(stmt_obj->stmt);
17761780

1781+
if (input_params) {
1782+
struct php_sqlite3_bound_param param = {0};
1783+
zval *tmp;
1784+
zend_string *key = NULL;
1785+
zend_ulong num_index;
1786+
1787+
if (stmt_obj->bound_params) {
1788+
zend_hash_destroy(stmt_obj->bound_params);
1789+
FREE_HASHTABLE(stmt_obj->bound_params);
1790+
stmt_obj->bound_params = NULL;
1791+
}
1792+
1793+
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input_params), num_index, key, tmp) {
1794+
memset(&param, 0, sizeof(param));
1795+
1796+
param.param_number = -1;
1797+
param.type = SQLITE3_TEXT;
1798+
ZVAL_COPY(&param.parameter, tmp);
1799+
1800+
if (key) {
1801+
param.name = key;
1802+
} else {
1803+
/* for easier use, we are zero-based here */
1804+
param.param_number = num_index + 1;
1805+
}
1806+
1807+
if (!register_bound_parameter_to_sqlite(&param, stmt_obj)) {
1808+
if (!Z_ISUNDEF(param.parameter)) {
1809+
zval_ptr_dtor(&(param.parameter));
1810+
ZVAL_UNDEF(&param.parameter);
1811+
}
1812+
RETURN_FALSE;
1813+
}
1814+
} ZEND_HASH_FOREACH_END();
1815+
}
1816+
17771817
/* Bind parameters to the statement */
17781818
bind_rc = php_sqlite3_bind_params(stmt_obj);
17791819

ext/sqlite3/sqlite3.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public function clear(): bool {}
363363
public function close(): bool {}
364364

365365
/** @tentative-return-type */
366-
public function execute(): SQLite3Result|false {}
366+
public function execute(?array $params = null): SQLite3Result|false {}
367367

368368
/** @tentative-return-type */
369369
public function getSQL(bool $expand = false): string|false {}

ext/sqlite3/sqlite3_arginfo.h

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
SQLite3::prepare and execute with parameters as array
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
8+
require_once(__DIR__ . '/new_db.inc');
9+
define('TIMENOW', time());
10+
11+
echo "Creating Table\n";
12+
var_dump($db->exec('CREATE TABLE test (time INTEGER, id STRING)'));
13+
14+
echo "INSERT into table\n";
15+
var_dump($db->exec("INSERT INTO test (time, id) VALUES (" . TIMENOW . ", 'a')"));
16+
var_dump($db->exec("INSERT INTO test (time, id) VALUES (" . TIMENOW . ", 'b')"));
17+
18+
echo "SELECTING results\n";
19+
$stmt = $db->prepare("SELECT * FROM test WHERE id = ? ORDER BY id ASC");
20+
$foo = 'a';
21+
echo "BINDING Value\n";
22+
$results = $stmt->execute([$foo]);
23+
while ($result = $results->fetchArray(SQLITE3_NUM))
24+
{
25+
var_dump($result);
26+
}
27+
$results->finalize();
28+
29+
echo "Closing database\n";
30+
var_dump($db->close());
31+
echo "Done\n";
32+
?>
33+
--EXPECTF--
34+
Creating Table
35+
bool(true)
36+
INSERT into table
37+
bool(true)
38+
bool(true)
39+
SELECTING results
40+
BINDING Value
41+
array(2) {
42+
[0]=>
43+
int(%d)
44+
[1]=>
45+
string(1) "a"
46+
}
47+
Closing database
48+
bool(true)
49+
Done

0 commit comments

Comments
 (0)