Skip to content

Commit 09bee40

Browse files
authored
Support doctrine/dbal ^3.0 (#75)
* Upgrade doctrine/dbal to ^3.0 * Update composer.json * Added test cases for PHP 7.3 and 7.4 * Use Exception instead of the removed DBALException * Doctrine\DBAL\Exception is avaliable in v2.11 * Fixed the removed methods * Don't support php7.1 and 7.2 * Fixed `ping()` does not works. * Fixed PHPUnit. * Added sort-packages
1 parent 6a810a6 commit 09bee40

12 files changed

+130
-92
lines changed

.travis.yml

+15-15
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,51 @@ matrix:
1616
include:
1717
- env:
1818
- DB=mariadb:5.5
19-
php: "7.1"
19+
php: "7.3"
2020
- env:
2121
- DB=mariadb:10.0
22-
php: "7.1"
22+
php: "7.3"
2323
- env:
2424
- DB=mariadb:10.1
25-
php: "7.1"
25+
php: "7.3"
2626
- env:
2727
- DB=mysql:5.5
28-
php: "7.1"
28+
php: "7.3"
2929
- env:
3030
- DB=mysql:5.6
31-
php: "7.1"
31+
php: "7.3"
3232
- env:
3333
- DB=mysql:5.7
34-
php: "7.1"
34+
php: "7.3"
3535
- env:
3636
- DB=mysql:8.0
3737
- TEST_AUTH=yes
38-
php: "7.1"
38+
php: "7.3"
3939
- env:
4040
- DB=mariadb:5.5
41-
php: "7.2"
41+
php: "7.4"
4242
- env:
4343
- DB=mariadb:10.0
44-
php: "7.2"
44+
php: "7.4"
4545
- env:
4646
- DB=mariadb:10.1
47-
php: "7.2"
47+
php: "7.4"
4848
- env:
4949
- DB=mysql:5.5
50-
php: "7.2"
50+
php: "7.4"
5151
- env:
5252
- DB=mysql:5.6
53-
php: "7.2"
53+
php: "7.4"
5454
- env:
5555
- DB=mysql:5.7
56-
php: "7.2"
56+
php: "7.4"
5757
- env:
5858
- DB=mysql:8.0
5959
- TEST_AUTH=yes
60-
php: "7.2"
60+
php: "7.4"
6161

6262
before_script:
6363
- ./.travis/initializedb.sh
6464

6565
install:
66-
travis_retry composer install --no-interaction --prefer-source;
66+
travis_retry composer install --no-interaction --prefer-source;

composer.json

+11-8
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
],
1313
"type": "library",
1414
"require": {
15-
"php": ">=7.1",
16-
"ext-sockets": "*",
17-
"ext-json": "*",
15+
"php": ">=7.3",
1816
"ext-bcmath": "*",
19-
"doctrine/dbal": "^2.5",
17+
"ext-json": "*",
18+
"ext-sockets": "*",
2019
"doctrine/collections": "^1.3",
21-
"symfony/event-dispatcher": "^3.1|^4.0|^5.0",
20+
"doctrine/dbal": "^3.0",
21+
"psr/simple-cache": "^1.0",
2222
"symfony/dependency-injection": "^3.1|^4.0|^5.0",
23-
"psr/simple-cache": "^1.0"
23+
"symfony/event-dispatcher": "^3.1|^4.0|^5.0"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^7.0"
26+
"phpunit/phpunit": "^9.0"
2727
},
2828
"license": "MIT",
2929
"authors": [
@@ -42,5 +42,8 @@
4242
"MySQLReplication\\Tests\\": "tests/"
4343
}
4444
},
45-
"minimum-stability": "stable"
45+
"minimum-stability": "stable",
46+
"config": {
47+
"sort-packages": true
48+
}
4649
}

src/MySQLReplication/Event/RowEvent/RowEvent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ protected function getDecimal(ColumnDTO $columnDTO): string
662662
$res .= sprintf('%0' . $compFractional . 'd', $value);
663663
}
664664

665-
return bcmul($res, '1', $columnDTO->getPrecision());
665+
return bcmul($res, '1', $columnDTO->getDecimals());
666666
}
667667

668668
protected function getDatetime(): ?string

src/MySQLReplication/MySQLReplicationFactory.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace MySQLReplication;
55

66
use Doctrine\DBAL\Connection;
7-
use Doctrine\DBAL\DBALException;
7+
use Doctrine\DBAL\Exception;
88
use Doctrine\DBAL\DriverManager;
99
use MySQLReplication\BinaryDataReader\BinaryDataReaderException;
1010
use MySQLReplication\BinLog\BinLogException;
@@ -37,7 +37,7 @@ class MySQLReplicationFactory
3737
/**
3838
* @throws BinLogException
3939
* @throws ConfigException
40-
* @throws DBALException
40+
* @throws Exception
4141
* @throws SocketException
4242
* @throws GtidException
4343
*/
@@ -129,4 +129,4 @@ public function consume(): void
129129
{
130130
$this->event->consume();
131131
}
132-
}
132+
}

src/MySQLReplication/Repository/MySQLRepository.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
namespace MySQLReplication\Repository;
55

66
use Doctrine\DBAL\Connection;
7-
use Doctrine\DBAL\DBALException;
7+
use Doctrine\DBAL\Exception;
88
use MySQLReplication\BinLog\BinLogException;
99
use 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MySQLReplication\Repository;
5+
6+
use Doctrine\DBAL\Connection;
7+
8+
/**
9+
* An interface for connections which support a "native" ping method.
10+
*/
11+
interface PingableConnection
12+
{
13+
/**
14+
* Pings the database server to determine if the connection is still
15+
* available. Return true/false based on if that was successful or not.
16+
*
17+
* @return bool
18+
*/
19+
public function ping(Connection $connection): bool;
20+
}

tests/Integration/BaseTest.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public function connect(): void
7171

7272
$this->connection = $this->mySQLReplicationFactory->getDbConnection();
7373

74-
$this->connection->exec('SET SESSION time_zone = "UTC"');
75-
$this->connection->exec('DROP DATABASE IF EXISTS ' . $this->database);
76-
$this->connection->exec('CREATE DATABASE ' . $this->database);
77-
$this->connection->exec('USE ' . $this->database);
78-
$this->connection->exec('SET SESSION sql_mode = \'\';');
74+
$this->connection->executeStatement('SET SESSION time_zone = "UTC"');
75+
$this->connection->executeStatement('DROP DATABASE IF EXISTS ' . $this->database);
76+
$this->connection->executeStatement('CREATE DATABASE ' . $this->database);
77+
$this->connection->executeStatement('USE ' . $this->database);
78+
$this->connection->executeStatement('SET SESSION sql_mode = \'\';');
7979
}
8080

8181
protected function getEvent(): EventDTO
@@ -101,7 +101,7 @@ protected function tearDown(): void
101101

102102
protected function checkForVersion(float $version): bool
103103
{
104-
return (float)$this->connection->fetchColumn('SELECT VERSION()') < $version;
104+
return (float)$this->connection->fetchOne('SELECT VERSION()') < $version;
105105
}
106106

107107
protected function disconnect(): void
@@ -113,13 +113,13 @@ protected function disconnect(): void
113113

114114
protected function createAndInsertValue(string $createQuery, string $insertQuery): EventDTO
115115
{
116-
$this->connection->exec($createQuery);
117-
$this->connection->exec($insertQuery);
116+
$this->connection->executeStatement($createQuery);
117+
$this->connection->executeStatement($insertQuery);
118118

119119
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
120120
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
121121
self::assertInstanceOf(TableMapDTO::class, $this->getEvent());
122122

123123
return $this->getEvent();
124124
}
125-
}
125+
}

0 commit comments

Comments
 (0)