Search in sources :

Example 46 with SQLTemplate

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

the class DataNodeQueriesIT method testRunMultiLineSQLTemplateUNIX.

@Test
public void testRunMultiLineSQLTemplateUNIX() throws Exception {
    String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME)" + "\n" + "VALUES (1, 'A')";
    Query template = new SQLTemplate(Object.class, templateString);
    node.performQueries(Collections.singletonList(template), new MockOperationObserver());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) Test(org.junit.Test)

Example 47 with SQLTemplate

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

the class DataContextExtrasIT method testSelectException.

/**
 * Testing behavior of Cayenne when a database exception is thrown in SELECT query.
 */
@Test
public void testSelectException() {
    SQLTemplate q = new SQLTemplate(Artist.class, "SELECT * FROM NON_EXISTENT_TABLE");
    try {
        context.performGenericQuery(q);
        fail("Query was invalid and was supposed to fail.");
    } catch (RuntimeException ex) {
    // exception expected
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Test(org.junit.Test)

Example 48 with SQLTemplate

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

the class DataContextIT method testPerformNonSelectingQueryCounts2.

@Test
public void testPerformNonSelectingQueryCounts2() throws Exception {
    createArtistsDataSet();
    SQLTemplate query = new SQLTemplate(Painting.class, "INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ARTIST_ID, ESTIMATED_PRICE) " + "VALUES ($pid, '$pt', $aid, #bind($price 'DECIMAL' 2))");
    Map<String, Object>[] maps = new Map[3];
    for (int i = 0; i < maps.length; i++) {
        maps[i] = new HashMap<>();
        maps[i].put("pid", new Integer(1 + i));
        maps[i].put("pt", "P-" + i);
        maps[i].put("aid", new Integer(33002));
        maps[i].put("price", new BigDecimal("1." + i));
    }
    // single batch of parameters
    query.setParameters(maps);
    int[] counts = context.performNonSelectingQuery(query);
    assertNotNull(counts);
    assertEquals(maps.length, counts.length);
    for (int i = 0; i < maps.length; i++) {
        assertEquals(1, counts[i]);
    }
    SQLTemplate delete = new SQLTemplate(Painting.class, "delete from PAINTING");
    counts = context.performNonSelectingQuery(delete);
    assertNotNull(counts);
    assertEquals(1, counts.length);
    assertEquals(3, counts[0]);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 49 with SQLTemplate

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

the class DataContextObjectIdQueryIT method testRefreshNullifiedValuesNew.

@Test
public void testRefreshNullifiedValuesNew() {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    a.setDateOfBirth(new Date());
    context.commitChanges();
    context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET DATE_OF_BIRTH = NULL"));
    long id = Cayenne.longPKForObject(a);
    ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, id), false, ObjectIdQuery.CACHE_REFRESH);
    Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
    assertNull(a1.getDateOfBirth());
    assertEquals("X", a1.getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) ObjectId(org.apache.cayenne.ObjectId) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Date(java.util.Date) Test(org.junit.Test)

Example 50 with SQLTemplate

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

the class DataContextObjectIdQueryIT method testRefreshNullifiedValuesExisting.

@Test
public void testRefreshNullifiedValuesExisting() {
    SQLTemplate insert = new SQLTemplate(Artist.class, "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH) VALUES (44, 'X', #bind($date 'DATE'))");
    insert.setParameters(Collections.singletonMap("date", new Date()));
    context.performGenericQuery(insert);
    Artist a = Cayenne.objectForPK(context, Artist.class, 44l);
    assertNotNull(a.getDateOfBirth());
    assertEquals("X", a.getArtistName());
    context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET DATE_OF_BIRTH = NULL"));
    ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 44l), false, ObjectIdQuery.CACHE_REFRESH);
    Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
    assertNull(a1.getDateOfBirth());
    assertEquals("X", a1.getArtistName());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Date(java.util.Date) 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