1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class SQLQueryBuilder extends AbstractQueryBuilder |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
private $queryBuilder; |
10
|
|
|
|
11
|
2 |
|
public function run(){ |
12
|
2 |
|
$this->queryBuilder = $this->getQueryBuilder(); |
13
|
2 |
|
$count = $this->getCount(); |
14
|
2 |
|
$numberOfRows = 0; |
15
|
2 |
|
if (isset($count[0]['total']) && ($count[0]['total']>0)) { |
16
|
2 |
|
$numberOfRows = $count[0]['total']; |
17
|
2 |
|
$this->setSortOrders(); |
18
|
2 |
|
$this->setReturnFields(); |
19
|
2 |
|
$this->setOffsetAndLimit(); |
20
|
2 |
|
$stmt = $this->soupmix->getConnection() |
21
|
2 |
|
->executeQuery( |
22
|
2 |
|
$this->queryBuilder->getSql(), |
23
|
2 |
|
$this->queryBuilder->getParameters() |
24
|
2 |
|
); |
25
|
2 |
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
26
|
2 |
|
if($this->distinctFieldName !== null){ |
27
|
1 |
|
$numberOfRows = count($result); |
28
|
1 |
|
} |
29
|
2 |
|
} |
30
|
2 |
|
return ['total' => $numberOfRows, 'data' => $result]; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
private function getQueryBuilder() |
34
|
|
|
{ |
35
|
2 |
|
if ($this->orFilters !== null){ |
36
|
2 |
|
$this->andFilters[] = $this->orFilters; |
37
|
2 |
|
} |
38
|
2 |
|
$this->filters = $this->andFilters; |
39
|
2 |
|
return $this->soupmix->buildQuery($this->collection, $this->filters); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
private function getCount() |
43
|
|
|
{ |
44
|
2 |
|
$queryBuilderCount = clone $this->queryBuilder; |
45
|
2 |
|
$queryBuilderCount->select(" COUNT(*) AS total "); |
46
|
2 |
|
$stmt = $this->soupmix->getConnection()->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters()); |
47
|
2 |
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
private function setSortOrders() |
51
|
|
|
{ |
52
|
2 |
|
if ($this->sortFields !== null) { |
53
|
|
|
foreach ($this->sortFields as $sortKey => $sortDir) { |
54
|
|
|
$this->queryBuilder->addOrderBy($sortKey, $sortDir); |
55
|
|
|
} |
56
|
|
|
} |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
private function setReturnFields() |
60
|
|
|
{ |
61
|
2 |
|
if ($this->distinctFieldName === null) { |
62
|
2 |
|
$fieldNames = ($this->fieldNames === null) ? "*" : $this->fieldNames; |
63
|
2 |
|
$this->queryBuilder->select($fieldNames); |
64
|
2 |
|
return; |
65
|
|
|
} |
66
|
1 |
|
$this->queryBuilder->select('DISTINCT (`' . $this->distinctFieldName . '`)'); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
2 |
|
private function setOffsetAndLimit() |
70
|
|
|
{ |
71
|
2 |
|
$this->queryBuilder->setFirstResult($this->offset) |
72
|
2 |
|
->setMaxResults($this->limit); |
73
|
|
|
} |
74
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: