|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @fabfuel <[email protected]> |
|
4
|
|
|
* @created 07.12.14, 13:12 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Fabfuel\Prophiler\Decorator\PDO; |
|
7
|
|
|
|
|
8
|
|
|
use Fabfuel\Prophiler\ProfilerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PDO |
|
12
|
|
|
* @package Fabfuel\Prophiler\Adapter\Pdo |
|
13
|
|
|
* @pattern decorator |
|
14
|
|
|
* @mixin \PDOStatement |
|
15
|
|
|
*/ |
|
16
|
|
|
class PDO |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \PDO |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $pdo; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ProfilerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $profiler; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param \PDO $pdo |
|
30
|
|
|
* @param ProfilerInterface $profiler |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function __construct(\PDO $pdo, ProfilerInterface $profiler) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->setPdo($pdo); |
|
35
|
1 |
|
$this->setProfiler($profiler); |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param array $arguments |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function __call($name, array $arguments) |
|
44
|
|
|
{ |
|
45
|
1 |
|
return call_user_func_array([$this->getPdo(), $name], $arguments); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $statement |
|
50
|
|
|
* @return \PDOStatement |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function query($statement) |
|
53
|
|
|
{ |
|
54
|
1 |
|
$metadata = ['statement' => $statement]; |
|
55
|
1 |
|
$benchmark = $this->getProfiler()->start('PDO::query', [], 'Database'); |
|
56
|
1 |
|
$result = $this->getPdo()->query($statement); |
|
57
|
1 |
|
if ($result instanceof \PDOStatement) { |
|
58
|
1 |
|
$metadata['rows'] = $result->rowCount(); |
|
59
|
1 |
|
$metadata['columns'] = $result->columnCount(); |
|
60
|
1 |
|
} |
|
61
|
1 |
|
$this->getProfiler()->stop($benchmark, $metadata); |
|
62
|
1 |
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $statement |
|
67
|
|
|
* @return int Number of rows affected |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function exec($statement) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$metadata = ['statement' => $statement]; |
|
72
|
1 |
|
$benchmark = $this->getProfiler()->start('PDO::exec', [], 'Database'); |
|
73
|
1 |
|
$result = $this->getPdo()->exec($statement); |
|
74
|
1 |
|
if ($result !== false) { |
|
75
|
1 |
|
$metadata['affected rows'] = $result; |
|
76
|
1 |
|
} |
|
77
|
1 |
|
$this->getProfiler()->stop($benchmark, $metadata); |
|
78
|
1 |
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $statement |
|
83
|
|
|
* @param array $options |
|
84
|
|
|
* @return PDOStatement|bool |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function prepare($statement, $options = null) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$benchmark = $this->getProfiler()->start('PDO::prepare', ['statement' => $statement], 'Database'); |
|
89
|
2 |
|
$pdoStatement = $this->getPdo()->prepare($statement, $options ?: []); |
|
90
|
2 |
|
$this->getProfiler()->stop($benchmark); |
|
91
|
2 |
|
if (!$pdoStatement) { |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
1 |
|
$profiledStatement = new PDOStatement($pdoStatement, $this->getProfiler()); |
|
95
|
1 |
|
return $profiledStatement; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return \PDO |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getPdo() |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->pdo; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param \PDO $pdo |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function setPdo(\PDO $pdo) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->pdo = $pdo; |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return ProfilerInterface |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function getProfiler() |
|
118
|
|
|
{ |
|
119
|
1 |
|
return $this->profiler; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param ProfilerInterface $profiler |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function setProfiler(ProfilerInterface $profiler) |
|
126
|
|
|
{ |
|
127
|
1 |
|
$this->profiler = $profiler; |
|
128
|
1 |
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|