Search in sources :

Example 1 with BatchQuery

use of org.apache.cayenne.query.BatchQuery in project cayenne by apache.

the class DataDomainFlushAction method runQueries.

private void runQueries() {
    DataDomainFlushObserver observer = new DataDomainFlushObserver(domain.getJdbcEventLogger());
    try {
        DataNode lastNode = null;
        DbEntity lastEntity = null;
        int rangeStart = 0;
        int len = queries.size();
        for (int i = 0; i < len; i++) {
            BatchQuery query = (BatchQuery) queries.get(i);
            if (query.getDbEntity() != lastEntity) {
                lastEntity = query.getDbEntity();
                DataNode node = domain.lookupDataNode(lastEntity.getDataMap());
                if (node != lastNode) {
                    if (i - rangeStart > 0) {
                        lastNode.performQueries(queries.subList(rangeStart, i), observer);
                    }
                    rangeStart = i;
                    lastNode = node;
                }
            }
        }
        // process last segment of the query list...
        lastNode.performQueries(queries.subList(rangeStart, len), observer);
    } catch (Throwable th) {
        BaseTransaction.getThreadTransaction().setRollbackOnly();
        throw new CayenneRuntimeException("Transaction was rolledback.", th);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) BatchQuery(org.apache.cayenne.query.BatchQuery)

Example 2 with BatchQuery

use of org.apache.cayenne.query.BatchQuery in project cayenne by apache.

the class FlushObserver method nextGeneratedRows.

/**
 * Processes generated keys.
 */
@Override
@SuppressWarnings("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 3 with BatchQuery

use of org.apache.cayenne.query.BatchQuery 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 4 with BatchQuery

use of org.apache.cayenne.query.BatchQuery 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, ObjectId idToUpdate) {
    // 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 (idToUpdate == null || !idToUpdate.isTemporary()) {
        // why would this happen?
        return;
    }
    if (keys.size() != 1) {
        throw new CayenneRuntimeException("One and only one PK row is expected, instead got %d", keys.size());
    }
    DataRow key = keys.get(0);
    // empty key?
    if (key.size() == 0) {
        throw new CayenneRuntimeException("Empty key generated.");
    }
    // infer the key name and currently will only support a single column...
    if (key.size() > 1) {
        throw new CayenneRuntimeException("Only a single column autogenerated PK is supported. " + "Generated key: %s", key);
    }
    BatchQuery batch = (BatchQuery) query;
    for (DbAttribute attribute : batch.getDbEntity().getGeneratedAttributes()) {
        // DB DEFAULT values. Ignore those.
        if (attribute.isPrimaryKey()) {
            Object 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) 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 BatchQuery

use of org.apache.cayenne.query.BatchQuery in project cayenne by apache.

the class LegacyDataDomainFlushAction method runQueries.

private void runQueries() {
    DataDomainFlushObserver observer = new DataDomainFlushObserver(domain.getJdbcEventLogger());
    try {
        DataNode lastNode = null;
        DbEntity lastEntity = null;
        int rangeStart = 0;
        int len = queries.size();
        for (int i = 0; i < len; i++) {
            BatchQuery query = (BatchQuery) queries.get(i);
            if (query.getDbEntity() != lastEntity) {
                lastEntity = query.getDbEntity();
                DataNode node = domain.lookupDataNode(lastEntity.getDataMap());
                if (node != lastNode) {
                    if (i - rangeStart > 0) {
                        lastNode.performQueries(queries.subList(rangeStart, i), observer);
                    }
                    rangeStart = i;
                    lastNode = node;
                }
            }
        }
        // process last segment of the query list...
        lastNode.performQueries(queries.subList(rangeStart, len), observer);
    } catch (Throwable th) {
        BaseTransaction.getThreadTransaction().setRollbackOnly();
        throw new CayenneRuntimeException("Transaction was rolledback.", th);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) BatchQuery(org.apache.cayenne.query.BatchQuery)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)5 BatchQuery (org.apache.cayenne.query.BatchQuery)5 DataRow (org.apache.cayenne.DataRow)3 DbAttribute (org.apache.cayenne.map.DbAttribute)3 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)3 ObjectId (org.apache.cayenne.ObjectId)2 DbEntity (org.apache.cayenne.map.DbEntity)2