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
|
|
|
* Set the table which the query is targeting. |
52
|
|
|
* |
53
|
|
|
* @param string $table |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function from($table) |
57
|
|
|
{ |
58
|
|
|
$this->table = $table; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add a basic where clause to the query. |
65
|
|
|
* |
66
|
|
|
* @param string|array|\Closure $column |
67
|
|
|
* @param mixed $operator |
68
|
|
|
* @param mixed $value |
69
|
|
|
* @param string $boolean |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
|
73
|
|
|
public function where($column, $operator = null, $value = null, $boolean = 'and') |
74
|
|
|
{ |
75
|
|
|
if (is_array($column)) { |
76
|
|
|
// 递归 |
77
|
|
|
foreach ($column as $key => $value) { |
78
|
|
|
if (is_numeric($key) && is_array($value)) { |
79
|
|
|
$this->where(...array_values($value)); |
|
|
|
|
80
|
|
|
} else { |
81
|
|
|
$this->where($key, '=', $value, $boolean); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return $this; |
85
|
|
|
// return $this->addArrayOfWheres($column, $boolean); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (func_num_args() === 2 || $this->invalidOperator($operator)) { |
89
|
|
|
list($value, $operator) = [$operator, '=']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// where in |
93
|
|
|
if (is_array($value)) { |
94
|
|
|
return $this->whereIn($column, $boolean); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// is null |
98
|
|
|
if (is_null($value)) { |
99
|
|
|
return $this->whereNull($column, $boolean, $operator !== '='); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$type = 'Basic'; |
103
|
|
|
$this->wheres[] = compact( |
104
|
|
|
'type', 'column', 'operator', 'value', 'boolean' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
// TODO 参数绑定 |
109
|
|
|
// $this->addBinding($value, 'where'); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determine if the given operator is supported. |
117
|
|
|
* |
118
|
|
|
* @param string $operator |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
protected function invalidOperator($operator) |
122
|
|
|
{ |
123
|
|
|
return !in_array(strtolower($operator), $this->operators, true) && |
124
|
|
|
!in_array(strtolower($operator), $this->grammar->getOperators(), true); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
// /** |
129
|
|
|
// * 向查询添加排序语句。 |
130
|
|
|
// * |
131
|
|
|
// * @param string $column |
132
|
|
|
// * @param string $direction |
133
|
|
|
// * @return $this |
134
|
|
|
// */ |
135
|
|
|
// public function orderBy($columns, $direction = 'asc') |
136
|
|
|
// { |
137
|
|
|
// $orders = is_array($columns) ? $columns : [$columns => $direction]; |
138
|
|
|
// $this->orders = array_merge($this->orders, $orders); |
139
|
|
|
// return $this; |
140
|
|
|
// } |
141
|
|
|
|
142
|
|
|
// public function offset($value) |
143
|
|
|
// { |
144
|
|
|
// $this->offset = max(0, $value); |
145
|
|
|
// return $this; |
146
|
|
|
// } |
147
|
|
|
|
148
|
|
|
// /** |
149
|
|
|
// * Alias to set the "offset" value of the query. |
150
|
|
|
// * |
151
|
|
|
// * @param int $value |
152
|
|
|
// * @return \Illuminate\Database\Query\Builder|static |
153
|
|
|
// */ |
154
|
|
|
// public function skip($value) |
155
|
|
|
// { |
156
|
|
|
// return $this->offset($value); |
157
|
|
|
// } |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Alias to set the "limit" value of the query. |
162
|
|
|
* |
163
|
|
|
* @param int $value |
164
|
|
|
* @return \Illuminate\Database\Query\Builder|static |
|
|
|
|
165
|
|
|
*/ |
166
|
|
|
public function take($value) |
167
|
|
|
{ |
168
|
|
|
return $this->limit($value); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the "limit" value of the query. |
174
|
|
|
* |
175
|
|
|
* @param int $value |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function limit($value) |
179
|
|
|
{ |
180
|
|
|
// FIXME ??? 为啥等于0 |
181
|
|
|
if ($value > 0) { |
182
|
|
|
$this->limit = $value; |
183
|
|
|
} |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set the limit and offset for a given page. |
189
|
|
|
* |
190
|
|
|
* @param int $page |
191
|
|
|
* @param int $perPage |
192
|
|
|
* @return \Illuminate\Database\Query\Builder|static |
193
|
|
|
*/ |
194
|
|
|
public function forPage($page, $perPage = 15) |
195
|
|
|
{ |
196
|
|
|
return $this->skip(($page - 1) * $perPage)->take($perPage); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the SQL representation of the query. |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function toSql() |
206
|
|
|
{ |
207
|
|
|
return $this->grammar->compileSelect($this); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get a single column's value from the first result of a query. |
213
|
|
|
* |
214
|
|
|
* @param string $column |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
public function value($column) |
218
|
|
|
{ |
219
|
|
|
$result = (array)$this->first([$column]); |
220
|
|
|
return count($result) > 0 ? reset($result) : null; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Execute the query and get the first result. |
226
|
|
|
* |
227
|
|
|
* @param array $columns |
228
|
|
|
* @return \Illuminate\Database\Eloquent\Model|object|static|null |
|
|
|
|
229
|
|
|
*/ |
230
|
|
|
public function first($columns = ['*']) |
231
|
|
|
{ |
232
|
|
|
return reset($this->take(1)->all($columns)); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Execute the query as a "select" statement. |
238
|
|
|
* |
239
|
|
|
* @param array $columns |
240
|
|
|
* @return Collection |
241
|
|
|
*/ |
242
|
|
|
public function all($columns = ['*']): Collection |
243
|
|
|
{ |
244
|
|
|
$columns = is_array($columns) ? $columns : func_get_args(); |
245
|
|
|
$this->columns = $columns; |
246
|
|
|
|
247
|
|
|
return $this->grammar->all(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param array $data |
252
|
|
|
* @return \Aws\Result |
253
|
|
|
*/ |
254
|
|
|
public function insert(array $data) |
255
|
|
|
{ |
256
|
|
|
return $this->grammar->insert($data); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
} |
261
|
|
|
|