1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Db |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Db; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Db\Traits\DriverAwareTrait; |
19
|
|
|
use Phossa2\Db\Interfaces\ProfilerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Profiler |
23
|
|
|
* |
24
|
|
|
* @package Phossa2\Db |
25
|
|
|
* @author Hong Zhang <[email protected]> |
26
|
|
|
* @see ObjectAbstract |
27
|
|
|
* @see ProfilerInterface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
class Profiler extends ObjectAbstract implements ProfilerInterface |
32
|
|
|
{ |
33
|
|
|
use DriverAwareTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Current executed SQL |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected $sql = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Parameters cache |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
protected $params = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Execution time |
53
|
|
|
* |
54
|
|
|
* @var float |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
protected $execution_time = 0.0; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
*/ |
62
|
|
|
public function setSql(/*# string */ $sql) |
63
|
|
|
{ |
64
|
|
|
// init |
65
|
|
|
$this->sql = $sql; |
66
|
|
|
$this->params = []; |
67
|
|
|
$this->execution_time = 0.0; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function setParameters(array $parameters) |
76
|
|
|
{ |
77
|
|
|
$this->params = $parameters; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
|
|
public function getSql()/*# : string */ |
85
|
|
|
{ |
86
|
|
|
return empty($this->params) ? $this->sql : $this->replaceParamInSql(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
|
|
public function setExecutionTime(/*# float */ $time) |
93
|
|
|
{ |
94
|
|
|
$this->execution_time = (float) $time; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function getExecutionTime()/*# : float */ |
102
|
|
|
{ |
103
|
|
|
return $this->execution_time; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Replace params in the SQL |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
* @access protected |
111
|
|
|
*/ |
112
|
|
|
protected function replaceParamInSql()/*# : string */ |
113
|
|
|
{ |
114
|
|
|
$count = 0; |
115
|
|
|
$params = $this->params; |
116
|
|
|
return preg_replace_callback( |
117
|
|
|
'/\?|\:\w+/', function($m) use ($count, $params) { |
118
|
|
|
if ('?' === $m[0]) { |
119
|
|
|
$res = $params[$count++]; |
120
|
|
|
} else { |
121
|
|
|
$res = isset($params[$m[0]]) ? $params[$m[0]] : |
122
|
|
|
$params[substr($m[0],1)]; |
123
|
|
|
} |
124
|
|
|
return $this->getDriver()->quote($res); |
125
|
|
|
}, $this->sql); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|