All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.etlunit.feature.database.BaseDatabaseImplemenation Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package org.etlunit.feature.database;

import org.etlunit.TestExecutionError;
import org.etlunit.parser.ETLTestValueObject;

import javax.inject.Inject;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public abstract class BaseDatabaseImplemenation implements DatabaseImplementation
{
	private final Map connectionMap = new HashMap();
	protected JDBCClient jdbcClient;

	public void prepareConnectionForInsert(Connection connection, RelationalDataSet target, String mode) throws Exception
	{
	}

	public data_format getDataFormatForExtract(ETLTestValueObject operands)
	{
		return data_format.delimited;
	}

	public database_state getDatabaseState(DatabaseConnection databaseConnection, String s)
	{
		return database_state.pass;
	}

	@Inject
	public void setJdbcClient(JDBCClient client)
	{
		jdbcClient = client;
	}

	public final Connection getConnection(DatabaseConnection dc, String mode) throws TestExecutionError
	{
		return getConnection(dc, mode, DEFAULT_ID);
	}

	public final Connection getConnection(DatabaseConnection dc, String mode, int id) throws TestExecutionError
	{
		String key = dc.getId() + "." + mode + "." + id;
		if (!connectionMap.containsKey(key))
		{
			try
			{
				Class hsqldbDriver = getJdbcDriverClass();
				// do this manually because the automatic way does not work in maven
				DriverManager.registerDriver((Driver) hsqldbDriver.newInstance());

				Connection connection = DriverManager.getConnection(getJdbcUrl(dc, mode, id), getLoginName(dc, mode, id), getPassword(dc, mode, id));
				prepareConnection(connection);
				connectionMap.put(key, connection);
			}
			catch(Exception exc)
			{
				throw new IllegalArgumentException("", exc);
			}
		}

		return connectionMap.get(key);
	}

	protected void prepareConnection(Connection connection) throws Exception
	{
	}

	protected String getPassword(DatabaseConnection dc, String mode, int id)
	{
		return dc.getPassword(mode);
	}

	protected String getLoginName(DatabaseConnection dc, String mode, int id)
	{
		return dc.getLoginName(mode);
	}

	public void returnConnection(Connection conn, DatabaseConnection dc, String mode, int id) throws TestExecutionError
	{
	}

	public final void dispose()
	{
		for (Map.Entry conn : connectionMap.entrySet())
		{
			try
			{
				Connection connection = conn.getValue();
				connection.close();
			}
			catch (SQLException e)
			{
				throw new RuntimeException(e);
			}
		}

		connectionMap.clear();
	}

	protected abstract String getJdbcUrl(DatabaseConnection dc, String mode, int id);

	protected abstract Class getJdbcDriverClass();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy