Skip to content

Commit 9868508

Browse files
committed
Queries throw errors explicitly, fixed api connector bug
1 parent 21cd90c commit 9868508

File tree

2 files changed

+72
-58
lines changed

2 files changed

+72
-58
lines changed

src/Connection.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,42 @@
1414

1515
class Connection extends BaseConnection
1616
{
17-
17+
1818
protected $client;
1919
protected $index;
2020
protected $maxSize;
2121
protected $indexPrefix;
22-
23-
22+
23+
2424
public function __construct(array $config)
2525
{
2626
$this->config = $config;
27-
27+
2828
if (!empty($config['index_prefix'])) {
2929
$this->indexPrefix = $config['index_prefix'];
3030
}
31-
31+
3232
$this->client = $this->buildConnection();
33-
33+
3434
$this->useDefaultPostProcessor();
35-
35+
3636
$this->useDefaultSchemaGrammar();
37-
37+
3838
$this->useDefaultQueryGrammar();
39-
39+
4040
}
41-
41+
4242
public function getIndexPrefix()
4343
{
4444
return $this->indexPrefix;
4545
}
46-
47-
46+
47+
4848
public function getTablePrefix()
4949
{
5050
return $this->getIndexPrefix();
5151
}
52-
52+
5353
public function setIndex($index)
5454
{
5555
$this->index = $index;
@@ -58,98 +58,98 @@ public function setIndex($index)
5858
$this->index = $this->indexPrefix.'_'.$index;
5959
}
6060
}
61-
61+
6262
return $this->getIndex();
6363
}
64-
64+
6565
public function getSchemaGrammar()
6666
{
6767
return new Schema\Grammar($this);
6868
}
69-
69+
7070
public function getIndex()
7171
{
7272
return $this->index;
7373
}
74-
74+
7575
public function setMaxSize($value)
7676
{
7777
$this->maxSize = $value;
7878
}
79-
79+
8080
public function table($table, $as = null)
8181
{
8282
return $this->setIndex($table);
8383
}
84-
85-
84+
85+
8686
/**
8787
* @inheritdoc
8888
*/
8989
public function getSchemaBuilder()
9090
{
9191
return new Schema\Builder($this);
9292
}
93-
94-
93+
94+
9595
/**
9696
* @inheritdoc
9797
*/
9898
public function disconnect()
9999
{
100100
unset($this->connection);
101101
}
102-
103-
102+
103+
104104
/**
105105
* @inheritdoc
106106
*/
107107
public function getDriverName()
108108
{
109109
return 'elasticsearch';
110110
}
111-
111+
112112
/**
113113
* @inheritdoc
114114
*/
115115
protected function getDefaultPostProcessor()
116116
{
117117
return new Query\Processor();
118118
}
119-
119+
120120
/**
121121
* @inheritdoc
122122
*/
123123
protected function getDefaultQueryGrammar()
124124
{
125125
return new Query\Grammar();
126126
}
127-
127+
128128
/**
129129
* @inheritdoc
130130
*/
131131
protected function getDefaultSchemaGrammar()
132132
{
133133
return new Schema\Grammar();
134134
}
135-
136-
135+
136+
137137
//----------------------------------------------------------------------
138138
// Connection Builder
139139
//----------------------------------------------------------------------
140-
140+
141141
protected function buildConnection()
142142
{
143143
$type = config('database.connections.elasticsearch.auth_type') ?? null;
144144
$type = strtolower($type);
145145
if (!in_array($type, ['https', 'cloud',])) {
146146
throw new RuntimeException('Invalid [auth_type] in database config. Must be: http, cloud or api');
147147
}
148-
148+
149149
return $this->{'_'.$type.'Connection'}();
150-
150+
151151
}
152-
152+
153153
protected function _httpsConnection()
154154
{
155155
$hosts = config('database.connections.elasticsearch.hosts') ?? null;
@@ -163,10 +163,10 @@ protected function _httpsConnection()
163163
if ($certPath) {
164164
$cb->setCABundle($certPath);
165165
}
166-
166+
167167
return $cb->build();
168168
}
169-
169+
170170
protected function _cloudConnection()
171171
{
172172
$cloudId = config('database.connections.elasticsearch.cloud_id') ?? null;
@@ -177,26 +177,26 @@ protected function _cloudConnection()
177177
$certPath = config('database.connections.elasticsearch.ssl_cert') ?? null;
178178
$cb = ClientBuilder::create()->setElasticCloudId($cloudId);
179179
if ($apiId && $apiKey) {
180-
$cb->setApiKey($apiId, $apiKey)->build();
180+
$cb->setApiKey($apiKey, $apiId)->build();
181181
} elseif ($username && $pass) {
182182
$cb->setBasicAuthentication($username, $pass)->build();
183183
}
184184
if ($certPath) {
185185
$cb->setSSLVerification($certPath);
186186
}
187-
187+
188188
return $cb->build();
189189
}
190190

191-
191+
192192
//----------------------------------------------------------------------
193193
// Dynamic call routing to DSL bridge
194194
//----------------------------------------------------------------------
195-
195+
196196
public function __call($method, $parameters)
197197
{
198198
$bridge = new Bridge($this->client, $this->index, $this->maxSize);
199-
199+
200200
return $bridge->{'process'.Str::studly($method)}(...$parameters);
201201
}
202202
}

src/DSL/Bridge.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ protected function _returnSearch($params, $source)
116116
return $this->_sanitizeSearchResponse($process, $params, $this->_queryTag($source));
117117
} catch (Exception $e) {
118118

119-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag($source));
119+
$error = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
120+
throw new Exception($error->errorMessage);
120121
}
121122
}
122123

@@ -146,7 +147,8 @@ public function processDistinct($column, $wheres): Results
146147
return $this->_return($data, $process, $params, $this->_queryTag(__FUNCTION__));
147148
} catch (Exception $e) {
148149

149-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
150+
$error = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
151+
throw new Exception($error->errorMessage);
150152
}
151153

152154

@@ -195,7 +197,8 @@ public function processSave($data, $refresh): Results
195197

196198
return $this->_return($savedData, $response, $params, $this->_queryTag(__FUNCTION__));
197199
} catch (Exception $e) {
198-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
200+
$error = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
201+
throw new Exception($error->errorMessage);
199202
}
200203

201204

@@ -291,11 +294,13 @@ public function processIncrementMany($wheres, $newValues, $options, $refresh): R
291294

292295
public function processDeleteAll($wheres, $options = []): Results
293296
{
297+
$params = [
298+
'index' => $this->index,
299+
'id' => $wheres['_id'],
300+
];
301+
294302
if (isset($wheres['_id'])) {
295-
$params = [
296-
'index' => $this->index,
297-
'id' => $wheres['_id'],
298-
];
303+
299304
try {
300305
$response = $this->client->delete($params);
301306
$response['deleteCount'] = $response['result'] === 'deleted' ? 1 : 0;
@@ -312,7 +317,8 @@ public function processDeleteAll($wheres, $options = []): Results
312317

313318
return $this->_return($response['deleteCount'], $response, $params, $this->_queryTag(__FUNCTION__));
314319
} catch (Exception $e) {
315-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
320+
$error = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
321+
throw new Exception($error->errorMessage);
316322
}
317323

318324
}
@@ -524,70 +530,78 @@ public function _countAggregate($wheres, $options, $columns): Results
524530

525531
private function _maxAggregate($wheres, $options, $columns): Results
526532
{
533+
$params = $this->buildParams($this->index, $wheres, $options);
527534
try {
528-
$params = $this->buildParams($this->index, $wheres, $options);
535+
529536
$params['body']['aggs']['max_value'] = ParameterBuilder::maxAggregation($columns[0]);
530537
$process = $this->client->search($params);
531538

532539
return $this->_return($process['aggregations']['max_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
533540
} catch (Exception $e) {
534541

535-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
542+
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
543+
throw new Exception($result->errorMessage);
536544
}
537545
}
538546

539547
private function _minAggregate($wheres, $options, $columns): Results
540548
{
549+
$params = $this->buildParams($this->index, $wheres, $options);
541550
try {
542-
$params = $this->buildParams($this->index, $wheres, $options);
551+
543552
$params['body']['aggs']['min_value'] = ParameterBuilder::minAggregation($columns[0]);
544553
$process = $this->client->search($params);
545554

546555
return $this->_return($process['aggregations']['min_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
547556
} catch (Exception $e) {
548-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
557+
558+
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
559+
throw new Exception($result->errorMessage);
549560
}
550561
}
551562

552563
private function _sumAggregate($wheres, $options, $columns): Results
553564
{
554565

566+
$params = $this->buildParams($this->index, $wheres, $options);
555567
try {
556-
$params = $this->buildParams($this->index, $wheres, $options);
557568
$params['body']['aggs']['sum_value'] = ParameterBuilder::sumAggregation($columns[0]);
558569
$process = $this->client->search($params);
559570

560571
return $this->_return($process['aggregations']['sum_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
561572
} catch (Exception $e) {
562573

563-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
574+
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
575+
throw new Exception($result->errorMessage);
564576
}
565577

566578
}
567579

568580
private function _avgAggregate($wheres, $options, $columns): Results
569581
{
582+
$params = $this->buildParams($this->index, $wheres, $options);
570583
try {
571-
$params = $this->buildParams($this->index, $wheres, $options);
572584
$params['body']['aggs']['avg_value'] = ParameterBuilder::avgAggregation($columns[0]);
573585
$process = $this->client->search($params);
574586

575587
return $this->_return($process['aggregations']['avg_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
576588
} catch (Exception $e) {
577-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
589+
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
590+
throw new Exception($result->errorMessage);
578591
}
579592
}
580593

581594
private function _matrixAggregate($wheres, $options, $columns): Results
582595
{
596+
$params = $this->buildParams($this->index, $wheres, $options);
583597
try {
584-
$params = $this->buildParams($this->index, $wheres, $options);
585598
$params['body']['aggs']['statistics'] = ParameterBuilder::matrixAggregation($columns);
586599
$process = $this->client->search($params);
587600

588601
return $this->_return($process['aggregations']['statistics'] ?? [], $process, $params, $this->_queryTag(__FUNCTION__));
589602
} catch (Exception $e) {
590-
return $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
603+
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
604+
throw new Exception($result->errorMessage);
591605
}
592606

593607
}

0 commit comments

Comments
 (0)