use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method getInternalParameters.
/**
* Return the internal map of parameters.
*/
protected Map<String, Parameter<?>> getInternalParameters() {
if (this.parameters == null) {
this.parameters = new HashMap<String, Parameter<?>>();
// Retrieve named
DatabaseQuery query = getDatabaseQueryInternal();
// query
int count = 0;
if (query.getArguments() != null && !query.getArguments().isEmpty()) {
boolean checkParameterType = query.getArgumentParameterTypes().size() == query.getArguments().size();
for (String argName : query.getArguments()) {
Parameter<?> param = null;
ParameterType type = null;
if (checkParameterType) {
type = query.getArgumentParameterTypes().get(count);
}
if (type == ParameterType.POSITIONAL) {
Integer position = Integer.parseInt(argName);
param = new ParameterExpressionImpl(null, query.getArgumentTypes().get(count), position);
} else {
param = new ParameterExpressionImpl(null, query.getArgumentTypes().get(count), argName);
}
this.parameters.put(argName, param);
++count;
}
}
}
return this.parameters;
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class AbstractPersistenceUnitResource method getQuery.
private Query getQuery(DatabaseQuery query, PersistenceContext context) {
String method = query.isReadQuery() ? "get" : "post";
String jpql = query.getJPQLString() == null ? "" : query.getJPQLString();
StringBuilder parameterString = new StringBuilder();
Iterator<String> argumentsIterator = query.getArguments().iterator();
while (argumentsIterator.hasNext()) {
String argument = argumentsIterator.next();
parameterString.append(";");
parameterString.append(argument).append("={").append(argument).append("}");
}
String version = context.getVersion();
Query returnQuery = null;
if (version != null) {
returnQuery = new Query(query.getName(), jpql, new LinkTemplate("execute", method, context.getBaseURI() + version + "/" + context.getName() + "/query/" + query.getName() + parameterString));
} else {
returnQuery = new Query(query.getName(), jpql, new LinkTemplate("execute", method, context.getBaseURI() + context.getName() + "/query/" + query.getName() + parameterString));
}
if (query.isReportQuery()) {
query.checkPrepare((AbstractSession) context.getServerSession(), new DatabaseRecord());
for (ReportItem item : ((ReportQuery) query).getItems()) {
if (item.getMapping() != null) {
if (item.getAttributeExpression() != null && item.getAttributeExpression().isMapEntryExpression()) {
if (((MapEntryExpression) item.getAttributeExpression()).shouldReturnMapEntry()) {
returnQuery.getReturnTypes().add(Map.Entry.class.getSimpleName());
} else {
returnQuery.getReturnTypes().add(((Class<?>) item.getMapping().getContainerPolicy().getKeyType()).getSimpleName());
}
} else {
returnQuery.getReturnTypes().add(item.getMapping().getAttributeClassification().getSimpleName());
}
} else if (item.getResultType() != null) {
returnQuery.getReturnTypes().add(item.getResultType().getSimpleName());
} else if (item.getDescriptor() != null) {
returnQuery.getReturnTypes().add(item.getDescriptor().getJavaClass().getSimpleName());
} else if (item.getAttributeExpression() != null && item.getAttributeExpression().isConstantExpression()) {
returnQuery.getReturnTypes().add(((ConstantExpression) item.getAttributeExpression()).getValue().getClass().getSimpleName());
} else {
// Use Object.class by default.
returnQuery.getReturnTypes().add(ClassConstants.OBJECT.getSimpleName());
}
}
} else {
returnQuery.getReturnTypes().add(query.getReferenceClassName() == null ? "" : query.getReferenceClass().getSimpleName());
}
return returnQuery;
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class AbstractQueryResource method namedQueryInternal.
/**
* Executes given named query.
*
* @param version the version
* @param persistenceUnit the persistence unit
* @param queryName named query to execute
* @param headers the http headers
* @param uriInfo the uri info
* @return the response
*/
protected Response namedQueryInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
JPARSLogger.entering(CLASS_NAME, "namedQueryInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
try {
final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
final Query query = context.buildQuery(getMatrixParameters(uriInfo, persistenceUnit), queryName, getMatrixParameters(uriInfo, queryName), getQueryParameters(uriInfo));
final DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
final FeatureSet featureSet = context.getSupportedFeatureSet();
final Response response;
if (featureSet.isSupported(Feature.PAGING)) {
response = processPageableQuery(context, queryName, dbQuery, query, headers, uriInfo);
} else {
response = namedQueryResponse(context, queryName, dbQuery, query, headers, uriInfo, featureSet.getResponseBuilder(Feature.NO_PAGING));
}
return response;
} catch (Exception ex) {
throw JPARSException.exceptionOccurred(ex);
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class AbstractQueryResource method buildQueryOptionsResponse.
protected Response buildQueryOptionsResponse(String version, String persistenceUnit, String queryName, HttpHeaders httpHeaders, UriInfo uriInfo) {
JPARSLogger.entering(CLASS_NAME, "buildQueryOptionsResponse", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
// We need to make sure that query with given name exists
final DatabaseQuery query = context.getServerSession().getQuery(queryName);
if (query == null) {
JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_query", new Object[] { queryName, persistenceUnit });
throw JPARSException.responseCouldNotBeBuiltForNamedQueryRequest(queryName, context.getName());
}
final String linkValue = "<" + HrefHelper.buildQueryMetadataHref(context, queryName) + ">; rel=describedby";
httpHeaders.getRequestHeaders().putSingle("Link", linkValue);
return Response.ok().header("Link", linkValue).build();
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class MetadataResource method buildMetadataCatalog.
private MetadataCatalog buildMetadataCatalog(PersistenceContext context) {
final MetadataCatalog result = new MetadataCatalog();
// Entities
final Map<Class<?>, ClassDescriptor> descriptors = context.getServerSession().getDescriptors();
for (ClassDescriptor descriptor : descriptors.values()) {
// Skip embeddables
if (!descriptor.isAggregateDescriptor()) {
result.addResource(buildEntityMetadata(context, descriptor));
}
}
// Queries
final Map<String, List<DatabaseQuery>> allQueries = context.getServerSession().getQueries();
for (List<DatabaseQuery> databaseQueries : allQueries.values()) {
if (databaseQueries != null) {
for (DatabaseQuery query : databaseQueries) {
if (query.getReferenceClassName() != null) {
result.addResource(buildQueryMetadata(context, query));
}
}
}
}
final String href = HrefHelper.buildMetadataCatalogHref(context);
final List<LinkV2> links = (new ItemLinksBuilder()).addCanonical(href).getList();
result.setLinks(links);
return result;
}
Aggregations