3 namespace Friendica\Test\Util\Database;
5 use Friendica\Database\Database;
9 class StaticDatabase extends Database
14 private static $staticConnection;
17 * Override the behaviour of connect, due there is just one, static connection at all
21 public function connect()
23 if (!is_null($this->connection) && $this->connected()) {
27 if (!isset(self::$staticConnection)) {
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]);
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'));
42 if (!(strlen($server) && strlen($user))) {
46 $connect = "mysql:host=" . $server . ";dbname=" . $db;
49 $connect .= ";port=" . $port;
53 $connect .= ";charset=" . $charset;
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!
65 $this->driver = 'pdo';
66 $this->connection = self::$staticConnection;
67 $this->connected = true;
69 return $this->connected;
73 * Override the transaction since there are now hierachical transactions possible
77 public function transaction()
79 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
83 $this->in_transaction = true;
88 * @brief Does a commit
90 * @return boolean Was the command executed successfully?
92 public function commit()
94 if (!$this->performCommit()) {
97 $this->in_transaction = false;
102 * @return ExtendedPDO
104 public static function getGlobConnection()
106 return self::$staticConnection;
109 public static function statCommit()
111 if (isset(self::$staticConnection)) {
112 while (self::$staticConnection->getTransactionDepth() > 0) {
113 self::$staticConnection->commit();
118 public static function statRollback()
120 if (isset(self::$staticConnection)) {
121 while (self::$staticConnection->getTransactionDepth() > 0) {
122 self::$staticConnection->rollBack();