001/**
002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.mybatisflex.core.mybatis;
017
018import com.mybatisflex.core.audit.AuditManager;
019import org.apache.ibatis.cursor.Cursor;
020import org.apache.ibatis.executor.Executor;
021import org.apache.ibatis.executor.ExecutorException;
022import org.apache.ibatis.executor.parameter.ParameterHandler;
023import org.apache.ibatis.executor.statement.CallableStatementHandler;
024import org.apache.ibatis.executor.statement.SimpleStatementHandler;
025import org.apache.ibatis.executor.statement.StatementHandler;
026import org.apache.ibatis.mapping.BoundSql;
027import org.apache.ibatis.mapping.MappedStatement;
028import org.apache.ibatis.session.Configuration;
029import org.apache.ibatis.session.ResultHandler;
030import org.apache.ibatis.session.RowBounds;
031
032import java.sql.Connection;
033import java.sql.SQLException;
034import java.sql.Statement;
035import java.util.List;
036
037/**
038 * 参考 {@link org.apache.ibatis.executor.statement.RoutingStatementHandler}
039 * 主要作用:
040 * 1、替换 PreparedStatementHandler 为 FlexPreparedStatementHandler
041 * 2、进行数据审计
042 */
043public class FlexStatementHandler implements StatementHandler {
044
045    private final StatementHandler delegate;
046    private final BoundSql boundSql;
047    private final boolean auditEnable = AuditManager.isAuditEnable();
048    private final Configuration configuration;
049
050    public FlexStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
051        configuration = ms.getConfiguration();
052        switch (ms.getStatementType()) {
053            case STATEMENT:
054                delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
055                break;
056            case PREPARED:
057                // delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
058                // use FlexPreparedStatementHandler to replace PreparedStatementHandler
059                delegate = new FlexPreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
060                break;
061            case CALLABLE:
062                delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
063                break;
064            default:
065                throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
066        }
067
068        this.boundSql = delegate.getBoundSql();
069    }
070
071    @Override
072    public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
073        return delegate.prepare(connection, transactionTimeout);
074    }
075
076    @Override
077    public void parameterize(Statement statement) throws SQLException {
078        delegate.parameterize(statement);
079    }
080
081    @Override
082    public void batch(Statement statement) throws SQLException {
083        if (auditEnable) {
084            AuditManager.startAudit(() -> {
085                delegate.batch(statement);
086                return null;
087            }, boundSql, configuration);
088        } else {
089            delegate.batch(statement);
090        }
091    }
092
093    @Override
094    public int update(Statement statement) throws SQLException {
095        return auditEnable ? AuditManager.startAudit(() -> delegate.update(statement), boundSql,configuration)
096                : delegate.update(statement);
097    }
098
099    @Override
100    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
101        return auditEnable ? AuditManager.startAudit(() -> delegate.query(statement, resultHandler), boundSql, configuration)
102                : delegate.query(statement, resultHandler);
103    }
104
105    @Override
106    public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
107        return auditEnable ? AuditManager.startAudit(() -> delegate.queryCursor(statement), boundSql, configuration)
108                : delegate.queryCursor(statement);
109    }
110
111    @Override
112    public BoundSql getBoundSql() {
113        return delegate.getBoundSql();
114    }
115
116    @Override
117    public ParameterHandler getParameterHandler() {
118        return delegate.getParameterHandler();
119    }
120
121}