hooklife /
DynamoDBODM
| 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]); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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)); |
||||||
|
0 ignored issues
–
show
array_values($value) is expanded, but the parameter $column of Hoooklife\DynamodbPodm\DynamoDB\Builder::where() does not expect variable arguments.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
$type of type string is incompatible with the type array expected by parameter $values of Hoooklife\DynamodbPodm\DynamoDB\Builder::whereIn().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$column can also be of type Closure; however, parameter $column of Hoooklife\DynamodbPodm\DynamoDB\Builder::whereIn() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | // is null |
||||||
| 111 | if (is_null($value)) { |
||||||
| 112 | return $this->whereNull($column, $type, $boolean, $operator !== '='); |
||||||
|
0 ignored issues
–
show
It seems like
$column can also be of type Closure; however, parameter $column of Hoooklife\DynamodbPodm\D...DB\Builder::whereNull() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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 |
||||||
|
0 ignored issues
–
show
The type
Illuminate\Database\Query\Builder was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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(); |
||||||
|
0 ignored issues
–
show
The method
toArray() does not exist on Hoooklife\DynamodbPodm\DynamoDB\Builder.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 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 |
||||||
|
0 ignored issues
–
show
The type
Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The method
scan() does not exist on Hoooklife\DynamodbPodm\DynamoDB\RawDynamoDbQuery.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 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; |
||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
| 259 | } |
||||||
| 260 | |||||||
| 261 | public function query($columns = []) |
||||||
|
0 ignored issues
–
show
The parameter
$columns is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 262 | { |
||||||
| 263 | $limit = isset($this->limit) ?: -1; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 264 | |||||||
| 265 | } |
||||||
| 266 | |||||||
| 267 | public function toDynamoDBQuery($columns, $limit) |
||||||
| 268 | { |
||||||
| 269 | if (!empty($this->wheres)) { |
||||||
| 270 | $this->dynamoDBBuilder->setTableName($this->table); |
||||||
|
0 ignored issues
–
show
The method
setTableName() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 271 | $this->dynamoDBBuilder->setKeyConditionExpression($this->keyConditionExpression->parse($this->wheres)); |
||||||
|
0 ignored issues
–
show
The method
setKeyConditionExpression() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The method
setLimit() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 281 | if (!empty($columns)) { |
||||||
| 282 | $this->dynamoDBBuilder->setProjectionExpression($this->projectionExpression->parse($columns)); |
||||||
|
0 ignored issues
–
show
The method
setProjectionExpression() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 283 | } |
||||||
| 284 | |||||||
| 285 | $this->dynamoDBBuilder |
||||||
| 286 | ->setExpressionAttributeNames($this->expressionAttributeNames->all()) |
||||||
|
0 ignored issues
–
show
The method
setExpressionAttributeNames() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 287 | ->setExpressionAttributeValues($this->expressionAttributeValues->all()); |
||||||
| 288 | |||||||
| 289 | $op = 'Query'; |
||||||
| 290 | $raw = new RawDynamoDbQuery($op, $this->dynamoDBBuilder->prepare()->query); |
||||||
|
0 ignored issues
–
show
The method
prepare() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 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 |
||||||
|
0 ignored issues
–
show
The type
Hoooklife\DynamodbPodm\DynamoDB\Collection was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 302 | { |
||||||
| 303 | $columns = is_array($columns) ? $columns : func_get_args(); |
||||||
| 304 | $this->columns = $columns; |
||||||
| 305 | |||||||
| 306 | return $this->grammar->all(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 307 | } |
||||||
| 308 | |||||||
| 309 | |||||||
| 310 | public function getGrammar() |
||||||
| 311 | { |
||||||
| 312 | return $this->grammar; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 313 | } |
||||||
| 314 | |||||||
| 315 | public function getBuilder() |
||||||
| 316 | { |
||||||
| 317 | return $this->grammar->getBuilder(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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..