]> git.mxchange.org Git - friendica.git/blob - tests/Util/Database/StaticDatabase.php
Add some phpdoc
[friendica.git] / tests / Util / Database / StaticDatabase.php
1 <?php
2
3 namespace Friendica\Test\Util\Database;
4
5 use Friendica\Database\Database;
6 use PDO;
7 use PDOException;
8
9 /**
10  * Overrides the Friendica database class for re-using the connection
11  * for different tests
12  */
13 class StaticDatabase extends Database
14 {
15         /**
16          * @var ExtendedPDO
17          */
18         private static $staticConnection;
19
20         /**
21          * Override the behaviour of connect, due there is just one, static connection at all
22          *
23          * @return bool|void
24          */
25         public function connect()
26         {
27                 if (!is_null($this->connection) && $this->connected()) {
28                         return true;
29                 }
30
31                 if (!isset(self::$staticConnection)) {
32
33                         $port       = 0;
34                         $serveraddr = trim($this->configCache->get('database', 'hostname'));
35                         $serverdata = explode(':', $serveraddr);
36                         $server     = $serverdata[0];
37                         if (count($serverdata) > 1) {
38                                 $port = trim($serverdata[1]);
39                         }
40                         $server  = trim($server);
41                         $user    = trim($this->configCache->get('database', 'username'));
42                         $pass    = trim($this->configCache->get('database', 'password'));
43                         $db      = trim($this->configCache->get('database', 'database'));
44                         $charset = trim($this->configCache->get('database', 'charset'));
45
46                         if (!(strlen($server) && strlen($user))) {
47                                 return false;
48                         }
49
50                         $connect = "mysql:host=" . $server . ";dbname=" . $db;
51
52                         if ($port > 0) {
53                                 $connect .= ";port=" . $port;
54                         }
55
56                         if ($charset) {
57                                 $connect .= ";charset=" . $charset;
58                         }
59
60
61                         try {
62                                 self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
63                                 self::$staticConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
64                         } catch (PDOException $e) {
65                                 /// @TODO At least log exception, don't ignore it!
66                         }
67                 }
68
69                 $this->driver = 'pdo';
70                 $this->connection = self::$staticConnection;
71                 $this->connected = true;
72
73                 return $this->connected;
74         }
75
76         /**
77          * Override the transaction since there are now hierachical transactions possible
78          *
79          * @return bool
80          */
81         public function transaction()
82         {
83                 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
84                         return false;
85                 }
86
87                 $this->in_transaction = true;
88                 return true;
89         }
90
91         /**
92          * @brief Does a commit
93          *
94          * @return boolean Was the command executed successfully?
95          */
96         public function commit()
97         {
98                 if (!$this->performCommit()) {
99                         return false;
100                 }
101                 $this->in_transaction = false;
102                 return true;
103         }
104
105         /**
106          * @return ExtendedPDO The global, static connection
107          */
108         public static function getGlobConnection()
109         {
110                 return self::$staticConnection;
111         }
112
113         /**
114          * Perform a global commit for every nested transaction of the static connection
115          */
116         public static function statCommit()
117         {
118                 if (isset(self::$staticConnection)) {
119                         while (self::$staticConnection->getTransactionDepth() > 0) {
120                                 self::$staticConnection->commit();
121                         }
122                 }
123         }
124
125         /**
126          * Perform a global rollback for every nested transaction of the static connection
127          */
128         public static function statRollback()
129         {
130                 if (isset(self::$staticConnection)) {
131                         while (self::$staticConnection->getTransactionDepth() > 0) {
132                                 self::$staticConnection->rollBack();
133                         }
134                 }
135         }
136 }