package cn.fateverse.query.submeter.connection;

import cn.fateverse.query.submeter.preparestatement.SubmeterPreparedStatement;
import lombok.Getter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.metadata.CachedDatabaseMetaData;

import javax.sql.DataSource;
import java.sql.*;

public class SubmeterConnection extends AbstractConnectionAdapter {

    @Getter
    private final DataSource dataSource;

    private CachedDatabaseMetaData cachedDatabaseMetaData;

    private DatabaseMetaData databaseMetaData;

    public SubmeterConnection(DataSource dataSource) {
        this.dataSource = dataSource;

        try (Connection connection = dataSource.getConnection()) {
            databaseMetaData = connection.getMetaData();
            cachedDatabaseMetaData = new CachedDatabaseMetaData(databaseMetaData);

        } catch (Exception e) {
            System.out.println("出错了");
        }

    }

    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return databaseMetaData;
    }


    @Override
    public Statement createStatement() throws SQLException {
//        return dataSource.getConnection().createStatement();
        return new SubmeterPreparedStatement(this);
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return new SubmeterPreparedStatement(this, sql);
    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
//        return dataSource.getConnection().createStatement(resultSetType, resultSetConcurrency);
        return new SubmeterPreparedStatement(this, resultSetType, resultSetConcurrency);

    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return new SubmeterPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return new SubmeterPreparedStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return new SubmeterPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        return new SubmeterPreparedStatement(this, sql, autoGeneratedKeys);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        return new SubmeterPreparedStatement(this, sql, Statement.RETURN_GENERATED_KEYS);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        return new SubmeterPreparedStatement(this, sql, Statement.RETURN_GENERATED_KEYS);
    }
}
