Skip to content

Commit

Permalink
Support expressions in group part
Browse files Browse the repository at this point in the history
  • Loading branch information
iquito committed May 4, 2022
1 parent 9238460 commit 1e6ad69
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
"squirrelphp/entities-bundle": "Automatic integration of squirrelphp/entities in Symfony"
},
"config": {
"sort-packages": false
"sort-packages": false,
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
"captainhook/plugin-composer": true,
"composer/package-versions-deprecated": true
}
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 6 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="v4.15.0@a1b5e489e6fcebe40cb804793d964e99fc347820">
<files psalm-version="4.23.0@f1fe6ff483bf325c803df9f510d09a03fd796f88">
<file src="src/Builder/FlattenedFieldsWithTypeTrait.php">
<InvalidReturnType occurrences="4">
<code>bool[]</code>
Expand All @@ -18,6 +18,11 @@
<code>$select['offset'] ?? null</code>
</PossiblyNullArgument>
</file>
<file src="src/Doctrine/DBErrorHandler.php">
<InternalMethod occurrences="1">
<code>connect</code>
</InternalMethod>
</file>
<file src="tests/DBPassToLowerLayerTest.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>\Mockery::mock(DBPassToLowerLayerTrait::class)-&gt;makePartial()</code>
Expand Down
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
errorLevel="2"
reportMixedIssues="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
31 changes: 29 additions & 2 deletions src/Doctrine/DBConvertStructuredQueryToSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,35 @@ public function buildGroupBy(array $groupByOptions): string
);
}

// Add to list of finished expressions
$groupByProcessed[] = ($this->quoteIdentifier)($expression);
// Check if this is a custom expression, not just a field name to value expression
if (
\str_contains($expression, ' ')
|| \str_contains($expression, '=')
|| \str_contains($expression, '<')
|| \str_contains($expression, '>')
|| \str_contains($expression, '(')
|| \str_contains($expression, ')')
|| \str_contains($expression, '+')
|| \str_contains($expression, '-')
|| \str_contains($expression, '*')
|| \str_contains($expression, '/')
|| \str_contains($expression, '%')
|| \str_contains($expression, '^')
|| \str_contains($expression, '|')
|| \str_contains($expression, '&')
|| \str_contains($expression, '~')
) {
// Colons found, which are used to escape variables
if (\str_contains($expression, ':')) {
$expression = ($this->quoteExpression)($expression);
}

// Add to list of finished expressions
$groupByProcessed[] = $expression;
} else { // We assume just a field name to value(s) expression
// Add to list of finished expressions
$groupByProcessed[] = ($this->quoteIdentifier)($expression);
}
}

return \implode(',', $groupByProcessed);
Expand Down
3 changes: 2 additions & 1 deletion tests/DoctrineImplementationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ public function testSelectStructuredComplicated(): void
'AND "boring_field_name" IN (?,?,?,?) ' .
'AND "another_boring_name" IS NULL ' .
'AND "boolean_field"=? ' .
'GROUP BY "a"."field" ' .
'GROUP BY "a"."field",DATE("a"."field") ' .
'ORDER BY "a"."field" DESC,"a"."field" + "b"."field" ASC';
$vars = [5, 'orders_xml_override', 5, 3, 8, 13, 1];

Expand Down Expand Up @@ -1149,6 +1149,7 @@ public function testSelectStructuredComplicated(): void
],
'group' => [
'a.field',
'DATE(:a.field:)',
],
'order' => [
'a.field' => 'DESC',
Expand Down
5 changes: 5 additions & 0 deletions vendor-bin/phpcs/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"require": {
"squizlabs/php_codesniffer": "^3.6",
"slevomat/coding-standard": "^7.0"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
}
}

0 comments on commit 1e6ad69

Please sign in to comment.