use of org.teiid.query.sql.lang.Update in project teiid by teiid.
the class UpdateResolver method getVariableValues.
/**
* @see org.teiid.query.resolver.VariableResolver#getVariableValues(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.QueryMetadataInterface)
*/
public Map<ElementSymbol, Expression> getVariableValues(Command command, boolean changingOnly, QueryMetadataInterface metadata) throws QueryMetadataException, TeiidComponentException {
Map<ElementSymbol, Expression> result = new HashMap<ElementSymbol, Expression>();
Update update = (Update) command;
Map<ElementSymbol, Expression> changing = update.getChangeList().getClauseMap();
for (Entry<ElementSymbol, Expression> entry : changing.entrySet()) {
ElementSymbol leftSymbol = entry.getKey().clone();
leftSymbol.getGroupSymbol().setName(ProcedureReservedWords.CHANGING);
leftSymbol.setType(DataTypeManager.DefaultDataClasses.BOOLEAN);
result.put(leftSymbol, new Constant(Boolean.TRUE));
if (!changingOnly) {
leftSymbol = entry.getKey().clone();
leftSymbol.getGroupSymbol().setName(SQLConstants.Reserved.NEW);
result.put(leftSymbol, entry.getValue());
}
}
Collection<ElementSymbol> insertElmnts = ResolverUtil.resolveElementsInGroup(update.getGroup(), metadata);
insertElmnts.removeAll(changing.keySet());
Iterator<ElementSymbol> defaultIter = insertElmnts.iterator();
while (defaultIter.hasNext()) {
ElementSymbol varSymbol = defaultIter.next().clone();
varSymbol.getGroupSymbol().setName(ProcedureReservedWords.CHANGING);
varSymbol.setType(DataTypeManager.DefaultDataClasses.BOOLEAN);
result.put(varSymbol, new Constant(Boolean.FALSE));
}
return result;
}
use of org.teiid.query.sql.lang.Update in project teiid by teiid.
the class TestStaticSymbolMappingVisitor method testVisitUpdate2.
public void testVisitUpdate2() {
Update update = new Update();
update.setGroup(exampleGroup(true, 0));
// $NON-NLS-1$
update.addChange(exampleElement(true, 0), new Constant("abc"));
// $NON-NLS-1$
update.addChange(exampleElement(true, 1), new Constant("abc"));
// $NON-NLS-1$
update.setCriteria(new CompareCriteria(exampleElement(true, 2), CompareCriteria.LT, new Constant("xyz")));
helpTest(update, getSymbolMap());
}
use of org.teiid.query.sql.lang.Update in project teiid by teiid.
the class ReferenceUpdateSQLBuilder method updateReference.
public Update updateReference(URI referenceId, boolean prepared, boolean delete) throws SQLException {
try {
if (referenceId != null) {
UriInfo uriInfo = ODataSQLBuilder.buildUriInfo(referenceId, this.baseURI, this.serviceMetadata, this.odata);
UriResourceEntitySet uriEnitytSet = (UriResourceEntitySet) uriInfo.asUriInfoResource().getUriResourceParts().get(0);
if (this.collection) {
this.updateTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
} else {
this.referenceTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
}
}
} catch (UriParserException e) {
throw new SQLException(e);
} catch (URISyntaxException e) {
throw new SQLException(e);
} catch (UriValidationException e) {
throw new SQLException(e);
}
try {
Update update = new Update();
update.setGroup(this.updateTable.getGroupSymbol());
List<String> columnNames = DocumentNode.getColumnNames(this.updateTable.getFk().getColumns());
for (int i = 0; i < columnNames.size(); i++) {
Column column = this.updateTable.getFk().getColumns().get(i);
String columnName = columnNames.get(i);
ElementSymbol symbol = new ElementSymbol(columnName, this.updateTable.getGroupSymbol());
EdmEntityType entityType = this.updateTable.getEdmEntityType();
EdmProperty edmProperty = (EdmProperty) entityType.getProperty(columnName);
// reference table keys will be null for delete scenario
Object value = null;
if (!delete) {
UriParameter parameter = getParameter(this.updateTable.getFk().getReferenceColumns().get(i), this.referenceTable.getKeyPredicates());
value = ODataTypeManager.parseLiteral(edmProperty, column.getJavaType(), parameter.getText());
}
if (prepared) {
update.addChange(symbol, new Reference(i++));
this.params.add(ODataSQLBuilder.asParam(edmProperty, value));
} else {
update.addChange(symbol, new Constant(ODataSQLBuilder.asParam(edmProperty, value).getValue()));
}
}
Criteria criteria = DocumentNode.buildEntityKeyCriteria(this.updateTable, null, this.metadata, this.odata, null, null);
update.setCriteria(criteria);
return update;
} catch (TeiidException e) {
throw new SQLException(e);
}
}
use of org.teiid.query.sql.lang.Update in project teiid by teiid.
the class TeiidServiceHandler method updateEntity.
@Override
public void updateEntity(DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
// TODO: need to match entityETag.
checkETag(entityETag);
UpdateResponse updateResponse = null;
if (merge) {
try {
ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
visitor.visit(request.getUriInfo());
EdmEntityType entityType = request.getEntitySet().getEntityType();
Update update = visitor.update(entityType, entity, this.prepared);
updateResponse = getClient().executeUpdate(update, visitor.getParameters());
} catch (SQLException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
} catch (TeiidException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
}
} else {
// delete, then insert
String txn = startTransaction();
boolean success = false;
try {
// build insert first as it could fail to validate
ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
visitor.visit(request.getUriInfo());
EdmEntityType entityType = request.getEntitySet().getEntityType();
List<UriParameter> keys = request.getKeyPredicates();
Insert command = visitor.insert(entityType, entity, keys, this.prepared);
// run delete
ODataSQLBuilder deleteVisitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
deleteVisitor.visit(request.getUriInfo());
Delete delete = deleteVisitor.delete();
updateResponse = getClient().executeUpdate(delete, deleteVisitor.getParameters());
// run insert
updateResponse = getClient().executeUpdate(command, visitor.getParameters());
commit(txn);
success = true;
} catch (SQLException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
} catch (TeiidException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
} finally {
if (!success) {
rollback(txn);
}
}
}
if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
response.writeUpdatedEntity();
} else {
response.writeNotModified();
}
}
use of org.teiid.query.sql.lang.Update in project teiid by teiid.
the class TeiidServiceHandler method manageReference.
private void manageReference(DataRequest request, URI referenceId, NoContentResponse response, boolean delete) throws ODataApplicationException {
UpdateResponse updateResponse = null;
try {
ReferenceUpdateSQLBuilder visitor = new ReferenceUpdateSQLBuilder(getClient().getMetadataStore(), request.getODataRequest().getRawBaseUri(), this.serviceMetadata, this.odata);
visitor.visit(request.getUriInfo());
Update update = visitor.updateReference(referenceId, this.prepared, delete);
updateResponse = getClient().executeUpdate(update, visitor.getParameters());
} catch (SQLException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
}
if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
response.writeNoContent();
} else {
response.writeNotModified();
}
}
Aggregations