Search in sources :

Example 51 with EJBQLQuery

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

the class DataContextEJBQLDateTimeFunctionalExpressionsIT method testCURRENT_DATE.

@Test
public void testCURRENT_DATE() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    DateTestEntity o1 = context.newObject(DateTestEntity.class);
    cal.set(year - 3, 1, 1);
    o1.setDateColumn(cal.getTime());
    DateTestEntity o2 = context.newObject(DateTestEntity.class);
    cal.set(year + 3, 1, 1);
    o2.setDateColumn(cal.getTime());
    context.commitChanges();
    EJBQLQuery query = new EJBQLQuery("SELECT d FROM DateTestEntity d WHERE d.dateColumn > CURRENT_DATE");
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    assertTrue(objects.contains(o2));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Test(org.junit.Test)

Example 52 with EJBQLQuery

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

the class DataContextEJBQLDateTimeFunctionalExpressionsIT method testCURRENT_TIME.

@Test
public void testCURRENT_TIME() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    DateTestEntity o1 = context.newObject(DateTestEntity.class);
    cal.set(year, month, day, 0, 0, 0);
    o1.setTimeColumn(cal.getTime());
    DateTestEntity o2 = context.newObject(DateTestEntity.class);
    cal.set(year, month, day, 23, 59, 59);
    o2.setTimeColumn(cal.getTime());
    context.commitChanges();
    EJBQLQuery query = new EJBQLQuery("SELECT d FROM DateTestEntity d WHERE d.timeColumn < CURRENT_TIME");
    List<?> objects = context.performQuery(query);
    if (!unitDbAdapter.supportsTimeSqlType()) {
        // result will be invalid most likely as DB doesn't support TIME data type
        return;
    }
    assertEquals(1, objects.size());
    assertTrue(objects.contains(o1));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Test(org.junit.Test)

Example 53 with EJBQLQuery

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

the class DataContextEJBQLDateTimeFunctionalExpressionsIT method testCURRENT_TIMESTAMP.

@Test
public void testCURRENT_TIMESTAMP() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int date = cal.get(Calendar.DATE);
    DateTestEntity o1 = context.newObject(DateTestEntity.class);
    cal.set(year, month, date, 0, 0, 0);
    o1.setTimestampColumn(cal.getTime());
    DateTestEntity o2 = context.newObject(DateTestEntity.class);
    cal.set(year, month, date, 23, 59, 59);
    o2.setTimestampColumn(cal.getTime());
    context.commitChanges();
    EJBQLQuery query = new EJBQLQuery("SELECT d FROM DateTestEntity d WHERE d.timestampColumn < CURRENT_TIMESTAMP");
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    assertTrue(objects.contains(o1));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Test(org.junit.Test)

Example 54 with EJBQLQuery

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

the class DataContextEJBQLDeleteIT method testDeleteNoIdVar.

@Test
public void testDeleteNoIdVar() throws Exception {
    createPaintingsDataSet();
    String ejbql = "delete from Painting";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(2, count[0]);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 55 with EJBQLQuery

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

the class DataContextEJBQLDeleteIT method testDeleteSameEntityQualifier.

@Test
public void testDeleteSameEntityQualifier() throws Exception {
    createPaintingsDataSet();
    String ejbql = "delete from Painting AS p WHERE p.paintingTitle = 'P2'";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(1, count[0]);
    ObjectContext freshContext = runtime.newContext();
    assertNotNull(Cayenne.objectForPK(freshContext, Painting.class, 33001));
    assertNull(Cayenne.objectForPK(freshContext, Painting.class, 33002));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) 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