2019-07-27 12:37:24 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2024-01-02 20:57:26 +00:00
|
|
|
* @copyright Copyright (C) 2010-2024, the Friendica project
|
2020-02-09 14:45:36 +00: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 12:37:24 +00:00
|
|
|
|
|
|
|
namespace Friendica\Test\Util\Database;
|
|
|
|
|
|
|
|
use Friendica\Database\Database;
|
2020-10-18 18:31:57 +00:00
|
|
|
use Friendica\Database\DatabaseException;
|
2019-07-27 12:37:24 +00:00
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
2019-07-27 13:35:53 +00:00
|
|
|
/**
|
|
|
|
* Overrides the Friendica database class for re-using the connection
|
|
|
|
* for different tests
|
2019-07-28 15:40:42 +00:00
|
|
|
*
|
|
|
|
* Overrides functionality to enforce one transaction per call (for nested transactions)
|
2019-07-27 13:35:53 +00:00
|
|
|
*/
|
2019-07-27 12:37:24 +00:00
|
|
|
class StaticDatabase extends Database
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ExtendedPDO
|
|
|
|
*/
|
|
|
|
private static $staticConnection;
|
|
|
|
|
2021-05-24 18:30:56 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $_locked = false;
|
|
|
|
|
2019-07-27 12:37:24 +00:00
|
|
|
/**
|
|
|
|
* Override the behaviour of connect, due there is just one, static connection at all
|
|
|
|
*
|
2022-06-21 15:54:23 +00:00
|
|
|
* @return bool Success
|
2019-07-27 12:37:24 +00:00
|
|
|
*/
|
2022-06-20 06:06:45 +00:00
|
|
|
public function connect(): bool
|
2019-07-27 12:37:24 +00:00
|
|
|
{
|
|
|
|
if (!is_null($this->connection) && $this->connected()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset(self::$staticConnection)) {
|
2019-07-28 15:40:42 +00:00
|
|
|
self::statConnect($_SERVER);
|
2019-07-27 12:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->driver = 'pdo';
|
|
|
|
$this->connection = self::$staticConnection;
|
|
|
|
$this->connected = true;
|
|
|
|
|
|
|
|
return $this->connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-22 04:07:23 +00:00
|
|
|
* Override the transaction since there are now hierarchical transactions possible
|
2019-07-27 12:37:24 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-05-29 18:23:57 +00:00
|
|
|
public function transaction(): bool
|
2019-07-27 12:37:24 +00:00
|
|
|
{
|
2019-07-28 15:40:42 +00:00
|
|
|
if (!$this->in_transaction && !$this->connection->beginTransaction()) {
|
2019-07-27 12:37:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->in_transaction = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:30:56 +00:00
|
|
|
/** Mock for locking tables */
|
2022-06-20 06:06:45 +00:00
|
|
|
public function lock($table): bool
|
2021-05-24 18:30:56 +00:00
|
|
|
{
|
|
|
|
if ($this->_locked) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->in_transaction = true;
|
|
|
|
$this->_locked = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Mock for unlocking tables */
|
2022-06-20 06:06:45 +00:00
|
|
|
public function unlock(): bool
|
2021-05-24 18:30:56 +00:00
|
|
|
{
|
|
|
|
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
|
|
|
|
$this->performCommit();
|
|
|
|
|
|
|
|
$this->in_transaction = false;
|
|
|
|
$this->_locked = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:37:24 +00:00
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* Does a commit
|
2019-07-27 12:37:24 +00:00
|
|
|
*
|
2020-10-18 18:31:57 +00:00
|
|
|
* @return bool Was the command executed successfully?
|
2019-07-27 12:37:24 +00:00
|
|
|
*/
|
2022-06-20 06:06:45 +00:00
|
|
|
public function commit(): bool
|
2019-07-27 12:37:24 +00:00
|
|
|
{
|
|
|
|
if (!$this->performCommit()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->in_transaction = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:40:42 +00:00
|
|
|
/**
|
|
|
|
* Setup of the global, static connection
|
|
|
|
* Either through explicit calling or through implicit using the Database
|
|
|
|
*
|
|
|
|
* @param array $server $_SERVER variables
|
2020-10-18 18:31:57 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2019-07-28 15:40:42 +00:00
|
|
|
*/
|
|
|
|
public static function statConnect(array $server)
|
|
|
|
{
|
2022-06-21 16:40:26 +00:00
|
|
|
// Init variables
|
|
|
|
$db_host = $db_user = $db_data = $db_pw = '';
|
|
|
|
|
2019-07-28 15:40:42 +00:00
|
|
|
// Use environment variables for mysql if they are set beforehand
|
|
|
|
if (!empty($server['MYSQL_HOST'])
|
2020-08-19 10:28:34 +00:00
|
|
|
&& (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
|
2019-07-28 15:40:42 +00:00
|
|
|
&& $server['MYSQL_PASSWORD'] !== false
|
|
|
|
&& !empty($server['MYSQL_DATABASE']))
|
|
|
|
{
|
|
|
|
$db_host = $server['MYSQL_HOST'];
|
|
|
|
if (!empty($server['MYSQL_PORT'])) {
|
|
|
|
$db_host .= ':' . $server['MYSQL_PORT'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($server['MYSQL_USERNAME'])) {
|
|
|
|
$db_user = $server['MYSQL_USERNAME'];
|
|
|
|
} else {
|
|
|
|
$db_user = $server['MYSQL_USER'];
|
|
|
|
}
|
|
|
|
$db_pw = (string) $server['MYSQL_PASSWORD'];
|
|
|
|
$db_data = $server['MYSQL_DATABASE'];
|
|
|
|
}
|
|
|
|
|
2020-10-18 18:31:57 +00:00
|
|
|
if (empty($db_host) || empty($db_user) || empty($db_data)) {
|
|
|
|
throw new DatabaseException('Either one of the following settings are missing: Host, User or Database', 999, 'CONNECT');
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:40:42 +00:00
|
|
|
$port = 0;
|
|
|
|
$serveraddr = trim($db_host);
|
|
|
|
$serverdata = explode(':', $serveraddr);
|
|
|
|
$server = $serverdata[0];
|
|
|
|
if (count($serverdata) > 1) {
|
2022-06-21 16:40:26 +00:00
|
|
|
$port = (int) trim($serverdata[1]);
|
2019-07-28 15:40:42 +00:00
|
|
|
}
|
|
|
|
$server = trim($server);
|
|
|
|
$user = trim($db_user);
|
2022-06-21 16:40:26 +00:00
|
|
|
$pass = trim($db_pw);
|
2019-07-28 15:40:42 +00:00
|
|
|
$db = trim($db_data);
|
|
|
|
|
2022-06-21 16:40:26 +00:00
|
|
|
if (!(strlen($server) && strlen($user) && strlen($db))) {
|
2019-07-28 15:40:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$connect = "mysql:host=" . $server . ";dbname=" . $db;
|
|
|
|
|
|
|
|
if ($port > 0) {
|
|
|
|
$connect .= ";port=" . $port;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
|
|
|
|
self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
|
|
|
|
} catch (PDOException $e) {
|
2022-06-21 19:12:43 +00:00
|
|
|
/*
|
2023-03-22 04:08:54 +00:00
|
|
|
* @TODO Try to find a way to log this exception as it contains valuable information
|
2022-06-21 19:12:43 +00:00
|
|
|
* @nupplaphil@github.com comment:
|
|
|
|
*
|
|
|
|
* There is no easy possibility to add a logger here, that's why
|
|
|
|
* there isn't any yet and instead a placeholder.. This execution
|
|
|
|
* point is a critical state during a testrun, and tbh I'd like to
|
|
|
|
* leave here no further logic (yet) because I spent hours debugging
|
|
|
|
* cases, where transactions weren't fully closed and
|
|
|
|
* strange/unpredictable errors occur (sometimes -mainly during
|
|
|
|
* debugging other errors :) ...)
|
|
|
|
*/
|
2019-07-28 15:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:37:24 +00:00
|
|
|
/**
|
2019-07-27 13:35:53 +00:00
|
|
|
* @return ExtendedPDO The global, static connection
|
2019-07-27 12:37:24 +00:00
|
|
|
*/
|
|
|
|
public static function getGlobConnection()
|
|
|
|
{
|
|
|
|
return self::$staticConnection;
|
|
|
|
}
|
|
|
|
|
2019-07-27 13:35:53 +00:00
|
|
|
/**
|
|
|
|
* Perform a global rollback for every nested transaction of the static connection
|
|
|
|
*/
|
2019-07-27 12:37:24 +00:00
|
|
|
public static function statRollback()
|
|
|
|
{
|
|
|
|
if (isset(self::$staticConnection)) {
|
|
|
|
while (self::$staticConnection->getTransactionDepth() > 0) {
|
2024-02-25 04:46:54 +00:00
|
|
|
self::$staticConnection->rollback();
|
2019-07-27 12:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|