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

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

There is a newer version: 4.6.0-eu
Show newest version
package org.bitbucket.bradleysmithllc.etlunit.feature.database;

import org.bitbucket.bradleysmithllc.etlunit.Log;
import org.bitbucket.bradleysmithllc.etlunit.TestExecutionError;
import org.bitbucket.bradleysmithllc.etlunit.feature.database.db.Catalog;
import org.bitbucket.bradleysmithllc.etlunit.feature.database.db.Database;
import org.bitbucket.bradleysmithllc.etlunit.feature.database.db.Schema;
import org.bitbucket.bradleysmithllc.etlunit.feature.database.db.Table;

import javax.inject.Inject;
import javax.inject.Named;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

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

	protected Log applicationLog;

	@Override
	public final Object processOperation(operation op, OperationRequest request) throws UnsupportedOperationException {
		switch (op) {
			case dropConstraints:
				InitializeRequest initializeRequest = request.getInitializeRequest();

				// use a jdbc meta data query to find all foreign keys in every table and drop
				try {
					jdbcClient.useStatement(initializeRequest.getConnection(), initializeRequest.getMode(), new JDBCClient.StatementClient() {
						@Override
						public void connection(Connection conn, Statement st, DatabaseConnection connection, String mode, int id) throws Exception {
							Database db = connection.getDatabase();
							DatabaseMetaData md = conn.getMetaData();

							for (Catalog catalog : db.getCatalogs()) {
								for (Schema schema : catalog.getSchemas()) {
									for (final Table table : schema.getTables()) {
										// grab all exported foreign keys and drop for tables
										if (table.getType() == Table.type.table || table.getType() == Table.type.temp_table) {
											ResultSet keysRS = md.getImportedKeys(table.getCatalog().getName(), table.getSchema().getName(), table.getName());

											// track constraints so we don't drop one twice.  This behavior has been observed in jtds
											Map cons = new HashMap();

											try {
												while (keysRS.next()) {
													String FKTABLE_CAT = keysRS.getString(5);
													String FKTABLE_SCHEM = keysRS.getString(6);
													String FKTABLE_NAME = keysRS.getString(7);
													String FK_NAME = keysRS.getString(12);

													String fktab = FKTABLE_CAT + "." + FKTABLE_SCHEM + "." + FKTABLE_NAME;

													// issue a drop for this constraint
													String fkId = escapeIdentifier(FK_NAME);
													String conKey = fktab + "." + fkId;

													if (!cons.containsKey(conKey)) {
														cons.put(conKey, "");

														String SQL = "ALTER TABLE " + escapeQualifiedIdentifier(table) + " DROP CONSTRAINT " + fkId;

														applicationLog.debug("Removing constraint from table " + fktab + " named " + fkId + " using sql '" + SQL + "'");

														st.addBatch(SQL);
													}
												}

												st.executeBatch();
											} finally {
												keysRS.close();
											}
										}
									}
								}
							}
						}
					});

					return null;
				} catch (TestExecutionError testExecutionError) {
					throw new RuntimeException(testExecutionError);
				}
		}

		return processOperationSub(op, request);
	}

	public abstract Object processOperationSub(operation op, OperationRequest request) throws UnsupportedOperationException;

	@Override
	public void prepareConnectionForInsert(Connection connection, Table target, DatabaseConnection dc, String mode) throws Exception {
	}

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

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

	@Inject
	public void setApplicationLog(@Named("applicationLog") Log log) {
		applicationLog = log;
	}

	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)) {
			String jdbcUrl = getJdbcUrl(dc, mode, id);

			applicationLog.info("Using JDBC url '" + jdbcUrl + "'");

			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(jdbcUrl, getLoginName(dc, mode, id), getPassword(dc, mode, id));
				prepareConnection(connection);
				connectionMap.put(key, connection);
			} catch (Exception exc) {
				throw new IllegalArgumentException("Could not connect to URL [" + jdbcUrl + "] using login name '" + getLoginName(dc, mode, id) + "'", 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();
	}

	/**
	 * Default is all system tables are excluded
	 *
	 * @param table
	 * @return
	 */
	@Override
	public boolean isTableTestVisible(DatabaseConnection dc, String mode, Table table) {
		return table.getType() != Table.type.system_table;
	}

	/**
	 * Default issues a truncate table statement, followed by a 'delete from' if that throws an error
	 *
	 * @param table
	 */
	@Override
	public void purgeTableForTest(DatabaseConnection dc, String mode, final Table table) throws Exception {
		// ignore views and system tables
		if (table.getType() != Table.type.system_table && table.getType() != Table.type.view && table.getType() != Table.type.synthetic && table.getType() != Table.type.sql) {
			// try with truncate, then try delete from
			try {
				jdbcClient.useStatement(dc, mode, new JDBCClient.StatementClient() {
					@Override
					public void connection(Connection conn, Statement st, DatabaseConnection connection, String mode, int id) throws Exception {
						StringBuilder stb = new StringBuilder();

						stb.append("TRUNCATE TABLE ");
						stb.append(escapeQualifiedIdentifier(table));

						st.execute(stb.toString());
					}
				});
			} catch (Exception exc) {
				jdbcClient.useStatement(dc, mode, new JDBCClient.StatementClient() {
					@Override
					public void connection(Connection conn, Statement st, DatabaseConnection connection, String mode, int id) throws Exception {
						StringBuilder stb = new StringBuilder();

						stb.append("DELETE FROM ");
						stb.append(escapeQualifiedIdentifier(table));

						st.execute(stb.toString());
					}
				});
			}
		}
	}

	@Override
	public Table.type translateTableType(String JDBCMetaTableTypeName) {
		if (JDBCMetaTableTypeName.equals("TABLE")) {
			return Table.type.table;
		} else if (JDBCMetaTableTypeName.equals("SYSTEM TABLE")) {
			return Table.type.system_table;
		} else if (JDBCMetaTableTypeName.equals("VIEW")) {
			return Table.type.view;
		} else if (JDBCMetaTableTypeName.equals("GLOBAL TEMPORARY")) {
			return Table.type.temp_table;
		} else if (JDBCMetaTableTypeName.equals("LOCAL TEMPORARY")) {
			return Table.type.temp_table;
		} else if (JDBCMetaTableTypeName.equals("ALIAS")) {
			return Table.type.table;
		} else if (JDBCMetaTableTypeName.equals("SYNONYM")) {
			return Table.type.table;
		}

		return null;
	}

	/**
	 * Use the common quote character for escaping.
	 *
	 * @param table
	 * @return
	 * @throws Exception
	 */
	@Override
	public String escapeQualifiedIdentifier(Table table) throws Exception {
		StringBuilder stb = new StringBuilder();

		String catalogName = table.getCatalog().getName();
		if (catalogName != null) {
			stb.append(escapeIdentifier(catalogName));
			stb.append('.');
		}

		String schemaName = table.getSchema().getName();
		if (schemaName != null) {
			stb.append(escapeIdentifier(schemaName));
			stb.append('.');
		}

		stb.append(escapeIdentifier(table.getName()));

		return stb.toString();
	}

	@Override
	public String escapeIdentifier(String name) throws Exception {
		return '"' + name + '"';
	}

	@Override
	public String getJdbcUrl(DatabaseConnection dc, String mode) {
		return getJdbcUrl(dc, mode, 0);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy