55
75
$this->log = $logger;
59
public function setConnection($name = 'default')
82
public function setConnection()
61
84
// Create a new connection
62
$this->conn[$name] = PdoStorageService::newConnection();
85
$this->conn = PdoStorageService::newConnection();
67
public function close($name = null)
90
* Closes the stored connection
92
public function close()
69
if ($name === null && isset($this->conn[$name])) {
70
$this->conn[$name] = null;
71
unset($this->conn[$name]);
124
148
public function connect($host, $user, $pass, $name = null)
126
if (!isset($this->conn['default']))
127
$this->close('default');
129
154
$dsn = PdoStorageService::createDsn($host, $name);
131
156
// Open the connection and set the error mode
132
$this->conn['default'] = new \PDO($dsn, $user, $pass);
133
$this->conn['default']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
134
$this->conn['default']->query("SET NAMES 'utf8'");
136
return $this->conn['default'];
157
$this->conn = new \PDO($dsn, $user, $pass);
158
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
160
$this->conn->query("SET NAMES 'utf8'");
140
public function getConnection($name = 'default')
167
* Get the Raw Connection
170
public function getConnection()
142
if (!isset($this->conn[$name]))
143
$this->conn[$name] = PdoStorageService::newConnection();
145
return $this->conn[$name];
180
207
if ($this->log != null)
181
208
$this->log->sql($sql, $params);
183
if (!$this->getConnection()->inTransaction())
184
$this->getConnection()->beginTransaction();
210
if (!$this->conn->inTransaction())
211
$this->conn->beginTransaction();
186
$sth = $this->getConnection()->prepare($sql);
213
$sth = $this->conn->prepare($sql);
188
215
$sth->execute($params);
190
$this->incrementStat('default', 'insert');
217
self::$countInserts++;
192
return intval($this->getConnection()->lastInsertId());
219
return intval($this->conn->lastInsertId());
225
* @param array $params
226
* @param \PDO[Optional] $dbh
227
* @throws \PDOException
196
229
public function update($sql, $params)
198
231
if ($this->log != null)
199
232
$this->log->sql($sql, $params);
201
if (!$this->getConnection()->inTransaction())
202
$this->getConnection()->beginTransaction();
234
if (!$this->conn->inTransaction())
235
$this->conn->beginTransaction();
204
$sth = $this->getConnection()->prepare($sql);
237
$sth = $this->conn->prepare($sql);
206
239
$sth->execute($params);
208
$this->incrementStat('default', 'update');
241
self::$countUpdates++;
220
253
if ($this->log != null)
221
254
$this->log->sql($sql, $params);
223
$sth = $this->getConnection()->prepare($sql);
256
$sth = $this->conn->prepare($sql);
225
258
$sth->execute($params);
227
$this->incrementStat('default', 'select');
260
self::$countSelects++;
229
262
return $sth->fetchAll(\PDO::FETCH_ASSOC);
233
public function isolated($sql, $params)
236
if ($this->log != null)
237
$this->log->sql($sql, $params);
239
$sth = $this->getConnection('isolated')->prepare($sql);
241
$sth->execute($params);
243
$this->incrementStat('isolated', 'update');
247
public function updateWithDeadlockLoop($sql, $params, $connection = null)
250
if ($this->log != null)
251
$this->log->sql($sql, $params);
253
if ($connection === null)
254
$connection = 'isolated';
256
// Prepare the statement
257
$statement = $this->getConnection($connection)->prepare($sql);
259
// Deadlock protect this statement
264
$this->incrementStat($connection, 'update');
265
$statement->execute($params);
269
} catch (\PDOException $PDOException) {
270
$errorCode = isset($PDOException->errorInfo[1]) ? $PDOException->errorInfo[1] : $PDOException->getCode();
272
if ($errorCode != 1213 && $errorCode != 1205)
279
// Sleep a bit, give the DB time to breathe
280
$queryHash = substr($sql, 0, 15) . '... [' . md5($sql . json_encode($params)) . ']';
281
$this->log->debug('Retrying query after a short nap, try: ' . (3 - $retries) . '. Query Hash: ' . $queryHash);
284
} while ($retries--);
287
throw new DeadlockException(__('Failed to write to database after %d retries. Please try again later.', $retries));
291
public function commitIfNecessary($name = 'default')
293
if ($this->getConnection($name)->inTransaction()) {
294
$this->incrementStat($name, 'commit');
295
$this->getConnection()->commit();
266
* Commit if necessary
268
public function commitIfNecessary()
270
if ($this->conn->inTransaction())
271
$this->conn->commit();
300
275
* Set the TimeZone for this connection
301
* @param string $timeZone e.g. -8:00
302
* @param string $connection
304
public function setTimeZone($timeZone, $connection = 'default')
306
$this->getConnection($connection)->query('SET time_zone = \'' . $timeZone . '\';');
308
$this->incrementStat($connection, 'utility');
276
* @param \PDO $connection
277
* @param string $timeZone e.g. -8:00
279
public function setTimeZone($timeZone, $connection = null)
281
if ($connection == null)
282
$connection = $this->conn;
284
$connection->query('SET time_zone = \'' . $timeZone . '\';');
286
self::$countSelects++;
315
public function stats()
321
public function incrementStat($connection, $key)
323
$currentCount = (isset(self::$stats[$connection][$key])) ? self::$stats[$connection][$key] : 0;
324
self::$stats[$connection][$key] = $currentCount + 1;
328
* Statically increment stats
332
public static function incrementStatStatic($connection, $key)
334
$currentCount = (isset(self::$stats[$connection][$key])) ? self::$stats[$connection][$key] : 0;
335
self::$stats[$connection][$key] = $currentCount + 1;
293
public static function stats()
296
'connections' => self::$countConnections,
297
'selects' => self::$countSelects,
298
'inserts' => self::$countInserts,
299
'updates' => self::$countUpdates
b'\\ No newline at end of file'