Search in sources :

Example 1 with DataRow

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

the class DataDomainQueryAction method polymorphicRowFromCache.

private DataRow polymorphicRowFromCache(EntityInheritanceTree superNode, Map<String, ?> idSnapshot) {
    for (EntityInheritanceTree child : superNode.getChildren()) {
        ObjectId id = new ObjectId(child.getEntity().getName(), idSnapshot);
        DataRow row = cache.getCachedSnapshot(id);
        if (row != null) {
            return row;
        }
        row = polymorphicRowFromCache(child, idSnapshot);
        if (row != null) {
            return row;
        }
    }
    return null;
}
Also used : EntityInheritanceTree(org.apache.cayenne.map.EntityInheritanceTree) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow)

Example 2 with DataRow

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

the class SelectActionIT method testFetchLimit_DistinctResultIterator.

@Test
public void testFetchLimit_DistinctResultIterator() throws Exception {
    if (accessStackAdapter.supportsLobs()) {
        insertClobDb();
        Expression qual = ExpressionFactory.exp("clobValue.value = 100");
        SelectQuery select = new SelectQuery(ClobTestEntity.class, qual);
        select.setFetchLimit(25);
        List<DataRow> resultRows = context.performQuery(select);
        assertNotNull(resultRows);
        assertEquals(25, resultRows.size());
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 3 with DataRow

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

the class DataRowDeserializer method readMap.

@Override
public Object readMap(AbstractHessianInput in) throws IOException {
    int size = in.readInt();
    DataRow row = new DataRow(size);
    try {
        versionField.set(row, Long.valueOf(in.readLong()));
    } catch (Exception e) {
        throw new IOException("Error reading 'version' field");
    }
    row.setReplacesVersion(in.readLong());
    in.addRef(row);
    while (!in.isEnd()) {
        row.put((String) in.readObject(), in.readObject());
    }
    in.readEnd();
    return row;
}
Also used : IOException(java.io.IOException) DataRow(org.apache.cayenne.DataRow) IOException(java.io.IOException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 4 with DataRow

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

the class DataDomainFlushObserver method nextGeneratedRows.

/**
 * Processes generated keys.
 *
 * @since 1.2
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void nextGeneratedRows(Query query, ResultIterator<?> keysIterator, List<ObjectId> idsToUpdate) {
    // read and close the iterator before doing anything else
    List<DataRow> keys;
    try {
        keys = (List<DataRow>) keysIterator.allRows();
    } finally {
        keysIterator.close();
    }
    if (!(query instanceof InsertBatchQuery)) {
        throw new CayenneRuntimeException("Generated keys only supported for InsertBatchQuery, instead got %s", query);
    }
    if (keys.size() != idsToUpdate.size()) {
        throw new CayenneRuntimeException("Mismatching number of generated PKs: expected %d, instead got %d", idsToUpdate.size(), keys.size());
    }
    for (int i = 0; i < keys.size(); i++) {
        DataRow key = keys.get(i);
        // empty key?
        if (key.size() == 0) {
            throw new CayenneRuntimeException("Empty key generated.");
        }
        ObjectId idToUpdate = idsToUpdate.get(i);
        if (idToUpdate == null || !idToUpdate.isTemporary()) {
            // why would this happen?
            return;
        }
        BatchQuery batch = (BatchQuery) query;
        for (DbAttribute attribute : batch.getDbEntity().getGeneratedAttributes()) {
            // DB DEFAULT values. Ignore those.
            if (attribute.isPrimaryKey()) {
                Object value = key.get(attribute.getName());
                // identity result sets, so a data row may contain garbage labels.
                if (value == null) {
                    value = key.values().iterator().next();
                }
                // Log the generated PK
                logger.logGeneratedKey(attribute, value);
                // I guess we should override any existing value,
                // as generated key is the latest thing that exists in the DB.
                idToUpdate.getReplacementIdMap().put(attribute.getName(), value);
                break;
            }
        }
    }
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbAttribute(org.apache.cayenne.map.DbAttribute) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) BatchQuery(org.apache.cayenne.query.BatchQuery) DataRow(org.apache.cayenne.DataRow)

Example 5 with DataRow

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

the class DataRowUtilsIT method testMerge.

@Test
public void testMerge() throws Exception {
    createOneArtist();
    String n1 = "changed";
    String n2 = "changed again";
    Artist a1 = ObjectSelect.query(Artist.class).select(context).get(0);
    a1.setArtistName(n1);
    DataRow s2 = new DataRow(2);
    s2.put("ARTIST_NAME", n2);
    s2.put("DATE_OF_BIRTH", new java.util.Date());
    ClassDescriptor d = context.getEntityResolver().getClassDescriptor("Artist");
    DataRowUtils.mergeObjectWithSnapshot(context, d, a1, s2);
    // name was modified, so it should not change during merge
    assertEquals(n1, a1.getArtistName());
    // date of birth came from database, it should be updated during merge
    assertEquals(s2.get("DATE_OF_BIRTH"), a1.getDateOfBirth());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) 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