use of org.apache.cayenne.ResultIterator in project cayenne by apache.
the class MixedResultIncrementalFaultList method fillIn.
@Override
protected void fillIn(final Query query, List<Object> elementsList) {
elementsList.clear();
try (ResultIterator it = dataContext.performIteratedQuery(query)) {
while (it.hasNextRow()) {
elementsList.add(it.nextRow());
}
}
unfetchedObjects = elementsList.size();
}
use of org.apache.cayenne.ResultIterator in project cayenne by apache.
the class BatchAction method processGeneratedKeys.
/**
* Implements generated keys extraction supported in JDBC 3.0 specification.
*
* @since 4.0
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void processGeneratedKeys(Statement statement, OperationObserver observer, BatchQueryRow row) throws SQLException, CayenneException {
ResultSet keysRS = statement.getGeneratedKeys();
// TODO: andrus, 7/4/2007 - (1) get the type of meaningful PK's from
// their
// ObjAttributes; (2) use a different form of Statement.execute -
// "execute(String,String[])" to be able to map generated column names
// (this way
// we can support multiple columns.. although need to check how well
// this works
// with most common drivers)
RowDescriptorBuilder builder = new RowDescriptorBuilder();
if (this.keyRowDescriptor == null) {
// attempt to figure out the right descriptor from the mapping...
Collection<DbAttribute> generated = query.getDbEntity().getGeneratedAttributes();
if (generated.size() == 1) {
DbAttribute key = generated.iterator().next();
ColumnDescriptor[] columns = new ColumnDescriptor[1];
// use column name from result set, but type and Java class from
// DB
// attribute
columns[0] = new ColumnDescriptor(keysRS.getMetaData(), 1);
columns[0].setJdbcType(key.getType());
columns[0].setJavaClass(TypesMapping.getJavaBySqlType(key.getType()));
builder.setColumns(columns);
} else {
builder.setResultSet(keysRS);
}
this.keyRowDescriptor = builder.getDescriptor(dataNode.getAdapter().getExtendedTypes());
}
RowReader<?> rowReader = dataNode.rowReader(keyRowDescriptor, query.getMetaData(dataNode.getEntityResolver()), Collections.<ObjAttribute, ColumnDescriptor>emptyMap());
ResultIterator iterator = new JDBCResultIterator(null, keysRS, rowReader);
observer.nextGeneratedRows(query, iterator, row.getObjectId());
}
use of org.apache.cayenne.ResultIterator in project cayenne by apache.
the class DataNodeQueryAction method runQuery.
public void runQuery(Connection connection, final Query originalQuery) throws SQLException, Exception {
// wrap to ensure that the result is mapped back to the original query, even if
// the underlying SQLAction uses query substitute...
OperationObserver wrapper = new OperationObserver() {
@Override
public void nextBatchCount(Query query, int[] resultCount) {
observer.nextBatchCount(originalQuery, resultCount);
}
@Override
public void nextCount(Query query, int resultCount) {
observer.nextCount(originalQuery, resultCount);
}
@Override
public void nextRows(Query query, List<?> dataRows) {
observer.nextRows(originalQuery, dataRows);
}
@Override
public void nextRows(Query q, ResultIterator it) {
observer.nextRows(originalQuery, it);
}
@Override
public void nextGeneratedRows(Query query, ResultIterator keys, ObjectId idToUpdate) {
observer.nextGeneratedRows(originalQuery, keys, idToUpdate);
}
@Override
public void nextGlobalException(Exception ex) {
observer.nextGlobalException(ex);
}
@Override
public void nextQueryException(Query query, Exception ex) {
observer.nextQueryException(originalQuery, ex);
}
@Override
public boolean isIteratedResult() {
return observer.isIteratedResult();
}
};
SQLAction action = node.getAdapter().getAction(originalQuery, node);
action.performAction(connection, wrapper);
}
use of org.apache.cayenne.ResultIterator in project cayenne by apache.
the class IncrementalFaultList method fillIn.
/**
* Performs initialization of the list of objects. Only the first page is
* fully resolved. For the rest of the list, only ObjectIds are read.
*
* @since 3.0
*/
protected void fillIn(final Query query, List<Object> elementsList) {
elementsList.clear();
try (ResultIterator it = dataContext.performIteratedQuery(query)) {
while (it.hasNextRow()) {
elementsList.add(it.nextRow());
}
}
unfetchedObjects = elementsList.size();
}
use of org.apache.cayenne.ResultIterator in project cayenne by apache.
the class SQLTemplateAction method processSelectResult.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void processSelectResult(SQLStatement compiled, Connection connection, Statement statement, ResultSet resultSet, OperationObserver callback, final long startTime) throws Exception {
boolean iteratedResult = callback.isIteratedResult();
ExtendedTypeMap types = dataNode.getAdapter().getExtendedTypes();
RowDescriptorBuilder builder = configureRowDescriptorBuilder(compiled, resultSet);
RowReader<?> rowReader = dataNode.rowReader(builder.getDescriptor(types), queryMetadata);
ResultIterator it = new JDBCResultIterator(statement, resultSet, rowReader);
if (iteratedResult) {
it = new ConnectionAwareResultIterator(it, connection) {
@Override
protected void doClose() {
dataNode.getJdbcEventLogger().logSelectCount(rowCounter, System.currentTimeMillis() - startTime);
super.doClose();
}
};
}
it = new LimitResultIterator(it, getFetchOffset(), query.getFetchLimit());
if (iteratedResult) {
try {
callback.nextRows(query, it);
} catch (Exception ex) {
it.close();
throw ex;
}
} else {
// note that we are not closing the iterator here, relying on caller
// to close the underlying ResultSet on its own... this is a hack,
// maybe a cleaner flow is due here.
List<DataRow> resultRows = (List<DataRow>) it.allRows();
dataNode.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis() - startTime);
callback.nextRows(query, resultRows);
}
}
Aggregations