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:
Sam Minnee 2009-07-31 05:41:59 +00:00
parent c7ac19f144
commit 51e28f078c

View File

@ -216,7 +216,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$fields[$field . 'ID'] = 'ForeignKey'; $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. * @return SQLQuery Query built.
*/ */
public function buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") { public function buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") {
// Find a default sort // Cache the big hairy part of buildSQL
if(!$sort) { if(!isset(self::$cache_buildSQL_query[$this->class])) {
$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\"";
// Get the tables to join to // Get the tables to join to
$tableClasses = ClassInfo::dataClassesFor($this->class); $tableClasses = ClassInfo::dataClassesFor($this->class);
if(!$tableClasses) { if(!$tableClasses) {
@ -2388,9 +2383,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
// Build our intial query // Build our intial query
$query = new SQLQuery(array()); $query = new SQLQuery(array());
$query->from("\"$baseClass\""); $query->from("\"$baseClass\"");
$query->where($filter);
$query->orderby($sort);
$query->limit($limit);
// Add SQL for multi-value fields on the base table // Add SQL for multi-value fields on the base table
$databaseFields = self::database_fields($baseClass); $databaseFields = self::database_fields($baseClass);
@ -2438,6 +2430,23 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$query->where[] = "\"$baseClass\".\"ClassName\" IN ('" . implode("','", $classNames) . "')"; $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) { if($having) {
$query->having[] = $having; $query->having[] = $having;
@ -2458,6 +2467,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
return $query; return $query;
} }
/**
* Cache for the hairy bit of buildSQL
*/
private static $cache_buildSQL_query;
/** /**
* Like {@link buildSQL}, but applies the extension modifications. * Like {@link buildSQL}, but applies the extension modifications.
* *