use of org.apache.cayenne.DataRow in project cayenne by apache.
the class ResultScanParentAttachmentStrategy method indexParents.
private void indexParents() {
partitionByChild = new HashMap<>();
List<Persistent> objects = parentNode.getObjects();
// this can be null if parent node returned no rows
if (objects == null) {
return;
}
List<DataRow> rows = parentNode.getDataRows();
if (rows == null) {
return;
}
int size = objects.size();
for (int i = 0; i < size; i++) {
DataRow row = rows.get(i);
Object key;
if (joins.length > 1) {
List<Object> values = new ArrayList<>(joins.length);
for (int j = 0; j < joins.length; j++) {
values.add(row.get(joins[j].getSourceName()));
}
key = values;
} else {
key = row.get(joins[0].getSourceName());
}
List<Persistent> parents = partitionByChild.get(key);
if (parents == null) {
parents = new ArrayList<>();
partitionByChild.put(key, parents);
}
parents.add(objects.get(i));
}
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class HierarchicalObjectResolverNode method objectsFromDataRows.
@Override
List<Persistent> objectsFromDataRows(List<? extends DataRow> rows) {
if (rows == null || rows.size() == 0) {
return new ArrayList<>(1);
}
List<Persistent> results = new ArrayList<>(rows.size());
for (DataRow row : rows) {
// determine entity to use
ClassDescriptor classDescriptor = descriptorResolutionStrategy.descriptorForRow(row);
// not using DataRow.createObjectId for performance reasons -
// ObjectResolver
// has all needed metadata already cached.
ObjectId anId = createObjectId(row, classDescriptor.getEntity(), null);
Persistent object = objectFromDataRow(row, anId, classDescriptor);
if (object == null) {
throw new CayenneRuntimeException("Can't build Object from row: %s", row);
}
// keep the dupe objects (and data rows) around, as there maybe an
// attached
// joint prefetch...
results.add(object);
node.getParentAttachmentStrategy().linkToParent(row, object);
}
// now deal with snapshots
// TODO: refactoring: dupes will clutter the lists and cause extra
// processing...
// removal of dupes happens only downstream, as we need the objects
// matching
// fetched rows for joint prefetch resolving... maybe pushback unique
// and
// non-unique lists to the "node", instead of returning a single list
// from this
// method
cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
return results;
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataNodeQueriesIT method testPerfomQueriesSelectingSQLTemplate2.
@Test
public void testPerfomQueriesSelectingSQLTemplate2() throws Exception {
createFourArtists();
String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = new SQLTemplate(Object.class, template);
sqlTemplateCustomizer.updateSQLTemplate(query);
MockOperationObserver observer = new MockOperationObserver();
node.performQueries(Collections.singletonList((Query) query), observer);
List<DataRow> data = observer.rowsForQuery(query);
assertEquals(4, data.size());
DataRow row = data.get(2);
assertEquals(3, row.size());
Number id = (Number) row.get("ARTIST_ID");
assertNotNull(id);
assertEquals(201, id.intValue());
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataNodeQueriesIT method testPerfomQueriesSelectingSQLTemplateAlias.
@Test
public void testPerfomQueriesSelectingSQLTemplateAlias() throws Exception {
createFourArtists();
String template = "SELECT #result('ARTIST_ID' 'int' 'A') FROM ARTIST ORDER BY ARTIST_ID";
Query query = new SQLTemplate(Object.class, template);
MockOperationObserver observer = new MockOperationObserver();
node.performQueries(Collections.singletonList(query), observer);
List<DataRow> data = observer.rowsForQuery(query);
assertEquals(4, data.size());
DataRow row = data.get(2);
assertEquals(1, row.size());
assertEquals(201, row.get("A"));
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextIT method testCurrentSnapshotWithToOneFault.
/**
* Testing snapshot with to-one fault. This was a bug CAY-96.
*/
@Test
public void testCurrentSnapshotWithToOneFault() throws Exception {
createGalleriesAndExhibitsDataSet();
// Exhibit with Gallery as Fault must still include Gallery
// Artist and Exhibit (Exhibit has unresolved to-one to gallery as in
// the
// CAY-96 bug report)
ObjectId eId = ObjectId.of("Exhibit", Exhibit.EXHIBIT_ID_PK_COLUMN, 2);
Exhibit e = (Exhibit) context.performQuery(new ObjectIdQuery(eId)).get(0);
assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
DataRow snapshot = context.currentSnapshot(e);
// assert that after taking a snapshot, we have FK in, but the
// relationship
// is still a Fault
assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
assertEquals(33002, snapshot.get("GALLERY_ID"));
}
Aggregations