Skip to content

Commit

Permalink
Fix validatePlatform for MariaDB, check for instance of MariaDBPlatform
Browse files Browse the repository at this point in the history
Versions of DBAL before 3.3 used or extended the MySQLPlatform when
connecting to MariaDB. Because of this the platform check was
requiring an instance of MySQLPlatform.

This commit fixes MariaDB compatibility with DBAL >=3.3, while remaining
 compatible with versions prior to that (for now).

Fixes #113
  • Loading branch information
annervisser authored and Hikariii committed Apr 10, 2024
1 parent f9773d8 commit 0d9c01a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/DBALCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ public static function sqlLitePlatform(): string

return 'Doctrine\DBAL\Platforms\SqlitePlatform';
}

public static function mariaDBPlatform(): string
{
if (!class_exists('\Doctrine\DBAL\Platforms\MariaDBPlatform')) {
// In DBAL versions prior to 3.3, MariaDB used or extended the MySQL platform
return '\Doctrine\DBAL\Platforms\MySQLPlatform';
}

// DBAL 3.3 and onwards
return '\Doctrine\DBAL\Platforms\MariaDBPlatform';
}
}
3 changes: 1 addition & 2 deletions src/Query/AST/Functions/Mariadb/MariadbJsonFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mariadb;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\ORM\Query\SqlWalker;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\AbstractJsonFunctionNode;
Expand All @@ -18,7 +17,7 @@ abstract class MariadbJsonFunctionNode extends AbstractJsonFunctionNode
*/
protected function validatePlatform(SqlWalker $sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof (DBALCompatibility::mariaDBPlatform())) {
throw DBALCompatibility::notSupportedPlatformException(static::FUNCTION_NAME);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Query/MariadbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Scienta\DoctrineJsonFunctions\Tests\Query;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\ORM\Configuration;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mariadb as DqlFunctions;
use Scienta\DoctrineJsonFunctions\Tests\Mocks\ConnectionMock;

Expand All @@ -17,7 +17,7 @@ public function setUp(): void

/** @var ConnectionMock $conn */
$conn = $this->entityManager->getConnection();
$conn->setDatabasePlatform(new MySQLPlatform());
$conn->setDatabasePlatform(new (DBALCompatibility::mariaDBPlatform())());

self::loadDqlFunctions($this->configuration);
}
Expand Down

0 comments on commit 0d9c01a

Please sign in to comment.