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

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

Introduction

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

Prototype

public BoundSql(Configuration configuration, String sql, List<ParameterMapping> parameterMappings,
            Object parameterObject) 

Source Link

Usage

From source file:com.springD.framework.persistence.interceptor.PaginationInterceptor.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {

    final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];

    //        //?SQL
    //        if (mappedStatement.getId().matches(_SQL_PATTERN)) {
    //        if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) {
    Object parameter = invocation.getArgs()[1];
    BoundSql boundSql = mappedStatement.getBoundSql(parameter);
    Object parameterObject = boundSql.getParameterObject();

    //??//w w  w  .  java2 s  .  c om
    Page<Object> page = null;
    if (parameterObject != null) {
        page = convertParameter(parameterObject, page);
    }

    //
    if (page != null && page.getPageSize() != -1) {

        if (org.apache.commons.lang3.StringUtils.isBlank(boundSql.getSql())) {
            return null;
        }
        String originalSql = boundSql.getSql().trim();

        //
        page.setCount(SQLHelper.getCount(originalSql, null, mappedStatement, parameterObject, boundSql, log));

        //  ??
        String pageSql = SQLHelper.generatePageSql(originalSql, page, DIALECT);
        //                if (log.isDebugEnabled()) {
        //                    log.debug("PAGE SQL:" + StringUtils.replace(pageSql, "\n", ""));
        //                }
        invocation.getArgs()[2] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
        BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pageSql,
                boundSql.getParameterMappings(), boundSql.getParameterObject());
        MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));

        invocation.getArgs()[0] = newMs;
    }
    //        }
    return invocation.proceed();
}

From source file:com.springD.framework.persistence.interceptor.SQLHelper.java

License:Open Source License

/**
 * /*from   ww w  .  j  av  a  2 s.c  o m*/
 * @param sql             SQL?
 * @param connection      ?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @return 
 * @throws SQLException sql
 */
public static int getCount(final String sql, final Connection connection, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql, Log log) throws SQLException {
    String tmpSql = sql.replaceAll("(?i)order by.+\\z", "");
    String countSql = "select count(1) from (" + tmpSql + ") tmp_count";
    //        final String countSql = "select count(1) " + removeSelect(removeOrders(sql));
    Connection conn = connection;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        if (log.isDebugEnabled()) {
            log.debug("COUNT SQL: " + org.apache.commons.lang3.StringUtils.replaceEach(countSql,
                    new String[] { "\n", "\t" }, new String[] { " ", " " }));
        }
        if (conn == null) {
            conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
        }
        ps = conn.prepareStatement(countSql);
        BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,
                boundSql.getParameterMappings(), parameterObject);
        SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);
        rs = ps.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        return count;
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

From source file:com.tj.mybatisplus.plugins.PaginationInterceptor.java

License:Apache License

/**
 * ?// w ww.j a  va 2 s . com
 * 
 * @param sql
 * @param connection
 * @param mappedStatement
 * @param boundSql
 * @param page
 */
public Pagination count(String sql, Connection connection, MappedStatement mappedStatement, BoundSql boundSql,
        Pagination page) {
    String sqlUse = sql;
    int order_by = sql.toUpperCase().lastIndexOf("ORDER BY");
    if (order_by > -1) {
        sqlUse = sql.substring(0, order_by);
    }
    StringBuffer countSql = new StringBuffer("SELECT COUNT(1) AS TOTAL FROM (");
    countSql.append(sqlUse).append(") A");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = connection.prepareStatement(countSql.toString());
        BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql.toString(),
                boundSql.getParameterMappings(), boundSql.getParameterObject());
        ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement,
                boundSql.getParameterObject(), countBS);
        parameterHandler.setParameters(pstmt);
        rs = pstmt.executeQuery();
        int total = 0;
        if (rs.next()) {
            total = rs.getInt(1);
        }
        page.setTotal(total);
        /**
         * ??
         */
        if (page.getCurrent() > page.getPages()) {
            page = new Pagination(1, page.getSize());
            page.setTotal(total);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return page;
}

From source file:com.wsun.seap.dao.interceptor.PaginationInterceptor.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    // ??/*from  w  w w  . j a v  a 2  s .  c om*/
    final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[MAPPED_STATEMENT_INDEX];
    // ?SQL
    // ??
    Object parameterObj = invocation.getArgs()[PARAMETER_INDEX];
    // ??QueryParam
    Object parameter;
    if (parameterObj instanceof QueryParam) {
        // ?QueryParam,??Map
        parameter = ((QueryParam) parameterObj).getAllParam();
        invocation.getArgs()[PARAMETER_INDEX] = parameter;
    } else {
        parameter = parameterObj;
    }

    BoundSql boundSql = mappedStatement.getBoundSql(parameter);
    if (StringUtils.isBlank(boundSql.getSql())) {
        return null;
    }
    // ??RowBounds
    RowBounds rowBounds = (RowBounds) invocation.getArgs()[ROWBOUNDS_INDEX];
    // 
    if (rowBounds != null && rowBounds != RowBounds.DEFAULT) {
        String originalSql = boundSql.getSql().trim();
        // ??sql
        String pageSql = dialect.getLimitString(originalSql, rowBounds.getOffset(), rowBounds.getLimit());
        invocation.getArgs()[ROWBOUNDS_INDEX] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
        BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pageSql,
                boundSql.getParameterMappings(), boundSql.getParameterObject());
        MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
        invocation.getArgs()[MAPPED_STATEMENT_INDEX] = newMs;
    }
    return invocation.proceed();
}

From source file:com.wsun.seap.dao.interceptor.SQLHelper.java

License:Open Source License

/**
 * //from   w  w  w.j a  va 2 s  . c  om
 * @param sql             SQL?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @return 
 * @throws java.sql.SQLException sql
 */
public static int getCount(final String sql, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql) throws SQLException {
    final String countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count";
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
        ps = conn.prepareStatement(countSql);
        BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,
                boundSql.getParameterMappings(), parameterObject);
        SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);
        rs = ps.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        return count;
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

From source file:com.zrx.authority.dao.plugin.PagePlugin.java

public Object intercept(Invocation ivk) throws Throwable {
    // TODO Auto-generated method stub
    if (ivk.getTarget() instanceof RoutingStatementHandler) {
        RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
        BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper
                .getValueByFieldName(statementHandler, "delegate");
        MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate,
                "mappedStatement");

        if (mappedStatement.getId().matches(pageSqlId)) { //?SQL
            BoundSql boundSql = delegate.getBoundSql();
            Object parameterObject = boundSql.getParameterObject();//SQL<select>parameterType??Mapper??,??
            if (parameterObject == null) {
                throw new NullPointerException("parameterObject?");
            } else {
                Connection connection = (Connection) ivk.getArgs()[0];
                String sql = boundSql.getSql();
                //String countSql = "select count(0) from (" + sql+ ") as tmp_count"; //
                String countSql = "select count(0) from (" + sql + ")  tmp_count"; // == oracle  as (SQL command not properly ended)
                PreparedStatement countStmt = connection.prepareStatement(countSql);
                BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,
                        boundSql.getParameterMappings(), parameterObject);
                setParameters(countStmt, mappedStatement, countBS, parameterObject);
                ResultSet rs = countStmt.executeQuery();
                int count = 0;
                if (rs.next()) {
                    count = rs.getInt(1);
                }//from   w w  w.j av  a2  s . c  om
                rs.close();
                countStmt.close();
                //System.out.println(count);
                Page page = null;
                if (parameterObject instanceof Page) { //?Page
                    page = (Page) parameterObject;
                    page.setEntityOrField(true); //?com.jalan.entity.Page.entityOrField 
                    page.setTotalResult(count);
                } else { //??Page
                    Field pageField = ReflectHelper.getFieldByFieldName(parameterObject, "page");
                    if (pageField != null) {
                        page = (Page) ReflectHelper.getValueByFieldName(parameterObject, "page");
                        if (page == null)
                            page = new Page();
                        page.setEntityOrField(false); //?com.jalan.entity.Page.entityOrField 
                        page.setTotalResult(count);
                        ReflectHelper.setValueByFieldName(parameterObject, "page", page); //??
                    } else {
                        throw new NoSuchFieldException(
                                parameterObject.getClass().getName() + "? page ?");
                    }
                }
                String pageSql = generatePageSql(sql, page);
                ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); //sql???BoundSql.
            }
        }
    }
    return ivk.proceed();
}

From source file:org.fire.platform.common.page.sqlsource.PageProviderSqlSource.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override/*from w w  w .j  a va  2 s.c o  m*/
public BoundSql getBoundSql(Object parameterObject) {
    BoundSql boundSql = null;
    if (parameterObject instanceof Map && ((Map) parameterObject).containsKey(PROVIDER_OBJECT)) {
        boundSql = providerSqlSource.getBoundSql(((Map) parameterObject).get(PROVIDER_OBJECT));
    } else {
        boundSql = providerSqlSource.getBoundSql(parameterObject);
    }
    if (count) {
        return new BoundSql(configuration, parser.getCountSql(boundSql.getSql()),
                boundSql.getParameterMappings(), parameterObject);
    } else {
        return new BoundSql(configuration, parser.getPageSql(boundSql.getSql()),
                parser.getPageParameterMapping(configuration, boundSql), parameterObject);
    }
}

From source file:org.mybatis.scripting.velocity.SQLScriptSource.java

License:Apache License

@Override
public BoundSql getBoundSql(Object parameterObject) {

    final Map<String, Object> context = new HashMap<String, Object>();
    final ParameterMappingCollector pmc = new ParameterMappingCollector(parameterMappingSources, context,
            configuration);//from  w  ww . jav  a2 s. c o  m

    context.put(DATABASE_ID_KEY, configuration.getDatabaseId());
    context.put(PARAMETER_OBJECT_KEY, parameterObject);
    context.put(MAPPING_COLLECTOR_KEY, pmc);

    final String sql = VelocityFacade.apply(compiledScript, context);
    BoundSql boundSql = new BoundSql(configuration, sql, pmc.getParameterMappings(), parameterObject);
    for (Map.Entry<String, Object> entry : context.entrySet()) {
        boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
    }

    return boundSql;

}

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

License:Open Source License

private BoundSql copyFromBoundSql(MappedStatement ms, BoundSql boundSql, String sql, Object parameterObject) {
    BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(),
            parameterObject);//from w ww  .j  av a  2 s.  c  o  m
    for (ParameterMapping mapping : boundSql.getParameterMappings()) {
        String prop = mapping.getProperty();
        if (boundSql.hasAdditionalParameter(prop)) {
            newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
        }
    }
    return newBoundSql;
}

From source file:plum.mybatis.SQLHelp.java

License:Apache License

/**
 * //from ww  w  .ja v  a 2 s .  co  m
 *
 * @param sql             SQL?
 * @param connection      ?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @param dialect         database dialect
 * @return 
 * @throws java.sql.SQLException sql
 */
public static int getCount(final String sql, final Connection connection, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String count_sql = dialect.getCountString(sql);
    if (LOG.isDebugEnabled()) {
        LOG.debug("count sql:" + count_sql);
    }
    PreparedStatement countStmt = null;
    ResultSet rs = null;
    try {
        countStmt = connection.prepareStatement(count_sql);
        final BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), count_sql,
                boundSql.getParameterMappings(), parameterObject);
        SQLHelp.setParameters(countStmt, mappedStatement, countBS, parameterObject);
        rs = countStmt.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        return count;
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (countStmt != null) {
            countStmt.close();
        }
    }
}