Skip to content

Commit 903beae

Browse files
committed
Query escape rebuild to support non Latin character sets
1 parent 20761d2 commit 903beae

File tree

2 files changed

+49
-55
lines changed

2 files changed

+49
-55
lines changed

src/Connection.php

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,47 @@
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Str;
1010
use InvalidArgumentException;
11-
use phpDocumentor\Reflection\Types\Scalar;
1211
use RuntimeException;
1312

1413

1514
class Connection extends BaseConnection
1615
{
17-
16+
1817
protected $client;
1918
protected $index;
2019
protected $maxSize;
2120
protected $indexPrefix;
22-
23-
21+
22+
2423
public function __construct(array $config)
2524
{
2625
$this->config = $config;
27-
26+
2827
if (!empty($config['index_prefix'])) {
2928
$this->indexPrefix = $config['index_prefix'];
3029
}
31-
30+
3231
$this->client = $this->buildConnection();
33-
32+
3433
$this->useDefaultPostProcessor();
35-
34+
3635
$this->useDefaultSchemaGrammar();
37-
36+
3837
$this->useDefaultQueryGrammar();
39-
38+
4039
}
41-
40+
4241
public function getIndexPrefix()
4342
{
4443
return $this->indexPrefix;
4544
}
46-
47-
45+
46+
4847
public function getTablePrefix()
4948
{
5049
return $this->getIndexPrefix();
5150
}
52-
51+
5352
public function setIndex($index)
5453
{
5554
$this->index = $index;
@@ -58,98 +57,98 @@ public function setIndex($index)
5857
$this->index = $this->indexPrefix.'_'.$index;
5958
}
6059
}
61-
60+
6261
return $this->getIndex();
6362
}
64-
63+
6564
public function getSchemaGrammar()
6665
{
6766
return new Schema\Grammar($this);
6867
}
69-
68+
7069
public function getIndex()
7170
{
7271
return $this->index;
7372
}
74-
73+
7574
public function setMaxSize($value)
7675
{
7776
$this->maxSize = $value;
7877
}
79-
78+
8079
public function table($table, $as = null)
8180
{
8281
return $this->setIndex($table);
8382
}
84-
85-
83+
84+
8685
/**
8786
* @inheritdoc
8887
*/
8988
public function getSchemaBuilder()
9089
{
9190
return new Schema\Builder($this);
9291
}
93-
94-
92+
93+
9594
/**
9695
* @inheritdoc
9796
*/
9897
public function disconnect()
9998
{
10099
unset($this->connection);
101100
}
102-
103-
101+
102+
104103
/**
105104
* @inheritdoc
106105
*/
107106
public function getDriverName()
108107
{
109108
return 'elasticsearch';
110109
}
111-
110+
112111
/**
113112
* @inheritdoc
114113
*/
115114
protected function getDefaultPostProcessor()
116115
{
117116
return new Query\Processor();
118117
}
119-
118+
120119
/**
121120
* @inheritdoc
122121
*/
123122
protected function getDefaultQueryGrammar()
124123
{
125124
return new Query\Grammar();
126125
}
127-
126+
128127
/**
129128
* @inheritdoc
130129
*/
131130
protected function getDefaultSchemaGrammar()
132131
{
133132
return new Schema\Grammar();
134133
}
135-
136-
134+
135+
137136
//----------------------------------------------------------------------
138137
// Connection Builder
139138
//----------------------------------------------------------------------
140-
139+
141140
protected function buildConnection()
142141
{
143142
$type = config('database.connections.elasticsearch.auth_type') ?? null;
144143
$type = strtolower($type);
145144
if (!in_array($type, ['http', 'cloud', 'api'])) {
146145
throw new RuntimeException('Invalid [auth_type] in database config. Must be: http, cloud or api');
147146
}
148-
147+
149148
return $this->{'_'.$type.'Connection'}();
150-
149+
151150
}
152-
151+
153152
protected function _httpConnection()
154153
{
155154
$hosts = config('database.connections.elasticsearch.hosts') ?? null;
@@ -163,10 +162,10 @@ protected function _httpConnection()
163162
if ($certPath) {
164163
$cb->setSSLVerification($certPath);
165164
}
166-
165+
167166
return $cb->build();
168167
}
169-
168+
170169
protected function _cloudConnection()
171170
{
172171
$cloudId = config('database.connections.elasticsearch.cloud_id') ?? null;
@@ -184,11 +183,11 @@ protected function _cloudConnection()
184183
if ($certPath) {
185184
$cb->setSSLVerification($certPath);
186185
}
187-
186+
188187
return $cb->build();
189188
}
190-
191-
189+
190+
192191
protected function _apiConnection()
193192
{
194193
$apiId = config('database.connections.elasticsearch.api_id') ?? null;
@@ -198,19 +197,19 @@ protected function _apiConnection()
198197
if ($certPath) {
199198
$cb->setSSLVerification($certPath);
200199
}
201-
200+
202201
return $cb->build();
203202
}
204-
205-
203+
204+
206205
//----------------------------------------------------------------------
207206
// Dynamic call routing to DSL bridge
208207
//----------------------------------------------------------------------
209-
208+
210209
public function __call($method, $parameters)
211210
{
212211
$bridge = new Bridge($this->client, $this->index, $this->maxSize);
213-
212+
214213
return $bridge->{'process'.Str::studly($method)}(...$parameters);
215214
}
216215
}

src/DSL/QueryBuilder.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,14 @@ private static function _parseParams($key, $value): string
254254

255255
}
256256

257-
public static function _escape($string): string
257+
public static function _escape($value): string
258258
{
259-
//+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
260-
$stripped = preg_replace('/\W/', '\\\\$0', $string);
261-
262-
//Put the spaces back;
263-
$stripped = str_replace('\ ', ' ', $stripped);
264-
//Edge cases
265-
$stripped = str_replace('\&\&', '\&&', $stripped);
266-
$stripped = str_replace('\|\|', '\||', $stripped);
267-
268-
return $stripped;
259+
$specialChars = ['"', '\\', '~', '^'];
260+
foreach ($specialChars as $char) {
261+
$value = str_replace($char, "\\".$char, $value);
262+
}
269263

264+
return $value;
270265
}
271266

272267
private function _buildQuery($wheres): array

0 commit comments

Comments
 (0)