Search in sources :

Example 26 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class NumericTypesIT method testBooleanBoolean.

@Test
public void testBooleanBoolean() throws Exception {
    // populate (testing insert as well)
    BooleanTestEntity trueObject = (BooleanTestEntity) context.newObject("BooleanTestEntity");
    trueObject.setBooleanColumn(Boolean.TRUE);
    BooleanTestEntity falseObject = (BooleanTestEntity) context.newObject("BooleanTestEntity");
    falseObject.setBooleanColumn(Boolean.FALSE);
    context.commitChanges();
    context.invalidateObjects(trueObject, falseObject);
    // fetch true...
    Expression trueQ = ExpressionFactory.matchExp("booleanColumn", Boolean.TRUE);
    List<?> trueResult = context1.performQuery(new SelectQuery<>(BooleanTestEntity.class, trueQ));
    assertEquals(1, trueResult.size());
    BooleanTestEntity trueRefetched = (BooleanTestEntity) trueResult.get(0);
    assertEquals(Boolean.TRUE, trueRefetched.getBooleanColumn());
    // CAY-320. Simplifying the use of booleans to allow identity comparison.
    assertNotSame(trueRefetched, trueObject);
    assertSame(Boolean.TRUE, trueRefetched.getBooleanColumn());
    // fetch false
    Expression falseQ = ExpressionFactory.matchExp("booleanColumn", Boolean.FALSE);
    List<?> falseResult = context1.performQuery(new SelectQuery<>(BooleanTestEntity.class, falseQ));
    assertEquals(1, falseResult.size());
    BooleanTestEntity falseRefetched = (BooleanTestEntity) falseResult.get(0);
    assertEquals(Boolean.FALSE, falseRefetched.getBooleanColumn());
    // CAY-320. Simplifying the use of booleans to allow identity comparison.
    assertNotSame(falseRefetched, falseObject);
    assertSame(Boolean.FALSE, falseRefetched.getBooleanColumn());
}
Also used : Expression(org.apache.cayenne.exp.Expression) BooleanTestEntity(org.apache.cayenne.testdo.numeric_types.BooleanTestEntity) Test(org.junit.Test)

Example 27 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class NumericTypesIT method testTinyintInQualifier.

@Test
public void testTinyintInQualifier() throws Exception {
    createTinyintDataSet();
    // test
    Expression qual = ExpressionFactory.matchExp("tinyintCol", (byte) 81);
    List<?> objects = context.performQuery(new SelectQuery<>(TinyintTestEntity.class, qual));
    assertEquals(1, objects.size());
    TinyintTestEntity object = (TinyintTestEntity) objects.get(0);
    assertEquals(new Byte((byte) 81), object.getTinyintCol());
}
Also used : TinyintTestEntity(org.apache.cayenne.testdo.numeric_types.TinyintTestEntity) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Example 28 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class DataContextRefreshQueryIT method testRefreshQueryResultsLocalCache.

@Test
public void testRefreshQueryResultsLocalCache() throws Exception {
    createRefreshCollectionDataSet();
    Expression qual = Painting.PAINTING_TITLE.eq("P2");
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class, qual);
    q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
    q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
    q.setCacheGroup("X");
    List<?> paints = context.performQuery(q);
    // fetch P1 separately from cached query
    Painting p1 = Cayenne.objectForPK(context, Painting.class, 33001);
    Painting p2 = (Painting) paints.get(0);
    Artist a1 = p2.getToArtist();
    assertSame(a1, p1.getToArtist());
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
    createRefreshCollectionToOneUpdateDataSet();
    RefreshQuery refresh = new RefreshQuery(q);
    context.performQuery(refresh);
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
    // probably refreshed eagerly
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
    assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, p2.getPersistenceState());
    assertSame(a1, p1.getToArtist());
    assertNotSame(a1, p2.getToArtist());
    assertEquals("c", p1.getToArtist().getArtistName());
    assertEquals("b", p2.getToArtist().getArtistName());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 29 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class DataContextRefreshQueryIT method testRefreshQueryResultGroupLocal.

@Test
public void testRefreshQueryResultGroupLocal() throws Exception {
    createRefreshCollectionDataSet();
    Expression qual = Painting.PAINTING_TITLE.eq("P2");
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class, qual);
    q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
    q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
    q.setCacheGroup("X");
    List<?> paints = context.performQuery(q);
    // fetch P1 separately from cached query
    Painting p1 = Cayenne.objectForPK(context, Painting.class, 33001);
    Painting p2 = (Painting) paints.get(0);
    Artist a1 = p2.getToArtist();
    assertSame(a1, p1.getToArtist());
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
    assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
    createRefreshCollectionToOneUpdateDataSet();
    // results are served from cache and therefore are not refreshed
    context.performQuery(q);
    assertSame(a1, p1.getToArtist());
    assertSame(a1, p2.getToArtist());
    assertEquals("c", p1.getToArtist().getArtistName());
    assertEquals("c", p2.getToArtist().getArtistName());
    RefreshQuery refresh = new RefreshQuery("X");
    // this should invalidate results for the next query run
    context.performQuery(refresh);
    // this should force a refresh
    context.performQuery(q);
    assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, p2.getPersistenceState());
    assertSame(a1, p1.getToArtist());
    assertNotSame(a1, p2.getToArtist());
    assertEquals("c", p1.getToArtist().getArtistName());
    assertEquals("b", p2.getToArtist().getArtistName());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 30 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class DataContextSharedCacheIT method testCacheRefreshingOnSelect.

/**
 * Checks that cache is refreshed when a query "refreshingObjects" property is set to
 * true.
 */
@Test
public void testCacheRefreshingOnSelect() throws Exception {
    String originalName = artist.getArtistName();
    final String newName = "version2";
    DataContext context = (DataContext) artist.getObjectContext();
    DataRow oldSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
    assertNotNull(oldSnapshot);
    assertEquals(originalName, oldSnapshot.get("ARTIST_NAME"));
    // update artist using raw SQL
    SQLTemplate update = sqlTemplateCustomizer.createSQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) WHERE ARTIST_NAME = #bind($oldName)");
    Map<String, Object> map = new HashMap<>(3);
    map.put("newName", newName);
    map.put("oldName", originalName);
    update.setParams(map);
    context.performNonSelectingQuery(update);
    // fetch updated artist without refreshing
    Expression qual = ExpressionFactory.matchExp("artistName", newName);
    SelectQuery query = new SelectQuery<>(Artist.class, qual);
    List artists = context.performQuery(query);
    assertEquals(1, artists.size());
    artist = (Artist) artists.get(0);
    // check underlying cache
    DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
    assertNotSame(oldSnapshot, freshSnapshot);
    assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
    // check an artist
    assertEquals(newName, artist.getArtistName());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) HashMap(java.util.HashMap) Expression(org.apache.cayenne.exp.Expression) List(java.util.List) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Aggregations

Expression (org.apache.cayenne.exp.Expression)298 Test (org.junit.Test)265 Artist (org.apache.cayenne.testdo.testmap.Artist)69 SelectQuery (org.apache.cayenne.query.SelectQuery)47 Painting (org.apache.cayenne.testdo.testmap.Painting)29 BigDecimal (java.math.BigDecimal)23 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)16 ObjEntity (org.apache.cayenne.map.ObjEntity)13 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 DbEntity (org.apache.cayenne.map.DbEntity)10 List (java.util.List)9 HashMap (java.util.HashMap)8 DataRow (org.apache.cayenne.DataRow)8 DbAttribute (org.apache.cayenne.map.DbAttribute)8 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)7 DbRelationship (org.apache.cayenne.map.DbRelationship)6 ObjAttribute (org.apache.cayenne.map.ObjAttribute)6 ObjRelationship (org.apache.cayenne.map.ObjRelationship)6 Gallery (org.apache.cayenne.testdo.testmap.Gallery)6