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

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

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

import org.etlunit.TestExecutionError;

import javax.inject.Inject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCClientImpl implements JDBCClient
{
	private DatabaseFeatureModule databaseFeatureModule;

	@Inject
	public void setDatabaseFeatureModule(DatabaseFeatureModule databaseFeatureModule)
	{
		this.databaseFeatureModule = databaseFeatureModule;
	}

	public void useConnection(DatabaseConnection connection, String mode, ConnectionClient client) throws TestExecutionError
	{
		useConnection(connection, mode, client, DatabaseImplementation.DEFAULT_ID);
	}

	public void useConnection(DatabaseConnection connection, String mode, ConnectionClient client, int id) throws TestExecutionError
	{
		DatabaseImplementation dbConn = databaseFeatureModule.getImplementation(connection.getId());

		try
		{
			Connection sqlConn = dbConn.getConnection(connection, mode, id);

			try
			{
				client.connection(sqlConn, connection, mode, id);
			}
			finally
			{
				dbConn.returnConnection(sqlConn, connection, mode, id);
			}
		}
		catch (SQLException exc)
		{
			throw new TestExecutionError("", DatabaseConstants.ERR_SQL_FAILURE, exc);
		}
		catch (TestExecutionError exc)
		{
			throw exc;
		}
		catch (Exception exc)
		{
			throw new TestExecutionError("", exc);
		}
	}

	public void useStatement(DatabaseConnection connection, String mode, StatementClient client) throws TestExecutionError
	{
		useStatement(connection, mode, client, DatabaseImplementation.DEFAULT_ID);
	}

	public void useStatement(DatabaseConnection connection, String mode, final StatementClient client, int id) throws TestExecutionError
	{
		useConnection(connection, mode, new ConnectionClient()
		{
			public void connection(Connection conn, DatabaseConnection connection, String mode, int id) throws Exception
			{
				Statement st = conn.createStatement();

				try
				{
					client.connection(conn, st, connection, mode, id);
				}
				finally
				{
					st.close();
				}
			}
		}, id);
	}

	public void usePreparedStatement(DatabaseConnection connection, String mode, PreparedStatementClient client) throws TestExecutionError
	{
		usePreparedStatement(connection, mode, client, DatabaseImplementation.DEFAULT_ID);
	}

	public void usePreparedStatement(DatabaseConnection connection, String mode, final PreparedStatementClient client, int id) throws TestExecutionError
	{
		useConnection(connection, mode, new ConnectionClient()
		{
			public void connection(Connection conn, DatabaseConnection connection, String mode, int id) throws Exception
			{
				PreparedStatement st = conn.prepareStatement(client.prepareText());

				try
				{
					client.connection(conn, st, connection, mode, id);
				}
				finally
				{
					st.close();
				}
			}
		}, id);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy