Try using parseSortColumn from ArrayList

This commit is contained in:
Daniel Hensby 2017-12-05 14:43:21 +00:00
parent 2e43780a8a
commit 89166a2ff2
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
3 changed files with 42 additions and 19 deletions

View File

@ -348,8 +348,8 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
/**
* Parses a specified column into a sort field and direction
*
* @param type $column String to parse containing the column name
* @param type $direction Optional Additional argument which may contain the direction
* @param string $column String to parse containing the column name
* @param string $direction Optional Additional argument which may contain the direction
* @return array Sort specification in the form array("Column", SORT_ASC).
*/
protected function parseSortColumn($column, $direction = null) {

View File

@ -3405,25 +3405,23 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
if ($sort && is_string($sort)) {
$sort = preg_split('/,(?![^()]*+\\))/', $sort);
$sort = preg_split('/,(?![^()]*+\\))/', $sort);
foreach ($sort as $value) {
$value = trim($value);
if(strpos($value, ' ') !== false) {
$parts = explode(' ', $value, 2);
$column = $parts[0];
} else {
$column = $value;
}
if (substr($column, 0, 1) === '"') {
$column = substr($column, 1, strlen($column)-1);
}
if (substr($column, -1, 1) === '"') {
$column = substr($column, 0, -1);
}
if ($this->hasOwnTableDatabaseField($column) && !array_key_exists($column, $indexes)) {
$indexes[$column] = true;
}
try {
list ($table, $column) = $this->parseSortColumn(trim($value));
$table = trim($table, '"');
$column = trim($column, '"');
if ($table && strtolower($table) !== strtolower($this->class)) {
continue;
}
if ($this->hasOwnTableDatabaseField($column) && !array_key_exists($column, $indexes)) {
$indexes[$column] = true;
}
} catch (InvalidArgumentException $e) { }
}
}
@ -3434,6 +3432,25 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
return $indexes;
}
/**
* Parses a specified column into a sort field and direction
*
* @param string $column String to parse containing the column name
* @return array Resolved table and column.
*/
protected function parseSortColumn($column) {
// Parse column specification, considering possible ansi sql quoting
// Note that table prefix is allowed, but discarded
if(preg_match('/^("?(?<table>[^"\s]+)"?\\.)?"?(?<column>[^"\s]+)"?(\s+(?<direction>((asc)|(desc))(ending)?))?$/i', $column, $match)) {
$table = $match['table'];
$column = $match['column'];
} else {
throw new InvalidArgumentException("Invalid sort() column");
}
return array($table, $column);
}
/**
* Check the database schema and update it as necessary.
*

View File

@ -195,6 +195,12 @@ class DataObjectSchemaGenerationTest extends SapphireTest {
$this->assertArrayHasKey('Sort', $indexes);
$this->assertTrue($indexes['Sort']);
Config::inst()->update('DataObjectSchemaGenerationTest_Sorted', 'default_sort', '"DataObjectSchemaGenerationTest_Sorted"."Sort" ASC');
$indexes = $object->databaseIndexes();
$this->assertArrayHasKey('Sort', $indexes);
$this->assertTrue($indexes['Sort']);
Config::inst()->update('DataObjectSchemaGenerationTest_Sorted', 'default_sort', '"Sort" DESC, "Title" ASC');
$indexes = $object->databaseIndexes();
$this->assertArrayHasKey('Sort', $indexes);