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