Commit ed825b27 authored by Morton Jonuschat's avatar Morton Jonuschat Committed by Wouter Wolters
Browse files

[TASK] Doctrine: Add support for column aliases in select()

Add support for selecting a column under a different identifier (alias)
when building a SELECT query.

Releases: master
Resolves: #75823
Change-Id: I3464c9d848c4892ea57b92cc1257654ff9e14110
Reviewed-on: https://review.typo3.org/47820


Reviewed-by: default avatarMichael Oehlhof <typo3@oehlhof.de>
Tested-by: default avatarMichael Oehlhof <typo3@oehlhof.de>
Reviewed-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
parent 445ff455
......@@ -923,18 +923,32 @@ class QueryBuilder
* @param array $input
*
* @return array
* @throws \InvalidArgumentException
*/
public function quoteIdentifiersForSelect(array $input): array
{
// The SQL * operator must not be quoted. As it can only occur either by itself
// or preceded by a tablename (tablename.*) check if the last character of a select
// expression is the * and quote only prepended table name. In all other cases the
// full expression is being quoted.
foreach ($input as &$select) {
if (substr($select, -2) === '.*') {
$select = $this->quoteIdentifier(substr($select, 0, -2)) . '.*';
} elseif ($select !== '*') {
$select = $this->quoteIdentifier($select);
list($fieldName, $alias, $suffix) = GeneralUtility::trimExplode(' AS ', $select, 3);
if (!empty($suffix)) {
throw new \InvalidArgumentException(
'QueryBuilder::quoteIdentifiersForSelect() could not parse the input "' . $input . '"',
1461170686
);
}
// The SQL * operator must not be quoted. As it can only occur either by itself
// or preceded by a tablename (tablename.*) check if the last character of a select
// expression is the * and quote only prepended table name. In all other cases the
// full expression is being quoted.
if (substr($fieldName, -2) === '.*') {
$select = $this->quoteIdentifier(substr($fieldName, 0, -2)) . '.*';
} elseif ($fieldName !== '*') {
$select = $this->quoteIdentifier($fieldName);
}
// Quote the alias for the current fieldName, if given
if (!empty($alias)) {
$select .= ' AS ' . $this->quoteIdentifier($alias);
}
}
return $input;
......
......@@ -279,6 +279,75 @@ class QueryBuilderTest extends UnitTestCase
$this->subject->select('aField', 'anotherField');
}
public function quoteIdentifiersForSelectDataProvider()
{
return [
'fieldName' => [
'fieldName',
'"fieldName"',
],
'tableName.fieldName' => [
'tableName.fieldName',
'"tableName"."fieldName"',
],
'tableName.*' => [
'tableName.*',
'"tableName".*',
],
'*' => [
'*',
'*',
],
'fieldName AS anotherFieldName' => [
'fieldName AS anotherFieldName',
'"fieldName" AS "anotherFieldName"',
],
'tableName.fieldName AS anotherFieldName' => [
'tableName.fieldName AS anotherFieldName',
'"tableName"."fieldName" AS "anotherFieldName"',
],
'tableName.fieldName AS anotherTable.anotherFieldName' => [
'tableName.fieldName AS anotherTable.anotherFieldName',
'"tableName"."fieldName" AS "anotherTable"."anotherFieldName"',
],
];
}
/**
* @test
* @dataProvider quoteIdentifiersForSelectDataProvider
* @param string $identifier
* @param string $expectedResult
*/
public function quoteIdentifiersForSelect($identifier, $expectedResult)
{
$this->connection->quoteIdentifier(Argument::cetera())->will(
function ($args) {
$platform = new MockPlatform();
return $platform->quoteIdentifier($args[0]);
}
);
$this->assertSame([$expectedResult], $this->subject->quoteIdentifiersForSelect([$identifier]));
}
/**
* @test
* @expectedException \InvalidArgumentException
*/
public function quoteIdentifiersForSelectWithInvalidAlias()
{
$this->connection->quoteIdentifier(Argument::cetera())->will(
function ($args) {
$platform = new MockPlatform();
return $platform->quoteIdentifier($args[0]);
}
);
$this->subject->quoteIdentifiersForSelect(['aField AS anotherField,someField AS someThing']);
}
/**
* @test
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment