Add caching to needlessly repeated SQL calls.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@72007 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-02-18 09:07:51 +00:00
parent 2f2d0c72b1
commit 4aac47340b
2 changed files with 20 additions and 3 deletions

View File

@ -24,13 +24,24 @@ class ClassInfo {
return isset($_ALL_CLASSES['exists'][$class]) ? $_ALL_CLASSES['exists'][$class] : null;
}
/**
* Cache for {@link hasTable()}
*/
private static $_cache_all_tables = null;
/**
* @todo Move this to Database or DB
*/
static function hasTable($class) {
if(DB::isActive()) {
$SQL_table = Convert::raw2sql($class);
return (bool)(DB::query("SHOW TABLES LIKE '$SQL_table'")->value());
// Cache the list of all table names to reduce on DB traffic
if(self::$_cache_all_tables === null) {
self::$_cache_all_tables = array();
$tables = DB::query("SHOW TABLES")->column();
foreach($tables as $table) self::$_cache_all_tables[strtolower($table)] = true;
}
return isset(self::$_cache_all_tables[strtolower($class)]);
} else {
return false;
}

View File

@ -269,6 +269,8 @@ class MySQLDatabase extends Database {
}
}
private static $_cache_collation_info = array();
public function fieldList($table) {
$fields = DB::query("SHOW FULL FIELDS IN `$table`");
foreach($fields as $field) {
@ -278,7 +280,11 @@ class MySQLDatabase extends Database {
}
if($field['Collation'] && $field['Collation'] != 'NULL') {
$collInfo = DB::query("SHOW COLLATION LIKE '$field[Collation]'")->record();
// Cache collation info to cut down on database traffic
if(!isset(self::$_cache_collation_info[$field['Collation']])) {
self::$_cache_collation_info[$field['Collation']] = DB::query("SHOW COLLATION LIKE '$field[Collation]'")->record();
}
$collInfo = self::$_cache_collation_info[$field['Collation']];
$fieldSpec .= " character set $collInfo[Charset] collate $field[Collation]";
}