|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MySQLReplication\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class MySQLRepository |
|
9
|
|
|
* @package MySQLReplication\Repository |
|
10
|
|
|
*/ |
|
11
|
|
|
class MySQLRepository implements RepositoryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Connection |
|
15
|
|
|
*/ |
|
16
|
|
|
private $connection; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Connection $connection) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->connection = $connection; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function __destruct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->connection->close(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $schema |
|
30
|
|
|
* @param string $table |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFields($schema, $table) |
|
34
|
|
|
{ |
|
35
|
|
|
$sql = ' |
|
36
|
|
|
SELECT |
|
37
|
|
|
c.`COLUMN_NAME`, |
|
38
|
|
|
c.`COLLATION_NAME`, |
|
39
|
|
|
c.`CHARACTER_SET_NAME`, |
|
40
|
|
|
c.`COLUMN_COMMENT`, |
|
41
|
|
|
c.`COLUMN_TYPE`, |
|
42
|
|
|
c.`COLUMN_KEY`, |
|
43
|
|
|
`kcu`.`REFERENCED_TABLE_NAME`, |
|
44
|
|
|
`kcu`.`REFERENCED_COLUMN_NAME` |
|
45
|
|
|
FROM |
|
46
|
|
|
`information_schema`.`COLUMNS` c |
|
47
|
|
|
LEFT JOIN |
|
48
|
|
|
`information_schema`.KEY_COLUMN_USAGE kcu |
|
49
|
|
|
ON |
|
50
|
|
|
c.`TABLE_SCHEMA` = kcu.`TABLE_SCHEMA` |
|
51
|
|
|
AND |
|
52
|
|
|
c.`TABLE_NAME` = kcu.`TABLE_NAME` |
|
53
|
|
|
AND |
|
54
|
|
|
c.`COLUMN_NAME` = kcu.`COLUMN_NAME` |
|
55
|
|
|
WHERE |
|
56
|
|
|
c.`TABLE_SCHEMA` = ? |
|
57
|
|
|
AND |
|
58
|
|
|
c.`TABLE_NAME` = ? |
|
59
|
|
|
'; |
|
60
|
|
|
|
|
61
|
|
|
return $this->getConnection()->fetchAll($sql, [$schema, $table]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Connection |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getConnection() |
|
68
|
|
|
{ |
|
69
|
|
|
if (false === $this->connection->ping()) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->connection->close(); |
|
72
|
|
|
$this->connection->connect(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->connection; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function isCheckSum() |
|
82
|
|
|
{ |
|
83
|
|
|
$res = $this->getConnection()->fetchAssoc('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"'); |
|
84
|
|
|
|
|
85
|
|
|
return isset($res['Value']); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getVersion() |
|
92
|
|
|
{ |
|
93
|
|
|
$r = ''; |
|
94
|
|
|
$versions = $this->getConnection()->fetchAll('SHOW VARIABLES LIKE "version%"'); |
|
95
|
|
|
if (is_array($versions) && 0 !== count($versions)) |
|
96
|
|
|
{ |
|
97
|
|
|
foreach ($versions as $version) |
|
98
|
|
|
{ |
|
99
|
|
|
$r .= $version['Value']; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
return $r; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* File |
|
107
|
|
|
* Position |
|
108
|
|
|
* Binlog_Do_DB |
|
109
|
|
|
* Binlog_Ignore_DB |
|
110
|
|
|
* Executed_Gtid_Set |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getMasterStatus() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getConnection()->fetchAssoc('SHOW MASTER STATUS'); |
|
117
|
|
|
} |
|
118
|
|
|
} |