Search in sources :

Example 86 with SQLTemplate

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

the class FlattenedRelationshipsIT method testUnsetJoinWithPK.

@Test
public void testUnsetJoinWithPK() throws Exception {
    createCircularJoinDataSet();
    SQLTemplate joinSelect = new SQLTemplate(FlattenedTest1.class, "SELECT * FROM COMPLEX_JOIN");
    joinSelect.setFetchingDataRows(true);
    assertEquals(3, context.performQuery(joinSelect).size());
    FlattenedTest1 ft1 = Cayenne.objectForPK(context, FlattenedTest1.class, 2);
    assertEquals("ft12", ft1.getName());
    List<FlattenedTest3> related = ft1.getFt3OverComplex();
    assertTrue(((ValueHolder) related).isFault());
    assertEquals(2, related.size());
    FlattenedTest3 ft3 = Cayenne.objectForPK(context, FlattenedTest3.class, 3);
    assertTrue(related.contains(ft3));
    ft1.removeFromFt3OverComplex(ft3);
    assertFalse(related.contains(ft3));
    context.commitChanges();
    // the thing here is that there are two join records between
    // FT1 and FT3 (emulating invalid data or extras in the join table that
    // are ignored in the object model).. all (2) joins must be deleted
    assertEquals(1, context.performQuery(joinSelect).size());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) FlattenedTest3(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest3) FlattenedTest1(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1) Test(org.junit.Test)

Example 87 with SQLTemplate

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

the class DataContextCharPKIT method testUpdate.

@Test
public void testUpdate() throws Exception {
    CharPkTestEntity object = context.newObject(CharPkTestEntity.class);
    object.setOtherCol("object-XYZ");
    object.setPkCol("PK1");
    context.commitChanges();
    object.setOtherCol("UPDATED");
    context.commitChanges();
    SQLTemplate q = new SQLTemplate(CharPkTestEntity.class, "SELECT * FROM CHAR_PK_TEST");
    q.setFetchingDataRows(true);
    List<?> rows = context.performQuery(q);
    assertNotNull(rows);
    assertEquals(1, rows.size());
    DataRow row = (DataRow) rows.get(0);
    Object val = row.get("OTHER_COL");
    if (val == null) {
        val = row.get("other_col");
    }
    assertEquals("UPDATED", val);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) CharPkTestEntity(org.apache.cayenne.testdo.compound.CharPkTestEntity) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 88 with SQLTemplate

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

the class DataContextCharPKIT method testInsert.

@Test
public void testInsert() throws Exception {
    CharPkTestEntity object = context.newObject(CharPkTestEntity.class);
    object.setOtherCol("object-XYZ");
    object.setPkCol("PK1");
    context.commitChanges();
    SQLTemplate q = new SQLTemplate(CharPkTestEntity.class, "SELECT * FROM CHAR_PK_TEST");
    q.setFetchingDataRows(true);
    List<?> rows = context.performQuery(q);
    assertNotNull(rows);
    assertEquals(1, rows.size());
    DataRow row = (DataRow) rows.get(0);
    Object val = row.get("OTHER_COL");
    if (val == null) {
        val = row.get("other_col");
    }
    assertEquals("object-XYZ", val);
    val = row.get("PK_COL");
    if (val == null) {
        val = row.get("pk_col");
    }
    assertEquals("PK1", val);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) CharPkTestEntity(org.apache.cayenne.testdo.compound.CharPkTestEntity) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 89 with SQLTemplate

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

the class DataContextDecoratedStackIT method testCommitDecorated.

@Test
public void testCommitDecorated() {
    DataDomain dd = runtime.getDataDomain();
    DataChannel decorator = new DataChannelDecorator(dd);
    DataContext context = (DataContext) runtime.newContext(decorator);
    Artist a = context.newObject(Artist.class);
    a.setArtistName("XXX");
    context.commitChanges();
    SQLTemplate query = new SQLTemplate(Artist.class, "select #result('count(1)' 'int' 'x') from ARTIST");
    query.setFetchingDataRows(true);
    query.setTemplate(FrontBaseAdapter.class.getName(), "select #result('COUNT(ARTIST_ID)' 'int' 'x') from ARTIST");
    query.setTemplate(OpenBaseAdapter.class.getName(), "select #result('COUNT(ARTIST_ID)' 'int' 'x') from ARTIST");
    Map<?, ?> count = (Map<?, ?>) Cayenne.objectForQuery(context, query);
    assertNotNull(count);
    assertEquals(new Integer(1), count.get("x"));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) DataChannel(org.apache.cayenne.DataChannel) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 90 with SQLTemplate

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

the class DataContextDeleteRulesIT method testCascadeToManyFlattened.

/**
 * Tests that deleting a source of a flattened relationship with CASCADE
 * rule results in deleting a join and a target.
 */
@Test
public void testCascadeToManyFlattened() {
    // testing Artist.groupArray relationship
    ArtGroup aGroup = context.newObject(ArtGroup.class);
    aGroup.setName("Group Name");
    Artist anArtist = context.newObject(Artist.class);
    anArtist.setArtistName("A Name");
    anArtist.addToGroupArray(aGroup);
    assertTrue(anArtist.getGroupArray().contains(aGroup));
    context.commitChanges();
    SQLTemplate checkQuery = new SQLTemplate(Artist.class, "SELECT * FROM ARTIST_GROUP");
    checkQuery.setFetchingDataRows(true);
    List<?> joins1 = context.performQuery(checkQuery);
    assertEquals(1, joins1.size());
    context.deleteObjects(anArtist);
    assertEquals(PersistenceState.DELETED, aGroup.getPersistenceState());
    assertFalse(anArtist.getGroupArray().contains(aGroup));
    context.commitChanges();
    List<?> joins2 = context.performQuery(checkQuery);
    assertEquals(0, joins2.size());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) 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