-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathMySQL.php
More file actions
187 lines (164 loc) · 5.82 KB
/
MySQL.php
File metadata and controls
187 lines (164 loc) · 5.82 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
<?php
namespace Utopia\Database\Adapter;
use PDOException;
use Utopia\Database\Database;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Dependency as DependencyException;
use Utopia\Database\Exception\Timeout as TimeoutException;
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::SRID . ")";
$geom = "ST_GeomFromText(:{$placeholder}_0, " . Database::SRID . ",'axis-order=long-lat')";
return "ST_Distance({$attr}, {$geom}, 'metre') {$operator} :{$placeholder}_1";
}
// Without meters, use default behavior
return "ST_Distance({$alias}.{$attribute}, ST_GeomFromText(:{$placeholder}_0)) {$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
{
// 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);
}
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;
}
}