@Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
        Object parameterObject = invocation.getArgs()[1];
        final Configuration configuration = ms.getConfiguration();
        final StatementHandler handler = configuration.newStatementHandler((Executor) invocation.getTarget(),
                ms, parameterObject, RowBounds.DEFAULT, null, null);
        final BoundSql boundSql = handler.getBoundSql();
        final String sql = boundSql.getSql();
        List<String> splitted = splitter.split(sql);
        int rc = 0;
        List<ParameterMapping> fullParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings());
        for (String sqlPart: splitted) {
            if (skipEmptyStatements && sqlPart.length() == 0) {
                continue;
            }
            int numParams = 0;
            for (int index = sqlPart.indexOf('?'); index >=0; index = sqlPart.indexOf('?', index + 1)) {
                numParams++;
            }
            MappedStatement subStatement = subStatements.get(ms);
            if (subStatement == null) {
                subStatement = new MappedStatement.Builder(
                    ms.getConfiguration(), ms.getId(), new SwitchingSqlSource(configuration), ms.getSqlCommandType())
                    .cache(ms.getCache())
                    .databaseId(ms.getDatabaseId())
                    .fetchSize(ms.getFetchSize())
                    .timeout(ms.getTimeout())
                    .flushCacheRequired(ms.isFlushCacheRequired())
                    .useCache(ms.isUseCache())
                    .build();
                subStatements.put(ms, subStatement);
            }
            List<ParameterMapping> subParameterMappings = fullParameterMappings.subList(0, numParams);
            ((SwitchingSqlSource)subStatement.getSqlSource()).switchParams(sqlPart, boundSql,
                    new ArrayList<ParameterMapping>(subParameterMappings));
            subParameterMappings.clear();
            int subRc = (Integer)invocation.getMethod().invoke(invocation.getTarget(), subStatement, parameterObject);
            if (rc >= 0) {
                rc = subRc < 0 ? subRc : rc + subRc;
            }
        }
        return rc;
    }