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