-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathMySQL.php
More file actions
112 lines (95 loc) · 3.38 KB
/
MySQL.php
File metadata and controls
112 lines (95 loc) · 3.38 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
<?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;
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;
}
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);
}
}