c95b690c635801bd04039ac9d19f93eb920cdb83
[friendica.git/.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                 $this->emulate_prepares = false;
60                 $this->pdo_emulate_prepares = false;
61
62                 return $this->connected;
63         }
64
65         /**
66          * Override the transaction since there are now hierachical transactions possible
67          *
68          * @return bool
69          */
70         public function transaction()
71         {
72                 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
73                         return false;
74                 }
75
76                 $this->in_transaction = true;
77                 return true;
78         }
79
80         /**
81          * Does a commit
82          *
83          * @return boolean Was the command executed successfully?
84          */
85         public function commit()
86         {
87                 if (!$this->performCommit()) {
88                         return false;
89                 }
90                 $this->in_transaction = false;
91                 return true;
92         }
93
94         /**
95          * Setup of the global, static connection
96          * Either through explicit calling or through implicit using the Database
97          *
98          * @param array $server $_SERVER variables
99          */
100         public static function statConnect(array $server)
101         {
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']))
107                 {
108                         $db_host = $server['MYSQL_HOST'];
109                         if (!empty($server['MYSQL_PORT'])) {
110                                 $db_host .= ':' . $server['MYSQL_PORT'];
111                         }
112
113                         if (!empty($server['MYSQL_USERNAME'])) {
114                                 $db_user = $server['MYSQL_USERNAME'];
115                         } else {
116                                 $db_user = $server['MYSQL_USER'];
117                         }
118                         $db_pw = (string) $server['MYSQL_PASSWORD'];
119                         $db_data = $server['MYSQL_DATABASE'];
120                 }
121
122                 $port       = 0;
123                 $serveraddr = trim($db_host);
124                 $serverdata = explode(':', $serveraddr);
125                 $server     = $serverdata[0];
126                 if (count($serverdata) > 1) {
127                         $port = trim($serverdata[1]);
128                 }
129                 $server  = trim($server);
130                 $user    = trim($db_user);
131                 $pass    = trim($db_pw);
132                 $db      = trim($db_data);
133
134                 if (!(strlen($server) && strlen($user))) {
135                         return;
136                 }
137
138                 $connect = "mysql:host=" . $server . ";dbname=" . $db;
139
140                 if ($port > 0) {
141                         $connect .= ";port=" . $port;
142                 }
143
144                 try {
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!
149                 }
150         }
151
152         /**
153          * @return ExtendedPDO The global, static connection
154          */
155         public static function getGlobConnection()
156         {
157                 return self::$staticConnection;
158         }
159
160         /**
161          * Perform a global commit for every nested transaction of the static connection
162          */
163         public static function statCommit()
164         {
165                 if (isset(self::$staticConnection)) {
166                         while (self::$staticConnection->getTransactionDepth() > 0) {
167                                 self::$staticConnection->commit();
168                         }
169                 }
170         }
171
172         /**
173          * Perform a global rollback for every nested transaction of the static connection
174          */
175         public static function statRollback()
176         {
177                 if (isset(self::$staticConnection)) {
178                         while (self::$staticConnection->getTransactionDepth() > 0) {
179                                 self::$staticConnection->rollBack();
180                         }
181                 }
182         }
183 }