mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
2f2d0c72b1
commit
4aac47340b
@ -24,13 +24,24 @@ class ClassInfo {
|
|||||||
return isset($_ALL_CLASSES['exists'][$class]) ? $_ALL_CLASSES['exists'][$class] : null;
|
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
|
* @todo Move this to Database or DB
|
||||||
*/
|
*/
|
||||||
static function hasTable($class) {
|
static function hasTable($class) {
|
||||||
if(DB::isActive()) {
|
if(DB::isActive()) {
|
||||||
$SQL_table = Convert::raw2sql($class);
|
// Cache the list of all table names to reduce on DB traffic
|
||||||
return (bool)(DB::query("SHOW TABLES LIKE '$SQL_table'")->value());
|
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 {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -269,6 +269,8 @@ class MySQLDatabase extends Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static $_cache_collation_info = array();
|
||||||
|
|
||||||
public function fieldList($table) {
|
public function fieldList($table) {
|
||||||
$fields = DB::query("SHOW FULL FIELDS IN `$table`");
|
$fields = DB::query("SHOW FULL FIELDS IN `$table`");
|
||||||
foreach($fields as $field) {
|
foreach($fields as $field) {
|
||||||
@ -278,7 +280,11 @@ class MySQLDatabase extends Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($field['Collation'] && $field['Collation'] != 'NULL') {
|
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]";
|
$fieldSpec .= " character set $collInfo[Charset] collate $field[Collation]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user