public static function getRelations(DB\Table $table) { //using namespace for the table identifier as there might be same name DBs on different servers $namespace = $table->getDatabase()->getNamespace(); $relation_cache = self::_getRelationCache($table); $relations = array(); //may not be any constraints if ($relation_cache !== null) { foreach ($relation_cache as $cached_row) { //--- check both ends of the relation can be built. $local_db = DB::getByDatabaseName($cached_row['TABLE_SCHEMA']); if ($local_db === null) { Wave\Log::write('mysql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['TABLE_SCHEMA']), Wave\Log::WARNING); continue; } $local_column = $local_db->getColumn($cached_row['TABLE_NAME'], $cached_row['COLUMN_NAME']); //skip if there's no referenced schema. This is because primary keys will be in the relation cache (no ref schema) if ($cached_row['REFERENCED_TABLE_SCHEMA'] === null) { continue; } $referenced_db = DB::getByDatabaseName($cached_row['REFERENCED_TABLE_SCHEMA']); if ($referenced_db === null) { Wave\Log::write('mysql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['REFERENCED_TABLE_SCHEMA']), Wave\Log::WARNING); continue; } $referenced_column = $referenced_db->getColumn($cached_row['REFERENCED_TABLE_NAME'], $cached_row['REFERENCED_COLUMN_NAME']); if ($referenced_column === null) { Wave\Log::write('mysql_driver', sprintf('Column [%s] is not referenced in the configuration - skipping building relations.', $cached_row['REFERENCED_COLUMN_NAME']), Wave\Log::WARNING); continue; } //----- if ($cached_row['REFERENCED_TABLE_SCHEMA'] != $cached_row['TABLE_SCHEMA']) { //print_r($cached_row); //exit; } $relation = DB\Relation::create($local_column, $referenced_column, $cached_row['CONSTRAINT_NAME'], isset($cached_row['REVERSE'])); if ($relation !== null) { $relations[$relation->getIdentifyingName()] = $relation; } else { Wave\Log::write('mysql_driver', sprintf('[%s.%s.%s] has duplicate relations.', $cached_row['TABLE_SCHEMA'], $cached_row['TABLE_NAME'], $cached_row['COLUMN_NAME']), Wave\Log::WARNING); } } } return $relations; }
public static function getRelations(DB\Table $table) { //using namespace for the table identifier as there might be same name DBs on different servers $namespace = $table->getDatabase()->getNamespace(); $relation_cache = self::_getRelationCache($table); $relations = array(); //may not be any constraints if ($relation_cache !== null) { foreach ($relation_cache as $cached_row) { //--- check both ends of the relation can be built. $local_db = DB::getByConfig(array('database' => $cached_row['table_catalog'], 'schema' => $cached_row['table_schema'])); if ($local_db === null) { Log::write('pgsql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['table_catalog']), Log::WARNING); continue; } $local_column = $local_db->getColumn($cached_row['table_name'], $cached_row['column_name']); //skip if there's no referenced schema. This is because primary keys will be in the relation cache (no ref schema) if ($cached_row['referenced_table_schema'] === null) { continue; } $referenced_db = DB::getByConfig(array('database' => $cached_row['referenced_table_catalog'], 'schema' => $cached_row['referenced_table_schema'])); if ($referenced_db === null) { Log::write('pgsql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['referenced_table_schema']), Log::WARNING); continue; } $referenced_column = $referenced_db->getColumn($cached_row['referenced_table_name'], $cached_row['referenced_column_name']); //----- $relation = DB\Relation::create($local_column, $referenced_column, $cached_row['constraint_name'], isset($cached_row['reverse'])); if ($relation !== null) { $relations[$relation->getIdentifyingName()] = $relation; } else { Log::write('mysql_driver', sprintf('[%s.%s.%s] has duplicate relations.', $cached_row['table_schema'], $cached_row['table_name'], $cached_row['column_name']), Log::WARNING); } } } return $relations; }