1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Indigerd\Repository\TableGateway; |
4
|
|
|
|
5
|
|
|
use Indigerd\Repository\Exception\DeleteException; |
6
|
|
|
use Indigerd\Repository\Exception\UpdateException; |
7
|
|
|
use Indigerd\Repository\Query\ElasticQueryFactory; |
8
|
|
|
use Indigerd\Repository\TableGateway\ConditionBuilder\ConditionBuilder; |
9
|
|
|
use yii\elasticsearch\Connection; |
10
|
|
|
use yii\elasticsearch\Query; |
11
|
|
|
|
12
|
|
|
class ElasticTableGateway implements TableGatewayInterface |
13
|
|
|
{ |
14
|
|
|
protected $connection; |
15
|
|
|
|
16
|
|
|
protected $queryFactory; |
17
|
|
|
|
18
|
|
|
protected $conditionBuilder; |
19
|
|
|
|
20
|
|
|
protected $collectionName; |
21
|
|
|
|
22
|
|
|
protected $documentType; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
Connection $connection, |
26
|
|
|
ElasticQueryFactory $queryFactory, |
27
|
|
|
ConditionBuilder $conditionBuilder, |
28
|
|
|
string $collectionName, |
29
|
|
|
string $documentType |
30
|
|
|
) { |
31
|
|
|
$this->connection = $connection; |
32
|
|
|
$this->queryFactory = $queryFactory; |
33
|
|
|
$this->conditionBuilder = $conditionBuilder; |
34
|
|
|
$this->collectionName = $collectionName; |
35
|
|
|
$this->documentType = $documentType; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function queryOne(array $conditions, array $relations = []): ?array |
39
|
|
|
{ |
40
|
|
|
$conditions = $this->conditionBuilder->build($conditions); |
41
|
|
|
/** @var Query $query */ |
42
|
|
|
$query = $this->queryFactory->create(); |
43
|
|
|
$query |
44
|
|
|
->from($this->collectionName) |
45
|
|
|
->where($conditions); |
46
|
|
|
$result = $query->one($this->connection); |
47
|
|
|
return $result ? $result : null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function insert(array $data, array $options = []): ?array |
51
|
|
|
{ |
52
|
|
|
/** @var \yii\elasticsearch\Connection $connection */ |
53
|
|
|
$connection = $this->connection; |
54
|
|
|
$result = $connection->createCommand()->insert( |
55
|
|
|
$this->collectionName, |
56
|
|
|
$this->documentType, |
57
|
|
|
$data, |
58
|
|
|
(isset($data['_id']) ? $data['_id'] : null), |
59
|
|
|
$options |
60
|
|
|
); |
61
|
|
|
return ['_id' => $result['_id']]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function updateOne(array $data, array $options = []): void |
65
|
|
|
{ |
66
|
|
|
if (empty($data['_id'])) { |
67
|
|
|
throw new UpdateException($data, "Primary key _id not provided"); |
68
|
|
|
} |
69
|
|
|
$key = $data['_id']; |
70
|
|
|
unset($data['_id']); |
71
|
|
|
$this->connection->createCommand()->update( |
72
|
|
|
$this->collectionName, |
73
|
|
|
$this->documentType, |
74
|
|
|
$key, |
75
|
|
|
$data, |
76
|
|
|
$options |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function updateAll(array $data, array $conditions): int |
81
|
|
|
{ |
82
|
|
|
throw new \RuntimeException('Not implemented'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function deleteOne(array $data, array $options = []): void |
86
|
|
|
{ |
87
|
|
|
if (empty($data['_id'])) { |
88
|
|
|
throw new DeleteException($data, "Primary key _id not provided"); |
89
|
|
|
} |
90
|
|
|
$this->connection->createCommand()->delete( |
91
|
|
|
$this->collectionName, |
92
|
|
|
$this->documentType, |
93
|
|
|
$data['_id'], |
94
|
|
|
$options |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function deleteAll(array $conditions): int |
99
|
|
|
{ |
100
|
|
|
throw new \RuntimeException('Not implemented'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function queryAll(array $conditions, array $order = [], int $limit = 0, int $offset = 0, array $relations = []): array |
104
|
|
|
{ |
105
|
|
|
$conditions = $this->conditionBuilder->build($conditions); |
106
|
|
|
/** @var Query $query */ |
107
|
|
|
$query = $this->queryFactory->create(); |
108
|
|
|
$query |
109
|
|
|
->from($this->collectionName) |
110
|
|
|
->where($conditions); |
111
|
|
|
|
112
|
|
|
if ($limit > 0) { |
113
|
|
|
$query->limit($limit)->offset($offset); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!empty($order)) { |
117
|
|
|
$query->orderBy($order); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $query->all($this->connection); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function aggregate(string $expression, array $conditions): string |
124
|
|
|
{ |
125
|
|
|
throw new \RuntimeException('Not implemented'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function aggregateCount(string $field = '', array $conditions = []): string |
129
|
|
|
{ |
130
|
|
|
$conditions = $this->conditionBuilder->build($conditions); |
131
|
|
|
/** @var Query $query */ |
132
|
|
|
$query = $this->queryFactory->create(); |
133
|
|
|
return (string)$query->from($this->collectionName)->where($conditions)->count('*', $this->connection); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function aggregateSum(string $field, array $conditions = []): string |
137
|
|
|
{ |
138
|
|
|
throw new \RuntimeException('Not implemented'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function aggregateAverage(string $field, array $conditions = []): string |
142
|
|
|
{ |
143
|
|
|
throw new \RuntimeException('Not implemented'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function aggregateMin(string $field, array $conditions = []): string |
147
|
|
|
{ |
148
|
|
|
throw new \RuntimeException('Not implemented'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function aggregateMax(string $field, array $conditions = []): string |
152
|
|
|
{ |
153
|
|
|
throw new \RuntimeException('Not implemented'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|