use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class EJBQLSelectTranslatorIT method testSelectPositionalParameters.
@Test
public void testSelectPositionalParameters() {
Map<Integer, Object> params = new HashMap<Integer, Object>();
params.put(new Integer(1), "X");
params.put(new Integer(2), "Y");
SQLTemplate query = translateSelect("select a from Artist a where a.artistName = ?1 or a.artistName = ?2", params);
String sql = query.getDefaultTemplate();
assertTrue(sql, sql.endsWith("t0.ARTIST_NAME = #bind($id0) OR t0.ARTIST_NAME = #bind($id1)"));
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class EJBQLSelectTranslatorIT method testSelectFromWhereLessOrEqual.
@Test
public void testSelectFromWhereLessOrEqual() {
SQLTemplate query = translateSelect("select p from Painting p where p.estimatedPrice <= 1.0");
String sql = query.getDefaultTemplate();
assertTrue(sql, sql.endsWith("WHERE t0.ESTIMATED_PRICE <= #bind($id0 'DECIMAL')"));
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class JointPrefetchIT method testJointPrefetchSQLTemplate.
@Test
public void testJointPrefetchSQLTemplate() throws Exception {
createJointPrefetchDataSet();
// correctly naming columns is the key..
SQLTemplate q = new SQLTemplate(Artist.class, "SELECT distinct " + "#result('ESTIMATED_PRICE' 'BigDecimal' '' 'paintingArray.ESTIMATED_PRICE'), " + "#result('PAINTING_TITLE' 'String' '' 'paintingArray.PAINTING_TITLE'), " + "#result('GALLERY_ID' 'int' '' 'paintingArray.GALLERY_ID'), " + "#result('PAINTING_ID' 'int' '' 'paintingArray.PAINTING_ID'), " + "#result('ARTIST_NAME' 'String'), " + "#result('DATE_OF_BIRTH' 'java.util.Date'), " + "#result('t0.ARTIST_ID' 'int' '' 'ARTIST_ID') " + "FROM ARTIST t0, PAINTING t1 " + "WHERE t0.ARTIST_ID = t1.ARTIST_ID");
q.addPrefetch(Artist.PAINTING_ARRAY.joint());
q.setFetchingDataRows(false);
@SuppressWarnings("unchecked") final List<Artist> objects = (List<Artist>) context.performQuery(q);
queryInterceptor.runWithQueriesBlocked(() -> {
// without OUTER join we will get fewer objects...
assertEquals(2, objects.size());
for (Artist a : objects) {
List<Painting> list = a.getPaintingArray();
assertNotNull(list);
assertFalse(((ValueHolder) list).isFault());
assertTrue(list.size() > 0);
for (Painting p : list) {
assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
// make sure properties are not null..
assertNotNull(p.getPaintingTitle());
}
}
});
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class AutoAdapterIT method testCreateSQLTemplateAction.
@Test
public void testCreateSQLTemplateAction() {
Provider<DbAdapter> adapterProvider = mock(Provider.class);
when(adapterProvider.get()).thenReturn(dataNode.getAdapter());
AutoAdapter autoAdapter = new AutoAdapter(adapterProvider, NoopJdbcEventLogger.getInstance());
SQLTemplateAction action = (SQLTemplateAction) autoAdapter.getAction(new SQLTemplate(Artist.class, "select * from artist"), dataNode);
// it is important for SQLTemplateAction to be used with unwrapped
// adapter, as the
// adapter class name is used as a key to the correct SQL template.
assertNotNull(action.getAdapter());
assertFalse(action.getAdapter() instanceof AutoAdapter);
assertSame(dataNode.getAdapter(), action.getAdapter());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextSQLTemplateCompoundIT method testBindObjectEqualCompound.
@Test
public void testBindObjectEqualCompound() throws Exception {
createTwoCompoundPKsAndCompoundFKsDataSet();
Map<String, String> pk = new HashMap<>();
pk.put(CompoundPkTestEntity.KEY1_PK_COLUMN, "a1");
pk.put(CompoundPkTestEntity.KEY2_PK_COLUMN, "a2");
CompoundPkTestEntity a = Cayenne.objectForPK(context, CompoundPkTestEntity.class, pk);
String template = "SELECT * FROM COMPOUND_FK_TEST t0" + " WHERE #bindObjectEqual($a [ 't0.F_KEY1', 't0.F_KEY2' ] [ 'KEY1', 'KEY2' ] ) ORDER BY PKEY";
SQLTemplate query = new SQLTemplate(CompoundFkTestEntity.class, template);
query.setColumnNamesCapitalization(CapsStrategy.UPPER);
query.setParams(Collections.singletonMap("a", a));
List<CompoundFkTestEntity> objects = context.performQuery(query);
assertEquals(1, objects.size());
CompoundFkTestEntity p = objects.get(0);
assertEquals(6, Cayenne.intPKForObject(p));
}
Aggregations