use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextSQLTemplateIT method testFetchOffset.
@Test
public void testFetchOffset() throws Exception {
createFourArtists();
int fetchOffset = 2;
// sanity check
assertTrue(fetchOffset < 4);
String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
query.setFetchOffset(fetchOffset);
List<?> objects = context.performQuery(query);
assertEquals(4 - fetchOffset, objects.size());
assertTrue(objects.get(0) instanceof Artist);
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextSQLTemplateIT method testBindObjectEqualShort.
@Test
public void testBindObjectEqualShort() throws Exception {
createFourArtistsAndThreePaintingsDataSet();
Artist a = Cayenne.objectForPK(context, Artist.class, 101);
String template = "SELECT * FROM PAINTING " + "WHERE #bindObjectEqual($a) ORDER BY PAINTING_ID";
SQLTemplate query = new SQLTemplate(Painting.class, template);
query.setColumnNamesCapitalization(CapsStrategy.UPPER);
query.setParams(Collections.singletonMap("a", a));
List<?> objects = context.performQuery(query);
assertEquals(1, objects.size());
Painting p = (Painting) objects.get(0);
assertEquals(7, Cayenne.intPKForObject(p));
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextSQLTemplateIT method testFetchLimit.
@Test
public void testFetchLimit() throws Exception {
createFourArtists();
int fetchLimit = 2;
// sanity check
assertTrue(fetchLimit < 4);
String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
query.setFetchLimit(fetchLimit);
List<?> objects = context.performQuery(query);
assertEquals(fetchLimit, objects.size());
assertTrue(objects.get(0) instanceof Artist);
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextSQLTemplateIT method testFetchDataRows.
@Test
public void testFetchDataRows() throws Exception {
createFourArtists();
String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = new SQLTemplate(Artist.class, template);
sqlTemplateCustomizer.updateSQLTemplate(query);
query.setFetchingDataRows(true);
List<DataRow> rows = context.performQuery(query);
assertEquals(4, rows.size());
DataRow row2 = rows.get(1);
assertEquals(3, row2.size());
Object id = row2.get("ARTIST_ID");
assertEquals(new Integer(101), new Integer(id.toString()));
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextSQLTemplateIT method testSQLResultSetMappingMixed.
@Test
public void testSQLResultSetMappingMixed() throws Exception {
createFourArtistsAndThreePaintingsDataSet();
String sql = "SELECT #result('t0.ARTIST_ID' 'long' 'X'), #result('t0.ARTIST_NAME' 'String' 'Y'), #result('t0.DATE_OF_BIRTH' 'Date' 'Z'), #result('count(t1.PAINTING_ID)' 'int' 'C') " + "FROM ARTIST t0 LEFT JOIN PAINTING t1 ON (t0.ARTIST_ID = t1.ARTIST_ID) " + "GROUP BY t0.ARTIST_ID, t0.ARTIST_NAME, t0.DATE_OF_BIRTH " + "ORDER BY t0.ARTIST_ID";
DataMap map = context.getEntityResolver().getDataMap("testmap");
SQLTemplate query = new SQLTemplate(map, sql, false);
query.setColumnNamesCapitalization(CapsStrategy.UPPER);
EntityResult artistResult = new EntityResult(Artist.class);
artistResult.addDbField(Artist.ARTIST_ID_PK_COLUMN, "X");
artistResult.addObjectField(Artist.ARTIST_NAME.getName(), "Y");
artistResult.addObjectField(Artist.DATE_OF_BIRTH.getName(), "Z");
SQLResult rsMap = new SQLResult();
rsMap.addEntityResult(artistResult);
rsMap.addColumnResult("C");
query.setResult(rsMap);
List<?> objects = context.performQuery(query);
assertEquals(4, objects.size());
Object o1 = objects.get(0);
assertTrue("Expected Object[]: " + o1, o1 instanceof Object[]);
Object[] array1 = (Object[]) o1;
assertEquals(2, array1.length);
Object[] array2 = (Object[]) objects.get(1);
assertEquals(2, array2.length);
Object[] array3 = (Object[]) objects.get(2);
assertEquals(2, array3.length);
Object[] array4 = (Object[]) objects.get(3);
assertEquals(2, array3.length);
assertEquals(new Integer(1), array1[1]);
assertEquals(new Integer(1), array2[1]);
assertEquals(new Integer(0), array3[1]);
assertEquals(new Integer(0), array4[1]);
assertTrue("Unexpected DataObject: " + array1[0], array1[0] instanceof Artist);
}
Aggregations