2019-07-27 08:37:24 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2024-01-02 15:57:26 -05:00
|
|
|
* @copyright Copyright (C) 2010-2024, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-07-27 08:37:24 -04: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 02:12:09 -04:00
|
|
|
protected static $_supportedDrivers = ['pgsql', 'mysql'];
|
2019-07-27 08:37:24 -04: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 17:04:30 -04:00
|
|
|
if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
2019-07-27 08:37:24 -04:00
|
|
|
parent::beginTransaction();
|
2021-05-24 14:30:56 -04:00
|
|
|
$this->_transactionDepth = 0;
|
2019-07-27 08:37:24 -04:00
|
|
|
} else {
|
|
|
|
$this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_transactionDepth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit current transaction
|
|
|
|
*
|
2022-06-20 02:12:09 -04:00
|
|
|
* @return bool
|
2019-07-27 08:37:24 -04:00
|
|
|
*/
|
2022-06-20 02:12:09 -04:00
|
|
|
public function commit(): bool
|
2019-07-27 08:37:24 -04:00
|
|
|
{
|
2021-05-24 14:30:56 -04: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 08:37:24 -04:00
|
|
|
$this->_transactionDepth--;
|
|
|
|
|
2022-06-20 02:12:09 -04:00
|
|
|
return $this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
|
2019-07-27 08:37:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback current transaction,
|
|
|
|
*
|
|
|
|
* @throws PDOException if there is no transaction started
|
2022-06-20 02:12:09 -04:00
|
|
|
* @return bool Whether rollback was successful
|
2019-07-27 08:37:24 -04:00
|
|
|
*/
|
2022-06-20 02:12:09 -04:00
|
|
|
public function rollback(): bool
|
2019-07-27 08:37:24 -04:00
|
|
|
{
|
|
|
|
$this->_transactionDepth--;
|
|
|
|
|
2022-06-20 02:12:09 -04:00
|
|
|
if ($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
2021-05-24 14:30:56 -04:00
|
|
|
$this->_transactionDepth = 0;
|
|
|
|
try {
|
2022-06-20 02:12:09 -04:00
|
|
|
return parent::rollBack();
|
2021-05-24 14:30:56 -04:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
// this shouldn't happen, but it does ...
|
|
|
|
}
|
2019-07-27 08:37:24 -04:00
|
|
|
} else {
|
2022-06-20 02:12:09 -04:00
|
|
|
return $this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
|
2019-07-27 08:37:24 -04:00
|
|
|
}
|
2022-06-20 02:12:09 -04:00
|
|
|
return false;
|
2019-07-27 08:37:24 -04:00
|
|
|
}
|
|
|
|
}
|