Example usage for org.apache.ibatis.mapping BoundSql getSql

List of usage examples for org.apache.ibatis.mapping BoundSql getSql

Introduction

In this page you can find the example usage for org.apache.ibatis.mapping BoundSql getSql.

Prototype

public String getSql() 

Source Link

Usage

From source file:org.solmix.datax.mybatis.page.PageInterceptor.java

License:Open Source License

/**
 * {@inheritDoc}//from  w  w  w  .j  a va2  s. com
 * 
 * @see org.apache.ibatis.plugin.Interceptor#intercept(org.apache.ibatis.plugin.Invocation)
 */
@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    MappedStatement statement = (MappedStatement) args[0];
    Object parameter = args[1];
    if (parameter instanceof PagedParameter) {
        PagedParameter p = (PagedParameter) parameter;
        BoundSql boundSql = statement.getBoundSql(p.getValues());
        String originalSql = boundSql.getSql().trim();
        SQLDialect sqlDriver = p.getSqlDialect();
        Integer total = p.page.getTotalRow();
        if (total == null || total <= 0) {
            String countSql = sqlDriver.getRowCountQueryString(originalSql);
            Connection connection = p.sqlSession.getConnection();
            PreparedStatement countStmt = connection.prepareStatement(countSql);
            BoundSql countBS = copyFromBoundSql(statement, boundSql, countSql, p.getValues());
            DefaultParameterHandler parameterHandler = new DefaultParameterHandler(statement, p.getValues(),
                    countBS);
            parameterHandler.setParameters(countStmt);
            ResultSet rs = null;
            try {
                rs = countStmt.executeQuery();
            } catch (Exception e) {
                LOG.error("Page sql:\n{} with values:\n{}", countSql, p.getValues());
                throw e;
            }
            if (rs.next()) {
                total = rs.getInt(1);
            }
            rs.close();
            countStmt.close();
        }
        p.page.setTotalRow(total);

        int end = p.page.getEndRow();
        int start = p.page.getStartRow();
        int batch = p.page.getBatchSize();
        if (end != -1 && end - start > batch) {
            batch = end - start;
            p.page.setBatchSize(batch);
        }
        String limitQuery = sqlDriver.limitQuery(originalSql, start, batch, null);
        BoundSql newBoundSql = copyFromBoundSql(statement, boundSql, limitQuery.toString(), p.values);
        MappedStatement newMs = copyFromMappedStatement(statement, new BoundSqlSqlSource(newBoundSql));
        invocation.getArgs()[0] = newMs;
        invocation.getArgs()[1] = p.values;
    }

    return invocation.proceed();
}

From source file:plum.mybatis.PaginationInterceptor.java

License:Apache License

/**
 * perform paging intercetion.//from  www  . j  av  a  2  s  .  co m
 *
 * @param queryArgs Executor.query params.
 */
private void processIntercept(final Object[] queryArgs) {
    //queryArgs = query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler)
    final MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];

    final Object parameter = queryArgs[PARAMETER_INDEX];
    //the need for paging intercept.
    boolean interceptor = ms.getId().matches(_sql_regex);
    //obtain paging information.
    final PageQuery pageQuery = interceptor ? PagingParametersFinder.getInstance().findCriteria(parameter)
            : new PageQuery(PageQuery.DEFAULT_PAGE_SIZE);
    if (interceptor) {
        PAGINATION_CRITERIA_THREAD_LOCAL.set(pageQuery);
    }
    final RowBounds rowBounds = (interceptor) ? offset_paging((RowBounds) queryArgs[ROWBOUNDS_INDEX])
            : (RowBounds) queryArgs[ROWBOUNDS_INDEX];
    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();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Pagination sql is <" + sql + ">");
        }
        //implementation of the access to the total number of SQL,to obtain  the total number and stored in the thread location

        Connection connection = null;
        try {
            //get connection
            connection = ms.getConfiguration().getEnvironment().getDataSource().getConnection();
            int count = SQLHelp.getCount(sql, connection, ms, parameter, boundSql, _dialect);
            final Pager pager = new Pager(pageQuery.getPage(), pageQuery.getPageSize(), count);
            PAGINATION_COUNT.set(pager);
        } catch (SQLException e) {
            LOG.error("The total number of access to the database failure.", e);
            PAGINATION_COUNT.set(null);
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException e) {
                LOG.error("Close the database connection error.", e);
            }
        }
        if (_dialect.supportsLimit()) {
            sql = _dialect.getLimitString(sql, offset, limit);
            offset = RowBounds.NO_ROW_OFFSET;
        } else {
            sql = _dialect.getLimitString(sql, 0, limit);
        }
        limit = RowBounds.NO_ROW_LIMIT;

        queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit);

        BoundSql newBoundSql = copyFromBoundSql(ms, boundSql, sql);

        MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));
        queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
    }
}