1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Core\SqlExecutor\InProcess; |
4
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\InProcess\CommandExecutor; |
6
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\InProcess\FileExecutor; |
|
|
|
|
7
|
|
|
use Db3v4l\API\Interfaces\TimedExecutor as TimedExecutorInterface; |
8
|
|
|
|
9
|
|
|
class TimedExecutor implements TimedExecutorInterface, CommandExecutor, FileExecutor |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** @var CommandExecutor|FileExecutor $wrappedExecutor */ |
|
|
|
|
12
|
|
|
protected $wrappedExecutor; |
13
|
|
|
protected $timingData = []; |
14
|
|
|
|
15
|
|
|
/** |
|
|
|
|
16
|
|
|
* @param CommandExecutor|FileExecutor $wrappedExecutor |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
public function __construct($wrappedExecutor) |
19
|
|
|
{ |
20
|
|
|
$this->wrappedExecutor = $wrappedExecutor; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @param string $sql |
|
|
|
|
25
|
|
|
* @return Callable |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function getExecuteCommandCallable($sql) |
28
|
|
|
{ |
29
|
|
|
$callable = $this->wrappedExecutor->getExecuteCommandCallable($sql); |
|
|
|
|
30
|
|
|
return function() use ($callable) { |
|
|
|
|
31
|
|
|
$time = microtime(true); |
32
|
|
|
$results = call_user_func($callable); |
33
|
|
|
$time = microtime(true) - $time; |
34
|
|
|
$this->timingData = ['memory' => null, 'time' => sprintf('%.3d', $time)]; |
35
|
|
|
return $results; |
36
|
|
|
}; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @param string $filename |
|
|
|
|
41
|
|
|
* @return Callable |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function getExecuteFileCallable($filename) |
44
|
|
|
{ |
45
|
|
|
$callable = $this->wrappedExecutor->getExecuteFileCallable($filename); |
|
|
|
|
46
|
|
|
return function() use ($callable) { |
|
|
|
|
47
|
|
|
$time = microtime(true); |
48
|
|
|
$results = call_user_func($callable); |
49
|
|
|
$time = microtime(true) - $time; |
50
|
|
|
$this->timingData = ['memory' => null, 'time' => sprintf('%.3d', $time)]; |
51
|
|
|
return $results; |
52
|
|
|
}; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function resultSetToArray($data) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
return $this->wrappedExecutor->resultSetToArray($data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getTimingData($onceIsEnough = true) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return $this->timingData; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|