Search in sources :

Example 1 with EJBQLParser

use of org.apache.cayenne.ejbql.EJBQLParser 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 EJBQLParser

use of org.apache.cayenne.ejbql.EJBQLParser 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 EJBQLParser

use of org.apache.cayenne.ejbql.EJBQLParser 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)

Aggregations

EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)3 EJBQLParser (org.apache.cayenne.ejbql.EJBQLParser)3 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)3 Test (org.junit.Test)2