Skip to content

SQLite3Stmt: add an optional array parameter to execute() method, like in PDOStatement #9814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion ext/sqlite3/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1762,18 +1762,58 @@ PHP_METHOD(SQLite3Stmt, execute)
php_sqlite3_stmt *stmt_obj;
php_sqlite3_result *result;
zval *object = ZEND_THIS;
zval *input_params = NULL;
int return_code = 0;
int bind_rc = 0;

stmt_obj = Z_SQLITE3_STMT_P(object);

ZEND_PARSE_PARAMETERS_NONE();
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_OR_NULL(input_params)
ZEND_PARSE_PARAMETERS_END();

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

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

if (input_params) {
struct php_sqlite3_bound_param param = {0};
zval *tmp;
zend_string *key = NULL;
zend_ulong num_index;

if (stmt_obj->bound_params) {
zend_hash_destroy(stmt_obj->bound_params);
FREE_HASHTABLE(stmt_obj->bound_params);
stmt_obj->bound_params = NULL;
}

ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input_params), num_index, key, tmp) {
memset(&param, 0, sizeof(param));

param.param_number = -1;
param.type = SQLITE3_TEXT;
ZVAL_COPY(&param.parameter, tmp);

if (key) {
param.name = key;
} else {
/* for easier use, we are zero-based here */
param.param_number = num_index + 1;
}

if (!register_bound_parameter_to_sqlite(&param, stmt_obj)) {
if (!Z_ISUNDEF(param.parameter)) {
zval_ptr_dtor(&(param.parameter));
ZVAL_UNDEF(&param.parameter);
}
RETURN_FALSE;
}
} ZEND_HASH_FOREACH_END();
}

/* Bind parameters to the statement */
bind_rc = php_sqlite3_bind_params(stmt_obj);

Expand Down
2 changes: 1 addition & 1 deletion ext/sqlite3/sqlite3.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public function clear(): bool {}
public function close(): bool {}

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

/** @tentative-return-type */
public function getSQL(bool $expand = false): string|false {}
Expand Down
3 changes: 2 additions & 1 deletion ext/sqlite3/sqlite3_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions ext/sqlite3/tests/sqlite3_41_prepared_stmt_params.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
SQLite3::prepare and execute with parameters as array
--EXTENSIONS--
sqlite3
--FILE--
<?php

require_once(__DIR__ . '/new_db.inc');
define('TIMENOW', time());

echo "Creating Table\n";
var_dump($db->exec('CREATE TABLE test (time INTEGER, id STRING)'));

echo "INSERT into table\n";
var_dump($db->exec("INSERT INTO test (time, id) VALUES (" . TIMENOW . ", 'a')"));
var_dump($db->exec("INSERT INTO test (time, id) VALUES (" . TIMENOW . ", 'b')"));

echo "SELECTING results\n";
$stmt = $db->prepare("SELECT * FROM test WHERE id = ? ORDER BY id ASC");
$foo = 'a';
echo "BINDING Value\n";
$results = $stmt->execute([$foo]);
while ($result = $results->fetchArray(SQLITE3_NUM))
{
var_dump($result);
}
$results->finalize();

echo "Closing database\n";
var_dump($db->close());
echo "Done\n";
?>
--EXPECTF--
Creating Table
bool(true)
INSERT into table
bool(true)
bool(true)
SELECTING results
BINDING Value
array(2) {
[0]=>
int(%d)
[1]=>
string(1) "a"
}
Closing database
bool(true)
Done