use of org.apache.cayenne.ejbql.EJBQLCompiledExpression in project cayenne by apache.
the class EJBQLSelectTranslatorIT method translateSelect.
private SQLTemplate translateSelect(String ejbql, final Map<Integer, Object> queryParameters) {
EJBQLParser parser = EJBQLParserFactory.getParser();
EJBQLCompiledExpression select = parser.compile(ejbql, runtime.getDataDomain().getEntityResolver());
EJBQLQuery query = new EJBQLQuery(ejbql) {
@Override
public Map<Integer, Object> getPositionalParameters() {
return queryParameters;
}
};
EJBQLTranslationContext tr = new EJBQLTranslationContext(runtime.getDataDomain().getEntityResolver(), query, select, new JdbcEJBQLTranslatorFactory(), adapter.getQuotingStrategy());
select.getExpression().visit(new EJBQLSelectTranslator(tr));
return tr.getQuery();
}
use of org.apache.cayenne.ejbql.EJBQLCompiledExpression in project cayenne by apache.
the class EJBQLSelectTranslatorIT method testEqualsNullAndNotNullParameter.
// if parameter value is null and more than one parameter in query
@Test
public void testEqualsNullAndNotNullParameter() {
String ejbql = "select p from Painting p WHERE p.toArtist=:x OR p.toArtist.artistName=:b";
EJBQLParser parser = EJBQLParserFactory.getParser();
EJBQLCompiledExpression select = parser.compile(ejbql, runtime.getDataDomain().getEntityResolver());
EJBQLQuery query = new EJBQLQuery(ejbql);
query.setParameter("x", null);
query.setParameter("b", "Y");
EJBQLTranslationContext tr = new EJBQLTranslationContext(runtime.getDataDomain().getEntityResolver(), query, select, new JdbcEJBQLTranslatorFactory(), adapter.getQuotingStrategy());
select.getExpression().visit(new EJBQLSelectTranslator(tr));
String sql = tr.getQuery().getDefaultTemplate();
assertTrue(sql, sql.endsWith("t0.ARTIST_ID IS NULL OR t1.ARTIST_NAME = #bind($id0)"));
}
use of org.apache.cayenne.ejbql.EJBQLCompiledExpression in project cayenne by apache.
the class EJBQLQueryMetadata method resolve.
boolean resolve(EntityResolver resolver, EJBQLQuery query) {
EJBQLCompiledExpression expression = query.getExpression(resolver);
setPrefetchTree(expression.getPrefetchTree());
resultSetMapping = expression.getResult() != null ? expression.getResult().getResolvedComponents(resolver) : null;
ObjEntity root = expression.getRootDescriptor().getEntity();
if (!super.resolve(root, resolver)) {
return false;
}
if (QueryCacheStrategy.NO_CACHE == getCacheStrategy()) {
return true;
}
// create a unique key based on entity, EJBQL, and parameters
StringBuilder key = new StringBuilder();
if (query.getEjbqlStatement() != null) {
key.append('/').append(query.getEjbqlStatement());
}
if (query.getFetchLimit() > 0) {
key.append('/').append(query.getFetchLimit());
}
Map<String, Object> namedParameters = query.getNamedParameters();
if (!namedParameters.isEmpty()) {
List<String> keys = new ArrayList<>(namedParameters.keySet());
Collections.sort(keys);
for (String parameterKey : keys) {
key.append('/').append(parameterKey).append('=').append(namedParameters.get(parameterKey));
}
}
Map<Integer, Object> positionalParameters = query.getPositionalParameters();
if (!positionalParameters.isEmpty()) {
List<Integer> keys = new ArrayList<>(positionalParameters.keySet());
Collections.sort(keys);
for (Integer parameterKey : keys) {
key.append('/').append(parameterKey).append('=').append(positionalParameters.get(parameterKey));
}
}
if (query.getFetchOffset() > 0 || query.getFetchLimit() > 0) {
key.append('/');
if (query.getFetchOffset() > 0) {
key.append('o').append(query.getFetchOffset());
}
if (query.getFetchLimit() > 0) {
key.append('l').append(query.getFetchLimit());
}
}
this.cacheKey = key.toString();
return true;
}
use of org.apache.cayenne.ejbql.EJBQLCompiledExpression in project cayenne by apache.
the class EJBQLSelectTranslatorIT method testEqualsNullParameter.
// if parameter value is null (in this test x := null) we will generate
// "IS NULL"
@Test
public void testEqualsNullParameter() {
String ejbql = "select p from Painting p WHERE p.toArtist=:x";
EJBQLParser parser = EJBQLParserFactory.getParser();
EJBQLCompiledExpression select = parser.compile(ejbql, runtime.getDataDomain().getEntityResolver());
EJBQLQuery query = new EJBQLQuery(ejbql);
query.setParameter("x", null);
EJBQLTranslationContext tr = new EJBQLTranslationContext(runtime.getDataDomain().getEntityResolver(), query, select, new JdbcEJBQLTranslatorFactory(), adapter.getQuotingStrategy());
select.getExpression().visit(new EJBQLSelectTranslator(tr));
String sql = tr.getQuery().getDefaultTemplate();
assertTrue(sql, sql.endsWith("t0.ARTIST_ID IS NULL"));
}
use of org.apache.cayenne.ejbql.EJBQLCompiledExpression in project cayenne by apache.
the class EJBQLQueryIT method testGetExpression.
@Test
public void testGetExpression() {
String ejbql = "select a FROM Artist a";
EJBQLQuery query = new EJBQLQuery(ejbql);
EJBQLCompiledExpression parsed = query.getExpression(runtime.getDataDomain().getEntityResolver());
assertNotNull(parsed);
assertEquals(ejbql, parsed.getSource());
}
Aggregations