2019-07-27 12:37:24 +00:00
|
|
|
<?php
|
2024-08-24 12:31:41 +00:00
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-07-27 12:37:24 +00:00
|
|
|
|
|
|
|
namespace Friendica\Test\Util\Database;
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class extends native PDO one but allow nested transactions
|
|
|
|
* by using the SQL statements `SAVEPOINT', 'RELEASE SAVEPOINT' AND 'ROLLBACK SAVEPOINT'
|
|
|
|
*/
|
|
|
|
class ExtendedPDO extends PDO
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array Database drivers that support SAVEPOINT * statements.
|
|
|
|
*/
|
2022-06-20 06:12:09 +00:00
|
|
|
protected static $_supportedDrivers = ['pgsql', 'mysql'];
|
2019-07-27 12:37:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int the current transaction depth
|
|
|
|
*/
|
|
|
|
protected $_transactionDepth = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getTransactionDepth()
|
|
|
|
{
|
|
|
|
return $this->_transactionDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if database driver support savepoints
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function hasSavepoint()
|
|
|
|
{
|
|
|
|
return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME),
|
|
|
|
self::$_supportedDrivers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start transaction
|
|
|
|
*
|
|
|
|
* @return bool|void
|
|
|
|
*/
|
|
|
|
public function beginTransaction()
|
|
|
|
{
|
2021-04-01 21:04:30 +00:00
|
|
|
if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
2019-07-27 12:37:24 +00:00
|
|
|
parent::beginTransaction();
|
2021-05-24 18:30:56 +00:00
|
|
|
$this->_transactionDepth = 0;
|
2019-07-27 12:37:24 +00:00
|
|
|
} else {
|
|
|
|
$this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_transactionDepth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit current transaction
|
|
|
|
*
|
2022-06-20 06:12:09 +00:00
|
|
|
* @return bool
|
2019-07-27 12:37:24 +00:00
|
|
|
*/
|
2022-06-20 06:12:09 +00:00
|
|
|
public function commit(): bool
|
2019-07-27 12:37:24 +00:00
|
|
|
{
|
2021-05-24 18:30:56 +00:00
|
|
|
// We don't want to "really" commit something, so skip the most outer hierarchy
|
|
|
|
if ($this->_transactionDepth <= 1 && $this->hasSavepoint()) {
|
|
|
|
$this->_transactionDepth = $this->_transactionDepth <= 0 ? 0 : 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:37:24 +00:00
|
|
|
$this->_transactionDepth--;
|
|
|
|
|
2022-06-20 06:12:09 +00:00
|
|
|
return $this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
|
2019-07-27 12:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback current transaction,
|
|
|
|
*
|
|
|
|
* @throws PDOException if there is no transaction started
|
2022-06-20 06:12:09 +00:00
|
|
|
* @return bool Whether rollback was successful
|
2019-07-27 12:37:24 +00:00
|
|
|
*/
|
2022-06-20 06:12:09 +00:00
|
|
|
public function rollback(): bool
|
2019-07-27 12:37:24 +00:00
|
|
|
{
|
|
|
|
$this->_transactionDepth--;
|
|
|
|
|
2022-06-20 06:12:09 +00:00
|
|
|
if ($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
2021-05-24 18:30:56 +00:00
|
|
|
$this->_transactionDepth = 0;
|
|
|
|
try {
|
2022-06-20 06:12:09 +00:00
|
|
|
return parent::rollBack();
|
2021-05-24 18:30:56 +00:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
// this shouldn't happen, but it does ...
|
|
|
|
}
|
2019-07-27 12:37:24 +00:00
|
|
|
} else {
|
2022-06-20 06:12:09 +00:00
|
|
|
return $this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
|
2019-07-27 12:37:24 +00:00
|
|
|
}
|
2022-06-20 06:12:09 +00:00
|
|
|
return false;
|
2019-07-27 12:37:24 +00:00
|
|
|
}
|
|
|
|
}
|