private Command rewriteInherentUpdate(Update update, UpdateInfo info)
throws QueryValidatorException, QueryMetadataException,
TeiidComponentException, QueryResolverException,
TeiidProcessingException {
UpdateMapping mapping = info.findUpdateMapping(update.getChangeList().getClauseMap().keySet(), false);
if (mapping == null) {
throw new QueryValidatorException(QueryPlugin.Util.getString("ValidationVisitor.nonUpdatable", update.getChangeList().getClauseMap().keySet())); //$NON-NLS-1$
}
Map<ElementSymbol, ElementSymbol> symbolMap = mapping.getUpdatableViewSymbols();
if (info.isSimple()) {
update.setGroup(mapping.getGroup().clone());
for (SetClause clause : update.getChangeList().getClauses()) {
clause.setSymbol(symbolMap.get(clause.getSymbol()));
}
//TODO: properly handle correlated references
DeepPostOrderNavigator.doVisit(update, new ExpressionMappingVisitor(symbolMap, true));
if (info.getViewDefinition().getCriteria() != null) {
update.setCriteria(Criteria.combineCriteria(update.getCriteria(), (Criteria)info.getViewDefinition().getCriteria().clone()));
}
//resolve
update.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(update.getGroup(), metadata, Command.TYPE_UPDATE));
return rewriteUpdate(update);
}
Query query = (Query)info.getViewDefinition().clone();
query.setOrderBy(null);
SymbolMap expressionMapping = SymbolMap.createSymbolMap(update.getGroup(), query.getProjectedSymbols(), metadata);
ArrayList<SingleElementSymbol> selectSymbols = mapChangeList(update, symbolMap);
query.setSelect(new Select(selectSymbols));
ExpressionMappingVisitor emv = new ExpressionMappingVisitor(expressionMapping.asMap(), true);
PostOrderNavigator.doVisit(query.getSelect(), emv);
Criteria crit = update.getCriteria();
if (crit != null) {
PostOrderNavigator.doVisit(crit, emv);
query.setCriteria(Criteria.combineCriteria(query.getCriteria(), crit));
}
GroupSymbol group = mapping.getGroup();
String correlationName = mapping.getCorrelatedName().getName();
return createUpdateProcedure(update, query, group, correlationName);
}