Search in sources :

Example 1 with LimitHandler

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();
}
Also used : LimitHandler(org.hibernate.dialect.pagination.LimitHandler) RowSelection(org.hibernate.engine.spi.RowSelection) SQLQuery(org.hibernate.SQLQuery)

Example 2 with LimitHandler

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();
}
Also used : LimitHandler(org.hibernate.dialect.pagination.LimitHandler) RowSelection(org.hibernate.engine.spi.RowSelection) SQLQuery(org.hibernate.SQLQuery) Timestamp(java.sql.Timestamp)

Example 3 with LimitHandler

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));
}
Also used : LimitHandler(org.hibernate.dialect.pagination.LimitHandler) NoopLimitHandler(org.hibernate.dialect.pagination.NoopLimitHandler) PreparedStatement(java.sql.PreparedStatement)

Example 4 with LimitHandler

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);
}
Also used : NoopLimitHandler(org.hibernate.dialect.pagination.NoopLimitHandler) LimitHandler(org.hibernate.dialect.pagination.LimitHandler) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

LimitHandler (org.hibernate.dialect.pagination.LimitHandler)4 PreparedStatement (java.sql.PreparedStatement)2 SQLQuery (org.hibernate.SQLQuery)2 NoopLimitHandler (org.hibernate.dialect.pagination.NoopLimitHandler)2 RowSelection (org.hibernate.engine.spi.RowSelection)2 CallableStatement (java.sql.CallableStatement)1 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1