Search in sources :

Example 31 with Query

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

the class DataContextDelegateIT method testWillPerformGenericQueryBlocked.

@Test
public void testWillPerformGenericQueryBlocked() throws Exception {
    final List<Query> queriesPerformed = new ArrayList<Query>(1);
    DataContextDelegate delegate = new MockDataContextDelegate() {

        @Override
        public Query willPerformGenericQuery(DataContext context, Query query) {
            queriesPerformed.add(query);
            return null;
        }
    };
    context.setDelegate(delegate);
    MockQuery query = new MockQuery();
    context.performGenericQuery(query);
    assertTrue("Delegate is not notified of a query being run.", queriesPerformed.contains(query));
    assertEquals(1, queriesPerformed.size());
    assertFalse("Delegate couldn't block the query.", query.isRouteCalled());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Query(org.apache.cayenne.query.Query) MockQuery(org.apache.cayenne.query.MockQuery) ArrayList(java.util.ArrayList) MockQuery(org.apache.cayenne.query.MockQuery) Test(org.junit.Test)

Example 32 with Query

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

the class ChildDiffLoader method findObject.

protected Persistent findObject(Object nodeId) {
    // first do a lookup in ObjectStore; if even a hollow object is found,
    // return it;
    // if not - fetch.
    Persistent object = (Persistent) context.getGraphManager().getNode(nodeId);
    if (object != null) {
        return object;
    }
    ObjectId id = (ObjectId) nodeId;
    // modified
    if (id.isTemporary()) {
        return null;
    }
    // skip context cache lookup, go directly to its channel
    Query query = new ObjectIdQuery((ObjectId) nodeId);
    QueryResponse response = context.getChannel().onQuery(context, query);
    List<?> objects = response.firstList();
    if (objects.size() == 0) {
        throw new CayenneRuntimeException("No object for ID exists: %s", nodeId);
    } else if (objects.size() > 1) {
        throw new CayenneRuntimeException("Expected zero or one object, instead query matched: %d", objects.size());
    }
    return (Persistent) objects.get(0);
}
Also used : ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) ObjectId(org.apache.cayenne.ObjectId) QueryResponse(org.apache.cayenne.QueryResponse) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 33 with Query

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

the class ServerRuntimeTest method testGetDataChannel_CustomModule.

@Test
public void testGetDataChannel_CustomModule() {
    final DataChannel channel = new DataChannel() {

        public EntityResolver getEntityResolver() {
            return null;
        }

        public EventManager getEventManager() {
            return null;
        }

        public QueryResponse onQuery(ObjectContext originatingContext, Query query) {
            return null;
        }

        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            return null;
        }
    };
    Module module = binder -> binder.bind(DataChannel.class).toInstance(channel);
    ServerRuntime runtime = new ServerRuntime(Collections.singleton(module));
    assertSame(channel, runtime.getChannel());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) DataContext(org.apache.cayenne.access.DataContext) Collection(java.util.Collection) Constants(org.apache.cayenne.configuration.Constants) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) List(java.util.List) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) EntityResolver(org.apache.cayenne.map.EntityResolver) EventManager(org.apache.cayenne.event.EventManager) Arrays.asList(java.util.Arrays.asList) GraphDiff(org.apache.cayenne.graph.GraphDiff) QueryResponse(org.apache.cayenne.QueryResponse) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) TransactionalOperation(org.apache.cayenne.tx.TransactionalOperation) Assert(org.junit.Assert) Collections(java.util.Collections) Key(org.apache.cayenne.di.Key) Mockito.mock(org.mockito.Mockito.mock) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 34 with Query

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

the class SQLTemplateActionIT method testExecuteUpdateBatch.

@Test
public void testExecuteUpdateBatch() throws Exception {
    String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH) " + "VALUES (#bind($id), #bind($name), #bind($dob 'DATE'))";
    SQLTemplate template = new SQLTemplate(Object.class, templateString);
    Map<String, Object> bindings1 = new HashMap<>();
    bindings1.put("id", new Long(1));
    bindings1.put("name", "a1");
    bindings1.put("dob", new Date(System.currentTimeMillis()));
    Map<String, Object> bindings2 = new HashMap<>();
    bindings2.put("id", new Long(33));
    bindings2.put("name", "a$$$$$");
    bindings2.put("dob", new Date(System.currentTimeMillis()));
    template.setParameters(new Map[] { bindings1, bindings2 });
    SQLAction genericAction = adapter.getAction(template, node);
    assertTrue(genericAction instanceof SQLTemplateAction);
    SQLTemplateAction action = (SQLTemplateAction) genericAction;
    assertSame(node, action.dataNode);
    assertSame(template, action.getQuery());
    try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
        MockOperationObserver observer = new MockOperationObserver();
        action.performAction(c, observer);
        int[] batches = observer.countsForQuery(template);
        assertNotNull(batches);
        assertEquals(2, batches.length);
        assertEquals(1, batches[0]);
        assertEquals(1, batches[1]);
    }
    MockOperationObserver observer = new MockOperationObserver();
    SelectQuery query = new SelectQuery(Artist.class);
    query.addOrdering("db:ARTIST_ID", SortOrder.ASCENDING);
    node.performQueries(Collections.singletonList((Query) query), observer);
    List<DataRow> data = observer.rowsForQuery(query);
    assertEquals(2, data.size());
    DataRow row1 = data.get(0);
    assertEquals(bindings1.get("id"), row1.get("ARTIST_ID"));
    assertEquals(bindings1.get("name"), row1.get("ARTIST_NAME"));
    // to compare dates we need to create the binding correctly
    // assertEquals(bindings1.get("dob"), row.get("DATE_OF_BIRTH"));
    DataRow row2 = data.get(1);
    assertEquals(bindings2.get("id"), row2.get("ARTIST_ID"));
    assertEquals(bindings2.get("name"), row2.get("ARTIST_NAME"));
// to compare dates we need to create the binding correctly
// assertEquals(bindings2.get("dob"), row2.get("DATE_OF_BIRTH"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Query(org.apache.cayenne.query.Query) HashMap(java.util.HashMap) Connection(java.sql.Connection) DataRow(org.apache.cayenne.DataRow) Date(java.sql.Date) SelectQuery(org.apache.cayenne.query.SelectQuery) SQLAction(org.apache.cayenne.query.SQLAction) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 35 with Query

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

the class SelectQueryDescriptorTest method testGetQueryProperties.

@Test
public void testGetQueryProperties() throws Exception {
    SelectQueryDescriptor builder = QueryDescriptor.selectQueryDescriptor();
    builder.setRoot("FakeRoot");
    builder.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, "5");
    builder.setProperty(QueryMetadata.STATEMENT_FETCH_SIZE_PROPERTY, "6");
    Query query = builder.buildQuery();
    assertTrue(query instanceof SelectQuery);
    assertEquals(5, ((SelectQuery) query).getFetchLimit());
    assertEquals(6, ((SelectQuery) query).getStatementFetchSize());
// TODO: test other properties...
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) SelectQuery(org.apache.cayenne.query.SelectQuery) Query(org.apache.cayenne.query.Query) Test(org.junit.Test)

Aggregations

Query (org.apache.cayenne.query.Query)43 Test (org.junit.Test)25 SelectQuery (org.apache.cayenne.query.SelectQuery)15 SQLTemplate (org.apache.cayenne.query.SQLTemplate)14 ArrayList (java.util.ArrayList)10 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)8 DataRow (org.apache.cayenne.DataRow)6 QueryResponse (org.apache.cayenne.QueryResponse)6 MockQuery (org.apache.cayenne.query.MockQuery)6 List (java.util.List)5 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)5 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 ObjectContext (org.apache.cayenne.ObjectContext)4 ObjectId (org.apache.cayenne.ObjectId)4 Persistent (org.apache.cayenne.Persistent)4 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)4 Map (java.util.Map)3 DbEntity (org.apache.cayenne.map.DbEntity)3 EntityResolver (org.apache.cayenne.map.EntityResolver)3