final RowBounds rowBounds = (interceptor) ? offset_paging(oldRow, pageRequest) : oldRow;
int offset = rowBounds.getOffset();
int limit = rowBounds.getLimit();
if (_dialect.supportsLimit() && (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT)) {
final BoundSql boundSql = ms.getBoundSql(parameter);
String sql = boundSql.getSql().trim();
Connection connection = null;
try {
//get connection
connection = ms.getConfiguration().getEnvironment().getDataSource().getConnection();
int count = CountHelper.getCount(sql, connection, ms, parameter, boundSql, _dialect);
PAGINATION_TOTAL.set(count);
} catch (SQLException e) {
log.error("The total number of access to the database failure.", e);
} finally {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
log.error("Close the database connection error.", e);
}
}
String new_sql = sortAndFilterSql(sql, pageRequest);
if (_dialect.supportsLimit()) {
new_sql = _dialect.getLimitString(new_sql, offset, limit);
offset = RowBounds.NO_ROW_OFFSET;
} else {
new_sql = _dialect.getLimitString(new_sql, 0, limit);
}
if (log.isDebugEnabled()) {
log.debug("pagination sql is :[" + new_sql + "]");
}
limit = RowBounds.NO_ROW_LIMIT;
queryArgs[2] = new RowBounds(offset, limit);
BoundSql newBoundSql = copyFromBoundSql(ms, boundSql, new_sql);
MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));
queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
}
}