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;
29 * Overrides the Friendica database class for re-using the connection
32 * Overrides functionality to enforce one transaction per call (for nested transactions)
34 class StaticDatabase extends Database
39 private static $staticConnection;
42 * Override the behaviour of connect, due there is just one, static connection at all
46 public function connect()
48 if (!is_null($this->connection) && $this->connected()) {
52 if (!isset(self::$staticConnection)) {
53 self::statConnect($_SERVER);
56 $this->driver = 'pdo';
57 $this->connection = self::$staticConnection;
58 $this->connected = true;
60 return $this->connected;
64 * Override the transaction since there are now hierachical transactions possible
68 public function transaction()
70 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
74 $this->in_transaction = true;
81 * @return boolean Was the command executed successfully?
83 public function commit()
85 if (!$this->performCommit()) {
88 $this->in_transaction = false;
93 * Setup of the global, static connection
94 * Either through explicit calling or through implicit using the Database
96 * @param array $server $_SERVER variables
98 public static function statConnect(array $server)
100 // Use environment variables for mysql if they are set beforehand
101 if (!empty($server['MYSQL_HOST'])
102 && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
103 && $server['MYSQL_PASSWORD'] !== false
104 && !empty($server['MYSQL_DATABASE']))
106 $db_host = $server['MYSQL_HOST'];
107 if (!empty($server['MYSQL_PORT'])) {
108 $db_host .= ':' . $server['MYSQL_PORT'];
111 if (!empty($server['MYSQL_USERNAME'])) {
112 $db_user = $server['MYSQL_USERNAME'];
114 $db_user = $server['MYSQL_USER'];
116 $db_pw = (string) $server['MYSQL_PASSWORD'];
117 $db_data = $server['MYSQL_DATABASE'];
121 $serveraddr = trim($db_host);
122 $serverdata = explode(':', $serveraddr);
123 $server = $serverdata[0];
124 if (count($serverdata) > 1) {
125 $port = trim($serverdata[1]);
127 $server = trim($server);
128 $user = trim($db_user);
129 $pass = trim($db_pw);
130 $db = trim($db_data);
132 if (!(strlen($server) && strlen($user))) {
136 $connect = "mysql:host=" . $server . ";dbname=" . $db;
139 $connect .= ";port=" . $port;
143 self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
144 self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
145 } catch (PDOException $e) {
146 /// @TODO At least log exception, don't ignore it!
151 * @return ExtendedPDO The global, static connection
153 public static function getGlobConnection()
155 return self::$staticConnection;
159 * Perform a global commit for every nested transaction of the static connection
161 public static function statCommit()
163 if (isset(self::$staticConnection)) {
164 while (self::$staticConnection->getTransactionDepth() > 0) {
165 self::$staticConnection->commit();
171 * Perform a global rollback for every nested transaction of the static connection
173 public static function statRollback()
175 if (isset(self::$staticConnection)) {
176 while (self::$staticConnection->getTransactionDepth() > 0) {
177 self::$staticConnection->rollBack();