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

Split sanitize and normalize to be separate operations #4267

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Draft
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
13 changes: 8 additions & 5 deletions modules/common/src/Storage/SelectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ private function setQueryProperties(Query $query) {
private function addDateExpressions($db_query, $fields, $meta_data) {
foreach ($meta_data as $definition) {
// Confirm definition name is in the fields list.
$name = $this->dbQuery->escapeField($definition['name']);
$sanitizedName = $fields[$name]['field'];
if ($sanitizedName && $definition['type'] == 'date') {
$db_query->addExpression("DATE_FORMAT(" . $sanitizedName . ", '" . $definition['format'] . "')", $sanitizedName);
if ($fields[$definition['name']]['field'] && $definition['type'] == 'date') {
$db_query->addExpression("DATE_FORMAT(" . $definition['name'] . ", '" . $definition['format'] . "')", $definition['name']);
}
}
}
Expand Down Expand Up @@ -143,7 +141,7 @@ private function normalizeProperty(mixed $property): object {
if (is_string($property) && self::safeProperty($property)) {
return (object) [
"collection" => $this->alias,
"property" => $this->dbQuery->escapeField($property),
"property" => $property,
"alias" => NULL,
];
}
Expand All @@ -152,6 +150,10 @@ private function normalizeProperty(mixed $property): object {
}
// Throw exception if obviously unsafe property name.
self::safeProperty($property->property);
return $property;
}

private function sanitizeProperty(object $property) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@janette I think splitting this function potentially causing the $alias error but I am not sure. It is one place to debug.

// Sanitize the property name.
$property->property = $this->dbQuery->escapeField($property->property);
$property->alias = isset($property->alias) ? $this->connection->escapeAlias($property->alias) : NULL;
Expand Down Expand Up @@ -254,6 +256,7 @@ private function normalizeOperand(mixed $operand) {
*/
private function propertyToString(mixed $property) {
$property = $this->normalizeProperty($property);
$property = $this->sanitizeProperty($property);
return "{$property->collection}.{$property->property}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function testDownloadWithDataDictionary() {
// Header should be using the dictionary title.
$this->assertEquals('a,b_title,c,d,e', $lines[0]);

// Set the machine name CSV header mode.
// Set the machine name CSV header mode before the import.
$this->config('metastore.settings')
->set('csv_headers_mode', 'machine_names')
->save();
Expand Down