Search in sources :

Example 76 with DataRow

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

the class ProcedureCallIT method testSelect.

@Test
public void testSelect() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    List<?> artists = runProcedureSelect(ProcedureCall.query(SELECT_STORED_PROCEDURE).param("aName", "An Artist").param("paintingPrice", 3000)).firstList();
    // 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) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 77 with DataRow

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

the class SelectById_RunIT method testDataRowMapPkMulti.

@Test
public void testDataRowMapPkMulti() throws Exception {
    createTwoArtists();
    ObjectId oid2 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 2);
    ObjectId oid3 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 3);
    List<DataRow> artists = SelectById.dataRowQuery(oid2, oid3).select(context);
    assertEquals(2, artists.size());
    assertThat(artists.get(0), instanceOf(DataRow.class));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 78 with DataRow

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

the class SelectById_RunIT method testDataRowObjectIdPk.

@Test
public void testDataRowObjectIdPk() throws Exception {
    createTwoArtists();
    ObjectId oid3 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 3);
    DataRow a3 = SelectById.dataRowQuery(oid3).selectOne(context);
    assertNotNull(a3);
    assertEquals("artist3", a3.get("ARTIST_NAME"));
    ObjectId oid2 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 2);
    DataRow a2 = SelectById.dataRowQuery(oid2).selectOne(context);
    assertNotNull(a2);
    assertEquals("artist2", a2.get("ARTIST_NAME"));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 79 with DataRow

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

the class SelectQueryIT method testDbEntityRoot.

@Test
public void testDbEntityRoot() throws Exception {
    createArtistsDataSet();
    DbEntity artistDbEntity = context.getEntityResolver().getDbEntity("ARTIST");
    SelectQuery<DataRow> query = new SelectQuery<>(artistDbEntity);
    List<DataRow> results = context.select(query);
    assertEquals(20, results.size());
    assertTrue(results.get(0) instanceof DataRow);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 80 with DataRow

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

the class DataDomainFlushObserver method nextGeneratedRows.

/**
 * Processes generated keys.
 *
 * @since 1.2
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void nextGeneratedRows(Query query, ResultIterator keysIterator, ObjectId idToUpdate) {
    // read and close the iterator before doing anything else
    List<DataRow> keys;
    try {
        keys = (List<DataRow>) keysIterator.allRows();
    } finally {
        keysIterator.close();
    }
    if (!(query instanceof InsertBatchQuery)) {
        throw new CayenneRuntimeException("Generated keys only supported for InsertBatchQuery, instead got %s", query);
    }
    if (idToUpdate == null || !idToUpdate.isTemporary()) {
        // why would this happen?
        return;
    }
    if (keys.size() != 1) {
        throw new CayenneRuntimeException("One and only one PK row is expected, instead got %d", keys.size());
    }
    DataRow key = keys.get(0);
    // empty key?
    if (key.size() == 0) {
        throw new CayenneRuntimeException("Empty key generated.");
    }
    // infer the key name and currently will only support a single column...
    if (key.size() > 1) {
        throw new CayenneRuntimeException("Only a single column autogenerated PK is supported. " + "Generated key: %s", key);
    }
    BatchQuery batch = (BatchQuery) query;
    for (DbAttribute attribute : batch.getDbEntity().getGeneratedAttributes()) {
        // DB DEFAULT values. Ignore those.
        if (attribute.isPrimaryKey()) {
            Object value = key.values().iterator().next();
            // Log the generated PK
            logger.logGeneratedKey(attribute, value);
            // I guess we should override any existing value,
            // as generated key is the latest thing that exists in the DB.
            idToUpdate.getReplacementIdMap().put(attribute.getName(), value);
            break;
        }
    }
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbAttribute(org.apache.cayenne.map.DbAttribute) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) BatchQuery(org.apache.cayenne.query.BatchQuery) DataRow(org.apache.cayenne.DataRow)

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