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;
59 $this->emulate_prepares = false;
60 $this->pdo_emulate_prepares = false;
62 return $this->connected;
66 * Override the transaction since there are now hierachical transactions possible
70 public function transaction()
72 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
76 $this->in_transaction = true;
83 * @return boolean Was the command executed successfully?
85 public function commit()
87 if (!$this->performCommit()) {
90 $this->in_transaction = false;
95 * Setup of the global, static connection
96 * Either through explicit calling or through implicit using the Database
98 * @param array $server $_SERVER variables
100 public static function statConnect(array $server)
102 // Use environment variables for mysql if they are set beforehand
103 if (!empty($server['MYSQL_HOST'])
104 && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
105 && $server['MYSQL_PASSWORD'] !== false
106 && !empty($server['MYSQL_DATABASE']))
108 $db_host = $server['MYSQL_HOST'];
109 if (!empty($server['MYSQL_PORT'])) {
110 $db_host .= ':' . $server['MYSQL_PORT'];
113 if (!empty($server['MYSQL_USERNAME'])) {
114 $db_user = $server['MYSQL_USERNAME'];
116 $db_user = $server['MYSQL_USER'];
118 $db_pw = (string) $server['MYSQL_PASSWORD'];
119 $db_data = $server['MYSQL_DATABASE'];
123 $serveraddr = trim($db_host);
124 $serverdata = explode(':', $serveraddr);
125 $server = $serverdata[0];
126 if (count($serverdata) > 1) {
127 $port = trim($serverdata[1]);
129 $server = trim($server);
130 $user = trim($db_user);
131 $pass = trim($db_pw);
132 $db = trim($db_data);
134 if (!(strlen($server) && strlen($user))) {
138 $connect = "mysql:host=" . $server . ";dbname=" . $db;
141 $connect .= ";port=" . $port;
145 self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
146 self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
147 } catch (PDOException $e) {
148 /// @TODO At least log exception, don't ignore it!
153 * @return ExtendedPDO The global, static connection
155 public static function getGlobConnection()
157 return self::$staticConnection;
161 * Perform a global commit for every nested transaction of the static connection
163 public static function statCommit()
165 if (isset(self::$staticConnection)) {
166 while (self::$staticConnection->getTransactionDepth() > 0) {
167 self::$staticConnection->commit();
173 * Perform a global rollback for every nested transaction of the static connection
175 public static function statRollback()
177 if (isset(self::$staticConnection)) {
178 while (self::$staticConnection->getTransactionDepth() > 0) {
179 self::$staticConnection->rollBack();