3 namespace Friendica\Test\Util\Database;
5 use Friendica\Database\Database;
10 * Overrides the Friendica database class for re-using the connection
13 class StaticDatabase extends Database
18 private static $staticConnection;
21 * Override the behaviour of connect, due there is just one, static connection at all
25 public function connect()
27 if (!is_null($this->connection) && $this->connected()) {
31 if (!isset(self::$staticConnection)) {
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]);
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'));
46 if (!(strlen($server) && strlen($user))) {
50 $connect = "mysql:host=" . $server . ";dbname=" . $db;
53 $connect .= ";port=" . $port;
57 $connect .= ";charset=" . $charset;
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!
69 $this->driver = 'pdo';
70 $this->connection = self::$staticConnection;
71 $this->connected = true;
73 return $this->connected;
77 * Override the transaction since there are now hierachical transactions possible
81 public function transaction()
83 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
87 $this->in_transaction = true;
92 * @brief Does a commit
94 * @return boolean Was the command executed successfully?
96 public function commit()
98 if (!$this->performCommit()) {
101 $this->in_transaction = false;
106 * @return ExtendedPDO The global, static connection
108 public static function getGlobConnection()
110 return self::$staticConnection;
114 * Perform a global commit for every nested transaction of the static connection
116 public static function statCommit()
118 if (isset(self::$staticConnection)) {
119 while (self::$staticConnection->getTransactionDepth() > 0) {
120 self::$staticConnection->commit();
126 * Perform a global rollback for every nested transaction of the static connection
128 public static function statRollback()
130 if (isset(self::$staticConnection)) {
131 while (self::$staticConnection->getTransactionDepth() > 0) {
132 self::$staticConnection->rollBack();