From 7abc6e09c0ba5100245450311d1c249160c9b85c Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 10 Sep 2007 22:22:23 +0000 Subject: [PATCH] Adding two new functions to search for check constraints from ddl stuff. Unused and unfinished for now. I need them in CVS. --- lib/ddllib.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/lib/ddllib.php b/lib/ddllib.php index 7a2b21df64..6867596f2a 100644 --- a/lib/ddllib.php +++ b/lib/ddllib.php @@ -354,6 +354,35 @@ function index_exists($table, $index) { return $exists; } +/** + * Given one XMLDBField, check if it has a check constraint in DB + * + * @uses, $db + * @param XMLDBTable the table + * @param XMLDBField the field to be searched for any existing constraint + * @return boolean true/false + */ +function check_constraint_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; + +/// Wrap over find_check_constraint_name to see if the index exists + if (!find_check_constraint_name($table, $field)) { + $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" @@ -471,6 +500,55 @@ function find_index_name($table, $index) { return false; } +/** + * Given one XMLDBField, the function returns the name of the check constraint in DB (if exists) + * of false if it doesn't exist. Note that XMLDB limits the number of check constrainst per field + * to 1 "enum-like" constraint. So, if more than one is returned, only the first one will be + * retrieved by this funcion. + * + * @uses, $db + * @param XMLDBTable the table to be searched + * @param XMLDBField the field to be searched + * @return string check consrtaint name or false + */ +function find_check_constraint_name($table, $field) { + + global $CFG, $db; + +/// 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; + } + +/// Check the field exists + if (!field_exists($table, $field)) { + $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 check_constraints in table + $checks = null; + if ($checks = $generator->getCheckConstraintsFromDB($table, $field)) { + $checks = array_change_key_case($checks, CASE_LOWER); + } + +/// Arriving here, index not found + $db->debug = $olddbdebug; //Re-set original $db->debug + return $checks; +} + /** * Given one XMLDBTable, the function returns the name of its sequence in DB (if exists) * of false if it doesn't exist -- 2.39.5