}
/// If the calculated name is in the cache, or if we detect it by introspecting the DB let's modify if
- if (in_array($namewithsuffix, $used_names) || $this->isNameInUse($namewithsuffix, $suffix)) {
+ if (in_array($namewithsuffix, $used_names) || $this->isNameInUse($namewithsuffix, $suffix, $tablename)) {
$counter = 2;
/// If have free space, we add 2
if (strlen($namewithsuffix) < $this->names_max_length) {
$newnamewithsuffix = $newnamewithsuffix . '_' . $suffix;
}
/// Now iterate until not used name is found, incrementing the counter
- while (in_array($newnamewithsuffix, $used_names) || $this->isNameInUse($newnamewithsuffix, $suffix)) {
+ while (in_array($newnamewithsuffix, $used_names) || $this->isNameInUse($newnamewithsuffix, $suffix, $tablename)) {
$counter++;
$newname = substr($name, 0, strlen($newname)-1) . $counter;
$newnamewithsuffix = $newname;
/**
* Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg)
* return if such name is currently in use (true) or no (false)
+ * (MySQL requires the whole XMLDBTable object to be specified, so we add it always)
* (invoked from getNameForObject()
* Only some DB have this implemented
*/
- function isNameInUse($object_name, $type) {
+ function isNameInUse($object_name, $type, $table_name) {
return false; //For generators not implementing introspecion,
//we always return with the name being free to be used
}
return array();
}
+ /**
+ * Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg)
+ * return if such name is currently in use (true) or no (false)
+ * (invoked from getNameForObject()
+ */
+ function isNameInUse($object_name, $type, $table_name) {
+
+ /// Calculate the real table name
+ $xmldb_table = new XMLDBTable($table_name);
+ $tname = $this->getTableName($xmldb_table);
+
+ switch($type) {
+ case 'ix':
+ case 'uix':
+ /// Fetch all the indexes in the table
+ if ($indexes = get_records_sql("SHOW INDEX FROM $tname")) {
+ foreach ($indexes as $index) {
+ /// Normalize array keys
+ $index = array_change_key_case((array)$index, CASE_LOWER);
+ /// Check if the name is being used
+ if (strtolower($object_name) == $index['key_name']) {
+ return true;
+ }
+ }
+ }
+ break;
+ }
+ return false; //No name in use found
+ }
+
+
/**
* Returns an array of reserved words (lowercase) for this DB
*/