44namespace MySQLReplication \Repository ;
55
66use Doctrine \DBAL \Connection ;
7- use Doctrine \DBAL \DBALException ;
7+ use Doctrine \DBAL \Exception ;
88use MySQLReplication \BinLog \BinLogException ;
99use MySQLReplication \Exception \MySQLReplicationException ;
1010
11- class MySQLRepository implements RepositoryInterface
11+ class MySQLRepository implements RepositoryInterface, PingableConnection
1212{
1313 private $ connection ;
1414
@@ -42,12 +42,12 @@ public function getFields(string $database, string $table): FieldDTOCollection
4242 ORDINAL_POSITION
4343 ' ;
4444
45- return FieldDTOCollection::makeFromArray ($ this ->getConnection ()->fetchAll ($ sql , [$ database , $ table ]));
45+ return FieldDTOCollection::makeFromArray ($ this ->getConnection ()->fetchAllAssociative ($ sql , [$ database , $ table ]));
4646 }
4747
4848 private function getConnection (): Connection
4949 {
50- if (false === $ this ->connection -> ping ()) {
50+ if (false === $ this ->ping ($ this -> connection )) {
5151 $ this ->connection ->close ();
5252 $ this ->connection ->connect ();
5353 }
@@ -56,19 +56,19 @@ private function getConnection(): Connection
5656 }
5757
5858 /**
59- * @throws DBALException
59+ * @throws Exception
6060 */
6161 public function isCheckSum (): bool
6262 {
63- $ res = $ this ->getConnection ()->fetchAssoc ('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM" ' );
63+ $ res = $ this ->getConnection ()->fetchAssociative ('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM" ' );
6464
6565 return isset ($ res ['Value ' ]) && $ res ['Value ' ] !== 'NONE ' ;
6666 }
6767
6868 public function getVersion (): string
6969 {
7070 $ r = '' ;
71- $ versions = $ this ->getConnection ()->fetchAll ('SHOW VARIABLES LIKE "version%" ' );
71+ $ versions = $ this ->getConnection ()->fetchAllAssociative ('SHOW VARIABLES LIKE "version%" ' );
7272 if (is_array ($ versions ) && 0 !== count ($ versions )) {
7373 foreach ($ versions as $ version ) {
7474 $ r .= $ version ['Value ' ];
@@ -80,12 +80,12 @@ public function getVersion(): string
8080
8181 /**
8282 * @inheritDoc
83- * @throws DBALException
83+ * @throws Exception
8484 * @throws BinLogException
8585 */
8686 public function getMasterStatus (): MasterStatusDTO
8787 {
88- $ data = $ this ->getConnection ()->fetchAssoc ('SHOW MASTER STATUS ' );
88+ $ data = $ this ->getConnection ()->fetchAssociative ('SHOW MASTER STATUS ' );
8989 if (empty ($ data )) {
9090 throw new BinLogException (
9191 MySQLReplicationException::BINLOG_NOT_ENABLED ,
@@ -95,4 +95,14 @@ public function getMasterStatus(): MasterStatusDTO
9595
9696 return MasterStatusDTO::makeFromArray ($ data );
9797 }
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