Search in sources :

Example 26 with SQLTemplate

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)"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 27 with SQLTemplate

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

Example 28 with SQLTemplate

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());
            }
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) List(java.util.List) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 29 with SQLTemplate

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

Example 30 with SQLTemplate

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));
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) CompoundFkTestEntity(org.apache.cayenne.testdo.compound.CompoundFkTestEntity) 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