Search in sources :

Example 1 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class CayenneContextEJBQLIT method testEJBQLSelectMixed.

@Test
public void testEJBQLSelectMixed() throws Exception {
    createTwoRecords();
    EJBQLQuery query = new EJBQLQuery("SELECT COUNT(a), a, a.serverAttribute1 FROM MtTable1 a Group By a ORDER BY a.serverAttribute1");
    List<Object[]> results = context.performQuery(query);
    assertEquals(2, results.size());
    assertEquals(Long.valueOf(1), results.get(0)[0]);
    assertTrue(results.get(0)[1] instanceof ClientMtTable1);
    assertEquals("s1", results.get(0)[2]);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 2 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class CayenneContextEJBQLIT method testEJBQLSelectScalar.

@Test
public void testEJBQLSelectScalar() throws Exception {
    createTwoRecords();
    EJBQLQuery query = new EJBQLQuery("SELECT COUNT(a) FROM MtTable1 a");
    List<Long> results = context.performQuery(query);
    assertEquals(Long.valueOf(2), results.get(0));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 3 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class VerticalInheritanceIT method testCountEjbqlQuery.

@Test
public void testCountEjbqlQuery() throws Exception {
    TableHelper ivRootTable = new TableHelper(dbHelper, "IV_ROOT");
    ivRootTable.setColumns("ID", "NAME", "DISCRIMINATOR");
    TableHelper ivSub1Table = new TableHelper(dbHelper, "IV_SUB1");
    ivSub1Table.setColumns("ID", "SUB1_NAME");
    TableHelper ivSub2Table = new TableHelper(dbHelper, "IV_SUB2");
    ivSub2Table.setColumns("ID", "SUB2_ATTR", "SUB2_NAME");
    // Root, IvSub1, IvSub2
    ivRootTable.insert(1, "root1", "");
    ivRootTable.insert(2, "sub11", "IvSub1");
    ivSub1Table.insert(2, "sub_name1_1");
    ivRootTable.insert(3, "sub21", "IvSub2");
    ivRootTable.insert(4, "sub22", "IvSub2");
    ivSub2Table.insert(3, "attr1", "sub_name2_1");
    ivSub2Table.insert(4, "attr2", "sub_name2_2");
    EJBQLQuery query1 = new EJBQLQuery("SELECT COUNT(a) FROM IvRoot a");
    assertEquals(Collections.singletonList(4L), context.performQuery(query1));
    EJBQLQuery query2 = new EJBQLQuery("SELECT COUNT(a) FROM IvSub1 a");
    assertEquals(Collections.singletonList(1L), context.performQuery(query2));
    EJBQLQuery query3 = new EJBQLQuery("SELECT COUNT(a) FROM IvSub2 a");
    assertEquals(Collections.singletonList(2L), context.performQuery(query3));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) TableHelper(org.apache.cayenne.test.jdbc.TableHelper) Test(org.junit.Test)

Example 4 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery 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 5 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery 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)

Aggregations

EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)160 Test (org.junit.Test)158 Artist (org.apache.cayenne.testdo.testmap.Artist)39 HashSet (java.util.HashSet)35 Painting (org.apache.cayenne.testdo.testmap.Painting)15 QueryResponse (org.apache.cayenne.QueryResponse)12 BigDecimal (java.math.BigDecimal)10 Persistent (org.apache.cayenne.Persistent)10 List (java.util.List)9 ArrayList (java.util.ArrayList)7 FlattenedTest1 (org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1)6 CompoundPainting (org.apache.cayenne.testdo.testmap.CompoundPainting)5 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)5 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 ValueHolder (org.apache.cayenne.ValueHolder)4 CompoundPkTestEntity (org.apache.cayenne.testdo.compound.CompoundPkTestEntity)4 Gallery (org.apache.cayenne.testdo.testmap.Gallery)4 Calendar (java.util.Calendar)3 EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)3