package cn.gov.customs.data.dm;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.identity.GetGeneratedKeysDelegate;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.PostInsertIdentityPersister;


public class DM8GetGeneratedKeysDelegate extends GetGeneratedKeysDelegate {

    private String[] keyColumns;


    public DM8GetGeneratedKeysDelegate(PostInsertIdentityPersister persister, Dialect dialect) {
        super(persister, dialect);
        this.keyColumns = getPersister().getRootTableKeyColumnNames();
        if ( keyColumns.length > 1 ) {
            throw new HibernateException( "Identity generator cannot be used with multi-column keys" );
        }
    }

    @Override
    protected PreparedStatement prepare(String insertSQL, SharedSessionContractImplementor session) throws SQLException {
        return session
                .getJdbcCoordinator()
                .getStatementPreparer()
                .prepareStatement( insertSQL, keyColumns );
    }
}
