Search in sources :

Example 6 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataContextSQLTemplateIT method testIteratedQuery.

@Test
public void testIteratedQuery() throws Exception {
    createFourArtists();
    String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
    try (ResultIterator<?> it = context.performIteratedQuery(query)) {
        long i = 0;
        while (it.hasNextRow()) {
            i++;
            DataRow row = (DataRow) it.nextRow();
            assertEquals(3, row.size());
            assertEquals("artist" + (1 + i), row.get("ARTIST_NAME"));
        }
        assertEquals(4, i);
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 7 with DataRow

use of org.apache.cayenne.DataRow 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
    List artists = ObjectSelect.query(Artist.class).where(Artist.ARTIST_NAME.eq(newName)).select(context);
    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) HashMap(java.util.HashMap) List(java.util.List) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 8 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataContextSharedCacheIT method testSnapshotEvictedForCommitted.

@Test
public void testSnapshotEvictedForCommitted() throws Exception {
    String newName = "version2";
    assertEquals(PersistenceState.COMMITTED, artist.getPersistenceState());
    context.getObjectStore().getDataRowCache().forgetSnapshot(artist.getObjectId());
    assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId()));
    // modify object and try to save
    artist.setArtistName(newName);
    context.commitChanges();
    assertEquals(newName, artist.getArtistName());
    DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
    assertNotNull(freshSnapshot);
    assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
}
Also used : DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 9 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataContextSharedCacheIT method testSnapshotEvictedForHollow.

@Test
public void testSnapshotEvictedForHollow() throws Exception {
    String originalName = artist.getArtistName();
    context.invalidateObjects(artist);
    assertEquals(PersistenceState.HOLLOW, artist.getPersistenceState());
    assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId()));
    // resolve object
    assertEquals(originalName, artist.getArtistName());
    DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
    assertNotNull(freshSnapshot);
    assertEquals(originalName, freshSnapshot.get("ARTIST_NAME"));
}
Also used : DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 10 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataRowStoreIT method testMaxSize.

/**
 * Tests LRU cache behavior.
 */
@Test
public void testMaxSize() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put(Constants.SNAPSHOT_CACHE_SIZE_PROPERTY, String.valueOf(2));
    cache = new DataRowStore("cacheXYZ", new DefaultRuntimeProperties(props), null);
    assertEquals(2, cache.maximumSize());
    assertEquals(0, cache.size());
    ObjectId key1 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 1);
    Map<ObjectId, DataRow> diff1 = new HashMap<>();
    diff1.put(key1, new DataRow(1));
    ObjectId key2 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 2);
    Map<ObjectId, DataRow> diff2 = new HashMap<>();
    diff2.put(key2, new DataRow(1));
    ObjectId key3 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 3);
    Map<ObjectId, DataRow> diff3 = new HashMap<>();
    diff3.put(key3, new DataRow(1));
    cache.processSnapshotChanges(this, diff1, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(1, cache.size());
    cache.processSnapshotChanges(this, diff2, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(2, cache.size());
    // this addition must overflow the cache, and throw out the first item
    cache.processSnapshotChanges(this, diff3, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(2, cache.size());
    assertNotNull(cache.getCachedSnapshot(key2));
    assertNotNull(cache.getCachedSnapshot(key3));
    assertNull(cache.getCachedSnapshot(key1));
}
Also used : HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Aggregations

DataRow (org.apache.cayenne.DataRow)152 Test (org.junit.Test)113 Artist (org.apache.cayenne.testdo.testmap.Artist)31 ObjectId (org.apache.cayenne.ObjectId)25 DataObject (org.apache.cayenne.DataObject)20 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)20 SQLTemplate (org.apache.cayenne.query.SQLTemplate)18 Date (java.util.Date)14 HashMap (java.util.HashMap)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Persistent (org.apache.cayenne.Persistent)8 Painting (org.apache.cayenne.testdo.testmap.Painting)8 Connection (java.sql.Connection)6 Calendar (java.util.Calendar)5 Map (java.util.Map)5 QueryResponse (org.apache.cayenne.QueryResponse)5 DataMap (org.apache.cayenne.map.DataMap)5