1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Rougin\Windstorm\QueryInterface; |
8
|
|
|
|
9
|
|
|
class Query implements QueryInterface |
10
|
|
|
{ |
11
|
|
|
protected $model; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Returns the safe and compiled SQL. |
15
|
|
|
* |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function __toString() |
19
|
|
|
{ |
20
|
|
|
return $this->sql(); |
21
|
|
|
} |
22
|
|
|
|
23
|
93 |
|
public function __construct(Model $model) |
24
|
|
|
{ |
25
|
93 |
|
$this->model = $model; |
26
|
93 |
|
} |
27
|
|
|
|
28
|
69 |
|
public function builder(Builder $builder) |
29
|
|
|
{ |
30
|
69 |
|
$this->model = $builder; |
31
|
|
|
|
32
|
69 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Generates a SELECT query. |
37
|
|
|
* |
38
|
|
|
* @param array|string $fields |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
90 |
|
public function select($fields) |
42
|
|
|
{ |
43
|
90 |
|
$this->model = $this->model->select($fields); |
44
|
|
|
|
45
|
90 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generates a FROM query. |
50
|
|
|
* |
51
|
|
|
* @param string $table |
52
|
|
|
* @param string|null $alias |
53
|
|
|
* @return self |
54
|
|
|
*/ |
55
|
90 |
|
public function from($table, $alias = null) |
56
|
|
|
{ |
57
|
90 |
|
$this->model = $this->model->from($table); |
58
|
|
|
|
59
|
90 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Generates an INNER JOIN query. |
64
|
|
|
* |
65
|
|
|
* @param string $table |
66
|
|
|
* @param string $local |
67
|
|
|
* @param string $foreign |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
3 |
|
public function innerJoin($table, $local, $foreign) |
71
|
|
|
{ |
72
|
3 |
|
$this->model = $this->model->join($table, $local, '=', $foreign); |
73
|
|
|
|
74
|
3 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Generates a LEFT JOIN query. |
79
|
|
|
* |
80
|
|
|
* @param string $table |
81
|
|
|
* @param string $local |
82
|
|
|
* @param string $foreign |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
3 |
|
public function leftJoin($table, $local, $foreign) |
86
|
|
|
{ |
87
|
3 |
|
$this->model = $this->model->join($table, $local, '=', $foreign, 'left'); |
88
|
|
|
|
89
|
3 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Generates a RIGHT JOIN query. |
94
|
|
|
* |
95
|
|
|
* @param string $table |
96
|
|
|
* @param string $local |
97
|
|
|
* @param string $foreign |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
3 |
|
public function rightJoin($table, $local, $foreign) |
101
|
|
|
{ |
102
|
3 |
|
$this->model = $this->model->join($table, $local, '=', $foreign, 'right'); |
103
|
|
|
|
104
|
3 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generates an INSERT INTO query. |
109
|
|
|
* |
110
|
|
|
* @param string $table |
111
|
|
|
* @return \Rougin\Windstorm\InsertInterface |
112
|
|
|
*/ |
113
|
|
|
public function insertInto($table) |
114
|
|
|
{ |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Generates an UPDATE query. |
119
|
|
|
* |
120
|
|
|
* @param string $table |
121
|
|
|
* @param string|null $alias |
122
|
|
|
* @return \Rougin\Windstorm\UpdateInterface |
123
|
|
|
*/ |
124
|
|
|
public function update($table, $alias = null) |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Generates a DELETE FROM query. |
130
|
|
|
* |
131
|
|
|
* @param string $table |
132
|
|
|
* @param string|null $alias |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
|
|
public function deleteFrom($table, $alias = null) |
136
|
|
|
{ |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Generates a WHERE query. |
141
|
|
|
* |
142
|
|
|
* @param string $key |
143
|
|
|
* @return \Rougin\Windstorm\WhereInterface |
144
|
|
|
*/ |
145
|
48 |
|
public function where($key) |
146
|
|
|
{ |
147
|
48 |
|
return new Where($this, $this->model, $key, 'and'); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Generates an AND WHERE query. |
152
|
|
|
* |
153
|
|
|
* @param string $key |
154
|
|
|
* @return \Rougin\Windstorm\WhereInterface |
155
|
|
|
*/ |
156
|
3 |
|
public function andWhere($key) |
157
|
|
|
{ |
158
|
3 |
|
return new Where($this, $this->model, $key, 'and'); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Generates an OR WHERE query. |
163
|
|
|
* |
164
|
|
|
* @param string $key |
165
|
|
|
* @return \Rougin\Windstorm\WhereInterface |
166
|
|
|
*/ |
167
|
3 |
|
public function orWhere($key) |
168
|
|
|
{ |
169
|
3 |
|
return new Where($this, $this->model, $key, 'or'); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Generates a GROUP BY query. |
174
|
|
|
* |
175
|
|
|
* @param array|string $fields |
176
|
|
|
* @return self |
177
|
|
|
*/ |
178
|
3 |
|
public function groupBy($fields) |
179
|
|
|
{ |
180
|
3 |
|
$this->model = $this->model->groupBy($fields); |
181
|
|
|
|
182
|
3 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Generates a HAVING query. |
187
|
|
|
* |
188
|
|
|
* @param string $key |
189
|
|
|
* @return \Rougin\Windstorm\HavingInterface |
190
|
|
|
*/ |
191
|
9 |
|
public function having($key) |
192
|
|
|
{ |
193
|
9 |
|
return new Having($this, $this->model, $key); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Generates an AND HAVING query. |
198
|
|
|
* |
199
|
|
|
* @param string $key |
200
|
|
|
* @return \Rougin\Windstorm\HavingInterface |
201
|
|
|
*/ |
202
|
3 |
|
public function andHaving($key) |
203
|
|
|
{ |
204
|
3 |
|
return new Having($this, $this->model, $key, 'and'); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Generates an OR HAVING query. |
209
|
|
|
* |
210
|
|
|
* @param string $key |
211
|
|
|
* @return \Rougin\Windstorm\HavingInterface |
212
|
|
|
*/ |
213
|
3 |
|
public function orHaving($key) |
214
|
|
|
{ |
215
|
3 |
|
return new Having($this, $this->model, $key, 'or'); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Generates an ORDER BY query. |
220
|
|
|
* |
221
|
|
|
* @param string $key |
222
|
|
|
* @return \Rougin\Windstorm\OrderInterface |
223
|
|
|
*/ |
224
|
12 |
|
public function orderBy($key) |
225
|
|
|
{ |
226
|
12 |
|
return new Order($this, $this->model, $key); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Generates a multiple ORDER BY query. |
231
|
|
|
* |
232
|
|
|
* @param string $key |
233
|
|
|
* @return \Rougin\Windstorm\OrderInterface |
234
|
|
|
*/ |
235
|
3 |
|
public function andOrderBy($key) |
236
|
|
|
{ |
237
|
3 |
|
return new Order($this, $this->model, $key); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Performs a LIMIT query. |
242
|
|
|
* |
243
|
|
|
* @param integer $limit |
244
|
|
|
* @param integer|null $offset |
245
|
|
|
* @return self |
246
|
|
|
*/ |
247
|
3 |
|
public function limit($limit, $offset = null) |
248
|
|
|
{ |
249
|
3 |
|
$this->model = $this->model->limit($limit); |
250
|
|
|
|
251
|
|
|
if ($offset) |
|
|
|
|
252
|
3 |
|
{ |
253
|
3 |
|
$this->model = $this->model->offset($offset); |
254
|
3 |
|
} |
255
|
|
|
|
256
|
3 |
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns the safe and compiled SQL. |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
87 |
|
public function sql() |
265
|
|
|
{ |
266
|
87 |
|
return $this->model->toSql(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns the SQL bindings specified. |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
|
|
public function bindings() |
275
|
|
|
{ |
276
|
|
|
return $this->model->getBindings(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Returns the data types of the bindings. |
281
|
|
|
* |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
3 |
|
public function types() |
285
|
|
|
{ |
286
|
3 |
|
return array(); |
287
|
|
|
} |
288
|
|
|
|
289
|
3 |
|
public function instance() |
290
|
|
|
{ |
291
|
3 |
|
return $this->model; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|