Search in sources :

Example 26 with DataObject

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

the class Expression_ParamsTest method testInParameter.

/**
 * Tests how parameter substitution algorithm works on an expression with no
 * parameters.
 */
@Test
public void testInParameter() throws Exception {
    Expression inExp = ExpressionFactory.exp("k1 in $test");
    Expression e1 = ExpressionFactory.exp("k1 in ('a', 'b')");
    Expression transformed = inExp.params(Collections.singletonMap("test", new Object[] { "a", "b" }));
    TstTraversalHandler.compareExps(e1, transformed);
    // just in case manually check params
    DataObject o1 = new CayenneDataObject();
    o1.writePropertyDirectly("k1", "a");
    assertTrue(transformed.match(o1));
    DataObject o2 = new CayenneDataObject();
    o2.writePropertyDirectly("k1", "x");
    assertFalse(transformed.match(o2));
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) Test(org.junit.Test)

Example 27 with DataObject

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

the class CayenneSQLTemplateProcessorTest method testProcessTemplateID.

@Test
public void testProcessTemplateID() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($helper.cayenneExp($a, 'db:ID_COLUMN'))";
    DataObject dataObject = new CayenneDataObject();
    dataObject.setObjectId(new ObjectId("T", "ID_COLUMN", 5));
    Map<String, Object> map = Collections.singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 = ?", compiled.getSql());
    assertEquals(1, compiled.getBindings().length);
    assertBindingValue(5, compiled.getBindings()[0]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 28 with DataObject

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

the class DataContext method objectFromDataRow.

/**
 * Creates a DataObject from DataRow. This variety of the
 * 'objectFromDataRow' method is normally used for generic classes.
 *
 * @see DataRow
 * @since 3.1
 */
public DataObject objectFromDataRow(String entityName, DataRow dataRow) {
    ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entityName);
    List<?> list = objectsFromDataRows(descriptor, Collections.singletonList(dataRow));
    return (DataObject) list.get(0);
}
Also used : DataObject(org.apache.cayenne.DataObject) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor)

Example 29 with DataObject

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

the class DataDomainSyncBucket method postprocess.

void postprocess() {
    if (!objectsByDescriptor.isEmpty()) {
        CompoundDiff result = parent.getResultDiff();
        Map<ObjectId, DataRow> modifiedSnapshots = parent.getResultModifiedSnapshots();
        Collection<ObjectId> deletedIds = parent.getResultDeletedIds();
        for (Map.Entry<ClassDescriptor, List<Persistent>> entry : objectsByDescriptor.entrySet()) {
            ClassDescriptor descriptor = entry.getKey();
            for (Persistent object : entry.getValue()) {
                ObjectId id = object.getObjectId();
                ObjectId finalId;
                // record id change and update attributes for generated ids
                if (id.isReplacementIdAttached()) {
                    Map<String, Object> replacement = id.getReplacementIdMap();
                    for (AttributeProperty property : descriptor.getIdProperties()) {
                        Object value = replacement.get(property.getAttribute().getDbAttributeName());
                        // if the id wasn't generated. We may need to optimize it...
                        if (value != null) {
                            property.writePropertyDirectly(object, null, value);
                        }
                    }
                    ObjectId replacementId = id.createReplacementId();
                    result.add(new NodeIdChangeOperation(id, replacementId));
                    // DataRowCache has no notion of replaced id...
                    if (!id.isTemporary()) {
                        deletedIds.add(id);
                    }
                    finalId = replacementId;
                } else if (id.isTemporary()) {
                    throw new CayenneRuntimeException("Temporary ID hasn't been replaced on commit: %s", object);
                } else {
                    finalId = id;
                }
                // do not take the snapshot until generated columns are processed (see code above)
                DataRow dataRow = parent.getContext().currentSnapshot(object);
                if (object instanceof DataObject) {
                    DataObject dataObject = (DataObject) object;
                    dataRow.setReplacesVersion(dataObject.getSnapshotVersion());
                    dataObject.setSnapshotVersion(dataRow.getVersion());
                }
                modifiedSnapshots.put(finalId, dataRow);
                // update Map reverse relationships
                for (ArcProperty arc : descriptor.getMapArcProperties()) {
                    ToManyMapProperty reverseArc = (ToManyMapProperty) arc.getComplimentaryReverseArc();
                    // must resolve faults... hopefully for to-one this will not cause extra fetches...
                    Object source = arc.readProperty(object);
                    if (source != null && !reverseArc.isFault(source)) {
                        remapTarget(reverseArc, source, object);
                    }
                }
            }
        }
    }
}
Also used : NodeIdChangeOperation(org.apache.cayenne.graph.NodeIdChangeOperation) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ToManyMapProperty(org.apache.cayenne.reflect.ToManyMapProperty) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) DataObject(org.apache.cayenne.DataObject) ArrayList(java.util.ArrayList) List(java.util.List) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) Map(java.util.Map) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 30 with DataObject

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

the class DataRowStore method snapshotsUpdatedForObjects.

/**
 * Updates cached snapshots for the list of objects.
 *
 * @since 1.2
 */
void snapshotsUpdatedForObjects(List<Persistent> objects, List<? extends DataRow> snapshots, boolean refresh) {
    int size = objects.size();
    // sanity check
    if (size != snapshots.size()) {
        throw new IllegalArgumentException("Counts of objects and corresponding snapshots do not match. " + "Objects count: " + objects.size() + ", snapshots count: " + snapshots.size());
    }
    Map<ObjectId, DataRow> modified = null;
    Object eventPostedBy = null;
    for (int i = 0; i < size; i++) {
        Persistent object = objects.get(i);
        // skip null objects... possible since 3.0 in some EJBQL results
        if (object == null) {
            continue;
        }
        // skip HOLLOW objects as they likely were created from partial snapshots
        if (object.getPersistenceState() == PersistenceState.HOLLOW) {
            continue;
        }
        ObjectId oid = object.getObjectId();
        // add snapshots if refresh is forced, or if a snapshot is
        // missing
        DataRow cachedSnapshot = this.snapshots.get(oid);
        if (refresh || cachedSnapshot == null) {
            DataRow newSnapshot = snapshots.get(i);
            if (cachedSnapshot != null) {
                // use old snapshot if no changes occurred
                if (object instanceof DataObject && cachedSnapshot.equals(newSnapshot)) {
                    ((DataObject) object).setSnapshotVersion(cachedSnapshot.getVersion());
                    continue;
                } else {
                    newSnapshot.setReplacesVersion(cachedSnapshot.getVersion());
                }
            }
            if (modified == null) {
                modified = new HashMap<>();
                eventPostedBy = object.getObjectContext().getGraphManager();
            }
            modified.put(oid, newSnapshot);
        }
    }
    if (modified != null) {
        processSnapshotChanges(eventPostedBy, modified, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) DataObject(org.apache.cayenne.DataObject) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Aggregations

DataObject (org.apache.cayenne.DataObject)38 Test (org.junit.Test)27 ObjectId (org.apache.cayenne.ObjectId)18 DataRow (org.apache.cayenne.DataRow)15 Persistent (org.apache.cayenne.Persistent)8 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)7 Artist (org.apache.cayenne.testdo.testmap.Artist)7 CayenneDataObject (org.apache.cayenne.CayenneDataObject)6 SelectQuery (org.apache.cayenne.query.SelectQuery)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)4 Expression (org.apache.cayenne.exp.Expression)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)3 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)3 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 MockDataObject (org.apache.cayenne.MockDataObject)2 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)2