]> git.mxchange.org Git - friendica.git/blob - tests/Util/Database/StaticDatabase.php
Merge pull request #8271 from MrPetovan/bug/8229-frio-mobile-back-to-top
[friendica.git] / tests / Util / Database / StaticDatabase.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\Util\Database;
23
24 use Friendica\Database\Database;
25 use PDO;
26 use PDOException;
27
28 /**
29  * Overrides the Friendica database class for re-using the connection
30  * for different tests
31  *
32  * Overrides functionality to enforce one transaction per call (for nested transactions)
33  */
34 class StaticDatabase extends Database
35 {
36         /**
37          * @var ExtendedPDO
38          */
39         private static $staticConnection;
40
41         /**
42          * Override the behaviour of connect, due there is just one, static connection at all
43          *
44          * @return bool|void
45          */
46         public function connect()
47         {
48                 if (!is_null($this->connection) && $this->connected()) {
49                         return true;
50                 }
51
52                 if (!isset(self::$staticConnection)) {
53                         self::statConnect($_SERVER);
54                 }
55
56                 $this->driver = 'pdo';
57                 $this->connection = self::$staticConnection;
58                 $this->connected = true;
59
60                 return $this->connected;
61         }
62
63         /**
64          * Override the transaction since there are now hierachical transactions possible
65          *
66          * @return bool
67          */
68         public function transaction()
69         {
70                 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
71                         return false;
72                 }
73
74                 $this->in_transaction = true;
75                 return true;
76         }
77
78         /**
79          * Does a commit
80          *
81          * @return boolean Was the command executed successfully?
82          */
83         public function commit()
84         {
85                 if (!$this->performCommit()) {
86                         return false;
87                 }
88                 $this->in_transaction = false;
89                 return true;
90         }
91
92         /**
93          * Setup of the global, static connection
94          * Either through explicit calling or through implicit using the Database
95          *
96          * @param array $server $_SERVER variables
97          */
98         public static function statConnect(array $server)
99         {
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']))
105                 {
106                         $db_host = $server['MYSQL_HOST'];
107                         if (!empty($server['MYSQL_PORT'])) {
108                                 $db_host .= ':' . $server['MYSQL_PORT'];
109                         }
110
111                         if (!empty($server['MYSQL_USERNAME'])) {
112                                 $db_user = $server['MYSQL_USERNAME'];
113                         } else {
114                                 $db_user = $server['MYSQL_USER'];
115                         }
116                         $db_pw = (string) $server['MYSQL_PASSWORD'];
117                         $db_data = $server['MYSQL_DATABASE'];
118                 }
119
120                 $port       = 0;
121                 $serveraddr = trim($db_host);
122                 $serverdata = explode(':', $serveraddr);
123                 $server     = $serverdata[0];
124                 if (count($serverdata) > 1) {
125                         $port = trim($serverdata[1]);
126                 }
127                 $server  = trim($server);
128                 $user    = trim($db_user);
129                 $pass    = trim($db_pw);
130                 $db      = trim($db_data);
131
132                 if (!(strlen($server) && strlen($user))) {
133                         return;
134                 }
135
136                 $connect = "mysql:host=" . $server . ";dbname=" . $db;
137
138                 if ($port > 0) {
139                         $connect .= ";port=" . $port;
140                 }
141
142                 try {
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!
147                 }
148         }
149
150         /**
151          * @return ExtendedPDO The global, static connection
152          */
153         public static function getGlobConnection()
154         {
155                 return self::$staticConnection;
156         }
157
158         /**
159          * Perform a global commit for every nested transaction of the static connection
160          */
161         public static function statCommit()
162         {
163                 if (isset(self::$staticConnection)) {
164                         while (self::$staticConnection->getTransactionDepth() > 0) {
165                                 self::$staticConnection->commit();
166                         }
167                 }
168         }
169
170         /**
171          * Perform a global rollback for every nested transaction of the static connection
172          */
173         public static function statRollback()
174         {
175                 if (isset(self::$staticConnection)) {
176                         while (self::$staticConnection->getTransactionDepth() > 0) {
177                                 self::$staticConnection->rollBack();
178                         }
179                 }
180         }
181 }