From: stronk7 Date: Sat, 30 Sep 2006 12:01:33 +0000 (+0000) Subject: Added new function table_exists() to be used later... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=68bc165bedd154e1c72b5e57f6da6319f37d09f1;p=moodle.git Added new function table_exists() to be used later... --- diff --git a/lib/ddllib.php b/lib/ddllib.php index d4d1636ab1..21ae1f2f07 100644 --- a/lib/ddllib.php +++ b/lib/ddllib.php @@ -239,6 +239,42 @@ function table_column($table, $oldfield, $field, $type='integer', $size='10', } } +/** + * Given one XMLDBTable, check if it exists in DB (true/false) + * + * @param XMLDBTable table to be searched for + * @return boolean true/false + */ +function table_exists($xmldb_table) { + + global $CFG, $db; + + $exists = true; + +/// Do this function silenty (to avoid output in install/upgrade process) + $olddbdebug = $db->debug; + $db->debug = false; + +/// Load the needed generator + $classname = 'XMLDB' . $CFG->dbtype; + $generator = new $classname(); + $generator->setPrefix($CFG->prefix); +/// Calculate the name of the table + $tablename = $generator->getTableName($xmldb_table, false); + +/// Search such tablename in DB + $metatables = $db->MetaTables(); + $metatables = array_flip($metatables); + $metatables = array_change_key_case($metatables, CASE_LOWER); + if (!array_key_exists($tablename, $metatables)) { + $exists = false; + } + +/// Re-set original debug + $db->debug = $olddbdebug; + + return $exists; +} /** * This function IS NOT IMPLEMENTED. ONCE WE'LL BE USING RELATIONAL * INTEGRITY IT WILL BECOME MORE USEFUL. FOR NOW, JUST CALCULATE "OFFICIAL" @@ -319,10 +355,7 @@ function find_index_name($xmldb_table, $xmldb_index) { $tablename = $CFG->prefix . $xmldb_table->getName(); /// Check the table exists - $metatables = $db->MetaTables(); - $metatables = array_flip($metatables); - $metatables = array_change_key_case($metatables, CASE_LOWER); - if (!array_key_exists($tablename, $metatables)) { + if (!table_exists($xmldb_table)) { $db->debug = $olddbdebug; //Re-set original $db->debug return false; }