use of org.teiid.query.sql.lang.Insert in project teiid by teiid.
the class TestGroupCollectorVisitor method testInsert.
public void testInsert() {
GroupSymbol gs1 = exampleGroupSymbol(1);
Insert insert = new Insert();
insert.setGroup(gs1);
Set groups = new HashSet();
groups.add(gs1);
helpTestGroups(insert, true, groups);
}
use of org.teiid.query.sql.lang.Insert in project teiid by teiid.
the class TestStaticSymbolMappingVisitor method testVisitInsert1.
public void testVisitInsert1() {
Insert insert = new Insert();
insert.setGroup(exampleGroup(true, 0));
List vars = new ArrayList();
vars.add(exampleElement(true, 0));
vars.add(exampleElement(true, 1));
insert.setVariables(vars);
List values = new ArrayList();
// $NON-NLS-1$
values.add(new Constant("abc"));
// $NON-NLS-1$
values.add(new Constant("abc"));
insert.setValues(values);
helpTest(insert, getSymbolMap());
}
use of org.teiid.query.sql.lang.Insert in project teiid by teiid.
the class TeiidServiceHandler method performInsert.
private UpdateResponse performInsert(String rawURI, UriInfo uriInfo, EdmEntityType entityType, Entity entity) throws SQLException, TeiidException {
ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, rawURI, this.serviceMetadata);
visitor.visit(uriInfo);
Insert command = visitor.insert(entityType, entity, null, this.prepared);
return getClient().executeUpdate(command, visitor.getParameters());
}
use of org.teiid.query.sql.lang.Insert 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();
}
}
Aggregations