diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index 7facfac380..2f76df24f4 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -363,20 +363,20 @@ class CMDBSource { self::$m_aTablesInfo = array(); } - private static function _TablesInfoCacheInit() + private static function _TableInfoCacheInit($sTableName) { - if (!empty(self::$m_aTablesInfo)) return; + if (isset(self::$m_aTablesInfo[strtolower($sTableName)]) + && (self::$m_aTablesInfo[strtolower($sTableName)] != null)) return; - $aTables = self::QueryToArray("SHOW TABLES FROM `".self::$m_sDBName."`", 0); - foreach ($aTables as $aTableData) + try { - $sTableName = $aTableData[0]; + // Check if the table exists $aFields = self::QueryToArray("SHOW COLUMNS FROM `$sTableName`"); // Note: without backticks, you get an error with some table names (e.g. "group") foreach ($aFields as $aFieldData) { $sFieldName = $aFieldData["Field"]; - self::$m_aTablesInfo[$sTableName]["Fields"][$sFieldName] = + self::$m_aTablesInfo[strtolower($sTableName)]["Fields"][$sFieldName] = array ( "Name"=>$aFieldData["Field"], @@ -388,25 +388,31 @@ class CMDBSource ); } } + catch(MySQLException $e) + { + // Table does not exist + self::$m_aTablesInfo[strtolower($sTableName)] = null; + } } - public static function EnumTables() - { - self::_TablesInfoCacheInit(); - return array_keys(self::$m_aTablesInfo); - } + //public static function EnumTables() + //{ + // self::_TablesInfoCacheInit(); + // return array_keys(self::$m_aTablesInfo); + //} public static function GetTableInfo($sTable) { - self::_TablesInfoCacheInit(); + self::_TableInfoCacheInit($sTable); // perform a case insensitive match because on Windows the table names become lowercase :-( - foreach(self::$m_aTablesInfo as $sTableName => $aInfo) - { - if (strtolower($sTableName) == strtolower($sTable)) - { - return $aInfo; - } - } - return null; + //foreach(self::$m_aTablesInfo as $sTableName => $aInfo) + //{ + // if (strtolower($sTableName) == strtolower($sTable)) + // { + // return $aInfo; + // } + //} + return self::$m_aTablesInfo[strtolower($sTable)]; + //return null; } } diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 7eae008092..e1927f8f51 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -1808,12 +1808,11 @@ abstract class MetaModel return false; } CMDBSource::SelectDB(self::$m_sDBName); - foreach (CMDBSource::EnumTables() as $sTable) - { - // perform a case insensitive test because on Windows the table names become lowercase :-( - if (strtolower(substr($sTable, 0, strlen(self::$m_sTablePrefix))) == strtolower(self::$m_sTablePrefix)) return true; - } - return false; + + // If a table matches the prefix, then consider that the database already exists + $sSQL = "SHOW TABLES LIKE '".strtolower(self::$m_sTablePrefix)."%' "; + $result = CMDBSource::Query($sSQL); + return (CMDBSource::NbRows($result) > 0); } public static function DBDrop()