3 namespace Friendica\Test\Util\Database;
5 use Friendica\Database\Database;
10 * Overrides the Friendica database class for re-using the connection
13 * Overrides functionality to enforce one transaction per call (for nested transactions)
15 class StaticDatabase extends Database
20 private static $staticConnection;
23 * Override the behaviour of connect, due there is just one, static connection at all
27 public function connect()
29 if (!is_null($this->connection) && $this->connected()) {
33 if (!isset(self::$staticConnection)) {
34 self::statConnect($_SERVER);
37 $this->driver = 'pdo';
38 $this->connection = self::$staticConnection;
39 $this->connected = true;
41 return $this->connected;
45 * Override the transaction since there are now hierachical transactions possible
49 public function transaction()
51 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
55 $this->in_transaction = true;
62 * @return boolean Was the command executed successfully?
64 public function commit()
66 if (!$this->performCommit()) {
69 $this->in_transaction = false;
74 * Setup of the global, static connection
75 * Either through explicit calling or through implicit using the Database
77 * @param array $server $_SERVER variables
79 public static function statConnect(array $server)
81 // Use environment variables for mysql if they are set beforehand
82 if (!empty($server['MYSQL_HOST'])
83 && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
84 && $server['MYSQL_PASSWORD'] !== false
85 && !empty($server['MYSQL_DATABASE']))
87 $db_host = $server['MYSQL_HOST'];
88 if (!empty($server['MYSQL_PORT'])) {
89 $db_host .= ':' . $server['MYSQL_PORT'];
92 if (!empty($server['MYSQL_USERNAME'])) {
93 $db_user = $server['MYSQL_USERNAME'];
95 $db_user = $server['MYSQL_USER'];
97 $db_pw = (string) $server['MYSQL_PASSWORD'];
98 $db_data = $server['MYSQL_DATABASE'];
102 $serveraddr = trim($db_host);
103 $serverdata = explode(':', $serveraddr);
104 $server = $serverdata[0];
105 if (count($serverdata) > 1) {
106 $port = trim($serverdata[1]);
108 $server = trim($server);
109 $user = trim($db_user);
110 $pass = trim($db_pw);
111 $db = trim($db_data);
113 if (!(strlen($server) && strlen($user))) {
117 $connect = "mysql:host=" . $server . ";dbname=" . $db;
120 $connect .= ";port=" . $port;
124 self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
125 self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
126 } catch (PDOException $e) {
127 /// @TODO At least log exception, don't ignore it!
132 * @return ExtendedPDO The global, static connection
134 public static function getGlobConnection()
136 return self::$staticConnection;
140 * Perform a global commit for every nested transaction of the static connection
142 public static function statCommit()
144 if (isset(self::$staticConnection)) {
145 while (self::$staticConnection->getTransactionDepth() > 0) {
146 self::$staticConnection->commit();
152 * Perform a global rollback for every nested transaction of the static connection
154 public static function statRollback()
156 if (isset(self::$staticConnection)) {
157 while (self::$staticConnection->getTransactionDepth() > 0) {
158 self::$staticConnection->rollBack();