Search in sources :

Example 6 with SQLAction

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

the class SQLTemplateActionIT method testSelectSQLTimestamp.

@Test
public void testSelectSQLTimestamp() throws Exception {
    createFourArtists();
    String templateString = "SELECT #result('DATE_OF_BIRTH' 'java.sql.Timestamp' 'DOB') " + "FROM ARTIST WHERE ARTIST_ID = #bind($id)";
    SQLTemplate template = new SQLTemplate(Object.class, templateString);
    sqlTemplateCustomizer.updateSQLTemplate(template);
    Map<String, Object> bindings = new HashMap<>();
    bindings.put("id", 201);
    template.setParameters(bindings);
    SQLAction plan = adapter.getAction(template, node);
    MockOperationObserver observer = new MockOperationObserver();
    try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
        plan.performAction(c, observer);
    }
    List<DataRow> rows = observer.rowsForQuery(template);
    assertNotNull(rows);
    assertEquals(1, rows.size());
    DataRow row = rows.get(0);
    assertNotNull(row.get("DOB"));
    // Sybase returns a Timestamp subclass... so can't test equality
    assertTrue(java.sql.Timestamp.class.isAssignableFrom(row.get("DOB").getClass()));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) Connection(java.sql.Connection) SQLAction(org.apache.cayenne.query.SQLAction) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 7 with SQLAction

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

the class SQLTemplateActionIT method testExecuteUpdateNoParameters.

@Test
public void testExecuteUpdateNoParameters() throws Exception {
    createFourArtists();
    SQLTemplate template = new SQLTemplate(Object.class, "delete from ARTIST where ARTIST_NAME like 'a%'");
    SQLAction action = adapter.getAction(template, node);
    try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
        MockOperationObserver observer = new MockOperationObserver();
        action.performAction(c, observer);
        int[] batches = observer.countsForQuery(template);
        assertNotNull(batches);
        assertEquals(1, batches.length);
        assertEquals(4, batches[0]);
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Connection(java.sql.Connection) SQLAction(org.apache.cayenne.query.SQLAction) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 8 with SQLAction

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

the class DataNodeQueryAction method runQuery.

public void runQuery(Connection connection, final Query originalQuery) throws SQLException, Exception {
    // wrap to ensure that the result is mapped back to the original query, even if
    // the underlying SQLAction uses query substitute...
    OperationObserver wrapper = new OperationObserver() {

        @Override
        public void nextBatchCount(Query query, int[] resultCount) {
            observer.nextBatchCount(originalQuery, resultCount);
        }

        @Override
        public void nextCount(Query query, int resultCount) {
            observer.nextCount(originalQuery, resultCount);
        }

        @Override
        public void nextRows(Query query, List<?> dataRows) {
            observer.nextRows(originalQuery, dataRows);
        }

        @Override
        public void nextRows(Query q, ResultIterator it) {
            observer.nextRows(originalQuery, it);
        }

        @Override
        public void nextGeneratedRows(Query query, ResultIterator keys, ObjectId idToUpdate) {
            observer.nextGeneratedRows(originalQuery, keys, idToUpdate);
        }

        @Override
        public void nextGlobalException(Exception ex) {
            observer.nextGlobalException(ex);
        }

        @Override
        public void nextQueryException(Query query, Exception ex) {
            observer.nextQueryException(originalQuery, ex);
        }

        @Override
        public boolean isIteratedResult() {
            return observer.isIteratedResult();
        }
    };
    SQLAction action = node.getAdapter().getAction(originalQuery, node);
    action.performAction(connection, wrapper);
}
Also used : Query(org.apache.cayenne.query.Query) ObjectId(org.apache.cayenne.ObjectId) ResultIterator(org.apache.cayenne.ResultIterator) SQLAction(org.apache.cayenne.query.SQLAction) List(java.util.List) SQLException(java.sql.SQLException)

Aggregations

SQLAction (org.apache.cayenne.query.SQLAction)8 Connection (java.sql.Connection)7 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)7 SQLTemplate (org.apache.cayenne.query.SQLTemplate)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 DataRow (org.apache.cayenne.DataRow)5 Date (java.sql.Date)2 Query (org.apache.cayenne.query.Query)2 SQLException (java.sql.SQLException)1 List (java.util.List)1 ObjectId (org.apache.cayenne.ObjectId)1 ResultIterator (org.apache.cayenne.ResultIterator)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1