1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hoooklife\DynamodbPodm\DynamoDB; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Aws\DynamoDb\Marshaler; |
7
|
|
|
use Closure; |
8
|
|
|
use Hoooklife\DynamodbPodm\ComparisonOperator; |
9
|
|
|
use Hoooklife\DynamodbPodm\Concerns\HasParsers; |
10
|
|
|
use Hoooklife\DynamodbPodm\DB; |
11
|
|
|
use Hoooklife\DynamodbPodm\Grammars\DynamoDBBuilder; |
12
|
|
|
use Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar; |
13
|
|
|
|
14
|
|
|
class Builder |
15
|
|
|
{ |
16
|
|
|
use HasParsers; |
17
|
|
|
/** |
18
|
|
|
* 查询排序 |
19
|
|
|
*/ |
20
|
|
|
public $limit; |
21
|
|
|
public $wheres = []; |
22
|
|
|
public $bindings = []; |
23
|
|
|
public $columns; |
24
|
|
|
public $table; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* All of the available clause operators. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
public $operators = [ |
33
|
|
|
'=', '<', '>', '<=', '>=', '<>', '!=' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public $reservedKey = [ |
37
|
|
|
'key' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** @var DynamoDBGrammar */ |
41
|
|
|
private $dynamoDBBuilder; |
42
|
|
|
private $client; |
43
|
|
|
|
44
|
|
|
public function __construct($connection = 'default') |
45
|
|
|
{ |
46
|
|
|
$this->setupExpressions(); |
47
|
|
|
switch (DB::$config[$connection]['driver']) { |
48
|
|
|
case "dynamedb": |
49
|
|
|
$this->dynamoDBBuilder = new DynamoDBBuilder(DB::$config[$connection]); |
|
|
|
|
50
|
|
|
$this->client = new DynamoDbClient(DB::$config[$connection]["S3Config"]); |
51
|
|
|
break; |
52
|
|
|
default: |
53
|
|
|
throw new \Exception("bad driver"); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the table which the query is targeting. |
60
|
|
|
* |
61
|
|
|
* @param string $table |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function from($table) |
65
|
|
|
{ |
66
|
|
|
$this->table = $table; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add a basic where clause to the query. |
73
|
|
|
* |
74
|
|
|
* @param string|array|\Closure $column |
75
|
|
|
* @param mixed $operator |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @param string $boolean |
78
|
|
|
* @param string $type |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
|
82
|
|
|
public function where($column, $operator = null, $value = null, $type = "key", $boolean = 'and') |
83
|
|
|
{ |
84
|
|
|
if (is_array($column)) { |
85
|
|
|
// 递归 |
86
|
|
|
foreach ($column as $key => $value) { |
87
|
|
|
if (is_numeric($key) && is_array($value)) { |
88
|
|
|
$this->where(...array_values($value)); |
|
|
|
|
89
|
|
|
} else { |
90
|
|
|
$this->where($key, '=', $value, $type, $boolean); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (func_num_args() === 2 || !ComparisonOperator::isValidOperator($operator)) { |
97
|
|
|
list($value, $operator) = [$operator, '=']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// if ($column instanceof Closure) { |
101
|
|
|
// return $this->whereNested($column, $boolean); |
102
|
|
|
// } |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
// where in |
106
|
|
|
if (is_array($value)) { |
107
|
|
|
return $this->whereIn($column, $type, $boolean); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// is null |
111
|
|
|
if (is_null($value)) { |
112
|
|
|
return $this->whereNull($column, $type, $boolean, $operator !== '='); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->wheres[] = [ |
116
|
|
|
'column' => $column, |
117
|
|
|
'operator' => ComparisonOperator::getDynamoDbOperator($operator), |
118
|
|
|
'value' => $value, |
119
|
|
|
'boolean' => $boolean, |
120
|
|
|
'type' => $type |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Add a "where in" clause to the query. |
128
|
|
|
* |
129
|
|
|
* @param string $column |
130
|
|
|
* @param array $values |
131
|
|
|
* @param string $type |
132
|
|
|
* @param string $boolean |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function whereIn($column, $values, $type = "key", $boolean = 'and') |
136
|
|
|
{ |
137
|
|
|
return $this->where($column, ComparisonOperator::IN, $values, $type, $boolean); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add a "where null" clause to the query. |
142
|
|
|
* |
143
|
|
|
* @param string $column |
144
|
|
|
* @param string $boolean |
145
|
|
|
* @param bool $not |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function whereNull($column, $type = "key", $boolean = 'and', $not = false) |
149
|
|
|
{ |
150
|
|
|
$operator = $not ? ComparisonOperator::NOT_NULL : ComparisonOperator::NULL; |
151
|
|
|
$this->wheres[] = compact('column', 'operator', 'boolean', 'type'); |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Add an "or where null" clause to the query. |
157
|
|
|
* |
158
|
|
|
* @param string $column |
159
|
|
|
* @param string $type |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function orWhereNull($column, $type = "key") |
163
|
|
|
{ |
164
|
|
|
return $this->whereNull($column, $type, 'or'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Add an "or where not null" clause to the query. |
169
|
|
|
* |
170
|
|
|
* @param string $column |
171
|
|
|
* @param string $type |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function orWhereNotNull($column, $type = "key") |
175
|
|
|
{ |
176
|
|
|
return $this->whereNotNull($column, $type, 'or'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Add a "where not null" clause to the query. |
181
|
|
|
* |
182
|
|
|
* @param string $column |
183
|
|
|
* @param string $type |
184
|
|
|
* @param string $boolean |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function whereNotNull($column, $type = "key", $boolean = 'and') |
188
|
|
|
{ |
189
|
|
|
return $this->whereNull($column, $type, $boolean, true); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Alias to set the "limit" value of the query. |
195
|
|
|
* |
196
|
|
|
* @param int $value |
197
|
|
|
* @return \Illuminate\Database\Query\Builder|static |
|
|
|
|
198
|
|
|
*/ |
199
|
|
|
public function take($value) |
200
|
|
|
{ |
201
|
|
|
return $this->limit($value); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function limit($value) |
205
|
|
|
{ |
206
|
|
|
$this->limit = $value; |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the SQL representation of the query. |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function toQuery() |
216
|
|
|
{ |
217
|
|
|
return $this->grammar->compileSelect($this); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get a single column's value from the first result of a query. |
223
|
|
|
* |
224
|
|
|
* @param string $column |
225
|
|
|
* @return mixed |
226
|
|
|
*/ |
227
|
|
|
public function value($column) |
228
|
|
|
{ |
229
|
|
|
$result = $this->first([$column])->toArray(); |
|
|
|
|
230
|
|
|
return $result !== null ? reset($result) : null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Execute the query and get the first result. |
236
|
|
|
* |
237
|
|
|
* @param array $columns |
238
|
|
|
* @return \Illuminate\Database\Eloquent\Model|object|static|null |
|
|
|
|
239
|
|
|
*/ |
240
|
|
|
public function first($columns = ['*']) |
241
|
|
|
{ |
242
|
|
|
return $this->take(1)->all($columns)->first(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function get($columns = []) |
246
|
|
|
{ |
247
|
|
|
$raw = $this->toDynamoDbQuery($columns, 10); |
248
|
|
|
if ($raw->op === 'Scan') { |
249
|
|
|
$res = $raw->scan($raw->query); |
|
|
|
|
250
|
|
|
} else { |
251
|
|
|
$res = $this->client->query($raw->query); |
252
|
|
|
$res = $res['Items']; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
foreach ($res as $item) { |
256
|
|
|
$results[] = (new Marshaler())->unmarshalItem($item); |
257
|
|
|
} |
258
|
|
|
return $results; |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function query($columns = []) |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
$limit = isset($this->limit) ?: -1; |
|
|
|
|
264
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function toDynamoDBQuery($columns, $limit) |
268
|
|
|
{ |
269
|
|
|
if (!empty($this->wheres)) { |
270
|
|
|
$this->dynamoDBBuilder->setTableName($this->table); |
|
|
|
|
271
|
|
|
$this->dynamoDBBuilder->setKeyConditionExpression($this->keyConditionExpression->parse($this->wheres)); |
|
|
|
|
272
|
|
|
foreach ($this->wheres as $where) { |
273
|
|
|
// if ($where['type'] === 'key') { |
274
|
|
|
// } else { |
275
|
|
|
// $this->dynamoDBBuilder->setFilterExpression($this->filterExpression->parse($where)); |
276
|
|
|
// } |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$this->dynamoDBBuilder->setLimit($limit); |
|
|
|
|
281
|
|
|
if (!empty($columns)) { |
282
|
|
|
$this->dynamoDBBuilder->setProjectionExpression($this->projectionExpression->parse($columns)); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$this->dynamoDBBuilder |
286
|
|
|
->setExpressionAttributeNames($this->expressionAttributeNames->all()) |
|
|
|
|
287
|
|
|
->setExpressionAttributeValues($this->expressionAttributeValues->all()); |
288
|
|
|
|
289
|
|
|
$op = 'Query'; |
290
|
|
|
$raw = new RawDynamoDbQuery($op, $this->dynamoDBBuilder->prepare()->query); |
|
|
|
|
291
|
|
|
|
292
|
|
|
return $raw; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Execute the query as a "select" statement. |
297
|
|
|
* |
298
|
|
|
* @param array $columns |
299
|
|
|
* @return Collection |
300
|
|
|
*/ |
301
|
|
|
public function all($columns = ['*']): Collection |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
$columns = is_array($columns) ? $columns : func_get_args(); |
304
|
|
|
$this->columns = $columns; |
305
|
|
|
|
306
|
|
|
return $this->grammar->all(); |
|
|
|
|
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
|
310
|
|
|
public function getGrammar() |
311
|
|
|
{ |
312
|
|
|
return $this->grammar; |
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getBuilder() |
316
|
|
|
{ |
317
|
|
|
return $this->grammar->getBuilder(); |
|
|
|
|
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..