Skip to content

Commit 86c6b3b

Browse files
committed
Support SQLite3 @name notation
Besides the common `:param` notation to designate named parameters in prepared statements, SQLite3 also supports `@param` and `$param`. While the latter is mostly to support the Tcl programming language, and would be confusing for PHP's sqlite3 binding due to the similarity with string interpolation, the former is common under .NET and raises no such issue. Therefore we add support for it. This patch has been developed in cooperation with @bohwaz.
1 parent f0251a8 commit 86c6b3b

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ PHP NEWS
3333
- SQLite3:
3434
. Unbundled libsqlite. (cmb)
3535
. Lifted requirements to SQLite 3.5.0. (cmb)
36+
. Added support for the SQLite @name notation. (cmb, BohwaZ)
3637

3738
- Standard:
3839
. Fixed bug #74764 (Bindto IPv6 works with file_get_contents but fails with

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ PHP 7.4 UPGRADE NOTES
119119
- SQLite3:
120120
. The bundled libsqlite has been removed. To build the SQLite3 and/or
121121
PDO_SQLite extensions a system libsqlite3 ≥ 3.5.0 is now required.
122+
. The @param notation can now also be used to denote SQL query parameters.
122123

123124
- Zip:
124125
. The bundled libzip library has been removed. A system libzip >= 0.11 is now

ext/sqlite3/sqlite3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *pa
14081408

14091409
/* We need a : prefix to resolve a name to a parameter number */
14101410
if (param->name) {
1411-
if (ZSTR_VAL(param->name)[0] != ':') {
1411+
if (ZSTR_VAL(param->name)[0] != ':' && ZSTR_VAL(param->name)[0] != '@') {
14121412
/* pre-increment for character + 1 for null */
14131413
zend_string *temp = zend_string_alloc(ZSTR_LEN(param->name) + 1, 0);
14141414
ZSTR_VAL(temp)[0] = ':';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
SQLite3::prepare Bound Value test
3+
--SKIPIF--
4+
<?php require_once(__DIR__ . '/skipif.inc'); ?>
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 = @id ORDER BY id ASC");
20+
$foo = 'a';
21+
echo "BINDING Value\n";
22+
var_dump($stmt->bindValue('@id', $foo, SQLITE3_TEXT));
23+
$results = $stmt->execute();
24+
while ($result = $results->fetchArray(SQLITE3_NUM))
25+
{
26+
var_dump($result);
27+
}
28+
$results->finalize();
29+
30+
echo "Closing database\n";
31+
var_dump($db->close());
32+
echo "Done\n";
33+
?>
34+
--EXPECTF--
35+
Creating Table
36+
bool(true)
37+
INSERT into table
38+
bool(true)
39+
bool(true)
40+
SELECTING results
41+
BINDING Value
42+
bool(true)
43+
array(2) {
44+
[0]=>
45+
int(%d)
46+
[1]=>
47+
string(1) "a"
48+
}
49+
Closing database
50+
bool(true)
51+
Done

0 commit comments

Comments
 (0)