Search in sources :

Example 81 with SQLTemplate

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

the class DataContextSharedCacheIT method testSnapshotChangePropagationOnSelect.

/**
 * Test case to prove that refreshing snapshots as a result of the database fetch will
 * be propagated across DataContexts.
 */
@Test
public void testSnapshotChangePropagationOnSelect() throws Exception {
    String originalName = artist.getArtistName();
    final String newName = "version2";
    // update artist using raw SQL
    SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) " + "WHERE ARTIST_NAME = #bind($oldName)");
    Map<String, Object> map = new HashMap<>(3);
    map.put("newName", newName);
    map.put("oldName", originalName);
    query.setParams(map);
    context.performNonSelectingQuery(query);
    // fetch updated artist into the new context, and see if the original
    // one gets updated
    Expression qual = ExpressionFactory.matchExp("artistName", newName);
    List artists = context1.performQuery(new SelectQuery<>(Artist.class, qual));
    assertEquals(1, artists.size());
    Artist altArtist = (Artist) artists.get(0);
    // check underlying cache
    DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(altArtist.getObjectId());
    assertNotNull(freshSnapshot);
    assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
    // check both artists
    assertEquals(newName, altArtist.getArtistName());
    ParallelTestContainer helper = new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertEquals("Peer object state wasn't refreshed on fetch", newName, artist.getArtistName());
        }
    };
    helper.runTest(3000);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) HashMap(java.util.HashMap) Expression(org.apache.cayenne.exp.Expression) List(java.util.List) DataRow(org.apache.cayenne.DataRow) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 82 with SQLTemplate

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

the class DataNodeQueriesIT method testRunMultiLineSQLTemplateMac.

@Test
public void testRunMultiLineSQLTemplateMac() throws Exception {
    String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME)" + "\r" + "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 83 with SQLTemplate

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

the class DataNodeQueriesIT method testPerfomQueriesSelectingSQLTemplate1.

@Test
public void testPerfomQueriesSelectingSQLTemplate1() throws Exception {
    createFourArtists();
    String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    MockOperationObserver observer = new MockOperationObserver();
    node.performQueries(Collections.singletonList((Query) query), observer);
    List<DataRow> data = observer.rowsForQuery(query);
    assertEquals(4, data.size());
    DataRow row = data.get(2);
    assertEquals(1, row.size());
    assertEquals(201, row.get("ARTIST_ID"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 84 with SQLTemplate

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

the class DataNodeQueriesIT method testRunMultiLineSQLTemplateWindows.

@Test
public void testRunMultiLineSQLTemplateWindows() throws Exception {
    String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME)" + "\r\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 85 with SQLTemplate

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

the class SQLTemplateDescriptor method buildQuery.

@Override
public SQLTemplate buildQuery() {
    SQLTemplate template = new SQLTemplate();
    if (root != null) {
        template.setRoot(root);
    }
    if (prefetchesMap != null) {
        for (Map.Entry<String, Integer> entry : prefetchesMap.entrySet()) {
            template.addPrefetch(PrefetchTreeNode.withPath(entry.getKey(), entry.getValue()));
        }
    }
    template.initWithProperties(this.getProperties());
    // init SQL
    template.setDefaultTemplate(this.getSql());
    Map<String, String> adapterSql = this.getAdapterSql();
    if (adapterSql != null) {
        for (Map.Entry<String, String> entry : adapterSql.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key != null && value != null) {
                template.setTemplate(key, value);
            }
        }
    }
    return template;
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Map(java.util.Map) HashMap(java.util.HashMap)

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