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