use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextCharPKIT method testDelete.
@Test
public void testDelete() throws Exception {
CharPkTestEntity object = context.newObject(CharPkTestEntity.class);
object.setOtherCol("object-XYZ");
object.setPkCol("PK1");
context.commitChanges();
context.deleteObjects(object);
context.commitChanges();
SQLTemplate q = new SQLTemplate(CharPkTestEntity.class, "SELECT * FROM CHAR_PK_TEST");
q.setFetchingDataRows(true);
List<?> rows = context.performQuery(q);
assertNotNull(rows);
assertEquals(0, rows.size());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextDeleteRulesIT method testNullifyToManyFlattened.
/**
* Tests that deleting a source of a flattened relationship with NULLIFY
* rule results in deleting a join together with the object deleted.
*/
@Test
public void testNullifyToManyFlattened() {
// testing ArtGroup.artistArray relationship
ArtGroup aGroup = context.newObject(ArtGroup.class);
aGroup.setName("Group Name");
Artist anArtist = context.newObject(Artist.class);
anArtist.setArtistName("A Name");
aGroup.addToArtistArray(anArtist);
context.commitChanges();
// Preconditions
assertTrue(aGroup.getArtistArray().contains(anArtist));
assertTrue(anArtist.getGroupArray().contains(aGroup));
SQLTemplate checkQuery = new SQLTemplate(Artist.class, "SELECT * FROM ARTIST_GROUP");
checkQuery.setFetchingDataRows(true);
List<?> joins1 = context.performQuery(checkQuery);
assertEquals(1, joins1.size());
context.deleteObjects(aGroup);
assertFalse(anArtist.getGroupArray().contains(aGroup));
context.commitChanges();
List<?> joins2 = context.performQuery(checkQuery);
assertEquals(0, joins2.size());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextEJBQLFunctionalExpressionsIT method testTRIM.
@Test
public void testTRIM() {
// insert via a SQL template to prevent adapter trimming and such...
QueryChain inserts = new QueryChain();
inserts.addQuery(new SQLTemplate(Artist.class, "INSERT INTO ARTIST (ARTIST_ID,ARTIST_NAME) VALUES(1, ' A')"));
inserts.addQuery(new SQLTemplate(Artist.class, "INSERT INTO ARTIST (ARTIST_ID,ARTIST_NAME) VALUES(2, 'A ')"));
context.performGenericQuery(inserts);
Artist a1 = Cayenne.objectForPK(context, Artist.class, 1);
Artist a2 = Cayenne.objectForPK(context, Artist.class, 2);
EJBQLQuery query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(a.artistName) = 'A'");
List<?> objects = context.performQuery(query);
assertEquals(2, objects.size());
assertTrue(objects.contains(a1));
assertTrue(objects.contains(a2));
query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(LEADING FROM a.artistName) = 'A'");
objects = context.performQuery(query);
// this is fuzzy cause some DB trim trailing data by default
assertTrue(objects.size() == 1 || objects.size() == 2);
assertTrue(objects.contains(a1));
query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(TRAILING FROM a.artistName) = 'A'");
objects = context.performQuery(query);
assertEquals(1, objects.size());
assertTrue(objects.contains(a2));
query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(BOTH FROM a.artistName) = 'A'");
objects = context.performQuery(query);
assertEquals(2, objects.size());
assertTrue(objects.contains(a1));
assertTrue(objects.contains(a2));
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class CayenneIT method testScalarObjectForQuery.
@Test
public void testScalarObjectForQuery() throws Exception {
createTwoArtists();
String sql = "SELECT count(1) AS X FROM ARTIST";
DataMap map = context.getEntityResolver().getDataMap("testmap");
SQLTemplate query = new SQLTemplate(map, sql, false);
query.setTemplate(FrontBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) AS X FROM ARTIST");
query.setTemplate(OpenBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) AS X FROM ARTIST");
query.setColumnNamesCapitalization(CapsStrategy.UPPER);
SQLResult rsMap = new SQLResult();
rsMap.addColumnResult("X");
query.setResult(rsMap);
Object object = Cayenne.objectForQuery(context, query);
assertNotNull(object);
assertTrue(object instanceof Number);
assertEquals(2, ((Number) object).intValue());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class GenericMappingIT method testSelect.
@Test
public void testSelect() {
context.performNonSelectingQuery(new SQLTemplate("Generic1", "INSERT INTO GENERIC1 (ID, NAME) VALUES (1, 'AAAA')"));
context.performNonSelectingQuery(new SQLTemplate("Generic1", "INSERT INTO GENERIC1 (ID, NAME) VALUES (2, 'BBBB')"));
context.performNonSelectingQuery(new SQLTemplate("Generic1", "INSERT INTO GENERIC2 (GENERIC1_ID, ID, NAME) VALUES (1, 1, 'CCCCC')"));
Expression qual = ExpressionFactory.matchExp("name", "AAAA");
SelectQuery q = new SelectQuery("Generic1", qual);
List<?> result = context.performQuery(q);
assertEquals(1, result.size());
}
Aggregations