1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MySQLReplication\Repository; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Exception; |
8
|
|
|
use MySQLReplication\BinLog\BinLogException; |
9
|
|
|
use MySQLReplication\Exception\MySQLReplicationException; |
10
|
|
|
|
11
|
|
|
class MySQLRepository implements RepositoryInterface, PingableConnection |
12
|
|
|
{ |
13
|
|
|
private $connection; |
14
|
|
|
|
15
|
64 |
|
public function __construct(Connection $connection) |
16
|
|
|
{ |
17
|
64 |
|
$this->connection = $connection; |
18
|
|
|
} |
19
|
|
|
|
20
|
59 |
|
public function __destruct() |
21
|
|
|
{ |
22
|
59 |
|
$this->connection->close(); |
23
|
|
|
} |
24
|
|
|
|
25
|
55 |
|
public function getFields(string $database, string $table): FieldDTOCollection |
26
|
|
|
{ |
27
|
55 |
|
$sql = ' |
28
|
|
|
SELECT |
29
|
|
|
`COLUMN_NAME`, |
30
|
|
|
`COLLATION_NAME`, |
31
|
|
|
`CHARACTER_SET_NAME`, |
32
|
|
|
`COLUMN_COMMENT`, |
33
|
|
|
`COLUMN_TYPE`, |
34
|
|
|
`COLUMN_KEY` |
35
|
|
|
FROM |
36
|
|
|
`information_schema`.`COLUMNS` |
37
|
|
|
WHERE |
38
|
|
|
`TABLE_SCHEMA` = ? |
39
|
|
|
AND |
40
|
|
|
`TABLE_NAME` = ? |
41
|
|
|
ORDER BY |
42
|
|
|
ORDINAL_POSITION |
43
|
55 |
|
'; |
44
|
|
|
|
45
|
55 |
|
return FieldDTOCollection::makeFromArray($this->getConnection()->fetchAllAssociative($sql, [$database, $table])); |
46
|
|
|
} |
47
|
|
|
|
48
|
63 |
|
private function getConnection(): Connection |
49
|
|
|
{ |
50
|
63 |
|
if (false === $this->ping($this->connection)) { |
51
|
1 |
|
$this->connection->close(); |
52
|
1 |
|
$this->connection->connect(); |
53
|
|
|
} |
54
|
|
|
|
55
|
63 |
|
return $this->connection; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws Exception |
60
|
|
|
*/ |
61
|
60 |
|
public function isCheckSum(): bool |
62
|
|
|
{ |
63
|
60 |
|
$res = $this->getConnection()->fetchAssociative('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"'); |
64
|
|
|
|
65
|
60 |
|
return isset($res['Value']) && $res['Value'] !== 'NONE'; |
66
|
|
|
} |
67
|
|
|
|
68
|
59 |
|
public function getVersion(): string |
69
|
|
|
{ |
70
|
59 |
|
$r = ''; |
71
|
59 |
|
$versions = $this->getConnection()->fetchAllAssociative('SHOW VARIABLES LIKE "version%"'); |
72
|
59 |
|
if (is_array($versions) && 0 !== count($versions)) { |
73
|
59 |
|
foreach ($versions as $version) { |
74
|
59 |
|
$r .= $version['Value']; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
59 |
|
return $r; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
* @throws Exception |
84
|
|
|
* @throws BinLogException |
85
|
|
|
*/ |
86
|
59 |
|
public function getMasterStatus(): MasterStatusDTO |
87
|
|
|
{ |
88
|
59 |
|
$data = $this->getConnection()->fetchAssociative('SHOW MASTER STATUS'); |
89
|
59 |
|
if (empty($data)) { |
90
|
|
|
throw new BinLogException( |
91
|
|
|
MySQLReplicationException::BINLOG_NOT_ENABLED, |
92
|
|
|
MySQLReplicationException::BINLOG_NOT_ENABLED_CODE |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
59 |
|
return MasterStatusDTO::makeFromArray($data); |
97
|
|
|
} |
98
|
|
|
|
99
|
63 |
|
public function ping(Connection $connection): bool |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
63 |
|
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL()); |
103
|
62 |
|
return true; |
104
|
1 |
|
} catch (Exception $e) { |
105
|
1 |
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|