Search in sources :

Example 66 with DataRow

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

the class DataContextIteratedQueryIT method testPerformIteratedQuery_CommitWithinIterator.

@Test
public void testPerformIteratedQuery_CommitWithinIterator() throws Exception {
    createArtistsAndPaintingsDataSet();
    assertEquals(7, tPainting.getRowCount());
    try (ResultIterator<?> it = context.performIteratedQuery(ObjectSelect.query(Artist.class))) {
        while (it.hasNextRow()) {
            DataRow row = (DataRow) it.nextRow();
            Artist artist = context.objectFromDataRow(Artist.class, row);
            Painting painting = context.newObject(Painting.class);
            painting.setPaintingTitle("P_" + artist.getArtistName());
            painting.setToArtist(artist);
            context.commitChanges();
        }
    }
    assertEquals(14, tPainting.getRowCount());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 67 with DataRow

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

the class DataContextIteratedQueryIT method testPerformIteratedQuery_resolve.

@Test
public void testPerformIteratedQuery_resolve() throws Exception {
    createArtistsAndPaintingsDataSet();
    try (ResultIterator<?> it = context.performIteratedQuery(ObjectSelect.query(Artist.class))) {
        while (it.hasNextRow()) {
            DataRow row = (DataRow) it.nextRow();
            // try instantiating an object and fetching its relationships
            Artist artist = context.objectFromDataRow(Artist.class, row);
            List<Painting> paintings = artist.getPaintingArray();
            assertNotNull(paintings);
            assertEquals("Expected one painting for artist: " + artist, 1, paintings.size());
        }
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 68 with DataRow

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

the class DataContextProcedureQueryIT method testSelect3.

@Test
public void testSelect3() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    // test ProcedureQuery with Procedure as root
    Procedure proc = context.getEntityResolver().getProcedure(SELECT_STORED_PROCEDURE);
    ProcedureQuery q = new ProcedureQuery(proc);
    q.addParameter("aName", "An Artist");
    q.addParameter("paintingPrice", new Integer(3000));
    List<?> artists = runProcedureSelect(q);
    // check the results
    assertNotNull("Null result from StoredProcedure.", artists);
    assertEquals(1, artists.size());
    DataRow artistRow = (DataRow) artists.get(0);
    Artist a = context.objectFromDataRow(Artist.class, uppercaseConverter(artistRow));
    Painting p = a.getPaintingArray().get(0);
    // invalidate painting, it may have been updated in the proc
    context.invalidateObjects(p);
    assertEquals(2000, p.getEstimatedPrice().intValue());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Procedure(org.apache.cayenne.map.Procedure) DataRow(org.apache.cayenne.DataRow) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 69 with DataRow

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

the class DataContextProcedureQueryIT method testSelect2.

@Test
public void testSelect2() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    ProcedureQuery q = new ProcedureQuery(SELECT_STORED_PROCEDURE);
    q.addParameter("aName", "An Artist");
    q.addParameter("paintingPrice", 3000);
    List<?> artists = runProcedureSelect(q);
    // check the results
    assertNotNull("Null result from StoredProcedure.", artists);
    assertEquals(1, artists.size());
    DataRow artistRow = (DataRow) artists.get(0);
    Artist a = context.objectFromDataRow(Artist.class, uppercaseConverter(artistRow));
    Painting p = a.getPaintingArray().get(0);
    // invalidate painting, it may have been updated in the proc
    context.invalidateObjects(p);
    assertEquals(2000, p.getEstimatedPrice().intValue());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 70 with DataRow

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

the class DataContextProcedureQueryIT method testColumnNameCapitalization.

@Test
public void testColumnNameCapitalization() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    ProcedureQuery q = new ProcedureQuery(SELECT_STORED_PROCEDURE);
    q.setColumnNamesCapitalization(CapsStrategy.LOWER);
    q.addParameter("aName", "An Artist");
    List<DataRow> artists = runProcedureSelect(q);
    ProcedureQuery q1 = new ProcedureQuery(SELECT_STORED_PROCEDURE);
    q1.setColumnNamesCapitalization(CapsStrategy.UPPER);
    q1.addParameter("aName", "An Artist");
    List<DataRow> artists1 = runProcedureSelect(q1);
    assertTrue(artists.get(0).containsKey("date_of_birth"));
    assertFalse(artists.get(0).containsKey("DATE_OF_BIRTH"));
    assertFalse(artists1.get(0).containsKey("date_of_birth"));
    assertTrue(artists1.get(0).containsKey("DATE_OF_BIRTH"));
}
Also used : DataRow(org.apache.cayenne.DataRow) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) 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