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