mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: Improved performance of DataObject::buildSQL(), by caching a suitable interim piece.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@83441 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
c7ac19f144
commit
51e28f078c
@ -216,7 +216,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
$fields[$field . 'ID'] = 'ForeignKey';
|
||||
}
|
||||
|
||||
return (array) $fields;
|
||||
return (array)$fields;
|
||||
}
|
||||
|
||||
|
||||
@ -2366,13 +2366,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
* @return SQLQuery Query built.
|
||||
*/
|
||||
public function buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") {
|
||||
// Find a default sort
|
||||
if(!$sort) {
|
||||
$sort = $this->stat('default_sort');
|
||||
}
|
||||
// Add quoting to sort expression if it's a simple column name
|
||||
if(preg_match('/^[A-Z][A-Z0-9_]*$/i', $sort)) $sort = "\"$sort\"";
|
||||
|
||||
// Cache the big hairy part of buildSQL
|
||||
if(!isset(self::$cache_buildSQL_query[$this->class])) {
|
||||
// Get the tables to join to
|
||||
$tableClasses = ClassInfo::dataClassesFor($this->class);
|
||||
if(!$tableClasses) {
|
||||
@ -2388,9 +2383,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
// Build our intial query
|
||||
$query = new SQLQuery(array());
|
||||
$query->from("\"$baseClass\"");
|
||||
$query->where($filter);
|
||||
$query->orderby($sort);
|
||||
$query->limit($limit);
|
||||
|
||||
// Add SQL for multi-value fields on the base table
|
||||
$databaseFields = self::database_fields($baseClass);
|
||||
@ -2438,6 +2430,23 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
|
||||
$query->where[] = "\"$baseClass\".\"ClassName\" IN ('" . implode("','", $classNames) . "')";
|
||||
}
|
||||
self::$cache_buildSQL_query[$this->class] = clone $query;
|
||||
} else {
|
||||
$query = clone self::$cache_buildSQL_query[$this->class];
|
||||
|
||||
}
|
||||
|
||||
// Find a default sort
|
||||
if(!$sort) {
|
||||
$sort = $this->stat('default_sort');
|
||||
}
|
||||
// Add quoting to sort expression if it's a simple column name
|
||||
if(preg_match('/^[A-Z][A-Z0-9_]*$/i', $sort)) $sort = "\"$sort\"";
|
||||
|
||||
$query->where($filter);
|
||||
$query->orderby($sort);
|
||||
$query->limit($limit);
|
||||
|
||||
|
||||
if($having) {
|
||||
$query->having[] = $having;
|
||||
@ -2458,6 +2467,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache for the hairy bit of buildSQL
|
||||
*/
|
||||
private static $cache_buildSQL_query;
|
||||
|
||||
/**
|
||||
* Like {@link buildSQL}, but applies the extension modifications.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user