|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hoooklife\DynamodbPodm\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Hoooklife\DynamodbPodm\Collection; |
|
6
|
|
|
use Hoooklife\DynamodbPodm\DB; |
|
7
|
|
|
use Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar; |
|
8
|
|
|
|
|
9
|
|
|
class Builder |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* 查询排序 |
|
14
|
|
|
*/ |
|
15
|
|
|
public $limit; |
|
16
|
|
|
public $wheres = []; |
|
17
|
|
|
public $bindings = []; |
|
18
|
|
|
public $columns; |
|
19
|
|
|
public $table; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* All of the available clause operators. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
public $operators = [ |
|
28
|
|
|
'=', '<', '>', '<=', '>=', '<>', '!=' |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
public $reservedKey = [ |
|
32
|
|
|
'key' |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** @var DynamoDBGrammar */ |
|
36
|
|
|
private $grammar; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct($connection = 'default') |
|
39
|
|
|
{ |
|
40
|
|
|
switch (DB::$config[$connection]['driver']) { |
|
41
|
|
|
case "dynamedb": |
|
42
|
|
|
$this->grammar = new DynamoDBGrammar($this, DB::$config[$connection]); |
|
43
|
|
|
break; |
|
44
|
|
|
default: |
|
45
|
|
|
throw new \Exception("bad driver"); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 设置要查出的列 |
|
52
|
|
|
* |
|
53
|
|
|
* @param array|mixed $columns |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function select($columns = ['*']) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->columns = is_array($columns) ? $columns : func_get_args(); |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
public function addSelect($columns) |
|
64
|
|
|
{ |
|
65
|
|
|
$columns = is_array($columns) ? $columns : func_get_args(); |
|
66
|
|
|
$this->columns = array_merge((array)$this->columns, $columns); |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set the table which the query is targeting. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $table |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function from($table) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->table = $table; |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add a basic where clause to the query. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string|array|\Closure $column |
|
88
|
|
|
* @param mixed $operator |
|
89
|
|
|
* @param mixed $value |
|
90
|
|
|
* @param string $boolean |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
|
|
94
|
|
|
public function where($column, $operator = null, $value = null, $boolean = 'and') |
|
95
|
|
|
{ |
|
96
|
|
|
if (is_array($column)) { |
|
97
|
|
|
// 递归 |
|
98
|
|
|
foreach ($column as $key => $value) { |
|
99
|
|
|
if (is_numeric($key) && is_array($value)) { |
|
100
|
|
|
$this->where(...array_values($value)); |
|
|
|
|
|
|
101
|
|
|
} else { |
|
102
|
|
|
$this->where($key, '=', $value, $boolean); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
return $this; |
|
106
|
|
|
// return $this->addArrayOfWheres($column, $boolean); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (func_num_args() === 2 || $this->invalidOperator($operator)) { |
|
110
|
|
|
list($value, $operator) = [$operator, '=']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// where in |
|
114
|
|
|
if (is_array($value)) { |
|
115
|
|
|
return $this->whereIn($column, $boolean); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// is null |
|
119
|
|
|
if (is_null($value)) { |
|
120
|
|
|
return $this->whereNull($column, $boolean, $operator !== '='); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$type = 'Basic'; |
|
124
|
|
|
$this->wheres[] = compact( |
|
125
|
|
|
'type', 'column', 'operator', 'value', 'boolean' |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
// TODO 参数绑定 |
|
130
|
|
|
// $this->addBinding($value, 'where'); |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Determine if the given operator is supported. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $operator |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function invalidOperator($operator) |
|
143
|
|
|
{ |
|
144
|
|
|
return !in_array(strtolower($operator), $this->operators, true) && |
|
145
|
|
|
!in_array(strtolower($operator), $this->grammar->getOperators(), true); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
// /** |
|
150
|
|
|
// * 向查询添加排序语句。 |
|
151
|
|
|
// * |
|
152
|
|
|
// * @param string $column |
|
153
|
|
|
// * @param string $direction |
|
154
|
|
|
// * @return $this |
|
155
|
|
|
// */ |
|
156
|
|
|
// public function orderBy($columns, $direction = 'asc') |
|
157
|
|
|
// { |
|
158
|
|
|
// $orders = is_array($columns) ? $columns : [$columns => $direction]; |
|
159
|
|
|
// $this->orders = array_merge($this->orders, $orders); |
|
160
|
|
|
// return $this; |
|
161
|
|
|
// } |
|
162
|
|
|
|
|
163
|
|
|
// public function offset($value) |
|
164
|
|
|
// { |
|
165
|
|
|
// $this->offset = max(0, $value); |
|
166
|
|
|
// return $this; |
|
167
|
|
|
// } |
|
168
|
|
|
|
|
169
|
|
|
// /** |
|
170
|
|
|
// * Alias to set the "offset" value of the query. |
|
171
|
|
|
// * |
|
172
|
|
|
// * @param int $value |
|
173
|
|
|
// * @return \Illuminate\Database\Query\Builder|static |
|
174
|
|
|
// */ |
|
175
|
|
|
// public function skip($value) |
|
176
|
|
|
// { |
|
177
|
|
|
// return $this->offset($value); |
|
178
|
|
|
// } |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Alias to set the "limit" value of the query. |
|
183
|
|
|
* |
|
184
|
|
|
* @param int $value |
|
185
|
|
|
* @return \Illuminate\Database\Query\Builder|static |
|
|
|
|
|
|
186
|
|
|
*/ |
|
187
|
|
|
public function take($value) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->limit($value); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Set the "limit" value of the query. |
|
195
|
|
|
* |
|
196
|
|
|
* @param int $value |
|
197
|
|
|
* @return $this |
|
198
|
|
|
*/ |
|
199
|
|
|
public function limit($value) |
|
200
|
|
|
{ |
|
201
|
|
|
// FIXME ??? 为啥等于0 |
|
202
|
|
|
if ($value > 0) { |
|
203
|
|
|
$this->limit = $value; |
|
204
|
|
|
} |
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Set the limit and offset for a given page. |
|
210
|
|
|
* |
|
211
|
|
|
* @param int $page |
|
212
|
|
|
* @param int $perPage |
|
213
|
|
|
* @return \Illuminate\Database\Query\Builder|static |
|
214
|
|
|
*/ |
|
215
|
|
|
public function forPage($page, $perPage = 15) |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->skip(($page - 1) * $perPage)->take($perPage); |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get the SQL representation of the query. |
|
223
|
|
|
* |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
|
|
public function toSql() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->grammar->compileSelect($this); |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get a single column's value from the first result of a query. |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $column |
|
236
|
|
|
* @return mixed |
|
237
|
|
|
*/ |
|
238
|
|
|
public function value($column) |
|
239
|
|
|
{ |
|
240
|
|
|
$result = (array)$this->first([$column]); |
|
241
|
|
|
return count($result) > 0 ? reset($result) : null; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Execute the query and get the first result. |
|
247
|
|
|
* |
|
248
|
|
|
* @param array $columns |
|
249
|
|
|
* @return \Illuminate\Database\Eloquent\Model|object|static|null |
|
|
|
|
|
|
250
|
|
|
*/ |
|
251
|
|
|
public function first($columns = ['*']) |
|
252
|
|
|
{ |
|
253
|
|
|
return reset($this->take(1)->all($columns)); |
|
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Execute the query as a "select" statement. |
|
259
|
|
|
* |
|
260
|
|
|
* @param array $columns |
|
261
|
|
|
* @return Collection |
|
262
|
|
|
*/ |
|
263
|
|
|
public function all($columns = ['*']): Collection |
|
264
|
|
|
{ |
|
265
|
|
|
return $this->grammar->all($columns); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @param array $data |
|
270
|
|
|
* @return \Aws\Result |
|
271
|
|
|
*/ |
|
272
|
|
|
public function insert(array $data) |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->grammar->insert($data); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
} |
|
279
|
|
|
|