Skip to content
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
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ PHP 8.6 UPGRADE NOTES
6. New Functions
========================================

- Pdo_Sqlite:
. Pdo\Sqlite::backup() analogous to Sqlite3::backup().

- Reflection:
. ReflectionConstant::inNamespace()
. Added ReflectionProperty::isReadable() and ReflectionProperty::isWritable().
Expand Down
51 changes: 51 additions & 0 deletions ext/pdo_sqlite/pdo_sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,57 @@ PHP_METHOD(Pdo_Sqlite, setAuthorizer)
RETURN_THROWS();
}

PHP_METHOD(Pdo_Sqlite, backup)
{
zval *destination_object;
char *source_dbname = "main", *destination_dbname = "main";
size_t source_dbname_len, destination_dbname_len;
int rc;

ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_OBJECT_OF_CLASS(destination_object, pdosqlite_ce)
Z_PARAM_OPTIONAL
Z_PARAM_PATH(source_dbname, source_dbname_len)
Z_PARAM_PATH(destination_dbname, destination_dbname_len)
ZEND_PARSE_PARAMETERS_END();

pdo_dbh_t *dbh = Z_PDO_DBH_P(destination_object);
PDO_CONSTRUCT_CHECK;
pdo_sqlite_db_handle *destination_db_handle = dbh->driver_data;
dbh = Z_PDO_DBH_P(ZEND_THIS);
PDO_CONSTRUCT_CHECK;
pdo_sqlite_db_handle *source_db_handle = dbh->driver_data;

sqlite3_backup *dbBackup = sqlite3_backup_init(destination_db_handle->db, destination_dbname, source_db_handle->db, source_dbname);

if (dbBackup) {
do {
rc = sqlite3_backup_step(dbBackup, -1);
} while (rc == SQLITE_OK);

/* Release resources allocated by sqlite3_backup_init(). */
rc = sqlite3_backup_finish(dbBackup);
} else {
rc = sqlite3_errcode(source_db_handle->db);
}

if (rc != SQLITE_OK) {
if (rc == SQLITE_BUSY) {
pdo_raise_impl_error(dbh, NULL, "HY000", "Backup failed: source database is busy");
} else if (rc == SQLITE_LOCKED) {
pdo_raise_impl_error(dbh, NULL, "HY000", "Backup failed: source database is locked");
} else {
char *message;
spprintf(&message, 0, "Backup failed: %s", sqlite3_errmsg(source_db_handle->db));
pdo_raise_impl_error(dbh, NULL, "HY000", message);
efree(message);
}
RETURN_FALSE;
}

RETURN_TRUE;
}

static int php_sqlite_collation_callback(void *context, int string1_len, const void *string1,
int string2_len, const void *string2)
{
Expand Down
2 changes: 2 additions & 0 deletions ext/pdo_sqlite/pdo_sqlite.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ public function openBlob(
) {}

public function setAuthorizer(?callable $callback): void {}

public function backup(Sqlite $destination, string $sourceDatabase = "main", string $destinationDatabase = "main"): bool {}
}
10 changes: 9 additions & 1 deletion ext/pdo_sqlite/pdo_sqlite_arginfo.h

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

83 changes: 83 additions & 0 deletions ext/pdo_sqlite/tests/subclasses/pdo_sqlite_backup.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
--TEST--
Pdo\Sqlite::backup test
--EXTENSIONS--
pdo
pdo_sqlite
--FILE--
<?php

$db = Pdo::connect('sqlite::memory:');

$db->exec('CREATE TABLE test (a, b);');
$db->exec('INSERT INTO test VALUES (42, \'php\');');

$db2 = Pdo::connect('sqlite::memory:');

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

echo "Checking if table has been copied\n";
var_dump($db2->query('SELECT COUNT(*) FROM sqlite_master;')->fetchAll());

echo "Checking backup contents\n";
var_dump($db2->query('SELECT a FROM test;')->fetchAll());
var_dump($db2->query('SELECT b FROM test;')->fetchAll());

echo "Resetting DB2\n";

unset($db2);
$db2 = Pdo::connect('sqlite::memory:');

echo "Locking DB1\n";
var_dump($db->exec('BEGIN EXCLUSIVE;'));

echo "Backup to DB2 (should fail)\n";
try {
$db->backup($db2);
} catch (PDOException $e) {
echo $e->getMessage(), "\n";
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
var_dump($db->backup($db2));

?>
--EXPECTF--
Backup to DB2
bool(true)
Checking if table has been copied
array(1) {
[0]=>
array(2) {
["COUNT(*)"]=>
int(1)
[0]=>
int(1)
}
}
Checking backup contents
array(1) {
[0]=>
array(2) {
["a"]=>
int(42)
[0]=>
int(42)
}
}
array(1) {
[0]=>
array(2) {
["b"]=>
string(3) "php"
[0]=>
string(3) "php"
}
}
Resetting DB2
Locking DB1
int(1)
Backup to DB2 (should fail)
SQLSTATE[HY000]: General error: Backup failed: source database is busy

Warning: Pdo\Sqlite::backup(): SQLSTATE[HY000]: General error: Backup failed: source database is busy in %s on line %d
bool(false)
Loading