Search in sources :

Example 81 with DataRow

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

the class ProcedureAction method readProcedureOutParameters.

/**
 * Helper method that reads OUT parameters of a CallableStatement.
 */
protected void readProcedureOutParameters(CallableStatement statement, OperationObserver delegate) throws SQLException, Exception {
    long t1 = System.currentTimeMillis();
    // build result row...
    DataRow result = null;
    List<ProcedureParameter> parameters = getProcedure().getCallParameters();
    for (int i = 0; i < parameters.size(); i++) {
        ProcedureParameter parameter = parameters.get(i);
        if (!parameter.isOutParam()) {
            continue;
        }
        if (result == null) {
            result = new DataRow(2);
        }
        ColumnDescriptor descriptor = new ColumnDescriptor(parameter);
        ExtendedType type = dataNode.getAdapter().getExtendedTypes().getRegisteredType(descriptor.getJavaClass());
        Object val = type.materializeObject(statement, i + 1, descriptor.getJdbcType());
        result.put(descriptor.getDataRowKey(), val);
    }
    if (result != null && !result.isEmpty()) {
        // treat out parameters as a separate data row set
        dataNode.getJdbcEventLogger().logSelectCount(1, System.currentTimeMillis() - t1);
        delegate.nextRows(query, Collections.singletonList(result));
    }
}
Also used : ProcedureParameter(org.apache.cayenne.map.ProcedureParameter) ExtendedType(org.apache.cayenne.access.types.ExtendedType) DataRow(org.apache.cayenne.DataRow)

Example 82 with DataRow

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

the class EntityInheritanceTreeTest method testEntityMatchingRow_SingleTableInheritance.

@Test
public void testEntityMatchingRow_SingleTableInheritance() {
    DataMap dataMap = new DataMap("map");
    DbEntity dbEntity = new DbEntity("e1");
    dbEntity.addAttribute(new DbAttribute("x"));
    dataMap.addDbEntity(dbEntity);
    ObjEntity entity = new ObjEntity("E1");
    entity.setDbEntityName(dbEntity.getName());
    entity.setDeclaredQualifier(ExpressionFactory.matchDbExp("x", 2));
    dataMap.addObjEntity(entity);
    ObjEntity subEntity = new ObjEntity("E2");
    subEntity.setSuperEntityName("E1");
    subEntity.setDeclaredQualifier(ExpressionFactory.matchDbExp("x", 1));
    dataMap.addObjEntity(subEntity);
    // creating EntityInheritanceTree via EntityResolver to ensure the entities are indexed
    EntityResolver resolver = new EntityResolver(Collections.singleton(dataMap));
    EntityInheritanceTree t1 = resolver.getInheritanceTree("E1");
    DataRow row11 = new DataRow(5);
    row11.put("x", 1);
    DataRow row12 = new DataRow(5);
    row12.put("x", 2);
    DataRow row13 = new DataRow(5);
    row13.put("x", 3);
    assertSame(subEntity, t1.entityMatchingRow(row11));
    assertSame(entity, t1.entityMatchingRow(row12));
    assertNull(t1.entityMatchingRow(row13));
}
Also used : DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 83 with DataRow

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

the class EntityInheritanceTreeTest method testEntityMatchingRow_CAY_2693.

@Test
public void testEntityMatchingRow_CAY_2693() {
    DataMap dataMap = new DataMap("map");
    DbEntity dbEntity = new DbEntity("a");
    dbEntity.addAttribute(new DbAttribute("type"));
    dataMap.addDbEntity(dbEntity);
    ObjEntity entityA = new ObjEntity("A");
    entityA.setAbstract(true);
    entityA.setDbEntityName(dbEntity.getName());
    dataMap.addObjEntity(entityA);
    // name it AC so it would be sorted before B
    ObjEntity subEntityC = new ObjEntity("AC");
    subEntityC.setSuperEntityName("A");
    subEntityC.setAbstract(true);
    dataMap.addObjEntity(subEntityC);
    ObjEntity subEntityB = new ObjEntity("B");
    subEntityB.setSuperEntityName("A");
    subEntityB.setDeclaredQualifier(ExpressionFactory.matchDbExp("type", 0));
    dataMap.addObjEntity(subEntityB);
    ObjEntity subEntityD = new ObjEntity("D");
    subEntityD.setSuperEntityName("AC");
    subEntityD.setDeclaredQualifier(ExpressionFactory.matchDbExp("type", 1));
    dataMap.addObjEntity(subEntityD);
    ObjEntity subEntityE = new ObjEntity("E");
    subEntityE.setSuperEntityName("AC");
    subEntityE.setDeclaredQualifier(ExpressionFactory.matchDbExp("type", 2));
    dataMap.addObjEntity(subEntityE);
    // creating EntityInheritanceTree via EntityResolver to ensure the entities are indexed
    EntityResolver resolver = new EntityResolver(Collections.singleton(dataMap));
    EntityInheritanceTree treeA = resolver.getInheritanceTree("A");
    EntityInheritanceTree treeC = resolver.getInheritanceTree("AC");
    DataRow row11 = new DataRow(5);
    row11.put("type", 0);
    DataRow row12 = new DataRow(5);
    row12.put("type", 1);
    DataRow row13 = new DataRow(5);
    row13.put("type", 2);
    assertSame(subEntityB, treeA.entityMatchingRow(row11));
    assertSame(subEntityD, treeA.entityMatchingRow(row12));
    assertSame(subEntityE, treeA.entityMatchingRow(row13));
    assertSame(subEntityD, treeC.entityMatchingRow(row12));
    assertSame(subEntityE, treeC.entityMatchingRow(row13));
    assertNull(treeA.qualifierForEntityAndSubclasses());
    assertEquals(ExpressionFactory.exp("(db:type = 1) or (db:type = 2)"), treeC.qualifierForEntityAndSubclasses());
}
Also used : DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 84 with DataRow

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

the class DataContext method currentSnapshot.

/**
 * Returns a DataRow reflecting current, possibly uncommitted, object state.
 * <p>
 * <strong>Warning:</strong> This method will return a partial snapshot if
 * an object or one of its related objects that propagate their keys to this
 * object have temporary ids. DO NOT USE this method if you expect a DataRow
 * to represent a complete object state.
 * </p>
 *
 * @since 1.1
 */
public DataRow currentSnapshot(final Persistent object) {
    // for a HOLLOW object return snapshot from cache
    if (object.getPersistenceState() == PersistenceState.HOLLOW && object.getObjectContext() != null) {
        return getObjectStore().getSnapshot(object.getObjectId());
    }
    ObjEntity entity = getEntityResolver().getObjEntity(object);
    final ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
    final DataRow snapshot = new DataRow(10);
    snapshot.setEntityName(entity.getName());
    descriptor.visitProperties(new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            ObjAttribute objAttr = property.getAttribute();
            // processing compound attributes correctly
            snapshot.put(objAttr.getDbAttributePath(), property.readPropertyDirectly(object));
            return true;
        }

        public boolean visitToMany(ToManyProperty property) {
            // do nothing
            return true;
        }

        public boolean visitToOne(ToOneProperty property) {
            ObjRelationship rel = property.getRelationship();
            // if target doesn't propagates its key value, skip it
            if (rel.isSourceIndependentFromTargetChange()) {
                return true;
            }
            Object targetObject = property.readPropertyDirectly(object);
            if (targetObject == null) {
                return true;
            }
            // to avoid unneeded fault triggering
            if (targetObject instanceof Fault) {
                DataRow storedSnapshot = getObjectStore().getSnapshot(object.getObjectId());
                if (storedSnapshot == null) {
                    throw new CayenneRuntimeException("No matching objects found for ObjectId %s" + ". Object may have been deleted externally.", object.getObjectId());
                }
                DbRelationship dbRel = rel.getDbRelationships().get(0);
                for (DbJoin join : dbRel.getJoins()) {
                    String key = join.getSourceName();
                    snapshot.put(key, storedSnapshot.get(key));
                }
                return true;
            }
            // target is resolved and we have an FK->PK to it,
            // so extract it from target...
            Persistent target = (Persistent) targetObject;
            Map<String, Object> idParts = target.getObjectId().getIdSnapshot();
            // this method.
            if (idParts.isEmpty()) {
                return true;
            }
            DbRelationship dbRel = rel.getDbRelationships().get(0);
            Map<String, Object> fk = dbRel.srcFkSnapshotWithTargetSnapshot(idParts);
            snapshot.putAll(fk);
            return true;
        }
    });
    // process object id map
    // we should ignore any object id values if a corresponding attribute
    // is a part of relationship "toMasterPK", since those values have been
    // set above when db relationships where processed.
    Map<String, Object> thisIdParts = object.getObjectId().getIdSnapshot();
    if (thisIdParts != null) {
        // put only those that do not exist in the map
        for (Map.Entry<String, Object> entry : thisIdParts.entrySet()) {
            String nextKey = entry.getKey();
            if (!snapshot.containsKey(nextKey)) {
                snapshot.put(nextKey, entry.getValue());
            }
        }
    }
    return snapshot;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjAttribute(org.apache.cayenne.map.ObjAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Fault(org.apache.cayenne.Fault) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ObjEntity(org.apache.cayenne.map.ObjEntity) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) DataObject(org.apache.cayenne.DataObject) Map(java.util.Map) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 85 with DataRow

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

the class ServerDataRowSerializer method writeObject.

@Override
public void writeObject(Object object, AbstractHessianOutput out) throws IOException {
    if (out.addRef(object)) {
        return;
    }
    DataRow row = (DataRow) object;
    out.writeMapBegin(DataRow.class.getName());
    out.writeInt(row.size());
    out.writeLong(row.getVersion());
    out.writeLong(row.getReplacesVersion());
    for (final Map.Entry<String, Object> entry : row.entrySet()) {
        out.writeObject(entry.getKey());
        out.writeObject(entry.getValue());
    }
    out.writeMapEnd();
}
Also used : DataRow(org.apache.cayenne.DataRow) Map(java.util.Map)

Aggregations

DataRow (org.apache.cayenne.DataRow)152 Test (org.junit.Test)113 Artist (org.apache.cayenne.testdo.testmap.Artist)31 ObjectId (org.apache.cayenne.ObjectId)25 DataObject (org.apache.cayenne.DataObject)20 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)20 SQLTemplate (org.apache.cayenne.query.SQLTemplate)18 Date (java.util.Date)14 HashMap (java.util.HashMap)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Persistent (org.apache.cayenne.Persistent)8 Painting (org.apache.cayenne.testdo.testmap.Painting)8 Connection (java.sql.Connection)6 Calendar (java.util.Calendar)5 Map (java.util.Map)5 QueryResponse (org.apache.cayenne.QueryResponse)5 DataMap (org.apache.cayenne.map.DataMap)5