3 * @copyright Copyright (C) 2020, Friendica
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\DBStructure;
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 * Override the behaviour of connect, due there is just one, static connection at all
47 public function connect()
49 if (!is_null($this->connection) && $this->connected()) {
53 if (!isset(self::$staticConnection)) {
54 self::statConnect($_SERVER);
57 $this->driver = 'pdo';
58 $this->connection = self::$staticConnection;
59 $this->connected = true;
61 $this->setTestmode(true);
63 DBStructure::checkInitialValues();
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;
86 * @return boolean Was the command executed successfully?
88 public function commit()
90 if (!$this->performCommit()) {
93 $this->in_transaction = false;
98 * Setup of the global, static connection
99 * Either through explicit calling or through implicit using the Database
101 * @param array $server $_SERVER variables
103 public static function statConnect(array $server)
105 // Use environment variables for mysql if they are set beforehand
106 if (!empty($server['MYSQL_HOST'])
107 && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
108 && $server['MYSQL_PASSWORD'] !== false
109 && !empty($server['MYSQL_DATABASE']))
111 $db_host = $server['MYSQL_HOST'];
112 if (!empty($server['MYSQL_PORT'])) {
113 $db_host .= ':' . $server['MYSQL_PORT'];
116 if (!empty($server['MYSQL_USERNAME'])) {
117 $db_user = $server['MYSQL_USERNAME'];
119 $db_user = $server['MYSQL_USER'];
121 $db_pw = (string) $server['MYSQL_PASSWORD'];
122 $db_data = $server['MYSQL_DATABASE'];
126 $serveraddr = trim($db_host);
127 $serverdata = explode(':', $serveraddr);
128 $server = $serverdata[0];
129 if (count($serverdata) > 1) {
130 $port = trim($serverdata[1]);
132 $server = trim($server);
133 $user = trim($db_user);
134 $pass = trim($db_pw);
135 $db = trim($db_data);
137 if (!(strlen($server) && strlen($user))) {
141 $connect = "mysql:host=" . $server . ";dbname=" . $db;
144 $connect .= ";port=" . $port;
148 self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
149 self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
150 } catch (PDOException $e) {
151 /// @TODO At least log exception, don't ignore it!
156 * @return ExtendedPDO The global, static connection
158 public static function getGlobConnection()
160 return self::$staticConnection;
164 * Perform a global commit for every nested transaction of the static connection
166 public static function statCommit()
168 if (isset(self::$staticConnection)) {
169 while (self::$staticConnection->getTransactionDepth() > 0) {
170 self::$staticConnection->commit();
176 * Perform a global rollback for every nested transaction of the static connection
178 public static function statRollback()
180 if (isset(self::$staticConnection)) {
181 while (self::$staticConnection->getTransactionDepth() > 0) {
182 self::$staticConnection->rollBack();