Skip to content

Commit 427a585

Browse files
committed
add HYDRATE mode to result
1 parent 4156622 commit 427a585

File tree

7 files changed

+83
-113
lines changed

7 files changed

+83
-113
lines changed

doc/quick_start.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ The final code will be like this:
126126
$query = \Zk2\SpsComponent\QueryBuilderFactory::createQueryBuilder($queryBuilder);
127127
$query
128128
->buildWhere($container)
129-
->buildOrderBy(['country.name', 'asc']);
129+
->buildOrderBy(
130+
[
131+
['a.name', 'asc'],
132+
]
133+
);
130134
131135
$result = $query->getResult($limit, $offset);

src/AbstractQueryBuilder.php

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Doctrine\DBAL\Query\QueryBuilder as DBALQueryBuilder;
1616
use Doctrine\ORM\Query\Parameter;
1717
use Doctrine\ORM\QueryBuilder as ORMQueryBuilder;
18+
use Zk2\SpsComponent\Condition\Condition;
1819
use Zk2\SpsComponent\Condition\ConditionInterface;
20+
use Zk2\SpsComponent\Condition\ContainerException;
1921
use Zk2\SpsComponent\Condition\ContainerInterface;
2022

2123
/**
@@ -27,6 +29,7 @@ abstract class AbstractQueryBuilder
2729
* @var array|ArrayCollection|Parameter[]
2830
*/
2931
protected $parameters;
32+
3033
/**
3134
* @var ORMQueryBuilder|DBALQueryBuilder
3235
*/
@@ -92,6 +95,20 @@ abstract class AbstractQueryBuilder
9295
*/
9396
protected $aliasMapping = [];
9497

98+
/**
99+
* @var array
100+
*/
101+
protected $aggregateFunctionsNames = ['COUNT', 'SUM', 'MAX', 'MIN', 'AVG'];
102+
103+
/**
104+
* @param int $limit
105+
* @param int $offset
106+
* @param null $mode
107+
*
108+
* @return mixed
109+
*/
110+
abstract public function getResult($limit, $offset, $mode);
111+
95112
/**
96113
* @param ORMQueryBuilder|DBALQueryBuilder $queryBuilder
97114
*/
@@ -158,15 +175,13 @@ public function buildOrderBy(array $fields)
158175
*/
159176
public function isAggFunc($func)
160177
{
161-
return preg_match('/'.implode('\(|', QueryBuilderInterface::AGGREGATE_FUNCTIONS).'\(/i', $func);
178+
return preg_match('/'.implode($this->aggregateFunctionsNames, '\(|').'\(/i', $func);
162179
}
163180

164181
/**
165182
* @param string $rootEntity
166183
*
167184
* @return null|string
168-
*
169-
* @throws \Doctrine\DBAL\DBALException
170185
*/
171186
public function getPrimaryKeyName($rootEntity)
172187
{
@@ -226,8 +241,6 @@ public function getSqlPart($qb, $partName)
226241

227242
/**
228243
* @return string
229-
*
230-
* @throws \Doctrine\DBAL\DBALException
231244
*/
232245
public function getPlatform()
233246
{
@@ -265,6 +278,14 @@ public function setHint($name, $value)
265278
return $this;
266279
}
267280

281+
/**
282+
* @param string $functionName
283+
*/
284+
protected function addAggregateFunction(string $functionName)
285+
{
286+
$this->aggregateFunctionsNames[] = $functionName;
287+
}
288+
268289
/**
269290
* @param ContainerInterface $container
270291
*
@@ -297,26 +318,23 @@ protected function doBuildWhere(ContainerInterface $container)
297318

298319
/**
299320
* @param ContainerInterface $container
321+
* @param int $suffix
300322
*
301323
* @return string
324+
* @throws ContainerException
302325
*/
303-
abstract protected function buildCondition(ContainerInterface $container);
304-
/*{
326+
protected function buildCondition(ContainerInterface $container, $suffix = 0)
327+
{
305328
if (!$condition = $container->getCondition()) {
306329
throw new ContainerException('Condition is empty');
307330
}
308-
309-
$suffix = $this->parameters->count() + 1;
310331
$condition->reconfigureParameters($suffix);
311332

312333
if ($condition->isAggregateFunction()) {
313334
$where = $this->aggregate($condition);
314335
} else {
315336
$where = $condition->buildCondition();
316-
foreach ($condition->getParameters() as $paramName => $paramValue) {
317-
$parameter = new Parameter($paramName, $paramValue);
318-
$this->parameters->add($parameter);
319-
}
337+
$this->addParameter($condition->getParameters());
320338
}
321339
if (!$where = $this->trimAndOr($where)) {
322340
return null;
@@ -327,7 +345,7 @@ abstract protected function buildCondition(ContainerInterface $container);
327345
$container->getAndOr() ? sprintf(' %s ', $container->getAndOr()) : null,
328346
$where
329347
);
330-
}*/
348+
}
331349

332350
/**
333351
* @param array $parameters
@@ -343,6 +361,30 @@ abstract protected function addParameter(array $parameters);
343361
*/
344362
abstract protected function aggregate(ConditionInterface $condition);
345363

364+
/**
365+
* @param ORMQueryBuilder|DBALQueryBuilder $queryBuilder
366+
* @param ConditionInterface $condition
367+
* @param string $prefix
368+
*
369+
* @return array
370+
*/
371+
protected function paramForAggregate($queryBuilder, ConditionInterface $condition, string $prefix)
372+
{
373+
$newParameters = [];
374+
foreach ($condition->getParameters() as $paramName => $paramValue) {
375+
$newParameters[str_replace(':', ':'.$prefix, $paramName)] = $paramValue;
376+
}
377+
378+
if (count($newParameters) === 2 && stripos($condition->getComparisonOperator(), Condition::BETWEEN) !== false) {
379+
$newParameterName = implode(array_keys($newParameters), ' AND ');
380+
} else {
381+
$newParameterName = key($newParameters);
382+
}
383+
$queryBuilder->andHaving($condition->getSqlFunctionDefinition($newParameterName, $prefix))->setParameters([]);
384+
385+
return ['name' => $newParameterName, 'params' => $newParameters];
386+
}
387+
346388
/**
347389
* @param ORMQueryBuilder|DBALQueryBuilder $qb
348390
* @param array|null $parts

src/Condition/Condition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function buildCondition()
193193
switch ($this->data[self::COMPARISON_OPERATOR_NAME]) {
194194
case self::TOKEN_BETWEEN:
195195
case self::TOKEN_NOT_BETWEEN:
196-
$parametersByString = implode(' AND ', array_keys($this->parameters));
196+
$parametersByString = implode(array_keys($this->parameters), ' AND ');
197197
break;
198198
default:
199199
$parametersByString = key($this->parameters);

src/Condition/Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function create($data)
5858
}
5959
if (null === $type) {
6060
throw new ContainerException(
61-
sprintf('Invalid container type. Use %s', implode(' or ', self::ALLOWED_TYPES))
61+
sprintf('Invalid container type. Use %s', implode(self::ALLOWED_TYPES, ' or '))
6262
);
6363
}
6464
if (!is_array($data[$type])) {
@@ -138,7 +138,7 @@ public function setAndOr($andOr)
138138
if ($andOr = strtoupper($andOr)) {
139139
if (!in_array($andOr, self::AND_OR_OPERATORS)) {
140140
throw new ContainerException(
141-
sprintf('Invalid operator. Use %s', implode(' or ', self::AND_OR_OPERATORS))
141+
sprintf('Invalid operator. Use %s', implode(self::AND_OR_OPERATORS, ' or '))
142142
);
143143
}
144144
$this->andOr = $andOr;

src/DBALQueryBuilder.php

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use Doctrine\DBAL\Connection;
1414
use Doctrine\DBAL\Query\QueryBuilder;
15-
use Zk2\SpsComponent\Condition\Condition;
1615
use Zk2\SpsComponent\Condition\ConditionInterface;
1716
use Zk2\SpsComponent\Condition\ContainerException;
1817
use Zk2\SpsComponent\Condition\ContainerInterface;
@@ -57,9 +56,6 @@ public function __construct(QueryBuilder $queryBuilder)
5756
* @param ContainerInterface $container
5857
*
5958
* @return $this
60-
*
61-
* @throws QueryBuilderException
62-
* @throws \Doctrine\DBAL\DBALException
6359
*/
6460
public function buildWhere(ContainerInterface $container)
6561
{
@@ -81,55 +77,35 @@ public function buildWhere(ContainerInterface $container)
8177
/**
8278
* @param int $limit
8379
* @param int $offset
80+
* @param int $mode
8481
*
8582
* @return array
86-
*
87-
* @throws QueryBuilderException
88-
* @throws \Doctrine\DBAL\DBALException
8983
*/
90-
public function getResult($limit = 0, $offset = 0)
84+
public function getResult($limit = 0, $offset = 0, $mode = \PDO::FETCH_ASSOC)
9185
{
9286
if ($limit > 0 && false === $this->limitOffset($limit, $offset)) {
9387
return [];
9488
}
9589

9690
$stmt = $this->queryBuilder->execute();
97-
$this->result = $stmt->fetchAll();
91+
$this->result = $stmt->fetchAll($mode);
9892

9993
return $this->result;
10094
}
10195

10296
/**
10397
* @param ContainerInterface $container
98+
* @param int $suffix
10499
*
105100
* @return string
106101
*
107102
* @throws ContainerException
108103
*/
109-
protected function buildCondition(ContainerInterface $container)
104+
protected function buildCondition(ContainerInterface $container, $suffix = 0)
110105
{
111-
if (!$condition = $container->getCondition()) {
112-
throw new ContainerException('Condition is empty');
113-
}
114-
115106
$suffix = count($this->parameters) + 1;
116-
$condition->reconfigureParameters($suffix);
117-
118-
if ($condition->isAggregateFunction()) {
119-
$where = $this->aggregate($condition);
120-
} else {
121-
$where = $condition->buildCondition();
122-
$this->addParameter($condition->getParameters());
123-
}
124-
if (!$where = $this->trimAndOr($where)) {
125-
return null;
126-
}
127107

128-
return sprintf(
129-
'%s%s',
130-
$container->getAndOr() ? sprintf(' %s ', $container->getAndOr()) : null,
131-
$where
132-
);
108+
return parent::buildCondition($container, $suffix);
133109
}
134110

135111
/**
@@ -180,31 +156,18 @@ protected function aggregate(ConditionInterface $condition)
180156
}
181157
}
182158

183-
$newParameters = [];
184-
foreach ($condition->getParameters() as $paramName => $paramValue) {
185-
$newParameters[str_replace(':', ':'.$prefix, $paramName)] = $paramValue;
186-
}
187-
188-
if (count($newParameters) === 2 && stripos($condition->getComparisonOperator(), Condition::BETWEEN) !== false) {
189-
$newParameterName = implode(' AND ', array_keys($newParameters));
190-
} else {
191-
$newParameterName = key($newParameters);
192-
}
193-
$qb->andHaving($condition->getSqlFunctionDefinition($newParameterName, $prefix))->setParameters([]);
159+
$newParameters = $this->paramForAggregate($qb, $condition, $prefix);
194160

195-
foreach ($newParameters as $paramName => $paramValue) {
196-
$this->parameters[$newParameterName] = $paramValue;
197-
$this->parametersTypes[$newParameterName] = $this->inferType($paramValue);
161+
foreach ($newParameters['params'] as $paramName => $paramValue) {
162+
$this->parameters[$newParameters['name']] = $paramValue;
163+
$this->parametersTypes[$newParameters['name']] = $this->inferType($paramValue);
198164
}
199165

200166
return sprintf('%s IN(%s)', $this->aliasDotPrimary(), $qb->getSQL());
201167
}
202168

203169
/**
204170
* @return int
205-
*
206-
* @throws QueryBuilderException
207-
* @throws \Doctrine\DBAL\DBALException
208171
*/
209172
private function count()
210173
{
@@ -225,9 +188,6 @@ private function count()
225188
* @param int $offset
226189
*
227190
* @return bool
228-
*
229-
* @throws QueryBuilderException
230-
* @throws \Doctrine\DBAL\DBALException
231191
*/
232192
private function limitOffset($limit, $offset)
233193
{
@@ -246,7 +206,6 @@ private function limitOffset($limit, $offset)
246206
* @return $this
247207
*
248208
* @throws QueryBuilderException
249-
* @throws \Doctrine\DBAL\DBALException
250209
*/
251210
private function initRoot()
252211
{

0 commit comments

Comments
 (0)