-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathQueryResult.php
More file actions
201 lines (182 loc) · 5.56 KB
/
QueryResult.php
File metadata and controls
201 lines (182 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace Mouf\Database\QueryWriter;
use Doctrine\DBAL\Types\Type;
use SQLParser\SqlRenderInterface;
use function method_exists;
use Mouf\Database\QueryWriter\Utils\DbHelper;
use Mouf\Utils\Value\ValueUtils;
use SQLParser\Query\Select;
use Mouf\Utils\Common\PaginableInterface;
use Mouf\Utils\Value\ArrayValueInterface;
use Mouf\Utils\Value\ValueInterface;
use Doctrine\DBAL\Connection;
use Mouf\Utils\Common\SortableInterface;
use SQLParser\Node\NodeInterface;
use SQLParser\Node\ColRef;
/**
* A class that can execute a query and expose the results.
*
* @Renderer { "smallLogo":"vendor/mouf/database.querywriter/icons/database_query.png" }
*
* @author David Negrier
*/
class QueryResult implements ArrayValueInterface, PaginableInterface, SortableInterface
{
/**
* The Select statement.
*
* @var Select
*/
private $select;
/**
* The connection to the database.
*
* @var Connection
*/
private $connection;
/**
* The list of parameters to apply to the SQL request.
*
* @var array<string, string>|array<string, ValueInterface>|ArrayValueInterface
*/
private $parameters = array();
/** @var int|null */
private $offset;
/** @var int|null */
private $limit;
private int $conditionsMode;
private bool $extrapolateParameters;
/**
* @var Type[]|int[]|string[]
*/
private array $parameterTypes = [];
/**
* @param Select $select
* @param Connection $connection
* @param int $conditionsMode
* @param bool $extrapolateParameters
*/
public function __construct(
Select $select,
Connection $connection,
int $conditionsMode = SqlRenderInterface::CONDITION_APPLY,
bool $extrapolateParameters = true
) {
$this->select = $select;
$this->connection = $connection;
$this->conditionsMode = $conditionsMode;
$this->extrapolateParameters = $extrapolateParameters;
}
/**
* The list of parameters to apply to the SQL request.
*
* @param array<string, string>|array<string, ValueInterface>|ArrayValueInterface $parameters
* @param Type[]|string[]|int[] $types Parameter types
*/
public function setParameters($parameters, array $types = []): void
{
$this->parameters = $parameters;
$this->parameterTypes = $types;
}
/**
* @return array<string, string>|array<string, ValueInterface>|ArrayValueInterface
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* (non-PHPdoc).
*
* @see \Mouf\Utils\Value\ArrayValueInterface::val()
*/
public function val()
{
$sql = $this->toSql().DbHelper::getFromLimitString($this->offset, $this->limit);
$pdoStatement = $this->connection->executeQuery($sql, $this->getParametersForBind(), $this->getParameterTypesForBind());
return new ResultSet($pdoStatement);
}
/**
* Returns the SQL for this query-result (without pagination, but with parameters accounted for).
*
* @return string|null
*/
public function toSql()
{
$parameters = ValueUtils::val($this->parameters);
return $this->select->toSql(
$parameters,
$this->connection->getDatabasePlatform(),
0,
$this->conditionsMode,
$this->extrapolateParameters
);
}
public function getParametersForBind(): array
{
return $this->extrapolateParameters ? [] : $this->parameters;
}
public function getParameterTypesForBind(): array
{
return $this->extrapolateParameters ? [] : array_intersect_key($this->parameterTypes, $this->parameters);
}
/**
* Paginates the result set.
*
* @param int $limit
* @param int $offset
*/
public function paginate($limit, $offset = 0): void
{
$this->limit = $limit;
$this->offset = $offset;
}
/* (non-PHPdoc)
* @see \Mouf\Utils\Common\SortableInterface::sort()
*/
public function sort($key, $direction = SortableInterface::ASC): void
{
$result = $this->findColumnByKey($key);
if ($result != null) {
$columnObj = clone($result);
if (method_exists($columnObj, 'setAlias')) {
$columnObj->setAlias(null);
}
if (method_exists($columnObj, 'setDirection')) {
$columnObj->setDirection($direction);
}
} else {
$columnObj = new ColRef();
$columnObj->setColumn($key);
$columnObj->setDirection($direction);
}
$this->select->setOrder(array($columnObj));
}
/**
* Returns the object representing a column from the key passed in parameter.
* It will first scan the column aliases to find if an alias match the key, then the column names, etc...
* It will throw an exception if the column cannot be found.
*
* @param string $key
*
* @return NodeInterface|null
*/
private function findColumnByKey($key): ?NodeInterface
{
$columns = $this->select->getColumns();
foreach ($columns as $column) {
if (method_exists($column, 'getAlias')) {
$alias = $column->getAlias();
if ($alias === $key) {
return $column;
}
}
if ($column instanceof ColRef) {
if ($column->getColumn() === $key) {
return $column;
}
}
}
return null;
}
}