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