API CHANGE: Refactored test for whether a SQLQuery can be sorted by a particular column into SQLQuery::canSortBy($fieldName) (from r95850)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@98096 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-02-04 00:53:39 +00:00
parent b23263b1cd
commit a03e43c8b2
2 changed files with 21 additions and 14 deletions

View File

@ -448,6 +448,24 @@ class SQLQuery {
$clone->groupby = null; $clone->groupby = null;
return $clone->execute()->value(); return $clone->execute()->value();
} }
/**
* Returns true if this query can be sorted by the given field.
* Note that the implementation of this method is a little crude at the moment, it wil return
* "false" more often that is strictly necessary.
*/
function canSortBy($fieldName) {
$sql = $this->sql();
$selects = $this->select;
foreach($selects as $i => $sel) {
if (preg_match('/"(.*)"\."(.*)"/', $sel, $matches)) $selects[$i] = $matches[2];
}
$SQL_fieldName = Convert::raw2sql($fieldName);
return (in_array($SQL_fieldName,$selects) || stripos($sql,"AS {$SQL_fieldName}"));
}
} }
?> ?>

View File

@ -362,20 +362,9 @@ JS
return false; return false;
} }
if($this->__cachedQuery) { if(!$this->__cachedQuery) $this->__cachedQuery = $this->getQuery();
$query = $this->__cachedQuery;
} else {
$query = $this->__cachedQuery = $this->getQuery();
}
$sql = $query->sql();
$selects = $query->select; return $this->__cachedQuery->canSortBy($fieldName);
foreach($selects as $i => $sel) {
if (preg_match('/"(.+?)"\."(.+?)"/', $sel, $matches)) $selects[$i] = $matches[2];
}
$SQL_fieldName = Convert::raw2sql($fieldName);
return (in_array($SQL_fieldName,$selects) || stripos($sql,"AS {$SQL_fieldName}"));
} }
/** /**