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));
}
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]);
}
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);
}
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);
}
}
}
}
}
}
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());
}
}
Aggregations