return convertName(schemaName, fkdn.getRefTableName());
}
public static void addJoin(final AISBuilder builder, final FKConstraintDefinitionNode fkdn,
final String defaultSchemaName, final String schemaName, final String tableName) {
TableName parentName = getReferencedName(defaultSchemaName, fkdn);
AkibanInformationSchema ais = builder.akibanInformationSchema();
// Check parent table exists
Table parentTable = ais.getTable(parentName);
if (parentTable == null) {
throw new JoinToUnknownTableException(new TableName(schemaName, tableName), parentName);
}
// Check child table exists
Table childTable = ais.getTable(schemaName, tableName);
if (childTable == null) {
throw new NoSuchTableException(schemaName, tableName);
}
// Check that we aren't joining to ourselves
if (parentTable == childTable) {
throw new JoinToSelfException(schemaName, tableName);
}
// Check that fk list and pk list are the same size
String[] fkColumns = columnNamesFromListOrPK(fkdn.getColumnList(), null); // No defaults for child table
String[] pkColumns = columnNamesFromListOrPK(fkdn.getRefResultColumnList(), parentTable.getPrimaryKey());
int actualPkColCount = parentTable.getPrimaryKeyIncludingInternal().getColumns().size();
if ((fkColumns.length != actualPkColCount) || (pkColumns.length != actualPkColCount)) {
throw new JoinColumnMismatchException(fkdn.getColumnList().size(),
new TableName(schemaName, tableName),
parentName,
parentTable.getPrimaryKeyIncludingInternal().getColumns().size());
}
int colPos = 0;
while((colPos < fkColumns.length) && (colPos < pkColumns.length)) {
String fkColumn = fkColumns[colPos];
String pkColumn = pkColumns[colPos];
if (childTable.getColumn(fkColumn) == null) {
throw new NoSuchColumnException(String.format("%s.%s.%s", schemaName, tableName, fkColumn));
}
if (parentTable.getColumn(pkColumn) == null) {
throw new JoinToWrongColumnsException(new TableName(schemaName, tableName),
fkColumn,
parentName,
pkColumn);
}
++colPos;
}
String joinName = builder.getNameGenerator().generateJoinName(parentName, childTable.getName(), pkColumns, fkColumns);
if (fkdn.getConstraintName() != null) {
joinName = fkdn.getConstraintName().getTableName();
}
builder.joinTables(joinName, parentName.getSchemaName(), parentName.getTableName(), schemaName, tableName);
colPos = 0;
while(colPos < fkColumns.length) {
builder.joinColumns(joinName,
parentName.getSchemaName(), parentName.getTableName(), pkColumns[colPos],
schemaName, tableName, fkColumns[colPos]);
++colPos;
}
builder.addJoinToGroup(parentTable.getGroup().getName(), joinName, 0);
}