Search in sources :

Example 1 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class StringIdQueryTest method testPerformQuery_SingleEntity.

@Test
public void testPerformQuery_SingleEntity() throws Exception {
    e1Helper.deleteAll();
    e1Helper.insert(3).insert(4);
    StringIdQuery query = new StringIdQuery("E1:3", "E1:4", "E1:5");
    QueryResponse response = runtime.newContext().performGenericQuery(query);
    assertEquals(1, response.size());
    assertEquals(2, response.firstList().size());
    Set<Number> ids = new HashSet<>();
    DataRow r1 = (DataRow) response.firstList().get(0);
    ids.add((Number) r1.get("ID"));
    DataRow r2 = (DataRow) response.firstList().get(1);
    ids.add((Number) r2.get("ID"));
    assertTrue(ids.contains(3L));
    assertTrue(ids.contains(4L));
}
Also used : QueryResponse(org.apache.cayenne.QueryResponse) DataRow(org.apache.cayenne.DataRow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class ClientChannel method onQuery.

@SuppressWarnings("unchecked")
public QueryResponse onQuery(ObjectContext context, Query query) {
    QueryResponse response = send(new QueryMessage(query), QueryResponse.class);
    if (context != null) {
        EntityResolver resolver = context.getEntityResolver();
        QueryMetadata info = query.getMetaData(resolver);
        if (!info.isFetchingDataRows()) {
            response.reset();
            while (response.next()) {
                if (response.isList()) {
                    List objects = response.currentList();
                    if (!objects.isEmpty()) {
                        DeepMergeOperation merger = new DeepMergeOperation(context);
                        List<Object> rsMapping = info.getResultSetMapping();
                        if (rsMapping == null) {
                            convertSingleObjects(objects, merger);
                        } else {
                            if (rsMapping.size() == 1) {
                                if (rsMapping.get(0) instanceof EntityResultSegment) {
                                    convertSingleObjects(objects, merger);
                                }
                            } else {
                                processMixedResult(objects, merger, rsMapping);
                            }
                        }
                    }
                }
            }
        }
    }
    return response;
}
Also used : QueryMetadata(org.apache.cayenne.query.QueryMetadata) DeepMergeOperation(org.apache.cayenne.util.DeepMergeOperation) QueryResponse(org.apache.cayenne.QueryResponse) List(java.util.List) EntityResolver(org.apache.cayenne.map.EntityResolver) EntityResultSegment(org.apache.cayenne.query.EntityResultSegment)

Example 3 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class DeletedDiffProcessor method nodeRemoved.

@Override
public void nodeRemoved(Object nodeId) {
    ObjectId id = (ObjectId) nodeId;
    final MutableObjectChange objectChangeSet = changeSet.getOrCreate(id, ObjectChangeType.DELETE);
    // TODO: rewrite with SelectById query after Cayenne upgrade
    ObjectIdQuery query = new ObjectIdQuery(id, true, ObjectIdQuery.CACHE);
    QueryResponse result = channel.onQuery(null, query);
    @SuppressWarnings("unchecked") List<DataRow> rows = result.firstList();
    if (rows.isEmpty()) {
        LOGGER.warn("No DB snapshot for object to be deleted, no changes will be recorded. ID: " + id);
        return;
    }
    final DataRow row = rows.get(0);
    ClassDescriptor descriptor = channel.getEntityResolver().getClassDescriptor(id.getEntityName());
    final CommitLogEntity entity = entityFactory.getEntity(id);
    descriptor.visitProperties(new PropertyVisitor() {

        @Override
        public boolean visitAttribute(AttributeProperty property) {
            if (!entity.isIncluded(property.getName())) {
                return true;
            }
            Object value;
            if (entity.isConfidential(property.getName())) {
                value = Confidential.getInstance();
            } else {
                String key = property.getAttribute().getDbAttributeName();
                value = row.get(key);
            }
            if (value != null) {
                objectChangeSet.attributeChanged(property.getName(), value, null);
            }
            return true;
        }

        @Override
        public boolean visitToOne(ToOneProperty property) {
            // TODO record FK changes?
            return true;
        }

        @Override
        public boolean visitToMany(ToManyProperty property) {
            return true;
        }
    });
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) CommitLogEntity(org.apache.cayenne.commitlog.meta.CommitLogEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) QueryResponse(org.apache.cayenne.QueryResponse) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 4 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class ClientServerChannelIT method testPerformQueryObjectIDInjection.

@Test
public void testPerformQueryObjectIDInjection() throws Exception {
    tMtTable1.insert(55, "g1", "s1");
    Query query = new SelectQuery("MtTable1");
    QueryResponse response = clientServerChannel.onQuery(null, query);
    assertNotNull(response);
    List<?> results = response.firstList();
    assertNotNull(results);
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertTrue(result instanceof ClientMtTable1);
    ClientMtTable1 clientObject = (ClientMtTable1) result;
    assertNotNull(clientObject.getObjectId());
    assertEquals(new ObjectId("MtTable1", MtTable1.TABLE1_ID_PK_COLUMN, 55), clientObject.getObjectId());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) SelectQuery(org.apache.cayenne.query.SelectQuery) MockQuery(org.apache.cayenne.query.MockQuery) Query(org.apache.cayenne.query.Query) ObjectId(org.apache.cayenne.ObjectId) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 5 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class ClientServerChannelIT method testPerformQueryValuePropagation.

@Test
public void testPerformQueryValuePropagation() throws Exception {
    byte[] bytes = new byte[] { 1, 2, 3 };
    tMtTable3.insert(1, bytes, "abc", 4);
    Query query = new SelectQuery("MtTable3");
    QueryResponse response = clientServerChannel.onQuery(null, query);
    assertNotNull(response);
    List<?> results = response.firstList();
    assertNotNull(results);
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertTrue("Result is of wrong type: " + result, result instanceof ClientMtTable3);
    ClientMtTable3 clientObject = (ClientMtTable3) result;
    assertEquals("abc", clientObject.getCharColumn());
    assertEquals(new Integer(4), clientObject.getIntColumn());
    assertTrue(new EqualsBuilder().append(clientObject.getBinaryColumn(), bytes).isEquals());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable3(org.apache.cayenne.testdo.mt.ClientMtTable3) SelectQuery(org.apache.cayenne.query.SelectQuery) MockQuery(org.apache.cayenne.query.MockQuery) Query(org.apache.cayenne.query.Query) QueryResponse(org.apache.cayenne.QueryResponse) EqualsBuilder(org.apache.cayenne.util.EqualsBuilder) Test(org.junit.Test)

Aggregations

QueryResponse (org.apache.cayenne.QueryResponse)31 Test (org.junit.Test)24 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)12 SelectQuery (org.apache.cayenne.query.SelectQuery)9 ObjectId (org.apache.cayenne.ObjectId)7 Query (org.apache.cayenne.query.Query)6 DataRow (org.apache.cayenne.DataRow)5 ObjectContext (org.apache.cayenne.ObjectContext)5 EntityResolver (org.apache.cayenne.map.EntityResolver)5 List (java.util.List)3 CayenneContext (org.apache.cayenne.CayenneContext)3 MockPersistentObject (org.apache.cayenne.MockPersistentObject)3 MockEventManager (org.apache.cayenne.event.MockEventManager)3 DataMap (org.apache.cayenne.map.DataMap)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)3 HashSet (java.util.HashSet)2 DataChannelFilter (org.apache.cayenne.DataChannelFilter)2 DataChannelFilterChain (org.apache.cayenne.DataChannelFilterChain)2 MockDataChannelFilter (org.apache.cayenne.MockDataChannelFilter)2