forked from krowinski/php-mysql-replication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLRepository.php
More file actions
102 lines (85 loc) · 2.66 KB
/
MySQLRepository.php
File metadata and controls
102 lines (85 loc) · 2.66 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
<?php
declare(strict_types=1);
namespace MySQLReplication\Repository;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use MySQLReplication\BinLog\BinLogException;
use MySQLReplication\Exception\MySQLReplicationException;
readonly class MySQLRepository implements RepositoryInterface, PingableConnection
{
public function __construct(
private Connection $connection
) {
}
public function __destruct()
{
$this->connection->close();
}
public function getFields(string $database, string $table): FieldDTOCollection
{
$sql = '
SELECT
`COLUMN_NAME`,
`COLLATION_NAME`,
`CHARACTER_SET_NAME`,
`COLUMN_COMMENT`,
`COLUMN_TYPE`,
`COLUMN_KEY`
FROM
`information_schema`.`COLUMNS`
WHERE
`TABLE_SCHEMA` = ?
AND
`TABLE_NAME` = ?
ORDER BY
ORDINAL_POSITION
';
return FieldDTOCollection::makeFromArray(
$this->getConnection()
->fetchAllAssociative($sql, [$database, $table])
);
}
public function isCheckSum(): bool
{
$res = $this->getConnection()
->fetchAssociative('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"');
return isset($res['Value']) && $res['Value'] !== 'NONE';
}
public function getVersion(): string
{
$res = $this->getConnection()
->fetchAssociative('SHOW VARIABLES LIKE "version"');
return $res['Value'] ?? '';
}
public function getMasterStatus(): MasterStatusDTO
{
$query = 'SHOW MASTER STATUS';
if (str_starts_with($this->getVersion(), '8.4')) {
$query = 'SHOW BINARY LOG STATUS';
}
$data = $this->getConnection()
->fetchAssociative($query);
if (empty($data)) {
throw new BinLogException(
MySQLReplicationException::BINLOG_NOT_ENABLED,
MySQLReplicationException::BINLOG_NOT_ENABLED_CODE
);
}
return MasterStatusDTO::makeFromArray($data);
}
public function ping(Connection $connection): bool
{
try {
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
return true;
} catch (Exception) {
return false;
}
}
private function getConnection(): Connection
{
// In DBAL 4.x, connections handle reconnection automatically
// No need for manual ping/reconnect logic
return $this->connection;
}
}