use of org.teiid.query.validator.UpdateValidator.UpdateInfo in project teiid by teiid.
the class QueryResolver method resolveView.
public static QueryNode resolveView(GroupSymbol virtualGroup, QueryNode qnode, String cacheString, QueryMetadataInterface qmi, boolean logValidation) throws TeiidComponentException, QueryMetadataException, QueryResolverException, QueryValidatorException {
qmi = qmi.getDesignTimeMetadata();
// $NON-NLS-1$
cacheString = "transformation/" + cacheString;
QueryNode cachedNode = (QueryNode) qmi.getFromMetadataCache(virtualGroup.getMetadataID(), cacheString);
if (cachedNode == null) {
Command result = qnode.getCommand();
List<String> bindings = null;
if (result == null) {
try {
result = QueryParser.getQueryParser().parseCommand(qnode.getQuery());
} catch (QueryParserException e) {
throw new QueryResolverException(QueryPlugin.Event.TEIID30065, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30065, virtualGroup));
}
bindings = qnode.getBindings();
} else {
result = (Command) result.clone();
}
if (bindings != null && !bindings.isEmpty()) {
QueryResolver.resolveWithBindingMetadata(result, qmi, qnode, true);
} else {
QueryResolver.resolveCommand(result, qmi, false);
}
Request.validateWithVisitor(new ValidationVisitor(), qmi, result);
validateProjectedSymbols(virtualGroup, qmi, result);
cachedNode = new QueryNode(qnode.getQuery());
cachedNode.setCommand(result);
if (isView(virtualGroup, qmi)) {
String updatePlan = qmi.getUpdatePlan(virtualGroup.getMetadataID());
String deletePlan = qmi.getDeletePlan(virtualGroup.getMetadataID());
String insertPlan = qmi.getInsertPlan(virtualGroup.getMetadataID());
// the elements must be against the view and not the alias
if (virtualGroup.getDefinition() != null) {
GroupSymbol group = new GroupSymbol(virtualGroup.getNonCorrelationName());
group.setMetadataID(virtualGroup.getMetadataID());
virtualGroup = group;
}
List<ElementSymbol> elements = ResolverUtil.resolveElementsInGroup(virtualGroup, qmi);
UpdateValidator validator = new UpdateValidator(qmi, determineType(insertPlan), determineType(updatePlan), determineType(deletePlan));
validator.validate(result, elements);
UpdateInfo info = validator.getUpdateInfo();
if (logValidation && qmi.groupSupports(virtualGroup.getMetadataID(), SupportConstants.Group.UPDATE)) {
if (info.isInherentInsert() && validator.getInsertReport().hasItems()) {
LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getInsertReport().getFailureMessage(), SQLConstants.Reserved.INSERT, qmi.getFullName(virtualGroup.getMetadataID())));
}
if (info.isInherentUpdate() && validator.getUpdateReport().hasItems()) {
LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getUpdateReport().getFailureMessage(), SQLConstants.Reserved.UPDATE, qmi.getFullName(virtualGroup.getMetadataID())));
}
if (info.isInherentDelete() && validator.getDeleteReport().hasItems()) {
LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getDeleteReport().getFailureMessage(), SQLConstants.Reserved.DELETE, qmi.getFullName(virtualGroup.getMetadataID())));
}
}
cachedNode.setUpdateInfo(info);
}
qmi.addToMetadataCache(virtualGroup.getMetadataID(), cacheString, cachedNode);
}
return cachedNode;
}
use of org.teiid.query.validator.UpdateValidator.UpdateInfo in project teiid by teiid.
the class TestUpdateValidator method helpTest.
private UpdateValidator helpTest(String sql, TransformationMetadata md, boolean failInsert, boolean failUpdate, boolean failDelete) {
try {
String vGroup = "gx";
Command command = createView(sql, md, vGroup);
UpdateValidator uv = new UpdateValidator(md, UpdateType.INHERENT, UpdateType.INHERENT, UpdateType.INHERENT);
GroupSymbol gs = new GroupSymbol(vGroup);
ResolverUtil.resolveGroup(gs, md);
uv.validate(command, ResolverUtil.resolveElementsInGroup(gs, md));
UpdateInfo info = uv.getUpdateInfo();
assertEquals(uv.getReport().getFailureMessage(), failInsert, info.getInsertValidationError() != null);
assertEquals(uv.getReport().getFailureMessage(), failUpdate, info.getUpdateValidationError() != null);
assertEquals(uv.getReport().getFailureMessage(), failDelete, info.getDeleteValidationError() != null);
return uv;
} catch (TeiidException e) {
throw new RuntimeException(e);
}
}
use of org.teiid.query.validator.UpdateValidator.UpdateInfo in project teiid by teiid.
the class QueryRewriter method rewriteUpdate.
private Command rewriteUpdate(Update update) throws TeiidComponentException, TeiidProcessingException {
if (update.getGroup().getDefinition() != null) {
removeAlias(update, update.getGroup());
}
Command c = rewriteForWriteThrough(update);
if (c != null) {
return c;
}
UpdateInfo info = update.getUpdateInfo();
if (info != null && info.isInherentUpdate()) {
if (!info.getUnionBranches().isEmpty()) {
List<Command> batchedUpdates = new ArrayList<Command>(info.getUnionBranches().size() + 1);
for (UpdateInfo branchInfo : info.getUnionBranches()) {
batchedUpdates.add(rewriteInherentUpdate((Update) update.clone(), branchInfo));
}
batchedUpdates.add(0, rewriteInherentUpdate(update, info));
return new BatchedUpdateCommand(batchedUpdates, true);
}
return rewriteInherentUpdate(update, info);
}
boolean preserveUnknownOld = preserveUnknown;
preserveUnknown = true;
// Evaluate any function on the right side of set clauses
for (SetClause entry : update.getChangeList().getClauses()) {
entry.setValue(rewriteExpressionDirect(entry.getValue()));
}
preserveUnknown = preserveUnknownOld;
// Rewrite criteria
Criteria crit = update.getCriteria();
if (crit != null) {
preserveUnknown = false;
update.setCriteria(rewriteCriteria(crit));
preserveUnknown = preserveUnknownOld;
}
return update;
}
use of org.teiid.query.validator.UpdateValidator.UpdateInfo in project teiid by teiid.
the class QueryRewriter method rewriteDelete.
private Command rewriteDelete(Delete delete) throws TeiidComponentException, TeiidProcessingException {
if (delete.getGroup().getDefinition() != null) {
removeAlias(delete, delete.getGroup());
}
Command c = rewriteForWriteThrough(delete);
if (c != null) {
return c;
}
UpdateInfo info = delete.getUpdateInfo();
if (info != null && info.isInherentDelete()) {
if (!info.getUnionBranches().isEmpty()) {
List<Command> batchedUpdates = new ArrayList<Command>(info.getUnionBranches().size() + 1);
for (UpdateInfo branchInfo : info.getUnionBranches()) {
batchedUpdates.add(rewriteInherentDelete((Delete) delete.clone(), branchInfo));
}
batchedUpdates.add(0, rewriteInherentDelete(delete, info));
return new BatchedUpdateCommand(batchedUpdates, true);
}
return rewriteInherentDelete(delete, info);
}
// Rewrite criteria
Criteria crit = delete.getCriteria();
if (crit != null) {
boolean preserveUnknownOld = preserveUnknown;
preserveUnknown = false;
delete.setCriteria(rewriteCriteria(crit));
preserveUnknown = preserveUnknownOld;
}
return delete;
}
use of org.teiid.query.validator.UpdateValidator.UpdateInfo in project teiid by teiid.
the class ValidationVisitor method validateUpdate.
protected void validateUpdate(Update update) {
try {
UpdateInfo info = update.getUpdateInfo();
// list of elements that are being updated
for (SetClause entry : update.getChangeList().getClauses()) {
ElementSymbol elementID = entry.getSymbol();
// Check that left side element is updatable
if (!getMetadata().elementSupports(elementID.getMetadataID(), SupportConstants.Element.UPDATE)) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0059", elementID), elementID);
}
Object metadataID = elementID.getMetadataID();
if (getMetadata().isMultiSourceElement(metadataID)) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("multi_source_update_not_allowed", elementID), elementID);
}
// Check that right expression is a constant and is non-null
Expression value = entry.getValue();
if (EvaluatableVisitor.isFullyEvaluatable(value, true)) {
try {
value = new Constant(Evaluator.evaluate(value));
} catch (ExpressionEvaluationException err) {
}
}
if (value instanceof Constant) {
// If value is null, check that element supports this as a nullable column
if (((Constant) value).isNull() && !getMetadata().elementSupports(elementID.getMetadataID(), SupportConstants.Element.NULL)) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0060", SQLStringVisitor.getSQLString(elementID)), elementID);
}
// end of if
}
}
if (info != null && info.isInherentUpdate()) {
validateUpdate(update, Command.TYPE_UPDATE, info);
Set<ElementSymbol> updateCols = update.getChangeList().getClauseMap().keySet();
if (!info.hasValidUpdateMapping(updateCols)) {
handleValidationError(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30376, updateCols), update);
}
}
} catch (TeiidException e) {
handleException(e, update);
}
validateSetClauseList(update.getChangeList());
}
Aggregations