Search in sources :

Example 61 with DataRow

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));
    }
}
Also used : ArrayList(java.util.ArrayList) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 62 with DataRow

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;
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 63 with DataRow

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());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 64 with DataRow

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"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 65 with DataRow

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"));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Exhibit(org.apache.cayenne.testdo.testmap.Exhibit) Fault(org.apache.cayenne.Fault) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

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