Search in sources :

Example 26 with EJBQLQuery

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

the class DataContextEJBQLSubqueryIT method testExists.

@Test
public void testExists() throws Exception {
    createTwoArtistsFourPaintings();
    String ejbql = "SELECT p FROM Painting p" + " WHERE EXISTS (" + " SELECT DISTINCT p1.paintingTitle FROM Painting p1" + " WHERE p1.paintingTitle = p.paintingTitle" + " AND p.estimatedPrice <> p1.estimatedPrice" + ")";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> objects = context.performQuery(query);
    assertEquals(2, objects.size());
    Set<Object> ids = new HashSet<Object>();
    Iterator<?> it = objects.iterator();
    while (it.hasNext()) {
        Object id = Cayenne.pkForObject((Persistent) it.next());
        ids.add(id);
    }
    assertTrue(ids.contains(new Integer(33001)));
    assertTrue(ids.contains(new Integer(33003)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with EJBQLQuery

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

the class DataContextEJBQLSubqueryIT method testAny.

@Test
public void testAny() throws Exception {
    if (!accessStackAdapter.supportsAllAnySome()) {
        return;
    }
    createTwoArtistsFourPaintings();
    String ejbql = "SELECT p FROM Painting p" + " WHERE p.estimatedPrice > ANY (" + " SELECT p1.estimatedPrice FROM Painting p1" + " WHERE p1.paintingTitle = 'P1'" + ")";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> objects = context.performQuery(query);
    assertEquals(3, objects.size());
    Set<Object> ids = new HashSet<Object>();
    Iterator<?> it = objects.iterator();
    while (it.hasNext()) {
        Object id = Cayenne.pkForObject((Persistent) it.next());
        ids.add(id);
    }
    assertTrue(ids.contains(new Integer(33002)));
    assertTrue(ids.contains(new Integer(33003)));
    assertTrue(ids.contains(new Integer(33004)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 28 with EJBQLQuery

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

the class DataContextEJBQLSubqueryIT method testSome.

@Test
public void testSome() throws Exception {
    if (!accessStackAdapter.supportsAllAnySome()) {
        return;
    }
    createTwoArtistsFourPaintings();
    String ejbql = "SELECT p FROM Painting p" + " WHERE p.estimatedPrice > SOME (" + " SELECT p1.estimatedPrice FROM Painting p1" + " WHERE p1.paintingTitle = 'P1'" + ")";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> objects = context.performQuery(query);
    assertEquals(3, objects.size());
    Set<Object> ids = new HashSet<Object>();
    Iterator<?> it = objects.iterator();
    while (it.hasNext()) {
        Object id = Cayenne.pkForObject((Persistent) it.next());
        ids.add(id);
    }
    assertTrue(ids.contains(new Integer(33002)));
    assertTrue(ids.contains(new Integer(33003)));
    assertTrue(ids.contains(new Integer(33004)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with EJBQLQuery

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

the class DataContextEJBQLSubqueryIT method testSubqueryNoQualifier.

@Test
public void testSubqueryNoQualifier() throws Exception {
    if (!accessStackAdapter.supportsAllAnySome()) {
        return;
    }
    createTwoArtistsFourPaintings();
    String ejbql = "SELECT DISTINCT p FROM Painting p" + " WHERE p.estimatedPrice = ALL (" + " SELECT MAX(p1.estimatedPrice) FROM Painting p1" + ")";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Set<Object> ids = new HashSet<Object>();
    Iterator<?> it = objects.iterator();
    while (it.hasNext()) {
        Object id = Cayenne.pkForObject((Persistent) it.next());
        ids.add(id);
    }
    assertTrue(ids.contains(new Integer(33004)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with EJBQLQuery

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

the class DataContextEJBQLUpdateIT method testUpdateNoQualifierDecimal.

@Test
public void testUpdateNoQualifierDecimal() throws Exception {
    createThreeArtistsTwoPaintings();
    EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.estimatedPrice is NULL or p.estimatedPrice <> 1.1");
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(2l), notUpdated);
    String ejbql = "UPDATE Painting AS p SET p.estimatedPrice = 1.1";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(2, count[0]);
    notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(0l), notUpdated);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) 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