Search in sources :

Example 6 with ProcedureQuery

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 7 with ProcedureQuery

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);
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) Procedure(org.apache.cayenne.map.Procedure) DataRow(org.apache.cayenne.DataRow) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Test(org.junit.Test)

Example 8 with ProcedureQuery

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());
}
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 9 with ProcedureQuery

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 10 with ProcedureQuery

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());
}
Also used : ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Test(org.junit.Test)

Aggregations

ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)12 Test (org.junit.Test)11 Artist (org.apache.cayenne.testdo.testmap.Artist)6 Painting (org.apache.cayenne.testdo.testmap.Painting)6 DataRow (org.apache.cayenne.DataRow)5 Procedure (org.apache.cayenne.map.Procedure)2 BaseTransaction (org.apache.cayenne.tx.BaseTransaction)2 ExternalTransaction (org.apache.cayenne.tx.ExternalTransaction)2 Map (java.util.Map)1 ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)1