package io.artur.test;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

public class ExceptionDataSource implements DataSource {

	private DataSource delegate;

	public ExceptionDataSource(final DataSource delegate) {
		this.delegate = delegate;
	}
	
	public PrintWriter getLogWriter() throws SQLException {
		return delegate.getLogWriter();
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
		delegate.setLogWriter(out);
	}

	public void setLoginTimeout(int seconds) throws SQLException {
		delegate.setLoginTimeout(seconds);
	}

	public int getLoginTimeout() throws SQLException {
		return delegate.getLoginTimeout();
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return delegate.getParentLogger();
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return delegate.unwrap(iface);
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return delegate.isWrapperFor(iface);
	}

	@Override
	public Connection getConnection() throws SQLException {
		if(exceptionNext) {
			exceptionNext = false;
			throw new SQLException("New Test Exception");
		}
		return delegate.getConnection();
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		if(exceptionNext) {
			exceptionNext = false;
			throw new SQLException("New Test Exception");
		}
		return delegate.getConnection();
	}
	
	private boolean exceptionNext;
	
	public void exceptionNext() {
		exceptionNext = true;
	}

}
