Search in sources :

Example 1 with EJBQLCompiledExpression

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();
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) EJBQLParser(org.apache.cayenne.ejbql.EJBQLParser)

Example 2 with EJBQLCompiledExpression

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)"));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) EJBQLParser(org.apache.cayenne.ejbql.EJBQLParser) Test(org.junit.Test)

Example 3 with EJBQLCompiledExpression

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;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) ArrayList(java.util.ArrayList)

Example 4 with EJBQLCompiledExpression

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"));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) EJBQLParser(org.apache.cayenne.ejbql.EJBQLParser) Test(org.junit.Test)

Example 5 with EJBQLCompiledExpression

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());
}
Also used : EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) Test(org.junit.Test)

Aggregations

EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)6 EJBQLParser (org.apache.cayenne.ejbql.EJBQLParser)3 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)1 EJBQLTranslationContext (org.apache.cayenne.access.translator.ejbql.EJBQLTranslationContext)1 EJBQLTranslatorFactory (org.apache.cayenne.access.translator.ejbql.EJBQLTranslatorFactory)1 EJBQLBaseVisitor (org.apache.cayenne.ejbql.EJBQLBaseVisitor)1 EJBQLExpression (org.apache.cayenne.ejbql.EJBQLExpression)1 EJBQLExpressionVisitor (org.apache.cayenne.ejbql.EJBQLExpressionVisitor)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 QueryMetadata (org.apache.cayenne.query.QueryMetadata)1 SQLTemplate (org.apache.cayenne.query.SQLTemplate)1