1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MySQLReplication\Repository; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\DBALException; |
8
|
|
|
use MySQLReplication\BinLog\BinLogException; |
9
|
|
|
use MySQLReplication\Exception\MySQLReplicationException; |
10
|
|
|
|
11
|
|
|
class MySQLRepository implements RepositoryInterface |
12
|
|
|
{ |
13
|
|
|
private $connection; |
14
|
|
|
|
15
|
62 |
|
public function __construct(Connection $connection) |
16
|
|
|
{ |
17
|
62 |
|
$this->connection = $connection; |
18
|
62 |
|
} |
19
|
|
|
|
20
|
57 |
|
public function __destruct() |
21
|
|
|
{ |
22
|
57 |
|
$this->connection->close(); |
23
|
57 |
|
} |
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
|
|
|
'; |
44
|
|
|
|
45
|
55 |
|
return FieldDTOCollection::makeFromArray($this->getConnection()->fetchAll($sql, [$database, $table])); |
46
|
|
|
} |
47
|
|
|
|
48
|
61 |
|
private function getConnection(): Connection |
49
|
|
|
{ |
50
|
61 |
|
if (false === $this->connection->ping()) { |
51
|
1 |
|
$this->connection->close(); |
52
|
1 |
|
$this->connection->connect(); |
53
|
|
|
} |
54
|
|
|
|
55
|
61 |
|
return $this->connection; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws DBALException |
60
|
|
|
*/ |
61
|
58 |
|
public function isCheckSum(): bool |
62
|
|
|
{ |
63
|
58 |
|
$res = $this->getConnection()->fetchAssoc('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"'); |
64
|
|
|
|
65
|
58 |
|
return isset($res['Value']) && $res['Value'] !== 'NONE'; |
66
|
|
|
} |
67
|
|
|
|
68
|
57 |
|
public function getVersion(): string |
69
|
|
|
{ |
70
|
57 |
|
$r = ''; |
71
|
57 |
|
$versions = $this->getConnection()->fetchAll('SHOW VARIABLES LIKE "version%"'); |
72
|
57 |
|
if (is_array($versions) && 0 !== count($versions)) { |
73
|
57 |
|
foreach ($versions as $version) { |
74
|
57 |
|
$r .= $version['Value']; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
57 |
|
return $r; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
* @throws DBALException |
84
|
|
|
* @throws BinLogException |
85
|
|
|
*/ |
86
|
57 |
|
public function getMasterStatus(): MasterStatusDTO |
87
|
|
|
{ |
88
|
57 |
|
$data = $this->getConnection()->fetchAssoc('SHOW MASTER STATUS'); |
89
|
57 |
|
if (empty($data)) { |
90
|
|
|
throw new BinLogException( |
91
|
|
|
MySQLReplicationException::BINLOG_NOT_ENABLED, |
92
|
|
|
MySQLReplicationException::BINLOG_NOT_ENABLED_CODE |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
57 |
|
return MasterStatusDTO::makeFromArray($data); |
97
|
|
|
} |
98
|
|
|
} |