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 * Parses a specified column into a sort field and direction
* *
* @param type $column String to parse containing the column name * @param string $column String to parse containing the column name
* @param type $direction Optional Additional argument which may contain the direction * @param string $direction Optional Additional argument which may contain the direction
* @return array Sort specification in the form array("Column", SORT_ASC). * @return array Sort specification in the form array("Column", SORT_ASC).
*/ */
protected function parseSortColumn($column, $direction = null) { protected function parseSortColumn($column, $direction = null) {

View File

@ -3408,22 +3408,20 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$sort = preg_split('/,(?![^()]*+\\))/', $sort); $sort = preg_split('/,(?![^()]*+\\))/', $sort);
foreach ($sort as $value) { foreach ($sort as $value) {
$value = trim($value); try {
if(strpos($value, ' ') !== false) { list ($table, $column) = $this->parseSortColumn(trim($value));
$parts = explode(' ', $value, 2);
$column = $parts[0]; $table = trim($table, '"');
} else { $column = trim($column, '"');
$column = $value;
} if ($table && strtolower($table) !== strtolower($this->class)) {
if (substr($column, 0, 1) === '"') { continue;
$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)) { if ($this->hasOwnTableDatabaseField($column) && !array_key_exists($column, $indexes)) {
$indexes[$column] = true; $indexes[$column] = true;
} }
} catch (InvalidArgumentException $e) { }
} }
} }
@ -3434,6 +3432,25 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
return $indexes; 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. * Check the database schema and update it as necessary.
* *

View File

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