1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class SQLQueryBuilder extends AbstractQueryBuilder |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
private $queryBuilder; |
10
|
|
|
|
11
|
1 |
|
public function run(){ |
12
|
1 |
|
$this->queryBuilder = $this->getQueryBuilder(); |
13
|
1 |
|
$count = $this->getCount(); |
14
|
1 |
|
$numberOfRows = 0; |
15
|
1 |
|
if (isset($count[0]['total']) && ($count[0]['total']>0)) { |
16
|
1 |
|
$numberOfRows = $count[0]['total']; |
17
|
1 |
|
$this->setSortOrders(); |
18
|
1 |
|
$this->setReturnFields(); |
19
|
1 |
|
$this->setOffsetAndLimit(); |
20
|
1 |
|
$stmt = $this->soupmix->getConnection() |
21
|
1 |
|
->executeQuery( |
22
|
1 |
|
$this->queryBuilder->getSql(), |
23
|
1 |
|
$this->queryBuilder->getParameters() |
24
|
1 |
|
); |
25
|
1 |
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
26
|
1 |
|
if($this->distinctFieldName !== null){ |
27
|
1 |
|
$numberOfRows = count($result); |
28
|
1 |
|
} |
29
|
1 |
|
} |
30
|
1 |
|
return ['total' => $numberOfRows, 'data' => $result]; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
private function getQueryBuilder() |
34
|
|
|
{ |
35
|
1 |
|
$this->andFilters[] = $this->orFilters; |
36
|
1 |
|
$this->filters = $this->andFilters; |
37
|
1 |
|
return $this->soupmix->buildQuery($this->collection, $this->filters); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
private function getCount() |
41
|
|
|
{ |
42
|
1 |
|
$queryBuilderCount = clone $this->queryBuilder; |
43
|
1 |
|
$queryBuilderCount->select(" COUNT(*) AS total "); |
44
|
1 |
|
$stmt = $this->soupmix->getConnection()->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters()); |
45
|
1 |
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
private function setSortOrders() |
49
|
|
|
{ |
50
|
1 |
|
if ($this->sortFields !== null) { |
51
|
|
|
foreach ($this->sortFields as $sort_key => $sort_dir) { |
52
|
|
|
$this->queryBuilder->addOrderBy($sort_key, $sort_dir); |
53
|
|
|
} |
54
|
|
|
} |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
private function setReturnFields() |
58
|
|
|
{ |
59
|
1 |
|
if ($this->distinctFieldName === null) { |
60
|
1 |
|
$fieldNames = ($this->fieldNames === null) ? "*" : $this->fieldNames; |
61
|
1 |
|
$this->queryBuilder->select($fieldNames); |
62
|
1 |
|
return; |
63
|
|
|
} |
64
|
1 |
|
$this->queryBuilder->select('DISTINCT (`' . $this->distinctFieldName . '`)'); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
private function setOffsetAndLimit() |
68
|
|
|
{ |
69
|
1 |
|
$this->queryBuilder->setFirstResult($this->offset) |
70
|
1 |
|
->setMaxResults($this->limit); |
71
|
|
|
} |
72
|
|
|
} |
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: