New function field_exists() to detect if one field exists

This commit is contained in:
stronk7
2006-09-30 15:35:32 +00:00
parent 73d111a820
commit 342da808c5
+47
View File
@@ -276,6 +276,53 @@ function table_exists($table) {
return $exists;
}
/**
* Given one XMLDBField, check if it exists in DB (true/false)
*
* @uses, $db
* @param XMLDBTable the table
* @param XMLDBField the field to be searched for
* @return boolean true/false
*/
function field_exists($table, $field) {
global $CFG, $db;
$exists = true;
/// Do this function silenty (to avoid output in install/upgrade process)
$olddbdebug = $db->debug;
$db->debug = false;
/// Check the table exists
if (!table_exists($table)) {
$db->debug = $olddbdebug; //Re-set original $db->debug
return 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($table, false);
/// Get list of fields in table
$fields = null;
if ($fields = $db->MetaColumns($tablename)) {
$fields = array_change_key_case($fields, CASE_LOWER);
}
if (!array_key_exists($field->getName(), $fields)) {
$exists = false;
}
/// Re-set original debug
$db->debug = $olddbdebug;
return $exists;
}
/**
* Given one XMLDBIndex, check if it exists in DB (true/false)
*