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.keygen.IMultiKeyGenerator;
019import com.mybatisflex.core.util.ArrayUtil;
020import org.apache.ibatis.executor.Executor;
021import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
022import org.apache.ibatis.executor.keygen.KeyGenerator;
023import org.apache.ibatis.executor.statement.PreparedStatementHandler;
024import org.apache.ibatis.mapping.BoundSql;
025import org.apache.ibatis.mapping.MappedStatement;
026import org.apache.ibatis.mapping.ResultSetType;
027import org.apache.ibatis.session.ResultHandler;
028import org.apache.ibatis.session.RowBounds;
029
030import java.sql.*;
031
032/**
033 * @author Michael Yang(fuhai999@gmail.com)
034 */
035public class FlexPreparedStatementHandler extends PreparedStatementHandler {
036
037    public FlexPreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
038        super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
039    }
040
041
042    @Override
043    protected Statement instantiateStatement(Connection connection) throws SQLException {
044        String sql = boundSql.getSql();
045        KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
046        if (keyGenerator instanceof Jdbc3KeyGenerator) {
047            String[] keyColumnNames = mappedStatement.getKeyColumns();
048            if (keyColumnNames == null) {
049                return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
050            } else {
051                return connection.prepareStatement(sql, keyColumnNames);
052            }
053        }
054        // 多主键的场景
055        else if (keyGenerator instanceof IMultiKeyGenerator) {
056            if (((IMultiKeyGenerator) keyGenerator).hasGeneratedKeys()) {
057                String[] keyColumnNames = ((IMultiKeyGenerator) keyGenerator).getKeyColumnNames();
058                if (ArrayUtil.isNotEmpty(keyColumnNames)) {
059                    return connection.prepareStatement(sql, keyColumnNames);
060                } else {
061                    return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
062                }
063            }
064        }
065
066        if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
067            return connection.prepareStatement(sql);
068        } else {
069            return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
070        }
071    }
072
073
074}