1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation\QueryManager; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\ConvertedResultIterator; |
13
|
|
|
use PommProject\Foundation\Listener\SendNotificationTrait; |
14
|
|
|
use PommProject\Foundation\Session\ResultHandler; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SimpleQueryManager |
18
|
|
|
* |
19
|
|
|
* Query system as a client. |
20
|
|
|
* |
21
|
|
|
* @package Foundation |
22
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
23
|
|
|
* @author Grégoire HUBERT |
24
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
25
|
|
|
*/ |
26
|
|
|
class SimpleQueryManager extends QueryManagerClient |
27
|
|
|
{ |
28
|
|
|
use SendNotificationTrait; |
29
|
|
|
use QueryParameterParserTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* query |
33
|
|
|
* |
34
|
|
|
* Perform a simple escaped query and return converted result iterator. |
35
|
|
|
* |
36
|
|
|
* @param string $sql |
37
|
|
|
* @param array $parameters |
38
|
|
|
* @return ConvertedResultIterator |
39
|
|
|
*/ |
40
|
|
|
public function query($sql, array $parameters = []) |
41
|
|
|
{ |
42
|
|
|
$parameters = $this->prepareArguments($sql, $parameters); |
43
|
|
|
$this->sendNotification( |
44
|
|
|
'query:pre', |
45
|
|
|
[ |
46
|
|
|
'sql' => $sql, |
47
|
|
|
'parameters' => $parameters, |
48
|
|
|
'session_stamp' => $this->getSession()->getStamp(), |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
$start = microtime(true); |
52
|
|
|
$resource = $this->doQuery($sql, $parameters); |
53
|
|
|
$end = microtime(true); |
54
|
|
|
|
55
|
|
|
$iterator = new ConvertedResultIterator( |
56
|
|
|
$resource, |
57
|
|
|
$this->getSession() |
58
|
|
|
); |
59
|
|
|
$this->sendNotification( |
60
|
|
|
'query:post', |
61
|
|
|
[ |
62
|
|
|
'result_count' => $iterator->count(), |
63
|
|
|
'time_ms' => sprintf("%03.1f", ($end - $start) * 1000), |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $iterator; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* doQuery |
72
|
|
|
* |
73
|
|
|
* Perform the query |
74
|
|
|
* |
75
|
|
|
* @param string $sql |
76
|
|
|
* @param array $parameters |
77
|
|
|
* @return ResultHandler |
78
|
|
|
*/ |
79
|
|
|
protected function doQuery($sql, array $parameters) |
80
|
|
|
{ |
81
|
|
|
return $this |
|
|
|
|
82
|
|
|
->getSession() |
83
|
|
|
->getConnection() |
84
|
|
|
->sendQueryWithParameters( |
85
|
|
|
$this->orderParameters($sql), |
86
|
|
|
$parameters |
87
|
|
|
) |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* prepareArguments |
93
|
|
|
* |
94
|
|
|
* Prepare and convert $parameters if needed. |
95
|
|
|
* |
96
|
|
|
* @param string $sql |
97
|
|
|
* @param array $parameters |
98
|
|
|
* @return array $parameters |
99
|
|
|
*/ |
100
|
|
|
protected function prepareArguments($sql, array $parameters) |
101
|
|
|
{ |
102
|
|
|
$types = $this->getParametersType($sql); |
103
|
|
|
|
104
|
|
|
foreach ($parameters as $index => $value) { |
105
|
|
|
if (isset($types[$index]) && $types[$index] !== '') { |
106
|
|
|
$parameters[$index] = $this |
107
|
|
|
->getSession() |
108
|
|
|
->getClientUsingPooler('converter', $types[$index]) |
109
|
|
|
->toPgStandardFormat($value, $types[$index]) |
110
|
|
|
; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $parameters; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|