Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processing binary binding type #11734

Open
wants to merge 16 commits into
base: 3.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-dbal3.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ parameters:
# Symfony cache supports passing a key prefix to the clear method.
- '/^Method Psr\\Cache\\CacheItemPoolInterface\:\:clear\(\) invoked with 1 parameter, 0 required\.$/'

# We can be certain that those values are not matched.
-
message: '~^Match expression does not handle remaining values:~'
path: src/Persisters/Entity/BasicEntityPersister.php

# DBAL 4 compatibility
-
message: '~^Method Doctrine\\ORM\\Query\\AST\\Functions\\TrimFunction::getTrimMode\(\) never returns .* so it can be removed from the return type\.$~'
Expand Down
6 changes: 1 addition & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ parameters:
# Symfony cache supports passing a key prefix to the clear method.
- '/^Method Psr\\Cache\\CacheItemPoolInterface\:\:clear\(\) invoked with 1 parameter, 0 required\.$/'

# We can be certain that those values are not matched.
-
message: '~^Match expression does not handle remaining values:~'
path: src/Persisters/Entity/BasicEntityPersister.php

# DBAL 4 compatibility
-
message: '~^Method Doctrine\\ORM\\Query\\AST\\Functions\\TrimFunction::getTrimMode\(\) never returns .* so it can be removed from the return type\.$~'
path: src/Query/AST/Functions/TrimFunction.php

-
message: '~^Method Doctrine\\ORM\\Persisters\\Entity\\BasicEntityPersister\:\:getArrayBindingType\(\) never returns .* so it can be removed from the return type\.$~'
path: src/Persisters/Entity/BasicEntityPersister.php
Expand Down
7 changes: 6 additions & 1 deletion src/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,10 @@
return $types;
}

/** @phpstan-return ArrayParameterType::* */
/**
* @psalm-return ArrayParameterType::*

Check failure on line 1933 in src/Persisters/Entity/BasicEntityPersister.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Incorrect annotations group.
* @throw QueryException˚
*/
private function getArrayBindingType(ParameterType|int|string $type): ArrayParameterType|int
{
if (! $type instanceof ParameterType) {
Expand All @@ -1940,6 +1943,8 @@
ParameterType::STRING => ArrayParameterType::STRING,
ParameterType::INTEGER => ArrayParameterType::INTEGER,
ParameterType::ASCII => ArrayParameterType::ASCII,
ParameterType::BINARY => ArrayParameterType::BINARY,
default => throw new QueryException('Unsupported type for array parameter'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the default branch here?

Copy link
Author

@rabikor rabikor Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is necessary because not all ParameterType cases, such as BOOLEAN, LARGE_OBJECT, or NULL, are supported by ArrayParameterType. The default branch ensures these unsupported types are handled gracefully by throwing a clear exception, preventing unexpected behavior or runtime errors.

};
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Tests/Models/CMS/CmsUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class CmsUser
#[GeneratedValue]
public $id;

/** @var string */
#[Column(type: 'binary', length: 50, nullable: true)]
public $reference;

/** @var string */
#[Column(type: 'string', length: 50, nullable: true)]
public $status;
Expand Down
10 changes: 5 additions & 5 deletions tests/Tests/ORM/Functional/NativeQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public function testGenerateSelectClauseNoRenameSingleEntity(): void

$selectClause = $rsm->generateSelectClause();

$this->assertSQLEquals('u.id AS id, u.status AS status, u.username AS username, u.name AS name, u.email_id AS email_id', $selectClause);
$this->assertSQLEquals('u.id AS id, u.reference as reference, u.status AS status, u.username AS username, u.name AS name, u.email_id AS email_id', $selectClause);
}

#[Group('DDC-2055')]
Expand All @@ -489,7 +489,7 @@ public function testGenerateSelectClauseCustomRenames(): void

$selectClause = $rsm->generateSelectClause();

$this->assertSQLEquals('u.id AS id1, u.status AS status, u.username AS username2, u.name AS name, u.email_id AS email_id', $selectClause);
$this->assertSQLEquals('u.id AS id1, u.reference as reference, u.status AS status, u.username AS username2, u.name AS name, u.email_id AS email_id', $selectClause);
}

#[Group('DDC-2055')]
Expand All @@ -500,7 +500,7 @@ public function testGenerateSelectClauseRenameTableAlias(): void

$selectClause = $rsm->generateSelectClause(['u' => 'u1']);

$this->assertSQLEquals('u1.id AS id, u1.status AS status, u1.username AS username, u1.name AS name, u1.email_id AS email_id', $selectClause);
$this->assertSQLEquals('u1.id AS id, u1.reference as reference, u1.status AS status, u1.username AS username, u1.name AS name, u1.email_id AS email_id', $selectClause);
}

#[Group('DDC-2055')]
Expand All @@ -511,7 +511,7 @@ public function testGenerateSelectClauseIncrement(): void

$selectClause = $rsm->generateSelectClause();

$this->assertSQLEquals('u.id AS id0, u.status AS status1, u.username AS username2, u.name AS name3, u.email_id AS email_id4', $selectClause);
$this->assertSQLEquals('u.id AS id0, u.reference as reference1, u.status as status2, u.username as username3, u.name as name4, u.email_id as email_id5', $selectClause);
}

#[Group('DDC-2055')]
Expand All @@ -520,7 +520,7 @@ public function testGenerateSelectClauseToString(): void
$rsm = new ResultSetMappingBuilder($this->_em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT);
$rsm->addRootEntityFromClassMetadata(CmsUser::class, 'u');

$this->assertSQLEquals('u.id AS id0, u.status AS status1, u.username AS username2, u.name AS name3, u.email_id AS email_id4', (string) $rsm);
$this->assertSQLEquals('u.id AS id0, u.reference as reference1, u.status as status2, u.username as username3, u.name as name4, u.email_id as email_id5', (string) $rsm);
}

#[Group('DDC-3899')]
Expand Down
57 changes: 44 additions & 13 deletions tests/Tests/ORM/Functional/QueryParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ protected function setUp(): void

parent::setUp();

$user = new CmsUser();
$user->name = 'John Doe';
$user->username = 'john';
$user2 = new CmsUser();
$user2->name = 'Jane Doe';
$user2->username = 'jane';
$user3 = new CmsUser();
$user3->name = 'Just Bill';
$user3->username = 'bill';
$user = new CmsUser();
$user->reference = '0b0001';
$user->name = 'John Doe';
$user->username = 'john';
$user2 = new CmsUser();
$user2->reference = '0b0010';
$user2->name = 'Jane Doe';
$user2->username = 'jane';
$user3 = new CmsUser();
$user3->reference = '0b0011';
$user3->name = 'Just Bill';
$user3->username = 'bill';

$this->_em->persist($user);
$this->_em->persist($user2);
Expand Down Expand Up @@ -99,9 +102,9 @@ public function testArrayParameterTypeInBuilder(): void
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.username IN (:usernames)')
->where('u.username IN (:values)')
->orderBy('u.username')
->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING)
->setParameter('values', ['john', 'jane'], ArrayParameterType::STRING)
->getQuery()
->getArrayResult();

Expand All @@ -113,10 +116,38 @@ public function testArrayParameterTypeInQuery(): void
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.username IN (:usernames)')
->where('u.username IN (:values)')
->orderBy('u.username')
->getQuery()
->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING)
->setParameter('values', ['john', 'jane'], ArrayParameterType::STRING)
->getArrayResult();

self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result);
}

public function testArrayParameterTypeBinaryInBuilder(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.reference IN (:values)')
->orderBy('u.username')
->setParameter('values', ['0b0001', '0b0010'], ArrayParameterType::BINARY)
->getQuery()
->getArrayResult();

self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result);
}

public function testArrayParameterTypeBinaryInQuery(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.reference IN (:values)')
->orderBy('u.username')
->getQuery()
->setParameter('values', ['0b0001', '0b0010'], ArrayParameterType::BINARY)
->getArrayResult();

self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result);
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function testGetCreateSchemaSql(): void
self::assertEquals('CREATE TABLE cms_groups (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB', $sql[0]);
self::assertThat($sql[1], self::logicalOr(
// DBAL 3
self::equalTo('CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, email_id INT DEFAULT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_3AF03EC5F85E0677 (username), UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 (email_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'),
self::equalTo('CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, email_id INT DEFAULT NULL, reference VARBINARY(50) DEFAULT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_3AF03EC5F85E0677 (username), UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 (email_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'),
// DBAL 4 (see https://github.com/doctrine/dbal/pull/4777)
self::equalTo('CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, email_id INT DEFAULT NULL, UNIQUE INDEX UNIQ_3AF03EC5F85E0677 (username), UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 (email_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'),
self::equalTo('CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, reference VARBINARY(50) DEFAULT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, email_id INT DEFAULT NULL, UNIQUE INDEX UNIQ_3AF03EC5F85E0677 (username), UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 (email_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'),
));
self::assertEquals('CREATE TABLE cms_users_groups (user_id INT NOT NULL, group_id INT NOT NULL, INDEX IDX_7EA9409AA76ED395 (user_id), INDEX IDX_7EA9409AFE54D947 (group_id), PRIMARY KEY(user_id, group_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB', $sql[2]);
self::assertEquals('CREATE TABLE cms_users_tags (user_id INT NOT NULL, tag_id INT NOT NULL, INDEX IDX_93F5A1ADA76ED395 (user_id), INDEX IDX_93F5A1ADBAD26311 (tag_id), PRIMARY KEY(user_id, tag_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB', $sql[3]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/ORM/Query/CustomTreeWalkersJoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testAddsJoin(): void
{
$this->assertSqlGeneration(
'select u from Doctrine\Tests\Models\CMS\CmsUser u',
'SELECT c0_.id AS id_0, c0_.status AS status_1, c0_.username AS username_2, c0_.name AS name_3, c1_.id AS id_4, c1_.country AS country_5, c1_.zip AS zip_6, c1_.city AS city_7, c0_.email_id AS email_id_8, c1_.user_id AS user_id_9 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id',
'SELECT c0_.id AS id_0, c0_.reference AS reference_1, c0_.status AS status_2, c0_.username AS username_3, c0_.name AS name_4, c1_.id AS id_5, c1_.country AS country_6, c1_.zip AS zip_7, c1_.city AS city_8, c0_.email_id AS email_id_9, c1_.user_id AS user_id_10 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id',
);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Tests/ORM/Query/CustomTreeWalkersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testSupportsQueriesWithoutWhere(): void
{
$this->assertSqlGeneration(
'select u from Doctrine\Tests\Models\CMS\CmsUser u',
'SELECT c0_.id AS id_0, c0_.status AS status_1, c0_.username AS username_2, c0_.name AS name_3, c0_.email_id AS email_id_4 FROM cms_users c0_ WHERE c0_.id = 1',
'SELECT c0_.id AS id_0, c0_.reference AS reference_1, c0_.status AS status_2, c0_.username AS username_3, c0_.name AS name_4, c0_.email_id AS email_id_5 FROM cms_users c0_ WHERE c0_.id = 1',
[CustomTreeWalker::class],
);
}
Expand All @@ -83,7 +83,7 @@ public function testSupportsQueriesWithMultipleConditionalExpressions(): void
{
$this->assertSqlGeneration(
'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name or u.name = :otherName',
'SELECT c0_.id AS id_0, c0_.status AS status_1, c0_.username AS username_2, c0_.name AS name_3, c0_.email_id AS email_id_4 FROM cms_users c0_ WHERE (c0_.name = ? OR c0_.name = ?) AND c0_.id = 1',
'SELECT c0_.id AS id_0, c0_.reference AS reference_1, c0_.status AS status_2, c0_.username AS username_3, c0_.name AS name_4, c0_.email_id AS email_id_5 FROM cms_users c0_ WHERE (c0_.name = ? OR c0_.name = ?) AND c0_.id = 1',
[CustomTreeWalker::class],
);
}
Expand All @@ -92,7 +92,7 @@ public function testSupportsQueriesWithSimpleConditionalExpression(): void
{
$this->assertSqlGeneration(
'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name',
'SELECT c0_.id AS id_0, c0_.status AS status_1, c0_.username AS username_2, c0_.name AS name_3, c0_.email_id AS email_id_4 FROM cms_users c0_ WHERE c0_.name = ? AND c0_.id = 1',
'SELECT c0_.id AS id_0, c0_.reference AS reference_1, c0_.status AS status_2, c0_.username AS username_3, c0_.name AS name_4, c0_.email_id AS email_id_5 FROM cms_users c0_ WHERE c0_.name = ? AND c0_.id = 1',
[CustomTreeWalker::class],
);
}
Expand All @@ -113,7 +113,7 @@ public function testSupportsSeveralHintsQueries(): void
{
$this->assertSqlGeneration(
'select u from Doctrine\Tests\Models\CMS\CmsUser u',
'SELECT c0_.id AS id_0, c0_.status AS status_1, c0_.username AS username_2, c0_.name AS name_3, c1_.id AS id_4, c1_.country AS country_5, c1_.zip AS zip_6, c1_.city AS city_7, c0_.email_id AS email_id_8, c1_.user_id AS user_id_9 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id WHERE c0_.id = 1',
'SELECT c0_.id AS id_0, c0_.reference AS reference_1, c0_.status AS status_2, c0_.username AS username_3, c0_.name AS name_4, c1_.id AS id_5, c1_.country AS country_6, c1_.zip AS zip_7, c1_.city AS city_8, c0_.email_id AS email_id_9, c1_.user_id AS user_id_10 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id WHERE c0_.id = 1',
[CustomTreeWalkerJoin::class, CustomTreeWalker::class],
);
}
Expand Down
Loading
Loading