Search in sources :

Example 51 with SQLTemplate

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

Example 52 with SQLTemplate

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

Example 53 with SQLTemplate

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

Example 54 with SQLTemplate

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());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 55 with SQLTemplate

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());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) 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