use of org.hibernate.dialect.pagination.LimitHandler in project midpoint by Evolveum.
the class SqlAuditServiceImpl method selectRecordsByNumberToKeep.
private int selectRecordsByNumberToKeep(Session session, String tempTable, Integer recordsToKeep, Dialect dialect) {
Number totalAuditRecords = (Number) session.createCriteria(RAuditEventRecord.class).setProjection(Projections.rowCount()).uniqueResult();
int recordsToDelete = totalAuditRecords.intValue() - recordsToKeep;
if (recordsToDelete <= 0) {
recordsToDelete = 0;
} else if (recordsToDelete > CLEANUP_AUDIT_BATCH_SIZE) {
recordsToDelete = CLEANUP_AUDIT_BATCH_SIZE;
}
LOGGER.debug("Total audit records: {}, records to keep: {} => records to delete in this batch: {}", totalAuditRecords, recordsToKeep, recordsToDelete);
if (recordsToDelete == 0) {
return 0;
}
StringBuilder selectSB = new StringBuilder();
selectSB.append("select a.id as id from ").append(RAuditEventRecord.TABLE_NAME).append(" a");
selectSB.append(" order by a.").append(RAuditEventRecord.COLUMN_TIMESTAMP).append(" asc");
String selectString = selectSB.toString();
// batch size
RowSelection rowSelection = new RowSelection();
rowSelection.setMaxRows(recordsToDelete);
LimitHandler limitHandler = dialect.buildLimitHandler(selectString, rowSelection);
selectString = limitHandler.getProcessedSql();
selectString = selectString.replace("?", String.valueOf(recordsToDelete));
String queryString = "insert into " + tempTable + " " + selectString;
LOGGER.trace("Query string = {}", queryString);
SQLQuery query = session.createSQLQuery(queryString);
return query.executeUpdate();
}
use of org.hibernate.dialect.pagination.LimitHandler in project midpoint by Evolveum.
the class SqlAuditServiceImpl method selectRecordsByMaxAge.
private int selectRecordsByMaxAge(Session session, String tempTable, Date minValue, Dialect dialect) {
// fill temporary table, we don't need to join task on object on
// container, oid and id is already in task table
StringBuilder selectSB = new StringBuilder();
selectSB.append("select a.id as id from ").append(RAuditEventRecord.TABLE_NAME).append(" a");
selectSB.append(" where a.").append(RAuditEventRecord.COLUMN_TIMESTAMP).append(" < ###TIME###");
String selectString = selectSB.toString();
// batch size
RowSelection rowSelection = new RowSelection();
rowSelection.setMaxRows(CLEANUP_AUDIT_BATCH_SIZE);
LimitHandler limitHandler = dialect.buildLimitHandler(selectString, rowSelection);
selectString = limitHandler.getProcessedSql();
// replace ? -> batch size, $ -> ?
// Sorry for that .... I just don't know how to write this query in HQL,
// nor I'm not sure if limiting max size in
// compound insert into ... select ... query via query.setMaxSize()
// would work - TODO write more nicely if anybody knows how)
selectString = selectString.replace("?", String.valueOf(CLEANUP_AUDIT_BATCH_SIZE));
selectString = selectString.replace("###TIME###", "?");
String queryString = "insert into " + tempTable + " " + selectString;
LOGGER.trace("Query string = {}", queryString);
SQLQuery query = session.createSQLQuery(queryString);
query.setParameter(0, new Timestamp(minValue.getTime()));
return query.executeUpdate();
}
use of org.hibernate.dialect.pagination.LimitHandler in project hibernate-orm by hibernate.
the class AbstractLoadPlanBasedLoader method executeQueryStatement.
protected SqlStatementWrapper executeQueryStatement(String sqlStatement, QueryParameters queryParameters, boolean scroll, List<AfterLoadAction> afterLoadActions, SharedSessionContractImplementor session) throws SQLException {
// Processing query filters.
queryParameters.processFilters(sqlStatement, session);
// Applying LIMIT clause.
final LimitHandler limitHandler = getLimitHandler(queryParameters.getRowSelection());
String sql = limitHandler.processSql(queryParameters.getFilteredSQL(), queryParameters.getRowSelection());
// Adding locks and comments.
sql = session.getJdbcServices().getJdbcEnvironment().getDialect().addSqlHintOrComment(sql, queryParameters, session.getFactory().getSessionFactoryOptions().isCommentsEnabled());
final PreparedStatement st = prepareQueryStatement(sql, queryParameters, limitHandler, scroll, session);
return new SqlStatementWrapper(st, getResultSet(st, queryParameters.getRowSelection(), limitHandler, queryParameters.hasAutoDiscoverScalarTypes(), session));
}
use of org.hibernate.dialect.pagination.LimitHandler in project hibernate-orm by hibernate.
the class Loader method executeQueryStatement.
protected SqlStatementWrapper executeQueryStatement(String sqlStatement, QueryParameters queryParameters, boolean scroll, List<AfterLoadAction> afterLoadActions, SharedSessionContractImplementor session) throws SQLException {
// Processing query filters.
queryParameters.processFilters(sqlStatement, session);
// Applying LIMIT clause.
final LimitHandler limitHandler = getLimitHandler(queryParameters.getRowSelection());
String sql = limitHandler.processSql(queryParameters.getFilteredSQL(), queryParameters.getRowSelection());
// Adding locks and comments.
sql = preprocessSQL(sql, queryParameters, getFactory(), afterLoadActions);
final PreparedStatement st = prepareQueryStatement(sql, queryParameters, limitHandler, scroll, session);
final ResultSet rs;
if (queryParameters.isCallable() && isTypeOf(st, CallableStatement.class)) {
final CallableStatement cs = st.unwrap(CallableStatement.class);
rs = getResultSet(cs, queryParameters.getRowSelection(), limitHandler, queryParameters.hasAutoDiscoverScalarTypes(), session);
} else {
rs = getResultSet(st, queryParameters.getRowSelection(), limitHandler, queryParameters.hasAutoDiscoverScalarTypes(), session);
}
return new SqlStatementWrapper(st, rs);
}
Aggregations