@Override public Long inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final EntitySqlDao<M, E> sqlDao = entitySqlDaoWrapperFactory.become(sqlDaoClazz); // TODO lame cast, but couldn't make sqlDaoClazz a Class<? extends S> final Iterator<M> dumbIterator = paginationIteratorBuilder.build((S) sqlDao, 1L); // Make sure to go through the results to close the connection while (dumbIterator.hasNext()) { dumbIterator.next(); } return sqlDao.getFoundRows(context); } });
public <E extends Entity, M extends EntityModelDao<E>, S extends EntitySqlDao<M, E>> Pagination<M> getPagination(final Class<? extends EntitySqlDao<M, E>> sqlDaoClazz, final PaginationIteratorBuilder<M, E, S> paginationIteratorBuilder, final Long offset, final Long limit, final InternalTenantContext context) { // Note: the connection will be busy as we stream the results out: hence we cannot use // SQL_CALC_FOUND_ROWS / FOUND_ROWS on the actual query. // We still need to know the actual number of results, mainly for the UI so that it knows if it needs to fetch // more pages. To do that, we perform a dummy search query with SQL_CALC_FOUND_ROWS (but limit 1). final Long count = transactionalSqlDao.execute(new EntitySqlDaoTransactionWrapper<Long>() { @Override public Long inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final EntitySqlDao<M, E> sqlDao = entitySqlDaoWrapperFactory.become(sqlDaoClazz); // TODO lame cast, but couldn't make sqlDaoClazz a Class<? extends S> final Iterator<M> dumbIterator = paginationIteratorBuilder.build((S) sqlDao, 1L); // Make sure to go through the results to close the connection while (dumbIterator.hasNext()) { dumbIterator.next(); } return sqlDao.getFoundRows(context); } }); // We usually always want to wrap our queries in an EntitySqlDaoTransactionWrapper... except here. // Since we want to stream the results out, we don't want to auto-commit when this method returns. final EntitySqlDao<M, E> sqlDao = transactionalSqlDao.onDemand(sqlDaoClazz); final Long totalCount = sqlDao.getCount(context); final Iterator<M> results = paginationIteratorBuilder.build((S) sqlDao, limit); return new DefaultPagination<M>(offset, limit, count, totalCount, results); }