]> git.mjollnir.org Git - moodle.git/commitdiff
New function field_exists() to detect if one field exists
authorstronk7 <stronk7>
Sat, 30 Sep 2006 15:35:32 +0000 (15:35 +0000)
committerstronk7 <stronk7>
Sat, 30 Sep 2006 15:35:32 +0000 (15:35 +0000)
lib/ddllib.php

index 075a03683d2cb09546f87b25946083fb94f7d3a7..417927d142f62921d549f4825a4f845676202b2b 100644 (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)
  *