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());
}
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));
}
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"));
}
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);
}
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;
}
}
}
Aggregations