From f47a8f23373823b4a195f358a2209a7d1da2d2f8 Mon Sep 17 00:00:00 2001 From: qwerin Date: Wed, 1 Jun 2022 23:11:05 +0200 Subject: [PATCH] fix nextras/dbal 5.x compatibility (#121) --- composer.json | 2 +- src/Bridges/NextrasDbal/NextrasAdapter.php | 29 ++++++++++++++----- .../matrix/dbal/nextras-5.0-php-7.4-to-8.1.sh | 6 ++++ 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 tests/matrix/dbal/nextras-5.0-php-7.4-to-8.1.sh diff --git a/composer.json b/composer.json index 5b4b004..aa356c8 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "nette/di": "~2.3.12 | ~2.4", "nette/tester": "~1.7 | ~2.0", "nette/utils": "~2.3", - "nextras/dbal": "~1.0 | ~2.0 | ~3.0 | ~4.0", + "nextras/dbal": "~1.0 | ~2.0 | ~3.0 | ~4.0 | ~5.0@dev", "symfony/config": "~2.6 | ~3.0 | ~4.0", "symfony/console": "~2.6 | ~3.0 | ~4.0", "symfony/dependency-injection": "~2.6 | ~3.0 | ~4.0", diff --git a/src/Bridges/NextrasDbal/NextrasAdapter.php b/src/Bridges/NextrasDbal/NextrasAdapter.php index e6a127e..e6d2526 100644 --- a/src/Bridges/NextrasDbal/NextrasAdapter.php +++ b/src/Bridges/NextrasDbal/NextrasAdapter.php @@ -21,15 +21,24 @@ class NextrasAdapter implements IDbal /** @var Connection */ private $conn; - /** @var bool */ - private $oldDriver; + /** @var int */ + private $version; public function __construct(Connection $connection) { $this->conn = $connection; $this->conn->connect(); - $this->oldDriver = method_exists($connection->getDriver(), 'convertToSql'); + + if (method_exists($connection->getDriver(), 'convertToSql')) { + $this->version = 1; + + } elseif (method_exists($connection->getDriver(), 'convertBoolToSql')) { + $this->version = 2; + + } else { + $this->version = 5; + } } @@ -51,7 +60,7 @@ public function exec($sql) public function escapeString($value) { - if (!$this->oldDriver) { + if ($this->version >= 2) { return $this->conn->getDriver()->convertStringToSql($value); } else { return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_STRING); @@ -67,7 +76,9 @@ public function escapeInt($value) public function escapeBool($value) { - if (!$this->oldDriver) { + if ($this->version >= 5) { + return $this->conn->getPlatform()->formatBool($value); + } elseif ($this->version >= 2) { return $this->conn->getDriver()->convertBoolToSql($value); } else { return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_BOOL); @@ -77,7 +88,9 @@ public function escapeBool($value) public function escapeDateTime(DateTime $value) { - if (!$this->oldDriver) { + if ($this->version >= 5) { + return $this->conn->getPlatform()->formatDateTime($value); + } elseif ($this->version >= 2) { return $this->conn->getDriver()->convertDateTimeToSql($value); } else { return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_DATETIME); @@ -87,7 +100,9 @@ public function escapeDateTime(DateTime $value) public function escapeIdentifier($value) { - if (!$this->oldDriver) { + if ($this->version >= 5) { + return $this->conn->getPlatform()->formatIdentifier($value); + } elseif ($this->version >= 2) { return $this->conn->getDriver()->convertIdentifierToSql($value); } else { return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_IDENTIFIER); diff --git a/tests/matrix/dbal/nextras-5.0-php-7.4-to-8.1.sh b/tests/matrix/dbal/nextras-5.0-php-7.4-to-8.1.sh new file mode 100644 index 0000000..07d3843 --- /dev/null +++ b/tests/matrix/dbal/nextras-5.0-php-7.4-to-8.1.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +PHP_VERSION_MIN="70400" +PHP_VERSION_MAX="80199" +COMPOSER_REQUIRE="$COMPOSER_REQUIRE nextras/dbal:~5.0@dev" +COMPOSER_REQUIRE="$COMPOSER_REQUIRE nette/tester:~2.3" +DBAL="nextras"