use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TestAssignmentStatement method sample2.
public static final AssignmentStatement sample2() {
Query query = new Query();
// $NON-NLS-1$
query.setSelect(new Select(Arrays.asList(new ElementSymbol("x"))));
// $NON-NLS-1$
query.setFrom(new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("y")))));
// $NON-NLS-1$
return new AssignmentStatement(new ElementSymbol("b"), query);
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TestAssignmentStatement method testGetCommand.
public void testGetCommand() throws Exception {
AssignmentStatement s2 = sample2();
// $NON-NLS-1$
Query query = (Query) QueryParser.getQueryParser().parseCommand("Select x from y");
// $NON-NLS-1$
assertEquals("Didn't get the same parts ", ((ScalarSubquery) s2.getExpression()).getCommand(), query);
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TeiidServiceHandler method createEntity.
@Override
public void createEntity(DataRequest request, Entity entity, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
EdmEntityType entityType = request.getEntitySet().getEntityType();
String txn;
try {
txn = getClient().startTransaction();
} catch (SQLException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
}
boolean success = false;
try {
List<ExpandNode> expands = new ArrayList<TeiidServiceHandler.ExpandNode>();
int insertDepth = insertDepth(entityType, entity);
// don't count the root
ODataSQLBuilder.checkExpandLevel(insertDepth - 1);
UpdateResponse updateResponse = performDeepInsert(request.getODataRequest().getRawBaseUri(), request.getUriInfo(), entityType, entity, expands);
if (updateResponse != null && updateResponse.getUpdateCount() == 1) {
ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), true, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
Query query = visitor.selectWithEntityKey(entityType, entity, updateResponse.getGeneratedKeys(), expands);
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logDetail(LogConstants.CTX_ODATA, null, "created entity = ", entityType.getName(), " with key=", query.getCriteria().toString());
EntityCollectionResponse result = new EntityCollectionResponse(request.getODataRequest().getRawBaseUri(), visitor.getContext());
getClient().executeSQL(query, visitor.getParameters(), false, null, null, null, 1, result);
if (!result.getEntities().isEmpty()) {
entity = result.getEntities().get(0);
String location = EntityResponse.buildLocation(request.getODataRequest().getRawBaseUri(), entity, request.getEntitySet().getName(), entityType);
entity.setId(new URI(location));
}
response.writeCreatedEntity(request.getEntitySet(), entity);
} else {
response.writeNotModified();
}
getClient().commit(txn);
success = true;
} catch (EdmPrimitiveTypeException | TeiidException | SQLException | URISyntaxException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
} finally {
if (!success) {
try {
getClient().rollback(txn);
} catch (SQLException e1) {
// ignore
}
}
}
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TeiidServiceHandler method crossJoin.
@Override
public void crossJoin(DataRequest request, List<String> entitySetNames, ODataResponse response) throws ODataLibraryException, ODataApplicationException {
final ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
visitor.visit(request.getUriInfo());
try {
Query query = visitor.selectQuery();
BaseResponse queryResponse = executeQuery(request, request.isCountRequest(), visitor, query);
ContextURL.Builder builder = new ContextURL.Builder().asCollection().entitySetOrSingletonOrType("Edm.ComplexType");
EdmComplexResponse complexResponse = EdmComplexResponse.getInstance(request, builder.build(), false, response);
sendResults(request, visitor, queryResponse, complexResponse);
} catch (Exception e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
}
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TeiidServiceHandler method read.
@Override
public <T extends ServiceResponse> void read(final DataRequest request, T response) throws ODataLibraryException, ODataApplicationException {
final ODataSQLBuilder visitor = new ODataSQLBuilder(odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
visitor.visit(request.getUriInfo());
final BaseResponse queryResponse;
try {
Query query = visitor.selectQuery();
queryResponse = executeQuery(request, request.isCountRequest(), visitor, query);
} catch (Throwable e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
}
response.accepts(new ServiceResponseVisior() {
public void visit(CountResponse response) throws ODataLibraryException, ODataApplicationException {
org.teiid.odata.api.CountResponse cr = (org.teiid.odata.api.CountResponse) queryResponse;
response.writeCount(cr.getCount());
}
public void visit(PrimitiveValueResponse response) throws ODataLibraryException, ODataApplicationException {
EntityCollection entitySet = (EntityCollection) queryResponse;
if (!entitySet.getEntities().isEmpty()) {
Entity entity = entitySet.getEntities().get(0);
EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
Property property = entity.getProperty(edmProperty.getName());
if (property == null) {
response.writeNotFound(true);
} else if (property.getValue() == null) {
response.writeNoContent(true);
} else {
response.write(property.getValue());
}
} else {
response.writeNotFound(true);
}
}
public void visit(PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
EntityCollection entitySet = (EntityCollection) queryResponse;
if (!entitySet.getEntities().isEmpty()) {
Entity entity = entitySet.getEntities().get(0);
EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
Property property = entity.getProperty(edmProperty.getName());
response.writeProperty(edmProperty.getType(), property);
} else {
response.writeNotFound(true);
}
}
public void visit(StreamResponse response) throws ODataLibraryException, ODataApplicationException {
EntityCollectionResponse entitySet = (EntityCollectionResponse) queryResponse;
EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
Object value = entitySet.getStream(edmProperty.getName());
if (value == null) {
response.writeNoContent(true);
} else {
try {
handleLobResult(getClient().getProperty(Client.CHARSET), value, response);
} catch (SQLException e) {
LogManager.logDetail(LogConstants.CTX_ODATA, e);
response.writeServerError(true);
}
}
}
public void visit(EntityResponse response) throws ODataLibraryException, ODataApplicationException {
EntityCollection entitySet = (EntityCollection) queryResponse;
if (entitySet.getEntities().isEmpty()) {
if (visitor.hasNavigation()) {
response.writeNoContent(true);
} else {
response.writeNotFound(true);
}
} else {
response.writeReadEntity(visitor.getContext().getEdmEntityType(), entitySet.getEntities().get(0));
}
}
public void visit(EntitySetResponse response) throws ODataLibraryException, ODataApplicationException {
sendResults(request, visitor, queryResponse, response);
}
});
}
Aggregations