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:cn.com.bricks.mybatis.rbac.DynamicRbacInterceptor.java

@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
    MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY,
            DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler
            .getValue("delegate.mappedStatement");

    // ?sql/* w  ww  . ja  va  2  s.  c o  m*/
    BoundSql bsql = statementHandler.getBoundSql();
    // ?sql
    TextSqlNode sqlNode = new TextSqlNode(bsql.getSql());
    BoundSql nbsql = getBoundSql(mappedStatement.getConfiguration(), bsql.getParameterObject(), sqlNode);
    // ?sql?
    metaStatementHandler.setValue("delegate.boundSql.sql", nbsql.getSql());
    return invocation.proceed();
}

From source file:com.appleframework.orm.mybatis.pagehelper.sqlsource.PageDynamicSqlSource.java

License:Open Source License

@Override
protected BoundSql getCountBoundSql(Object parameterObject) {
    DynamicContext context = new DynamicContext(configuration, parameterObject);
    rootSqlNode.apply(context);//from  w ww  . j  a v a  2 s  .  c o m
    SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
    Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
    SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    sqlSource = new StaticSqlSource(configuration, localParser.get().getCountSql(boundSql.getSql()),
            boundSql.getParameterMappings());
    boundSql = sqlSource.getBoundSql(parameterObject);
    //??
    for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
        boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
    }
    return boundSql;
}

From source file:com.appleframework.orm.mybatis.pagehelper.sqlsource.PageDynamicSqlSource.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override//  w  w w  .  j  a  va2 s .  c o m
protected BoundSql getPageBoundSql(Object parameterObject) {
    DynamicContext context;
    //??parameterObject???
    //??Map?KEY
    //bug#25:http://git.oschina.net/free/Mybatis_PageHelper/issues/25
    if (parameterObject != null && parameterObject instanceof Map
            && ((Map) parameterObject).containsKey(ORIGINAL_PARAMETER_OBJECT)) {
        context = new DynamicContext(configuration, ((Map) parameterObject).get(ORIGINAL_PARAMETER_OBJECT));
    } else {
        context = new DynamicContext(configuration, parameterObject);
    }
    rootSqlNode.apply(context);
    SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
    Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
    SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
    sqlSource = new OrderByStaticSqlSource((StaticSqlSource) sqlSource);
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    sqlSource = new StaticSqlSource(configuration, localParser.get().getPageSql(boundSql.getSql()),
            localParser.get().getPageParameterMapping(configuration, boundSql));
    boundSql = sqlSource.getBoundSql(parameterObject);
    //??
    for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
        boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
    }
    return boundSql;
}

From source file:com.appleframework.orm.mybatis.pagehelper.sqlsource.PageProviderSqlSource.java

License:Open Source License

@Override
protected BoundSql getCountBoundSql(Object parameterObject) {
    BoundSql boundSql;
    SqlSource sqlSource = createSqlSource(parameterObject);
    boundSql = sqlSource.getBoundSql(parameterObject);
    return new BoundSql(configuration, localParser.get().getCountSql(boundSql.getSql()),
            boundSql.getParameterMappings(), parameterObject);
}

From source file:com.appleframework.orm.mybatis.pagehelper.sqlsource.PageProviderSqlSource.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override/*from   w ww.  ja  v  a 2s . c  o  m*/
protected BoundSql getPageBoundSql(Object parameterObject) {
    BoundSql boundSql;
    if (parameterObject instanceof Map && ((Map) parameterObject).containsKey(PROVIDER_OBJECT)) {
        SqlSource sqlSource = createSqlSource(((Map) parameterObject).get(PROVIDER_OBJECT));
        boundSql = sqlSource.getBoundSql(((Map) parameterObject).get(PROVIDER_OBJECT));
    } else {
        SqlSource sqlSource = createSqlSource(parameterObject);
        boundSql = sqlSource.getBoundSql(parameterObject);
    }
    return new BoundSql(configuration, localParser.get().getPageSql(boundSql.getSql()),
            localParser.get().getPageParameterMapping(configuration, boundSql), parameterObject);
}

From source file:com.autonavi.tsp.workbackend.util.page.SqlUtil.java

License:Open Source License

/**
 * ?sqlSource//from   www. java2s  .  c om
 *
 * @param configuration
 * @param sqlSource
 * @return
 */
private SqlSource getPageSqlSource(Configuration configuration, SqlSource sqlSource, Object parameterObject) {
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    return new StaticSqlSource(configuration, sqlParser.getPageSql(boundSql.getSql()),
            getPageParameterMapping(configuration, boundSql));
}

From source file:com.autonavi.tsp.workbackend.util.page.SqlUtil.java

License:Open Source License

/**
 * ?countsqlSource//w  w w . j a v a  2  s .c  om
 *
 * @param sqlSource
 * @return
 */
private SqlSource getCountSqlSource(Configuration configuration, SqlSource sqlSource, Object parameterObject) {
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    return new StaticSqlSource(configuration, sqlParser.getCountSql(boundSql.getSql()),
            boundSql.getParameterMappings());
}

From source file:com.baomidou.mybatisplus.plugins.CachePaginationInterceptor.java

License:Apache License

/**
 * Physical Pagination Interceptor for all the queries with parameter
 * {@link org.apache.ibatis.session.RowBounds}
 *//*ww  w .  ja  va2 s .  c  o m*/
public Object intercept(Invocation invocation) throws Throwable {

    Object target = invocation.getTarget();
    if (target instanceof StatementHandler) {
        StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
        RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");

        if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        }
        BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
        String originalSql = boundSql.getSql();

        if (rowBounds instanceof Pagination) {
            Pagination page = (Pagination) rowBounds;
            boolean orderBy = true;
            if (page.isSearchCount()) {
                CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                        page.isOptimizeCount());
                orderBy = countOptimize.isOrderBy();
            }
            String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
            originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
        } else {
            // support physical Pagination for RowBounds
            originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz);
        }

        metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
        metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
        metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
    } else {
        RowBounds rowBounds = (RowBounds) invocation.getArgs()[2];
        if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        }
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Executor executor = (Executor) invocation.getTarget();
        Connection connection = executor.getTransaction().getConnection();
        Object parameterObject = invocation.getArgs()[1];
        BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
        String originalSql = boundSql.getSql();
        if (rowBounds instanceof Pagination) {
            Pagination page = (Pagination) rowBounds;
            if (page.isSearchCount()) {
                CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                        page.isOptimizeCount());
                super.queryTotal(countOptimize.getCountSQL(), mappedStatement, boundSql, page, connection);
                if (page.getTotal() <= 0) {
                    return invocation.proceed();
                }
            }
        }
    }
    return invocation.proceed();
}

From source file:com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor.java

License:Apache License

@SuppressWarnings("unchecked")
private void changSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, LockerCache lockerCache)
        throws Exception {
    Field versionField = lockerCache.field;
    String versionColumn = lockerCache.column;
    final Object versionValue = versionField.get(parameterObject);
    if (versionValue != null) {// ???version,?
        Configuration configuration = ms.getConfiguration();
        // /*  w w  w . j a  v a2  s. com*/
        lockerCache.versionHandler.plusVersion(parameterObject, versionField, versionValue);
        // ?where?,?
        Update jsqlSql = (Update) CCJSqlParserUtil.parse(boundSql.getSql());
        BinaryExpression expression = (BinaryExpression) jsqlSql.getWhere();
        if (expression != null && !expression.toString().contains(versionColumn)) {
            EqualsTo equalsTo = new EqualsTo();
            equalsTo.setLeftExpression(new Column(versionColumn));
            equalsTo.setRightExpression(RIGHT_EXPRESSION);
            jsqlSql.setWhere(new AndExpression(equalsTo, expression));
            List<ParameterMapping> parameterMappings = new LinkedList<>(boundSql.getParameterMappings());
            parameterMappings.add(jsqlSql.getExpressions().size(), getVersionMappingInstance(configuration));
            MetaObject boundSqlMeta = configuration.newMetaObject(boundSql);
            boundSqlMeta.setValue("sql", jsqlSql.toString());
            boundSqlMeta.setValue("parameterMappings", parameterMappings);
        }
        // ?
        boundSql.setAdditionalParameter("originVersionValue", versionValue);
    }
}

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

License:Apache License

/**
 * Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
 *//*from ww  w  .jav a 2s  .  c  o  m*/
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
    // ?SELECT?
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler
            .getValue("delegate.mappedStatement");
    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }
    RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
    /* ??? */
    if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    }
    BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Connection connection = (Connection) invocation.getArgs()[0];
    if (isDynamicDataSource()) {
        dialectType = JdbcUtils.getDbType(connection.getMetaData().getURL()).getDb();
    }
    if (rowBounds instanceof Pagination) {
        Pagination page = (Pagination) rowBounds;
        boolean orderBy = true;
        if (page.isSearchCount()) {
            CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                    page.isOptimizeCount());
            orderBy = countOptimize.isOrderBy();
            this.queryTotal(countOptimize.getCountSQL(), mappedStatement, boundSql, page, connection);
            if (page.getTotal() <= 0) {
                return invocation.proceed();
            }
        }
        String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
        originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
    } else {
        // support physical Pagination for RowBounds
        originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz);
    }

    /*
       * <p> ? </p> <p> ???????</p>
     */
    metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
    metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
    metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
    return invocation.proceed();
}