-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathMySQL.php
More file actions
312 lines (276 loc) · 9.94 KB
/
MySQL.php
File metadata and controls
312 lines (276 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
namespace Utopia\Database\Adapter;
use PDOException;
use Utopia\Database\Database;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Character as CharacterException;
use Utopia\Database\Exception\Dependency as DependencyException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Operator;
use Utopia\Database\Query;
class MySQL extends MariaDB
{
/**
* Set max execution time
* @param int $milliseconds
* @param string $event
* @return void
* @throws DatabaseException
*/
public function setTimeout(int $milliseconds, string $event = Database::EVENT_ALL): void
{
if (!$this->getSupportForTimeouts()) {
return;
}
if ($milliseconds <= 0) {
throw new DatabaseException('Timeout must be greater than 0');
}
$this->timeout = $milliseconds;
$this->before($event, 'timeout', function ($sql) use ($milliseconds) {
return \preg_replace(
pattern: '/SELECT/',
replacement: "SELECT /*+ max_execution_time({$milliseconds}) */",
subject: $sql,
limit: 1
);
});
}
/**
* Get size of collection on disk
* @param string $collection
* @return int
* @throws DatabaseException
*/
public function getSizeOfCollectionOnDisk(string $collection): int
{
$collection = $this->filter($collection);
$collection = $this->getNamespace() . '_' . $collection;
$database = $this->getDatabase();
$name = $database . '/' . $collection;
$permissions = $database . '/' . $collection . '_perms';
$collectionSize = $this->getPDO()->prepare("
SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE)
FROM INFORMATION_SCHEMA.INNODB_TABLESPACES
WHERE NAME = :name
");
$permissionsSize = $this->getPDO()->prepare("
SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE)
FROM INFORMATION_SCHEMA.INNODB_TABLESPACES
WHERE NAME = :permissions
");
$collectionSize->bindParam(':name', $name);
$permissionsSize->bindParam(':permissions', $permissions);
try {
$collectionSize->execute();
$permissionsSize->execute();
$size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn();
} catch (PDOException $e) {
throw new DatabaseException('Failed to get collection size: ' . $e->getMessage());
}
return $size;
}
/**
* Handle distance spatial queries
*
* @param Query $query
* @param array<string, mixed> $binds
* @param string $attribute
* @param string $type
* @param string $alias
* @param string $placeholder
* @return string
*/
protected function handleDistanceSpatialQueries(Query $query, array &$binds, string $attribute, string $type, string $alias, string $placeholder): string
{
$distanceParams = $query->getValues()[0];
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($distanceParams[0]);
$binds[":{$placeholder}_1"] = $distanceParams[1];
$useMeters = isset($distanceParams[2]) && $distanceParams[2] === true;
switch ($query->getMethod()) {
case Query::TYPE_DISTANCE_EQUAL:
$operator = '=';
break;
case Query::TYPE_DISTANCE_NOT_EQUAL:
$operator = '!=';
break;
case Query::TYPE_DISTANCE_GREATER_THAN:
$operator = '>';
break;
case Query::TYPE_DISTANCE_LESS_THAN:
$operator = '<';
break;
default:
throw new DatabaseException('Unknown spatial query method: ' . $query->getMethod());
}
if ($useMeters) {
$attr = "ST_SRID({$alias}.{$attribute}, " . Database::DEFAULT_SRID . ")";
$geom = $this->getSpatialGeomFromText(":{$placeholder}_0", null);
return "ST_Distance({$attr}, {$geom}, 'metre') {$operator} :{$placeholder}_1";
}
// need to use srid 0 because of geometric distance
$attr = "ST_SRID({$alias}.{$attribute}, " . 0 . ")";
$geom = $this->getSpatialGeomFromText(":{$placeholder}_0", 0);
return "ST_Distance({$attr}, {$geom}) {$operator} :{$placeholder}_1";
}
public function getSupportForIndexArray(): bool
{
/**
* @link https://bugs.mysql.com/bug.php?id=111037
*/
return true;
}
public function getSupportForCastIndexArray(): bool
{
if (!$this->getSupportForIndexArray()) {
return false;
}
return true;
}
protected function processException(PDOException $e): \Exception
{
if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1366) {
return new CharacterException('Invalid character', $e->getCode(), $e);
}
// Timeout
if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 3024) {
return new TimeoutException('Query timed out', $e->getCode(), $e);
}
// Functional index dependency
if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 3837) {
return new DependencyException('Attribute cannot be deleted because it is used in an index', $e->getCode(), $e);
}
if ($e->getCode() === '22004' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1138) {
return new StructureException('Attribute does not allow null values', $e->getCode(), $e);
}
return parent::processException($e);
}
/**
* Does the adapter includes boundary during spatial contains?
*
* @return bool
*/
public function getSupportForBoundaryInclusiveContains(): bool
{
return false;
}
/**
* Does the adapter support order attribute in spatial indexes?
*
* @return bool
*/
public function getSupportForSpatialIndexOrder(): bool
{
return false;
}
/**
* Does the adapter support calculating distance(in meters) between multidimension geometry(line, polygon,etc)?
*
* @return bool
*/
public function getSupportForDistanceBetweenMultiDimensionGeometryInMeters(): bool
{
return true;
}
/**
* Spatial type attribute
*/
public function getSpatialSQLType(string $type, bool $required): string
{
switch ($type) {
case Database::VAR_POINT:
$type = 'POINT SRID 4326';
if (!$this->getSupportForSpatialIndexNull()) {
if ($required) {
$type .= ' NOT NULL';
} else {
$type .= ' NULL';
}
}
return $type;
case Database::VAR_LINESTRING:
$type = 'LINESTRING SRID 4326';
if (!$this->getSupportForSpatialIndexNull()) {
if ($required) {
$type .= ' NOT NULL';
} else {
$type .= ' NULL';
}
}
return $type;
case Database::VAR_POLYGON:
$type = 'POLYGON SRID 4326';
if (!$this->getSupportForSpatialIndexNull()) {
if ($required) {
$type .= ' NOT NULL';
} else {
$type .= ' NULL';
}
}
return $type;
}
return '';
}
/**
* Does the adapter support spatial axis order specification?
*
* @return bool
*/
public function getSupportForSpatialAxisOrder(): bool
{
return true;
}
/**
* Get the spatial axis order specification string for MySQL
* MySQL with SRID 4326 expects lat-long by default, but our data is in long-lat format
*
* @return string
*/
protected function getSpatialAxisOrderSpec(): string
{
return "'axis-order=long-lat'";
}
/**
* Adapter supports optional spatial attributes with existing rows.
*
* @return bool
*/
public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
{
return false;
}
/**
* Get SQL expression for operator
* Override for MySQL-specific operator implementations
*
* @param string $column
* @param \Utopia\Database\Operator $operator
* @param int &$bindIndex
* @return ?string
*/
protected function getOperatorSQL(string $column, \Utopia\Database\Operator $operator, int &$bindIndex): ?string
{
$quotedColumn = $this->quote($column);
$method = $operator->getMethod();
switch ($method) {
case Operator::TYPE_ARRAY_APPEND:
$bindKey = "op_{$bindIndex}";
$bindIndex++;
return "{$quotedColumn} = JSON_MERGE_PRESERVE(IFNULL({$quotedColumn}, JSON_ARRAY()), :$bindKey)";
case Operator::TYPE_ARRAY_PREPEND:
$bindKey = "op_{$bindIndex}";
$bindIndex++;
return "{$quotedColumn} = JSON_MERGE_PRESERVE(:$bindKey, IFNULL({$quotedColumn}, JSON_ARRAY()))";
case Operator::TYPE_ARRAY_UNIQUE:
return "{$quotedColumn} = IFNULL((
SELECT JSON_ARRAYAGG(value)
FROM (
SELECT DISTINCT value
FROM JSON_TABLE({$quotedColumn}, '\$[*]' COLUMNS(value TEXT PATH '\$')) AS jt
) AS distinct_values
), JSON_ARRAY())";
}
// For all other operators, use parent implementation
return parent::getOperatorSQL($column, $operator, $bindIndex);
}
}