From 4611138abae6dad2a06a3c7e28597e3c91827176 Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Sun, 15 Oct 2023 13:21:41 -0400 Subject: [PATCH] Add DBAL 4.x compat --- .github/workflows/run-tests.yml | 5 ++++ composer.json | 2 +- .../Doctrine/DBAL/SingleTableQueryAdapter.php | 25 ++++++++----------- .../Doctrine/DBAL/Tests/DBALTestCase.php | 15 +++++------ .../Doctrine/DBAL/Tests/QueryAdapterTest.php | 2 +- .../Tests/SingleTableQueryAdapterTest.php | 12 +-------- lib/Adapter/Doctrine/DBAL/composer.json | 2 +- 7 files changed, 26 insertions(+), 37 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 9d4da09f..cec737ab 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -103,6 +103,11 @@ jobs: dbal: '' composer-flags: '--prefer-stable --prefer-dist --prefer-lowest' next-php: false + - php: '8.2' + # Revert to @rc or stable after RC2/stable tag + dbal: '^4.0@dev' + composer-flags: '--prefer-stable --prefer-dist' + next-php: false - php: '8.2' composer-flags: '--prefer-stable --prefer-dist' dbal: '' diff --git a/composer.json b/composer.json index 94847893..a494fc5d 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ }, "require-dev": { "doctrine/collections": "^1.8 || ^2.0", - "doctrine/dbal": "^3.5", + "doctrine/dbal": "^3.5 || ^4.0", "doctrine/mongodb-odm": "^2.4", "doctrine/orm": "^2.14", "doctrine/phpcr-odm": "^1.7", diff --git a/lib/Adapter/Doctrine/DBAL/SingleTableQueryAdapter.php b/lib/Adapter/Doctrine/DBAL/SingleTableQueryAdapter.php index 393d027b..8a232d65 100644 --- a/lib/Adapter/Doctrine/DBAL/SingleTableQueryAdapter.php +++ b/lib/Adapter/Doctrine/DBAL/SingleTableQueryAdapter.php @@ -17,30 +17,27 @@ class SingleTableQueryAdapter extends QueryAdapter /** * @param string $countField Primary key for the table in query, used in the count expression. Must include table alias. * - * @throws InvalidArgumentException if the query has JOIN statements or the count field does not have a table alias + * @throws InvalidArgumentException if the count field does not have a table alias */ public function __construct(QueryBuilder $queryBuilder, string $countField) { - if ($this->hasQueryBuilderJoins($queryBuilder)) { - throw new InvalidArgumentException('The query builder cannot have joins.'); - } - parent::__construct($queryBuilder, $this->createCountQueryModifier($countField)); } - private function hasQueryBuilderJoins(QueryBuilder $queryBuilder): bool - { - return !empty($queryBuilder->getQueryPart('join')); - } - private function createCountQueryModifier(string $countField): \Closure { $select = $this->createSelectForCountField($countField); - return function (QueryBuilder $queryBuilder) use ($select): void { - $queryBuilder->select($select) - ->resetQueryPart('orderBy') - ->setMaxResults(1); + return static function (QueryBuilder $queryBuilder) use ($select): void { + $queryBuilder->select($select); + + if (method_exists($queryBuilder, 'resetOrderBy')) { + $queryBuilder->resetOrderBy(); + } else { + $queryBuilder->resetQueryPart('orderBy'); + } + + $queryBuilder->setMaxResults(1); }; } diff --git a/lib/Adapter/Doctrine/DBAL/Tests/DBALTestCase.php b/lib/Adapter/Doctrine/DBAL/Tests/DBALTestCase.php index afe66b2a..6f36953f 100644 --- a/lib/Adapter/Doctrine/DBAL/Tests/DBALTestCase.php +++ b/lib/Adapter/Doctrine/DBAL/Tests/DBALTestCase.php @@ -22,17 +22,16 @@ protected function setUp(): void private function createConnection(): Connection { - return DriverManager::getConnection( - [ - 'driver' => 'pdo_sqlite', - 'memory' => true, - ] - ); + return DriverManager::getConnection([ + 'driver' => 'pdo_sqlite', + 'memory' => true, + ]); } private function createSchema(): void { $schema = new Schema(); + $posts = $schema->createTable('posts'); $posts->addColumn('id', Types::INTEGER, ['unsigned' => true, 'autoincrement' => true]); $posts->addColumn('username', Types::STRING, ['length' => 32]); @@ -46,9 +45,7 @@ private function createSchema(): void $comments->addColumn('content', Types::TEXT); $comments->setPrimaryKey(['id']); - $queries = $schema->toSql($this->connection->getDatabasePlatform()); // get queries to create this schema. - - foreach ($queries as $sql) { + foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) { $this->connection->executeQuery($sql); } } diff --git a/lib/Adapter/Doctrine/DBAL/Tests/QueryAdapterTest.php b/lib/Adapter/Doctrine/DBAL/Tests/QueryAdapterTest.php index 69609158..e6c161da 100644 --- a/lib/Adapter/Doctrine/DBAL/Tests/QueryAdapterTest.php +++ b/lib/Adapter/Doctrine/DBAL/Tests/QueryAdapterTest.php @@ -13,7 +13,7 @@ protected function setUp(): void { parent::setUp(); - $this->qb = new QueryBuilder($this->connection); + $this->qb = $this->connection->createQueryBuilder(); $this->qb->select('p.*')->from('posts', 'p'); } diff --git a/lib/Adapter/Doctrine/DBAL/Tests/SingleTableQueryAdapterTest.php b/lib/Adapter/Doctrine/DBAL/Tests/SingleTableQueryAdapterTest.php index bf471419..805a9883 100644 --- a/lib/Adapter/Doctrine/DBAL/Tests/SingleTableQueryAdapterTest.php +++ b/lib/Adapter/Doctrine/DBAL/Tests/SingleTableQueryAdapterTest.php @@ -19,7 +19,7 @@ protected function setUp(): void { parent::setUp(); - $this->qb = new QueryBuilder($this->connection); + $this->qb = $this->connection->createQueryBuilder(); $this->qb->select('p.*')->from('posts', 'p'); $this->adapter = new SingleTableQueryAdapter($this->qb, 'p.id'); @@ -33,16 +33,6 @@ public function testACountFieldWithoutAnAliasIsRejected(): void new SingleTableQueryAdapter($this->qb, 'id'); } - public function testAQueryWithJoinStatementsIsRejected(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('The query builder cannot have joins.'); - - $this->qb->innerJoin('p', 'comments', 'c', 'c.post_id = p.id'); - - new SingleTableQueryAdapter($this->qb, 'p.id'); - } - public function testAdapterReturnsNumberOfResults(): void { self::assertSame(50, $this->adapter->getNbResults()); diff --git a/lib/Adapter/Doctrine/DBAL/composer.json b/lib/Adapter/Doctrine/DBAL/composer.json index 7b625de2..5a39e56f 100644 --- a/lib/Adapter/Doctrine/DBAL/composer.json +++ b/lib/Adapter/Doctrine/DBAL/composer.json @@ -6,7 +6,7 @@ "license": "MIT", "require": { "php": "^8.1", - "doctrine/dbal": "^3.5", + "doctrine/dbal": "^3.5 || ^4.0", "pagerfanta/core": "^3.7 || ^4.0" }, "require-dev": {