Skip to content
Snippets Groups Projects
Verified Commit b9b6fa58 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3191623 by mondrake, Medha Kumari, daffie, alexpott: Select queries do...

Issue #3191623 by mondrake, Medha Kumari, daffie, alexpott: Select queries do not escape the GROUP BY fields

(cherry picked from commit 5a193a82)
parent aac629ad
No related branches found
No related tags found
20 merge requests!8394[warning] array_flip(): Can only flip STRING and INTEGER values, when saving a non-revisionable custom content entity,!7780issue 3443822: fix for 'No route found for the specified format html. Supported formats: json, xml.',!5013Issue #3071143: Table Render Array Example Is Incorrect,!4848Issue #1566662: Update module should send notifications on Thursdays,!4792Issue #2230689: Remove redundant "Italic" style,!4220Issue #3368223: Link field > Access to internal links is not checked on display.,!3884Issue #3356842,!3870Issue #3087868,!3812Draft: Issue #3339373 by alexpott, andypost, mondrake:...,!3686Issue #3219967 against 9.5.x,!3683Issue #2939397: Clearing AliasManager cache with root path raises warning,!3543Issue #3344259: Allow ajax dialog to have focus configurable,!3356Issue #3209129: Scrolling problems when adding a block via layout builder,!2205Quote all names in the regions section.,!2050Issue #3272969: Remove UnqiueField constraint.,!1956Issue #3268872: hook_views_invalidate_cache not called when a view is deleted,!1893Issue #3217260: Add a way to make media captions not editable in CKEditor,!1459Issue #3087632: menu_name max length is too long,!866Issue #2845319: The highlighting of the 'Home' menu-link does not respect query strings and fragment identifiers,!204Issue #3040556: It is not possible to react to an entity being duplicated
......@@ -868,7 +868,10 @@ public function __toString() {
// GROUP BY
if ($this->group) {
$query .= "\nGROUP BY " . implode(', ', $this->group);
$group_by_fields = array_map(function (string $field): string {
return $this->connection->escapeField($field);
}, $this->group);
$query .= "\nGROUP BY " . implode(', ', $group_by_fields);
}
// HAVING
......
......@@ -230,7 +230,7 @@ public function testGroupByBaseField() {
$view->displayHandlers->get('default')->options['fields']['name']['group_type'] = 'min';
unset($view->displayHandlers->get('default')->options['fields']['id']['group_type']);
$this->executeView($view);
$this->assertStringContainsString('GROUP BY entity_test.id', (string) $view->build_info['query'], 'GROUP BY field includes the base table name when grouping on the base field.');
$this->assertMatchesRegularExpression('/GROUP BY .*[^\w\s]entity_test[^\w\s]\.[^\w\s]id[^\w\s]/', (string) $view->build_info['query'], 'GROUP BY field includes the base table name when grouping on the base field.');
}
/**
......
......@@ -80,4 +80,30 @@ public function testSelectReservedWordAliasAllFields() {
$this->assertSame('27', $record->age);
}
/**
* Tests SELECT query with GROUP BY clauses on fields with reserved names.
*/
public function testGroupBy() {
$this->connection->insert('select')
->fields([
'id' => 2,
'update' => 'Update value 1',
])
->execute();
// Using aliases.
$query = $this->connection->select('select', 's');
$query->addExpression('COUNT([id])', 'num');
$query->addField('s', 'update');
$query->groupBy('s.update');
$this->assertSame('2', $query->execute()->fetchAssoc()['num']);
// Not using aliases.
$query = $this->connection->select('select');
$query->addExpression('COUNT([id])', 'num');
$query->addField('select', 'update');
$query->groupBy('update');
$this->assertSame('2', $query->execute()->fetchAssoc()['num']);
}
}
......@@ -82,6 +82,10 @@ public function testGroupBy() {
$task_field = $query->addField('t', 'task');
$query->orderBy($count_field);
$query->groupBy($task_field);
$this->assertMatchesRegularExpression("/ORDER BY .*[^\w\s]num[^\w\s]/", (string) $query);
$this->assertMatchesRegularExpression("/GROUP BY .*[^\w\s]task[^\w\s]/", (string) $query);
$result = $query->execute();
$num_records = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment