Skip to content

[RFC] SQLite3: remove enableExceptions method, default to exceptions #9816

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 0 additions & 2 deletions ext/sqlite3/php_sqlite3_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ typedef struct _php_sqlite3_db_object {
zend_fcall_info authorizer_fci;
zend_fcall_info_cache authorizer_fcc;

bool exception;

zend_llist free_list;
zend_object zo;
} php_sqlite3_db_object;
Expand Down
165 changes: 79 additions & 86 deletions ext/sqlite3/sqlite3.c

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions ext/sqlite3/sqlite3.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
const SQLITE3_DETERMINISTIC = UNKNOWN;
#endif

class SQLite3Exception extends RuntimeException
{
/** @var int|string */
protected $code = 0;
}

/** @not-serializable */
class SQLite3
{
Expand Down Expand Up @@ -335,9 +341,6 @@ public function createCollation(string $name, callable $callback): bool {}
/** @return resource|false */
public function openBlob(string $table, string $column, int $rowid, string $database = "main", int $flags = SQLITE3_OPEN_READONLY) {}

/** @tentative-return-type */
public function enableExceptions(bool $enable = false): bool {}

/** @tentative-return-type */
public function enableExtendedResultCodes(bool $enable = true): bool {}

Expand Down
29 changes: 22 additions & 7 deletions ext/sqlite3/sqlite3_arginfo.h

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

11 changes: 8 additions & 3 deletions ext/sqlite3/tests/bug69972.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ sqlite3
<?php
$db = new SQLite3(':memory:');
echo "SELECTING from invalid table\n";
$result = $db->query("SELECT * FROM non_existent_table");

try {
$result = $db->query("SELECT * FROM non_existent_table");
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}
echo "Closing database\n";
var_dump($db->close());
echo "Done\n";
Expand All @@ -17,8 +23,7 @@ echo "Error Msg: " . $db->lastErrorMsg() . "\n";
?>
--EXPECTF--
SELECTING from invalid table

Warning: SQLite3::query(): Unable to prepare statement: 1, no such table: non_existent_table in %sbug69972.php on line %d
Unable to prepare statement: no such table: non_existent_table
Closing database
bool(true)
Done
Expand Down
1 change: 0 additions & 1 deletion ext/sqlite3/tests/bug77051.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ sqlite3
<?php

$db = new SQLite3(':memory:');
$db->enableExceptions(true);

$stmt = $db->prepare('SELECT :a, :b, ?;');

Expand Down
3 changes: 1 addition & 2 deletions ext/sqlite3/tests/gh9032.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ open_basedir=.
--FILE--
<?php
$db = new SQLite3(":memory:");
$db->enableExceptions(true);

$db->exec('attach database \':memory:\' AS "db1"');
var_dump($db->exec('create table db1.r (id int)'));
Expand All @@ -23,4 +22,4 @@ try {
?>
--EXPECT--
bool(true)
Unable to prepare statement: 23, not authorized
Unable to prepare statement: not authorized
12 changes: 8 additions & 4 deletions ext/sqlite3/tests/sqlite3_02_create.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ echo "Creating Table\n";
var_dump($db->exec('CREATE TABLE test (time INTEGER, id STRING)'));

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

try {
var_dump($db->exec('CREATE TABLE test (time INTEGER, id STRING)'));
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}

echo "Dropping database\n";
var_dump($db->exec('DROP TABLE test'));
Expand All @@ -24,9 +30,7 @@ echo "Done\n";
Creating Table
bool(true)
Creating Same Table Again

Warning: SQLite3::exec(): table test already exists in %s on line %d
bool(false)
table test already exists
Dropping database
bool(true)
Closing database
Expand Down
13 changes: 10 additions & 3 deletions ext/sqlite3/tests/sqlite3_20_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ sqlite3
require_once(__DIR__ . '/new_db.inc');

echo "SELECTING from invalid table\n";
$result = $db->query("SELECT * FROM non_existent_table");
$result = null;

try {
$result = $db->query("SELECT * FROM non_existent_table");
}
catch (SQLite3Exception $e) {
echo $e->getCode() . ": " . $e->getMessage() . "\n";
}

if (!$result) {
echo "Error Code: " . $db->lastErrorCode() . "\n";
echo "Error Msg: " . $db->lastErrorMsg() . "\n";
Expand All @@ -19,8 +27,7 @@ echo "Done\n";
?>
--EXPECTF--
SELECTING from invalid table

Warning: SQLite3::query(): Unable to prepare statement: 1, no such table: non_existent_table in %s on line %d
1: Unable to prepare statement: no such table: non_existent_table
Error Code: 1
Error Msg: no such table: non_existent_table
Closing database
Expand Down
10 changes: 7 additions & 3 deletions ext/sqlite3/tests/sqlite3_22_loadextension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ $directory = __DIR__;

touch($directory . '/myext.txt');

var_dump($db->loadExtension('myext.txt'));
try {
var_dump($db->loadExtension('myext.txt'));
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}
var_dump($db->close());
unlink($directory . '/myext.txt');

echo "Done\n";
?>
--EXPECTF--
Warning: SQLite3::loadExtension(): Unable to load extension at '.%emyext.txt' in %s on line %d
bool(false)
Unable to load extension at '.%emyext.txt'
bool(true)
Done
22 changes: 14 additions & 8 deletions ext/sqlite3/tests/sqlite3_30_blobopen.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ var_dump($stream);
echo "Stream Contents\n";
var_dump(stream_get_contents($stream));
echo "Writing to read-only stream\n";
var_dump(fwrite($stream, 'ABCD'));
try {
var_dump(fwrite($stream, 'ABCD'));
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}
echo "Closing Stream\n";
var_dump(fclose($stream));
echo "Opening stream in write mode\n";
Expand All @@ -38,7 +43,12 @@ echo "Stream Contents\n";
fseek($stream, 0);
var_dump(stream_get_contents($stream));
echo "Expanding blob size\n";
var_dump(fwrite($stream, 'ABCD ABCD ABCD'));
try {
var_dump(fwrite($stream, 'ABCD ABCD ABCD'));
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}
echo "Closing Stream\n";
var_dump(fclose($stream));
echo "Closing database\n";
Expand All @@ -58,9 +68,7 @@ resource(%d) of type (stream)
Stream Contents
string(9) "TEST TEST"
Writing to read-only stream

Warning: fwrite(): Can't write to blob stream: is open as read only in %s on line %d
bool(false)
Can't write to blob stream: is open as read only
Closing Stream
bool(true)
Opening stream in write mode
Expand All @@ -70,9 +78,7 @@ int(4)
Stream Contents
string(9) "ABCD TEST"
Expanding blob size

Warning: fwrite(): It is not possible to increase the size of a BLOB in %s on line %d
bool(false)
It is not possible to increase the size of a BLOB
Closing Stream
bool(true)
Closing database
Expand Down
6 changes: 3 additions & 3 deletions ext/sqlite3/tests/sqlite3_33_load_extension_param.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ $db = new SQLite3(':memory:');

try {
$db->loadExtension("");
} catch (Extension $ex) {
var_dump($ex->getMessage());
} catch (SQLite3Exception $ex) {
echo $ex->getMessage() . "\n";
}

?>
--EXPECTF--
Warning: SQLite3::loadExtension(): Empty string as an extension in %s on line %d
Empty string as an extension
6 changes: 3 additions & 3 deletions ext/sqlite3/tests/sqlite3_34_load_extension_ext_dir.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ $db = new SQLite3(':memory:');

try {
$db->loadExtension("");
} catch (Extension $ex) {
var_dump($ex->getMessage());
} catch (SQLite3Exception $ex) {
echo $ex->getMessage() . "\n";
}

?>
--EXPECTF--
Warning: SQLite3::loadExtension(): SQLite Extension are disabled in %s on line %d
SQLite Extension are disabled
12 changes: 8 additions & 4 deletions ext/sqlite3/tests/sqlite3_38_backup.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ echo "Locking DB1\n";
var_dump($db->exec('BEGIN EXCLUSIVE;'));

echo "Backup to DB2 (should fail)\n";
var_dump($db->backup($db2));

try {
var_dump($db->backup($db2));
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECTF--
Expand All @@ -53,6 +59,4 @@ Resetting DB2
Locking DB1
bool(true)
Backup to DB2 (should fail)

Warning: SQLite3::backup(): Backup failed: source database is busy in %s on line %d
bool(false)
Backup failed: source database is busy
21 changes: 17 additions & 4 deletions ext/sqlite3/tests/sqlite3_38_extended_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@ require_once(__DIR__ . '/new_db.inc');
$db->query("CREATE TABLE dog ( id INTEGER PRIMARY KEY, name TEXT, annoying INTEGER )");

echo "Inserting first time which should succeed" . PHP_EOL;
$result = $db->query("INSERT INTO dog VALUES (1, 'Annoying Dog', 1)");

try {
$result = $db->query("INSERT INTO dog VALUES (1, 'Annoying Dog', 1)");
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}

echo "First Error Code: " . $db->lastErrorCode() . PHP_EOL;

echo "Inserting second time which should fail" . PHP_EOL;
$result = $db->query("INSERT INTO dog VALUES (1, 'Annoying Dog', 1)");

try {
$result = $db->query("INSERT INTO dog VALUES (1, 'Annoying Dog', 1)");
}
catch (SQLite3Exception $e) {
echo $e->getMessage() . "\n";
}

echo "Second Error Code: " . $db->lastErrorCode() . PHP_EOL;
echo "Second Extended Error Code: " . $db->lastExtendedErrorCode() . PHP_EOL;

Expand All @@ -26,8 +40,7 @@ echo "Done" . PHP_EOL;
Inserting first time which should succeed
First Error Code: 0
Inserting second time which should fail

Warning: SQLite3::query(): Unable to execute statement: UNIQUE constraint failed: dog.id in %s on line %d
Unable to execute statement: UNIQUE constraint failed: dog.id
Second Error Code: 19
Second Extended Error Code: 1555
Closing database
Expand Down
Loading