/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */
package com.crunii.micro.domain.common.jdbc;

import org.springframework.jdbc.core.PreparedStatementCreator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author 田平 create 2021/12/9 17:17
 */
public class BatchPreparedStatementCreator implements PreparedStatementCreator {
	private String[] generatedKeysColumnNames;
	private boolean returnGeneratedKeys = true;
	private String actualSql;

	public BatchPreparedStatementCreator(String actualSql) {
		this.actualSql = actualSql;
	}

	public void setGeneratedKeysColumnNames(String[] generatedKeysColumnNames) {
		this.generatedKeysColumnNames = generatedKeysColumnNames;
	}

	public void setReturnGeneratedKeys(boolean returnGeneratedKeys) {
		this.returnGeneratedKeys = returnGeneratedKeys;
	}

	@Override
	public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
		PreparedStatement ps;
		if (generatedKeysColumnNames != null) {
			ps = con.prepareStatement(this.actualSql, generatedKeysColumnNames);
		} else {
			ps = con.prepareStatement(this.actualSql, PreparedStatement.RETURN_GENERATED_KEYS);
		}
		return ps;
	}
}

