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