|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bolt\Storage\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Bolt\Events\FailedConnectionEvent; |
|
6
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile; |
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Extension of DBAL's Connection class to allow catching of database connection |
|
11
|
|
|
* exceptions. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Gawain Lynch <[email protected]> |
|
14
|
|
|
* @author Carson Full <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Connection extends \Doctrine\DBAL\Connection |
|
17
|
|
|
{ |
|
18
|
|
|
protected $_queryCacheProfile; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function connect() |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
|
|
return parent::connect(); |
|
27
|
|
|
} catch (DBALException $e) { |
|
28
|
|
|
if ($this->_eventManager->hasListeners('failConnect')) { |
|
29
|
|
|
$eventArgs = new FailedConnectionEvent($this, $e); |
|
30
|
|
|
$this->_eventManager->dispatchEvent('failConnect', $eventArgs); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* This method wraps the native fetchAll method to pass in the configured QueryCacheProfile. |
|
37
|
|
|
* If the profile is set to null then operation will continue identically to the standard, otherwise |
|
38
|
|
|
* the existence of a cache profile will result in the executeQueryCache() method being called. |
|
39
|
|
|
* |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $sql The SQL query. |
|
42
|
|
|
* @param array $params The query parameters. |
|
43
|
|
|
* @param array $types The query parameter types. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function fetchAll($sql, array $params = [], $types = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$stmt = $this->executeQuery($sql, $params, $types, $this->_queryCacheProfile); |
|
50
|
|
|
$result = $stmt->fetchAll(); |
|
51
|
|
|
$stmt->closeCursor(); |
|
52
|
|
|
|
|
53
|
|
|
return $result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
58
|
|
|
* and returns the number of affected rows. |
|
59
|
|
|
* |
|
60
|
|
|
* This method supports PDO binding types as well as DBAL mapping types. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $query The SQL query. |
|
63
|
|
|
* @param array $params The query parameters. |
|
64
|
|
|
* @param array $types The parameter types. |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
67
|
|
|
* |
|
68
|
|
|
* @return integer The number of affected rows. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function executeUpdate($query, array $params = [], array $types = []) |
|
71
|
|
|
{ |
|
72
|
|
|
$result = parent::executeUpdate($query, $params, $types); |
|
73
|
|
|
$this->_queryCacheProfile->getResultCacheDriver()->flushAll(); |
|
74
|
|
|
|
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets an optional Query Cache handler on the connection class |
|
80
|
|
|
* |
|
81
|
|
|
* @param QueryCacheProfile $profile |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setQueryCacheProfile(QueryCacheProfile $profile) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->_queryCacheProfile = $profile; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|