use of siena.core.options.QueryOptionState in project siena by mandubian.
the class GaeQueryUtils method paginate.
public static <T> void paginate(QueryData<T> query) {
QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext) query.option(QueryOptionGaeContext.ID);
QueryOptionState state = (QueryOptionState) query.option(QueryOptionState.ID);
if (gaeCtx == null) {
gaeCtx = new QueryOptionGaeContext();
query.options().put(gaeCtx.type, gaeCtx);
}
// resets the realoffset to 0 if stateless
if (state.isStateless()) {
gaeCtx.realOffset = 0;
}
}
use of siena.core.options.QueryOptionState in project siena by mandubian.
the class GaeQueryUtils method previousPage.
public static <T> void previousPage(QueryData<T> query) {
QueryOptionPage pag = (QueryOptionPage) query.option(QueryOptionPage.ID);
QueryOptionState state = (QueryOptionState) query.option(QueryOptionState.ID);
QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext) query.option(QueryOptionGaeContext.ID);
if (gaeCtx == null) {
gaeCtx = new QueryOptionGaeContext();
query.options().put(gaeCtx.type, gaeCtx);
}
// if no more data before, doesn't try to go before
if (gaeCtx.noMoreDataBefore) {
return;
}
// if no more data after, removes flag to be able to go before
if (gaeCtx.noMoreDataAfter) {
gaeCtx.noMoreDataAfter = false;
}
if (pag.isPaginating()) {
gaeCtx.realPageSize = pag.pageSize;
if (state.isStateless()) {
//if(offset.isActive()){
if (gaeCtx.realOffset >= pag.pageSize) {
gaeCtx.realOffset -= pag.pageSize;
} else {
gaeCtx.realOffset = 0;
gaeCtx.noMoreDataBefore = true;
}
//}
} else {
if (!gaeCtx.isActive()) {
if (!gaeCtx.useCursor) {
//if(offset.isActive()){
if (gaeCtx.realOffset >= pag.pageSize) {
gaeCtx.realOffset -= pag.pageSize;
} else {
gaeCtx.realOffset = 0;
gaeCtx.noMoreDataBefore = true;
}
//}
} else // if the cursor is active, verifies this is not the first page
// with the offset (active or passive) and sets noMoreData in this case
{
//QueryOptionOffset offset = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
if (gaeCtx.realOffset == 0) {
gaeCtx.noMoreDataBefore = true;
}
}
} else {
QueryOptionOffset offset = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
if (!gaeCtx.useCursor) {
//if(offset.isActive()){
if (gaeCtx.realOffset >= pag.pageSize) {
gaeCtx.realOffset -= pag.pageSize;
} else // passivates offset and computes the page before because we are at the first page
{
offset.passivate();
gaeCtx.noMoreDataBefore = true;
previousPage(query);
}
//}
} else {
String cursor = gaeCtx.previousCursor();
// and recall the previousPage with the offset mechanism
if (cursor == null) {
offset.activate();
gaeCtx.useCursor = false;
previousPage(query);
} else {
offset.passivate();
gaeCtx.useCursor = true;
if (gaeCtx.realOffset >= pag.pageSize) {
gaeCtx.realOffset -= pag.pageSize;
} else // passivates offset and computes the page before because we are at the first page
{
gaeCtx.noMoreDataBefore = true;
previousPage(query);
}
//previousPage(query);
}
}
}
}
} else {
// throws exception because it's impossible to reuse nextPage when paginating has been interrupted, the cases are too many
throw new SienaException("Can't use nextPage after pagination has been interrupted...");
}
}
use of siena.core.options.QueryOptionState in project siena by mandubian.
the class JdbcPersistenceManager method doFetch.
private <T> List<T> doFetch(Query<T> query, int limit, int offset) {
QueryOptionJdbcContext jdbcCtx = (QueryOptionJdbcContext) query.option(QueryOptionJdbcContext.ID);
if (jdbcCtx == null) {
jdbcCtx = new QueryOptionJdbcContext();
query.customize(jdbcCtx);
}
// activates page and offset options as there are always used in SQL requests
QueryOptionPage pag = (QueryOptionPage) query.option(QueryOptionPage.ID);
if (!pag.isPaginating()) {
if (pag.isActive()) {
if (limit != Integer.MAX_VALUE) {
jdbcCtx.realPageSize = limit;
} else {
jdbcCtx.realPageSize = pag.pageSize;
}
} else {
jdbcCtx.realPageSize = limit;
}
} else {
jdbcCtx.realPageSize = pag.pageSize;
}
QueryOptionOffset offsetOpt = (QueryOptionOffset) (query.option(QueryOptionOffset.ID));
// if local offset has been set, uses it
if (offset != 0) {
offsetOpt.activate();
offsetOpt.offset = offset;
}
QueryOptionState state = (QueryOptionState) query.option(QueryOptionState.ID);
// if previousPage has detected there is no more data, simply returns an empty list
if (jdbcCtx.noMoreDataBefore) {
return new ArrayList<T>();
}
if (state.isStateless() || (state.isStateful() && !jdbcCtx.isActive()) || (state.isStateful() && jdbcCtx.isActive() && jdbcCtx.isClosed())) {
if (state.isStateless()) {
if (pag.isPaginating()) {
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
} else {
// if page is active, immediately passivates it not to keep is active
if (pag.isActive()) {
pag.passivate();
}
if (offsetOpt.isActive()) {
jdbcCtx.realOffset = offsetOpt.offset;
offsetOpt.passivate();
} else {
jdbcCtx.realOffset = 0;
}
}
} else {
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
}
Class<T> clazz = query.getQueriedClass();
List<Object> parameters = new ArrayList<Object>();
StringBuilder sql = JdbcDBUtils.buildSqlSelect(query);
appendSqlWhere(query, sql, parameters);
JdbcDBUtils.appendSqlOrder(query, sql);
JdbcDBUtils.appendSqlLimitOffset(query, sql, parameters);
//sql.append(suffix);
PreparedStatement statement = null;
ResultSet rs = null;
try {
statement = createStatement(sql.toString(), parameters);
if (pag.isPaginating()) {
// this is just a hint to the DB so wonder if it should be used
statement.setFetchSize(jdbcCtx.realPageSize);
}
rs = statement.executeQuery();
List<T> result = JdbcMappingUtils.mapList(clazz, rs, ClassInfo.getClassInfo(clazz).tableName, JdbcMappingUtils.getJoinFields(query), jdbcCtx.realPageSize);
if (pag.isPaginating()) {
if (result.size() == 0) {
jdbcCtx.noMoreDataAfter = true;
} else {
jdbcCtx.noMoreDataAfter = false;
}
} else {
if (state.isStateful()) {
jdbcCtx.realOffset += result.size();
}
}
if (state.isStateless()) {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, statement);
} else {
Integer offsetParamIdx = parameters.size();
Integer limitParamIdx = offsetParamIdx - 1;
// store indexes of offset and limit for reuse
jdbcCtx.activate();
jdbcCtx.statement = statement;
jdbcCtx.limitParamIdx = limitParamIdx;
jdbcCtx.offsetParamIdx = offsetParamIdx;
}
return result;
} catch (SQLException e) {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, statement);
throw new SienaException(e);
}
} else {
// payload has been initialized so goes on
Class<T> clazz = query.getQueriedClass();
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
ResultSet rs = null;
try {
// when paginating, should update limit and offset
//if(pag.isActive()){
// update limit and offset
jdbcCtx.statement.setObject(jdbcCtx.limitParamIdx, jdbcCtx.realPageSize);
//}
//if(offsetOpt.isActive()){
jdbcCtx.statement.setObject(jdbcCtx.offsetParamIdx, jdbcCtx.realOffset);
//}
rs = jdbcCtx.statement.executeQuery();
List<T> result = JdbcMappingUtils.mapList(clazz, rs, ClassInfo.getClassInfo(clazz).tableName, JdbcMappingUtils.getJoinFields(query), jdbcCtx.realPageSize);
if (pag.isPaginating()) {
if (result.size() == 0) {
jdbcCtx.noMoreDataAfter = true;
} else {
jdbcCtx.noMoreDataAfter = false;
}
} else {
jdbcCtx.realOffset += result.size();
}
return result;
} catch (SQLException ex) {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, jdbcCtx.statement);
throw new SienaException(ex);
}
}
}
use of siena.core.options.QueryOptionState in project siena by mandubian.
the class JdbcPersistenceManager method doIter.
private <T> Iterable<T> doIter(Query<T> query, int limit, int offset) {
QueryOptionJdbcContext jdbcCtx = (QueryOptionJdbcContext) query.option(QueryOptionJdbcContext.ID);
if (jdbcCtx == null) {
jdbcCtx = new QueryOptionJdbcContext();
query.customize(jdbcCtx);
}
// activates page and offset options as there are always used in SQL requests
QueryOptionPage pag = (QueryOptionPage) query.option(QueryOptionPage.ID);
// else {
if (!pag.isPaginating()) {
if (pag.isActive()) {
if (limit != Integer.MAX_VALUE) {
jdbcCtx.realPageSize = limit;
} else {
jdbcCtx.realPageSize = pag.pageSize;
}
} else {
jdbcCtx.realPageSize = limit;
}
} else {
jdbcCtx.realPageSize = pag.pageSize;
}
// }
QueryOptionOffset offsetOpt = (QueryOptionOffset) (query.option(QueryOptionOffset.ID));
// if local offset has been set, uses it
if (offset != 0) {
offsetOpt.activate();
offsetOpt.offset = offset;
}
QueryOptionState state = (QueryOptionState) query.option(QueryOptionState.ID);
// if previousPage has detected there is no more data, simply returns an empty list
if (jdbcCtx.noMoreDataBefore) {
return new ArrayList<T>();
}
if (state.isStateless() || (state.isStateful() && !jdbcCtx.isActive())) {
if (state.isStateless()) {
if (pag.isPaginating()) {
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
} else {
// if page is active, immediately passivates it not to keep is active
if (pag.isActive()) {
pag.passivate();
}
if (offsetOpt.isActive()) {
jdbcCtx.realOffset = offsetOpt.offset;
offsetOpt.passivate();
} else {
jdbcCtx.realOffset = 0;
}
}
} else {
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
}
List<Object> parameters = new ArrayList<Object>();
StringBuilder sql = JdbcDBUtils.buildSqlSelect(query);
appendSqlWhere(query, sql, parameters);
JdbcDBUtils.appendSqlOrder(query, sql);
JdbcDBUtils.appendSqlLimitOffset(query, sql, parameters);
//sql.append(suffix);
PreparedStatement statement = null;
ResultSet rs = null;
try {
statement = createStatement(sql.toString(), parameters);
if (pag.isActive()) {
// this is just a hint to the DB so wonder if it should be used
statement.setFetchSize(jdbcCtx.realPageSize);
}
rs = statement.executeQuery();
if (state.isStateless()) {
//in iteration, doesn't close the resultset to reuse it
//JdbcDBUtils.closeResultSet(rs);
//JdbcDBUtils.closeStatement(statement);
} else {
Integer offsetParamIdx = parameters.size();
Integer limitParamIdx = offsetParamIdx - 1;
// store indexes of offset and limit for reuse
jdbcCtx.activate();
jdbcCtx.statement = statement;
jdbcCtx.limitParamIdx = limitParamIdx;
jdbcCtx.offsetParamIdx = offsetParamIdx;
}
return new JdbcSienaIterable<T>(this, statement, rs, query);
} catch (SQLException e) {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, statement);
throw new SienaException(e);
}
} else {
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
// payload has been initialized so goes on
try {
// when paginating, should update limit and offset
//if(pag.isActive()){
// update limit and offset
jdbcCtx.statement.setObject(jdbcCtx.limitParamIdx, jdbcCtx.realPageSize);
//}
//if(offsetOpt.isActive()){
jdbcCtx.statement.setObject(jdbcCtx.offsetParamIdx, jdbcCtx.realOffset);
//}
ResultSet rs = jdbcCtx.statement.executeQuery();
return new JdbcSienaIterable<T>(this, jdbcCtx.statement, rs, query);
} catch (SQLException ex) {
JdbcDBUtils.closeStatementAndConnection(this, jdbcCtx.statement);
throw new SienaException(ex);
}
}
}
use of siena.core.options.QueryOptionState in project siena by mandubian.
the class JdbcPersistenceManager method doFetchKeys.
private <T> List<T> doFetchKeys(Query<T> query, int limit, int offset) {
QueryOptionJdbcContext jdbcCtx = (QueryOptionJdbcContext) query.option(QueryOptionJdbcContext.ID);
if (jdbcCtx == null) {
jdbcCtx = new QueryOptionJdbcContext();
query.customize(jdbcCtx);
}
// activates page and offset options as there are always used in SQL requests
QueryOptionPage pag = (QueryOptionPage) query.option(QueryOptionPage.ID);
if (!pag.isPaginating()) {
if (pag.isActive()) {
if (limit != Integer.MAX_VALUE) {
jdbcCtx.realPageSize = limit;
} else {
jdbcCtx.realPageSize = pag.pageSize;
}
} else {
jdbcCtx.realPageSize = limit;
}
} else {
jdbcCtx.realPageSize = pag.pageSize;
}
QueryOptionOffset offsetOpt = (QueryOptionOffset) (query.option(QueryOptionOffset.ID));
// if local offset has been set, uses it
if (offset != 0) {
offsetOpt.activate();
offsetOpt.offset = offset;
}
QueryOptionState state = (QueryOptionState) query.option(QueryOptionState.ID);
// if previousPage has detected there is no more data, simply returns an empty list
if (jdbcCtx.noMoreDataBefore) {
return new ArrayList<T>();
}
if (state.isStateless() || (state.isStateful() && !jdbcCtx.isActive())) {
if (state.isStateless()) {
if (pag.isPaginating()) {
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
} else {
// if page is active, immediately passivates it not to keep is active
if (pag.isActive()) {
pag.passivate();
}
if (offsetOpt.isActive()) {
jdbcCtx.realOffset = offsetOpt.offset;
offsetOpt.passivate();
} else {
jdbcCtx.realOffset = 0;
}
}
} else {
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
}
Class<T> clazz = query.getQueriedClass();
List<Object> parameters = new ArrayList<Object>();
StringBuilder sql = JdbcDBUtils.buildSqlSelect(query);
appendSqlWhere(query, sql, parameters);
JdbcDBUtils.appendSqlOrder(query, sql);
JdbcDBUtils.appendSqlLimitOffset(query, sql, parameters);
//sql.append(suffix);
PreparedStatement statement = null;
ResultSet rs = null;
try {
statement = createStatement(sql.toString(), parameters);
if (pag.isActive()) {
// this is just a hint to the DB so wonder if it should be used
statement.setFetchSize(jdbcCtx.realPageSize);
}
rs = statement.executeQuery();
List<T> result = JdbcMappingUtils.mapListKeys(clazz, rs, ClassInfo.getClassInfo(clazz).tableName, JdbcMappingUtils.getJoinFields(query), jdbcCtx.realPageSize);
if (pag.isPaginating()) {
if (result.size() == 0) {
jdbcCtx.noMoreDataAfter = true;
} else {
jdbcCtx.noMoreDataAfter = false;
}
} else {
if (state.isStateful()) {
jdbcCtx.realOffset += result.size();
}
}
if (state.isStateless()) {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, statement);
} else {
Integer offsetParamIdx = parameters.size();
Integer limitParamIdx = offsetParamIdx - 1;
// store indexes of offset and limit for reuse
jdbcCtx.activate();
jdbcCtx.statement = statement;
jdbcCtx.limitParamIdx = limitParamIdx;
jdbcCtx.offsetParamIdx = offsetParamIdx;
}
return result;
} catch (SQLException e) {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, statement);
throw new SienaException(e);
}
} else {
// payload has been initialized so goes on
Class<T> clazz = query.getQueriedClass();
if (offsetOpt.isActive()) {
jdbcCtx.realOffset += offsetOpt.offset;
offsetOpt.passivate();
} else {
// keeps realOffset
}
ResultSet rs = null;
try {
// when paginating, should update limit and offset
//if(pag.isActive()){
// update limit and offset
jdbcCtx.statement.setObject(jdbcCtx.limitParamIdx, jdbcCtx.realPageSize);
//}
//if(offsetOpt.isActive()){
jdbcCtx.statement.setObject(jdbcCtx.offsetParamIdx, jdbcCtx.realOffset);
//}
rs = jdbcCtx.statement.executeQuery();
List<T> result = JdbcMappingUtils.mapListKeys(clazz, rs, ClassInfo.getClassInfo(clazz).tableName, JdbcMappingUtils.getJoinFields(query), jdbcCtx.realPageSize);
if (pag.isPaginating()) {
if (result.size() == 0) {
jdbcCtx.noMoreDataAfter = true;
} else {
jdbcCtx.noMoreDataAfter = false;
}
} else {
jdbcCtx.realOffset += result.size();
}
return result;
} catch (SQLException ex) {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, jdbcCtx.statement);
throw new SienaException(ex);
}
}
}
Aggregations