|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hoooklife\DynamodbPodm\Grammars; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\DynamoDb\Marshaler; |
|
6
|
|
|
use Hoooklife\DynamodbPodm\Collection; |
|
7
|
|
|
use Hoooklife\DynamodbPodm\DB; |
|
8
|
|
|
use Hoooklife\DynamodbPodm\Query\Builder; |
|
9
|
|
|
use Hoooklife\DynamodbPodm\Grammars\DynamoDBBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class DynamoDBGrammar |
|
12
|
|
|
{ |
|
13
|
|
|
protected $operators = []; |
|
14
|
|
|
|
|
15
|
|
|
protected $params = []; |
|
16
|
|
|
|
|
17
|
|
|
protected $insertParam = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Builder |
|
21
|
|
|
*/ |
|
22
|
|
|
private $builder; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
private $dynamoDBBuilder; |
|
29
|
|
|
|
|
30
|
|
|
private $attributeValues = []; |
|
31
|
|
|
|
|
32
|
|
|
/** @var Marshaler $marshaler */ |
|
33
|
|
|
private $marshaler; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* DynamoDBGrammar constructor. |
|
37
|
|
|
* @param Builder $builder |
|
38
|
|
|
* @param array $config |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Builder $builder, array $config) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->builder = $builder; |
|
43
|
|
|
$this->config = $config; |
|
44
|
|
|
|
|
45
|
|
|
$this->dynamoDBBuilder = new DynamoDBBuilder($config); |
|
46
|
|
|
$this->marshaler = new Marshaler(); |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// 表达式解析 where |
|
51
|
|
|
private function parseKeyConditionExpression() |
|
52
|
|
|
{ |
|
53
|
|
|
$expression = []; |
|
54
|
|
|
foreach ($this->builder->wheres as $index => $where) { |
|
55
|
|
|
$expression[] = "{$where['column']} {$where['operator']} :{$index}"; |
|
56
|
|
|
// param bind |
|
57
|
|
|
$this->attributeValues[':' . $index] = $where['value']; |
|
58
|
|
|
} |
|
59
|
|
|
return implode("and", $expression); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function parseExpressionAttributeValues() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->marshaler->marshalItem($this->attributeValues); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// select |
|
68
|
|
|
public function parseProjectionExpression() |
|
69
|
|
|
{ |
|
70
|
|
|
if (reset($this->builder->columns) != '*') { |
|
71
|
|
|
return implode(",", $this->columns); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// limit |
|
77
|
|
|
public function parseLimit() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->builder->limit; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the grammar specific operators. |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getOperators() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->operators; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function insert($data) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->createInsertParam($data); |
|
95
|
|
|
|
|
96
|
|
|
$builder = $this->dynamoDBBuilder |
|
97
|
|
|
->setRequestItems([$this->builder->table => $this->insertParam]); |
|
98
|
|
|
|
|
99
|
|
|
return $builder->batchWriteItem(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function createInsertParam($data) |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ($data as $key => $value) { |
|
105
|
|
|
if (is_numeric($key) && is_array($value)) { |
|
106
|
|
|
$this->createInsertParam($value); |
|
107
|
|
|
} else { |
|
108
|
|
|
$this->insertParam[] = [ |
|
109
|
|
|
'PutRequest' => [ |
|
110
|
|
|
'Item' => $this->marshaler->marshalItem($data) |
|
111
|
|
|
] |
|
112
|
|
|
]; |
|
113
|
|
|
break; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function all($columns): Collection |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
$builder = $this->dynamoDBBuilder |
|
121
|
|
|
->setTableName($this->builder->table) |
|
122
|
|
|
->setKeyConditionExpression($this->parseKeyConditionExpression()) |
|
123
|
|
|
->setExpressionAttributeValues($this->parseExpressionAttributeValues()); |
|
124
|
|
|
return new Collection($builder->query()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
} |