1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the miBadger package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace miBadger\ActiveRecord; |
11
|
|
|
|
12
|
|
|
use miBadger\Query\Query; |
13
|
|
|
use miBadger\Query\QueryInterface; |
14
|
|
|
use miBadger\Query\QueryExpression; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The active record exception class. |
18
|
|
|
* |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
class ActiveRecordQuery |
22
|
|
|
{ |
23
|
|
|
private $table; |
24
|
|
|
|
25
|
|
|
private $hooks; |
|
|
|
|
26
|
|
|
|
27
|
|
|
private $results; |
28
|
|
|
|
29
|
|
|
private $query; |
30
|
|
|
|
31
|
|
|
private $whereExpression = null; |
32
|
|
|
|
33
|
|
|
private $clauses = []; |
34
|
|
|
|
35
|
17 |
|
public function __construct(AbstractActiveRecord $instance, $table, Array $additionalWhereClauses) |
36
|
|
|
{ |
37
|
17 |
|
$this->table = $table; |
38
|
17 |
|
$this->query = new Query($instance->getPdo(), $table); |
39
|
17 |
|
$this->type = $instance; |
|
|
|
|
40
|
17 |
|
$this->clauses = $additionalWhereClauses; |
41
|
17 |
|
} |
42
|
|
|
|
43
|
16 |
|
private function execute() |
44
|
|
|
{ |
45
|
16 |
|
$clauses = $this->clauses; |
46
|
|
|
|
47
|
|
|
// Optionally add user concatenated where expression |
48
|
16 |
|
if ($this->whereExpression !== null) |
49
|
|
|
{ |
50
|
9 |
|
$clauses[] = $this->whereExpression; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Construct where clause |
54
|
16 |
|
if (count($clauses) == 1) |
55
|
|
|
{ |
56
|
10 |
|
$this->query->where($clauses[0]); |
57
|
6 |
|
} else if (count($clauses) >= 2) |
58
|
|
|
{ |
59
|
|
|
$rest = array_slice($clauses, 1); |
60
|
|
|
$this->query->where(Query::And($clauses[0], ...$rest)); |
61
|
|
|
} |
62
|
|
|
|
63
|
16 |
|
$this->query->select(); |
64
|
|
|
|
65
|
16 |
|
$this->results = $this->query->execute(); |
66
|
|
|
|
67
|
13 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
13 |
|
public function fetchAll() |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
// TODO: Should execute call be explicit? |
74
|
13 |
|
$this->execute(); |
75
|
|
|
|
76
|
11 |
|
$typedResults = []; |
77
|
|
|
|
78
|
11 |
|
$entries = $this->results->fetchAll(); |
79
|
11 |
|
if ($entries === false) { |
80
|
|
|
throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
81
|
|
|
} |
82
|
|
|
|
83
|
11 |
|
foreach ($entries as $entry) { |
84
|
11 |
|
$typedEntry = clone $this->type; |
85
|
11 |
|
$typedEntry->fill($entry); |
86
|
11 |
|
$typedResults[] = $typedEntry; |
87
|
|
|
} |
88
|
|
|
|
89
|
11 |
|
return $typedResults; |
90
|
2 |
|
} catch (\PDOException $e) { |
91
|
2 |
|
throw new ActiveRecordException($e->getMessage(), 0, $e); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
public function fetch() |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
3 |
|
$this->execute(); |
99
|
|
|
|
100
|
2 |
|
$typedResult = clone $this->type; |
101
|
|
|
|
102
|
2 |
|
$entry = $this->results->fetch(); |
103
|
2 |
|
if ($entry === false) { |
104
|
1 |
|
throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$typedResult->fill($entry); |
108
|
|
|
|
109
|
1 |
|
return $typedResult; |
110
|
2 |
|
} catch (\PDOException $e) { |
111
|
1 |
|
throw new ActiveRecordException($e->getMessage(), 0, $e); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the where condition |
118
|
|
|
* |
119
|
|
|
* @param QueryExpression $expression the query expression |
120
|
|
|
* @return $this |
121
|
|
|
* @see https://en.wikipedia.org/wiki/SQL#Operators |
122
|
|
|
* @see https://en.wikipedia.org/wiki/Where_(SQL) |
123
|
|
|
*/ |
124
|
9 |
|
public function where(QueryExpression $expression) |
125
|
|
|
{ |
126
|
9 |
|
$this->whereExpression = $expression; |
127
|
9 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set an additional group by. |
132
|
|
|
* |
133
|
|
|
* @param string $column |
134
|
|
|
* @return $this |
135
|
|
|
* @see https://en.wikipedia.org/wiki/SQL#Queries |
136
|
|
|
*/ |
137
|
|
|
public function groupBy($column) |
138
|
|
|
{ |
139
|
|
|
$this->query->groupBy($column); |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set an additional order condition. |
145
|
|
|
* |
146
|
|
|
* @param string $column |
147
|
|
|
* @param string|null $order |
148
|
|
|
* @return $this |
149
|
|
|
* @see https://en.wikipedia.org/wiki/SQL#Queries |
150
|
|
|
* @see https://en.wikipedia.org/wiki/Order_by |
151
|
|
|
*/ |
152
|
1 |
|
public function orderBy($column, $order = null) |
153
|
|
|
{ |
154
|
1 |
|
$this->query->orderBy($column, $order); |
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the limit. |
160
|
|
|
* |
161
|
|
|
* @param mixed $limit |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
2 |
|
public function limit($limit) |
165
|
|
|
{ |
166
|
2 |
|
$this->query->limit($limit); |
167
|
2 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set the offset. |
172
|
|
|
* |
173
|
|
|
* @param mixed $offset |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
1 |
|
public function offset($offset) |
177
|
|
|
{ |
178
|
1 |
|
$this->query->offset($offset); |
179
|
1 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|