3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\Util\Database;
24 use Friendica\Database\Database;
25 use Friendica\Database\DatabaseException;
30 * Overrides the Friendica database class for re-using the connection
33 * Overrides functionality to enforce one transaction per call (for nested transactions)
35 class StaticDatabase extends Database
40 private static $staticConnection;
43 private $_locked = false;
46 * Override the behaviour of connect, due there is just one, static connection at all
50 public function connect()
52 if (!is_null($this->connection) && $this->connected()) {
56 if (!isset(self::$staticConnection)) {
57 self::statConnect($_SERVER);
60 $this->driver = 'pdo';
61 $this->connection = self::$staticConnection;
62 $this->connected = true;
63 $this->emulate_prepares = false;
65 return $this->connected;
69 * Override the transaction since there are now hierachical transactions possible
73 public function transaction()
75 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
79 $this->in_transaction = true;
83 /** Mock for locking tables */
84 public function lock($table)
90 $this->in_transaction = true;
91 $this->_locked = true;
96 /** Mock for unlocking tables */
97 public function unlock()
99 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
100 $this->performCommit();
102 $this->in_transaction = false;
103 $this->_locked = false;
111 * @return bool Was the command executed successfully?
113 public function commit()
115 if (!$this->performCommit()) {
118 $this->in_transaction = false;
123 * Setup of the global, static connection
124 * Either through explicit calling or through implicit using the Database
126 * @param array $server $_SERVER variables
130 public static function statConnect(array $server)
132 // Use environment variables for mysql if they are set beforehand
133 if (!empty($server['MYSQL_HOST'])
134 && (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
135 && $server['MYSQL_PASSWORD'] !== false
136 && !empty($server['MYSQL_DATABASE']))
138 $db_host = $server['MYSQL_HOST'];
139 if (!empty($server['MYSQL_PORT'])) {
140 $db_host .= ':' . $server['MYSQL_PORT'];
143 if (!empty($server['MYSQL_USERNAME'])) {
144 $db_user = $server['MYSQL_USERNAME'];
146 $db_user = $server['MYSQL_USER'];
148 $db_pw = (string) $server['MYSQL_PASSWORD'];
149 $db_data = $server['MYSQL_DATABASE'];
152 if (empty($db_host) || empty($db_user) || empty($db_data)) {
153 throw new DatabaseException('Either one of the following settings are missing: Host, User or Database', 999, 'CONNECT');
157 $serveraddr = trim($db_host);
158 $serverdata = explode(':', $serveraddr);
159 $server = $serverdata[0];
160 if (count($serverdata) > 1) {
161 $port = trim($serverdata[1]);
163 $server = trim($server);
164 $user = trim($db_user);
165 $pass = trim($db_pw ?? '');
166 $db = trim($db_data);
168 if (!(strlen($server) && strlen($user))) {
172 $connect = "mysql:host=" . $server . ";dbname=" . $db;
175 $connect .= ";port=" . $port;
179 self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
180 self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
181 } catch (PDOException $e) {
182 /// @TODO At least log exception, don't ignore it!
187 * @return ExtendedPDO The global, static connection
189 public static function getGlobConnection()
191 return self::$staticConnection;
195 * Perform a global rollback for every nested transaction of the static connection
197 public static function statRollback()
199 if (isset(self::$staticConnection)) {
200 while (self::$staticConnection->getTransactionDepth() > 0) {
201 self::$staticConnection->rollBack();