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//    if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
046//      String[] keyColumnNames = mappedStatement.getKeyColumns();
047//      if (keyColumnNames == null) {
048//        return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
049//      } else {
050//        return connection.prepareStatement(sql, keyColumnNames);
051//      }
052//    } else if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
053//      return connection.prepareStatement(sql);
054//    } else {
055//      return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
056//    }
057
058        String sql = boundSql.getSql();
059        KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
060        if (keyGenerator instanceof Jdbc3KeyGenerator) {
061            String[] keyColumnNames = mappedStatement.getKeyColumns();
062            if (keyColumnNames == null) {
063                return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
064            } else {
065                return connection.prepareStatement(sql, keyColumnNames);
066            }
067        }
068        // 多主键的场景
069        else if (keyGenerator instanceof IMultiKeyGenerator) {
070            if (((IMultiKeyGenerator) keyGenerator).hasGeneratedKeys()) {
071                String[] keyColumnNames = ((IMultiKeyGenerator) keyGenerator).getKeyColumnNames();
072                if (ArrayUtil.isNotEmpty(keyColumnNames)) {
073                    return connection.prepareStatement(sql, keyColumnNames);
074                } else {
075                    return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
076                }
077            }
078        }
079
080        if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
081            return connection.prepareStatement(sql);
082        } else {
083            return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
084        }
085    }
086
087
088}