use of org.apache.hadoop.hive.metastore.messaging.AddForeignKeyMessage in project hive by apache.
the class AddForeignKeyHandler method handle.
@Override
public List<Task<? extends Serializable>> handle(Context context) throws SemanticException {
AddForeignKeyMessage msg = deserializer.getAddForeignKeyMessage(context.dmd.getPayload());
List<SQLForeignKey> fks = null;
try {
fks = msg.getForeignKeys();
} catch (Exception e) {
if (!(e instanceof SemanticException)) {
throw new SemanticException("Error reading message members", e);
} else {
throw (SemanticException) e;
}
}
List<Task<? extends Serializable>> tasks = new ArrayList<Task<? extends Serializable>>();
if (fks.isEmpty()) {
return tasks;
}
String actualDbName = context.isDbNameEmpty() ? fks.get(0).getFktable_db() : context.dbName;
String actualTblName = context.isTableNameEmpty() ? fks.get(0).getFktable_name() : context.tableName;
for (SQLForeignKey fk : fks) {
// Otherwise, keep db name
if (fk.getPktable_db().equals(fk.getFktable_db())) {
fk.setPktable_db(actualDbName);
}
fk.setFktable_db(actualDbName);
fk.setFktable_name(actualTblName);
}
AlterTableDesc addConstraintsDesc = new AlterTableDesc(actualDbName + "." + actualTblName, new ArrayList<SQLPrimaryKey>(), fks, new ArrayList<SQLUniqueConstraint>(), context.eventOnlyReplicationSpec());
Task<DDLWork> addConstraintsTask = TaskFactory.get(new DDLWork(readEntitySet, writeEntitySet, addConstraintsDesc));
tasks.add(addConstraintsTask);
context.log.debug("Added add constrains task : {}:{}", addConstraintsTask.getId(), actualTblName);
updatedMetadata.set(context.dmd.getEventTo().toString(), actualDbName, actualTblName, null);
return Collections.singletonList(addConstraintsTask);
}
Aggregations