3
3
namespace Kitar\Dynamodb\Query;
4
4
5
5
use Closure;
6
+ use BadMethodCallException;
6
7
use Kitar\Dynamodb\Connection;
7
8
use Kitar\Dynamodb\Query\Grammar;
8
9
use Kitar\Dynamodb\Query\Processor;
9
10
use Kitar\Dynamodb\Query\ExpressionAttributes;
11
+ use Illuminate\Support\Str;
10
12
use Illuminate\Database\Query\Expression;
11
13
use Illuminate\Database\Query\Builder as BaseBuilder;
12
14
@@ -56,19 +58,25 @@ class Builder extends BaseBuilder
56
58
public $expression_attributes;
57
59
58
60
/**
59
- * Query for building FilterExpression.
61
+ * Available where methods which will pass to dedicated queries.
62
+ * @var array
63
+ */
64
+ public $available_wheres;
65
+
66
+ /**
67
+ * Dedicated query for building FilterExpression.
60
68
* @var Kitar\Dynamodb\Query\Builder
61
69
*/
62
70
public $filter_query;
63
71
64
72
/**
65
- * Query for building ConditionExpression.
73
+ * Dedicated query for building ConditionExpression.
66
74
* @var Kitar\Dynamodb\Query\Builder
67
75
*/
68
76
public $condition_query;
69
77
70
78
/**
71
- * Query for building KeyConditionExpression.
79
+ * Dedicated query for building KeyConditionExpression.
72
80
* @var Kitar\Dynamodb\Query\Builder
73
81
*/
74
82
public $key_condition_query;
@@ -87,9 +95,7 @@ public function __construct(Connection $connection, Grammar $grammar, Processor
87
95
$this->expression_attributes = $expression_attributes ?? new ExpressionAttributes;
88
96
89
97
if (! $is_nested_query) {
90
- $this->filter_query = $this->newQuery()->whereAs('FilterExpression');
91
- $this->condition_query = $this->newQuery()->whereAs('ConditionExpression');
92
- $this->key_condition_query = $this->newQuery()->whereAs('KeyConditionExpression');
98
+ $this->initializeDedicatedQueries();
93
99
}
94
100
}
95
101
@@ -141,39 +147,6 @@ public function dryRun($active = true)
141
147
return $this;
142
148
}
143
149
144
- /**
145
- * where for FilterExpression.
146
- * @return $this
147
- */
148
- public function filter($column, $operator = null, $value = null, $boolean = 'and')
149
- {
150
- $this->filter_query = $this->filter_query->where($column, $operator, $value, $boolean);
151
-
152
- return $this;
153
- }
154
-
155
- /**
156
- * where for ConditionExpression.
157
- * @return $this
158
- */
159
- public function condition($column, $operator = null, $value = null, $boolean = 'and')
160
- {
161
- $this->condition_query = $this->condition_query->where($column, $operator, $value, $boolean);
162
-
163
- return $this;
164
- }
165
-
166
- /**
167
- * where for KeyConditionExpression.
168
- * @return $this
169
- */
170
- public function keyCondition($column, $operator = null, $value = null, $boolean = 'and')
171
- {
172
- $this->key_condition_query = $this->key_condition_query->where($column, $operator, $value, $boolean);
173
-
174
- return $this;
175
- }
176
-
177
150
/**
178
151
* Set key name of wheres. eg. FilterExpression
179
152
* @param string $condition_key_name
@@ -241,6 +214,51 @@ public function scan()
241
214
return $this->process('scan', 'processMultipleItems');
242
215
}
243
216
217
+ /**
218
+ * Make individual Builder instance for condition types. (Filter, Condition and KeyCondition)
219
+ * @return void
220
+ */
221
+ public function initializeDedicatedQueries()
222
+ {
223
+ $this->filter_query = $this->newQuery()->whereAs('FilterExpression');
224
+ $this->condition_query = $this->newQuery()->whereAs('ConditionExpression');
225
+ $this->key_condition_query = $this->newQuery()->whereAs('KeyConditionExpression');
226
+
227
+ // Make method map.
228
+ // Array of: 'incomingMethodName' => [ 'target_builder_instance_name', 'targetMethodName' ]
229
+ foreach (['filter', 'condition', 'key_condition'] as $query_type) {
230
+ foreach (['', 'or'] as $boolean) {
231
+ foreach ([''] as $where_type) {
232
+ $target_query = $query_type . '_query';
233
+ $source_method = Str::camel(implode('_', [$boolean, $query_type, $where_type]));
234
+ $target_method = Str::camel(implode('_', [$boolean, 'where', $where_type]));
235
+
236
+ $this->available_wheres[$source_method] = [$target_query, $target_method];
237
+ }
238
+ }
239
+ }
240
+ }
241
+
242
+ /**
243
+ * Perform where methods within dedicated queries.
244
+ * @param string $method
245
+ * @param array $parameters
246
+ * @return $this
247
+ */
248
+ public function __call($method, $parameters)
249
+ {
250
+ if (isset($this->available_wheres[$method])) {
251
+ $target_query = $this->available_wheres[$method][0];
252
+ $target_method = $this->available_wheres[$method][1];
253
+
254
+ $this->$target_query = call_user_func_array([$this->$target_query, $target_method], $parameters);
255
+
256
+ return $this;
257
+ }
258
+
259
+ throw new BadMethodCallException('Call to undefined method ' . static::class . "::{$method}()");
260
+ }
261
+
244
262
/**
245
263
* @inheritdoc
246
264
*/
@@ -249,7 +267,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
249
267
// Convert column and value to ExpressionAttributes.
250
268
if (! $column instanceof Closure) {
251
269
$column = $this->expression_attributes->addName($column);
252
- if (! empty( $value) ) {
270
+ if ($value !== null ) {
253
271
$value = $this->expression_attributes->addValue($value);
254
272
}
255
273
}
0 commit comments