Search in sources :

Example 36 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testFetchObjects.

@Test
public void testFetchObjects() throws Exception {
    createFourArtists();
    String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
    query.setFetchingDataRows(false);
    List<?> objects = context.performQuery(query);
    assertEquals(4, objects.size());
    assertTrue(objects.get(1) instanceof Artist);
    Artist artist2 = (Artist) objects.get(1);
    assertEquals("artist3", artist2.getArtistName());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) Test(org.junit.Test)

Example 37 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testIteratedQuery.

@Test
public void testIteratedQuery() throws Exception {
    createFourArtists();
    String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
    try (ResultIterator<?> it = context.performIteratedQuery(query)) {
        long i = 0;
        while (it.hasNextRow()) {
            i++;
            DataRow row = (DataRow) it.nextRow();
            assertEquals(3, row.size());
            assertEquals("artist" + (1 + i), row.get("ARTIST_NAME"));
        }
        assertEquals(4, i);
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 38 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testSQLResultSetMappingScalar.

@Test
public void testSQLResultSetMappingScalar() throws Exception {
    createFourArtists();
    String sql = "SELECT count(1) AS X FROM ARTIST";
    DataMap map = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate query = new SQLTemplate(map, sql, false);
    query.setTemplate(FrontBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X FROM ARTIST");
    query.setTemplate(OpenBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X FROM ARTIST");
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    SQLResult rsMap = new SQLResult();
    rsMap.addColumnResult("X");
    query.setResult(rsMap);
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Object o = objects.get(0);
    assertTrue("Expected Number: " + o, o instanceof Number);
    assertEquals(4, ((Number) o).intValue());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 39 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testPageSize.

@Test
public void testPageSize() throws Exception {
    createFourArtists();
    int pageSize = 3;
    // sanity check
    assertTrue(pageSize < 4);
    String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
    query.setPageSize(pageSize);
    List<?> objects = context.performQuery(query);
    assertEquals(4, objects.size());
    assertTrue(objects.get(0) instanceof Artist);
    assertTrue(objects instanceof IncrementalFaultList<?>);
    IncrementalFaultList<?> pagedList = (IncrementalFaultList<?>) objects;
    assertEquals(4 - pageSize, pagedList.getUnfetchedObjects());
    // check if we can resolve subsequent pages
    Artist artist = (Artist) objects.get(pageSize);
    int expectUnresolved = 4 - pageSize - pageSize;
    if (expectUnresolved < 0) {
        expectUnresolved = 0;
    }
    assertEquals(expectUnresolved, pagedList.getUnfetchedObjects());
    assertEquals("artist" + (pageSize + 2), artist.getArtistName());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) Test(org.junit.Test)

Example 40 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class DataContextSQLTemplateIT method testRootless_DataNodeName.

@Test
public void testRootless_DataNodeName() throws Exception {
    createFourArtists();
    SQLTemplate query = new SQLTemplate("SELECT * FROM ARTIST", true);
    query.setDataNodeName("testmap");
    assertEquals(4, context.performQuery(query).size());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Test(org.junit.Test)

Aggregations

SQLTemplate (org.apache.cayenne.query.SQLTemplate)118 Test (org.junit.Test)108 Artist (org.apache.cayenne.testdo.testmap.Artist)27 DataRow (org.apache.cayenne.DataRow)18 HashMap (java.util.HashMap)16 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)14 Query (org.apache.cayenne.query.Query)14 SelectQuery (org.apache.cayenne.query.SelectQuery)11 Painting (org.apache.cayenne.testdo.testmap.Painting)11 List (java.util.List)8 Connection (java.sql.Connection)7 SQLAction (org.apache.cayenne.query.SQLAction)7 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)6 Map (java.util.Map)5 OperationObserver (org.apache.cayenne.access.OperationObserver)5 DataMap (org.apache.cayenne.map.DataMap)5 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)5 Date (java.sql.Date)4 FrontBaseAdapter (org.apache.cayenne.dba.frontbase.FrontBaseAdapter)4