4
4
namespace MySQLReplication \Repository ;
5
5
6
6
use Doctrine \DBAL \Connection ;
7
- use Doctrine \DBAL \DBALException ;
7
+ use Doctrine \DBAL \Exception ;
8
8
use MySQLReplication \BinLog \BinLogException ;
9
9
use MySQLReplication \Exception \MySQLReplicationException ;
10
10
11
- class MySQLRepository implements RepositoryInterface
11
+ class MySQLRepository implements RepositoryInterface, PingableConnection
12
12
{
13
13
private $ connection ;
14
14
@@ -42,12 +42,12 @@ public function getFields(string $database, string $table): FieldDTOCollection
42
42
ORDINAL_POSITION
43
43
' ;
44
44
45
- return FieldDTOCollection::makeFromArray ($ this ->getConnection ()->fetchAll ($ sql , [$ database , $ table ]));
45
+ return FieldDTOCollection::makeFromArray ($ this ->getConnection ()->fetchAllAssociative ($ sql , [$ database , $ table ]));
46
46
}
47
47
48
48
private function getConnection (): Connection
49
49
{
50
- if (false === $ this ->connection -> ping ()) {
50
+ if (false === $ this ->ping ($ this -> connection )) {
51
51
$ this ->connection ->close ();
52
52
$ this ->connection ->connect ();
53
53
}
@@ -56,19 +56,19 @@ private function getConnection(): Connection
56
56
}
57
57
58
58
/**
59
- * @throws DBALException
59
+ * @throws Exception
60
60
*/
61
61
public function isCheckSum (): bool
62
62
{
63
- $ res = $ this ->getConnection ()->fetchAssoc ('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM" ' );
63
+ $ res = $ this ->getConnection ()->fetchAssociative ('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM" ' );
64
64
65
65
return isset ($ res ['Value ' ]) && $ res ['Value ' ] !== 'NONE ' ;
66
66
}
67
67
68
68
public function getVersion (): string
69
69
{
70
70
$ r = '' ;
71
- $ versions = $ this ->getConnection ()->fetchAll ('SHOW VARIABLES LIKE "version%" ' );
71
+ $ versions = $ this ->getConnection ()->fetchAllAssociative ('SHOW VARIABLES LIKE "version%" ' );
72
72
if (is_array ($ versions ) && 0 !== count ($ versions )) {
73
73
foreach ($ versions as $ version ) {
74
74
$ r .= $ version ['Value ' ];
@@ -80,12 +80,12 @@ public function getVersion(): string
80
80
81
81
/**
82
82
* @inheritDoc
83
- * @throws DBALException
83
+ * @throws Exception
84
84
* @throws BinLogException
85
85
*/
86
86
public function getMasterStatus (): MasterStatusDTO
87
87
{
88
- $ data = $ this ->getConnection ()->fetchAssoc ('SHOW MASTER STATUS ' );
88
+ $ data = $ this ->getConnection ()->fetchAssociative ('SHOW MASTER STATUS ' );
89
89
if (empty ($ data )) {
90
90
throw new BinLogException (
91
91
MySQLReplicationException::BINLOG_NOT_ENABLED ,
@@ -95,4 +95,14 @@ public function getMasterStatus(): MasterStatusDTO
95
95
96
96
return MasterStatusDTO::makeFromArray ($ data );
97
97
}
98
- }
98
+
99
+ public function ping (Connection $ connection ): bool
100
+ {
101
+ try {
102
+ $ connection ->executeQuery ($ connection ->getDatabasePlatform ()->getDummySelectSQL ());
103
+ return true ;
104
+ } catch (Exception $ e ) {
105
+ return false ;
106
+ }
107
+ }
108
+ }
0 commit comments