use of org.apache.cayenne.query.ProcedureQuery in project cayenne by apache.
the class DataContextProcedureQueryIT method testUpdateNoParam.
@Test
public void testUpdateNoParam() throws Exception {
if (!accessStackAdapter.supportsStoredProcedures()) {
return;
}
// create an artist with painting in the database
createArtist(1000.0);
ProcedureQuery q = new ProcedureQuery(UPDATE_STORED_PROCEDURE_NOPARAM);
// since stored procedure commits its stuff, we must use an explicit
// non-committing transaction
BaseTransaction t = new ExternalTransaction(jdbcEventLogger);
BaseTransaction.bindThreadTransaction(t);
try {
context.performGenericQuery(q);
} finally {
BaseTransaction.bindThreadTransaction(null);
t.commit();
}
// check that price have doubled
ObjectSelect<Artist> select = ObjectSelect.query(Artist.class).prefetch("paintingArray", PrefetchTreeNode.UNDEFINED_SEMANTICS);
List<Artist> artists = select.select(context);
assertEquals(1, artists.size());
Artist a = artists.get(0);
Painting p = a.getPaintingArray().get(0);
assertEquals(2000, p.getEstimatedPrice().intValue());
}
use of org.apache.cayenne.query.ProcedureQuery in project cayenne by apache.
the class DataContextProcedureQueryIT method testSelectWithRowDescriptor.
@Test
public void testSelectWithRowDescriptor() 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.setFetchingDataRows(true);
q.addParameter("aName", "An Artist");
q.addParameter("paintingPrice", new Integer(3000));
// TESTING THIS ***
// A.ARTIST_ID, A.DATE_OF_BIRTH, A.ARTIST_NAME
ColumnDescriptor[] columns = new ColumnDescriptor[3];
// read ID as Long, and everything else as default types
columns[0] = new ColumnDescriptor("ARTIST_ID", Types.BIGINT);
columns[1] = new ColumnDescriptor("ARTIST_NAME", Types.CHAR);
columns[2] = new ColumnDescriptor("DATE_OF_BIRTH", Types.DATE);
q.addResultDescriptor(columns);
List<?> rows = runProcedureSelect(q);
// check the results
assertNotNull("Null result from StoredProcedure.", rows);
assertEquals(1, rows.size());
DataRow artistRow = (DataRow) rows.get(0);
assertEquals(3, artistRow.size());
artistRow = uppercaseConverter(artistRow);
Object id = artistRow.get("ARTIST_ID");
assertNotNull(id);
assertTrue("Expected Long, got: " + id.getClass().getName(), id instanceof Long);
}
use of org.apache.cayenne.query.ProcedureQuery in project cayenne by apache.
the class DataContextProcedureQueryIT method testSelect1.
@Test
public void testSelect1() 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());
}
use of org.apache.cayenne.query.ProcedureQuery in project cayenne by apache.
the class DataContextProcedureQueryIT method testUpdate.
@Test
public void testUpdate() throws Exception {
if (!accessStackAdapter.supportsStoredProcedures()) {
return;
}
// create an artist with painting in the database
createArtist(1000.0);
ProcedureQuery q = new ProcedureQuery(UPDATE_STORED_PROCEDURE);
q.addParameter("paintingPrice", new Integer(3000));
// since stored procedure commits its stuff, we must use an explicit
// non-committing transaction
BaseTransaction t = new ExternalTransaction(jdbcEventLogger);
BaseTransaction.bindThreadTransaction(t);
try {
context.performGenericQuery(q);
} finally {
BaseTransaction.bindThreadTransaction(null);
t.commit();
}
// check that price have doubled
ObjectSelect<Artist> select = ObjectSelect.query(Artist.class).prefetch("paintingArray", PrefetchTreeNode.UNDEFINED_SEMANTICS);
List<Artist> artists = select.select(context);
assertEquals(1, artists.size());
Artist a = artists.get(0);
Painting p = a.getPaintingArray().get(0);
assertEquals(2000, p.getEstimatedPrice().intValue());
}
use of org.apache.cayenne.query.ProcedureQuery in project cayenne by apache.
the class DataContextProcedureQueryIT method testFetchLimit.
@Test
public void testFetchLimit() throws Exception {
if (!accessStackAdapter.supportsStoredProcedures()) {
return;
}
// create an artist with painting in the database
createArtist(1000.0);
createArtist(2000.0);
createArtist(3000.0);
ProcedureQuery q = new ProcedureQuery(SELECT_STORED_PROCEDURE);
q.addParameter("aName", "An Artist");
q.addParameter("paintingPrice", new Integer(3000));
q.setFetchLimit(2);
List<?> artists = runProcedureSelect(q);
assertEquals(2, artists.size());
}
Aggregations