Skip to content

Commit

Permalink
More migrator updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Nov 1, 2018
1 parent e425d19 commit 9906243
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 144 deletions.
Expand Up @@ -8,8 +8,6 @@
import org.springframework.beans.factory.DisposableBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;

Expand All @@ -18,7 +16,6 @@
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

/*-
* #%L
Expand Down Expand Up @@ -77,13 +74,17 @@ public ConnectionProperties newConnectionProperties(String theUrl, String theUse
throw new InternalErrorException("Unable to find driver class: " + myDriverClassName, e);
}

BasicDataSource dataSource = new BasicDataSource();
// dataSource.setAutoCommit(false);
BasicDataSource dataSource = new BasicDataSource(){
@Override
public Connection getConnection() throws SQLException {
ourLog.info("Creating new DB connection");
return super.getConnection();
}
};
dataSource.setDriverClassName(myDriverClassName);
dataSource.setUrl(theUrl);
dataSource.setUsername(theUsername);
dataSource.setPassword(thePassword);
// dataSource.setSuppressClose(true);

DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
Expand Down
Expand Up @@ -45,56 +45,56 @@ public class JdbcUtils {
public static Set<String> getIndexNames(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName) throws SQLException {

DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, true);
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, true);

Set<String> indexNames = new HashSet<>();
while (indexes.next()) {
Set<String> indexNames = new HashSet<>();
while (indexes.next()) {

ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));
ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));

String indexName = indexes.getString("INDEX_NAME");
indexName = toUpperCase(indexName, Locale.US);
indexNames.add(indexName);
}

return indexNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
String indexName = indexes.getString("INDEX_NAME");
indexName = toUpperCase(indexName, Locale.US);
indexNames.add(indexName);
}

return indexNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
}
}

@SuppressWarnings("ConstantConditions")
public static boolean isIndexUnique(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theIndexName) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, false);

while (indexes.next()) {
String indexName = indexes.getString("INDEX_NAME");
if (theIndexName.equalsIgnoreCase(indexName)) {
boolean nonUnique = indexes.getBoolean("NON_UNIQUE");
return !nonUnique;
}
}
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, false);

} catch (SQLException e) {
throw new InternalErrorException(e);
}
while (indexes.next()) {
String indexName = indexes.getString("INDEX_NAME");
if (theIndexName.equalsIgnoreCase(indexName)) {
boolean nonUnique = indexes.getBoolean("NON_UNIQUE");
return !nonUnique;
}
}

throw new InternalErrorException("Can't find index: " + theIndexName + " on table " + theTableName);
});
} catch (SQLException e) {
throw new InternalErrorException(e);
}

throw new InternalErrorException("Can't find index: " + theIndexName + " on table " + theTableName);
});
}
}

/**
Expand Down Expand Up @@ -153,130 +153,132 @@ public static String getColumnType(DriverTypeEnum.ConnectionProperties theConnec
*/
public static Set<String> getForeignKeys(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theForeignTable) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getCrossReference(null, null, theTableName, null, null, theForeignTable);

Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("PKTABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
tableName = toUpperCase(indexes.getString("FKTABLE_NAME"), Locale.US);
if (!theForeignTable.equalsIgnoreCase(tableName)) {
continue;
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getCrossReference(null, null, theTableName, null, null, theForeignTable);

Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("PKTABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
tableName = toUpperCase(indexes.getString("FKTABLE_NAME"), Locale.US);
if (!theForeignTable.equalsIgnoreCase(tableName)) {
continue;
}

String fkName = indexes.getString("FK_NAME");
fkName = toUpperCase(fkName, Locale.US);
columnNames.add(fkName);
}

String fkName = indexes.getString("FK_NAME");
fkName = toUpperCase(fkName, Locale.US);
columnNames.add(fkName);
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}

return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
});
}
}

/**
* Retrieve all index names
*/
public static Set<String> getColumnNames(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getColumns(null, null, null, null);

Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getColumns(null, null, null, null);

String columnName = indexes.getString("COLUMN_NAME");
columnName = toUpperCase(columnName, Locale.US);
columnNames.add(columnName);
}
Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}

return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
String columnName = indexes.getString("COLUMN_NAME");
columnName = toUpperCase(columnName, Locale.US);
columnNames.add(columnName);
}

return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
}
}

public static Set<String> getTableNames(DriverTypeEnum.ConnectionProperties theConnectionProperties) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getTables(null, null, null, null);

Set<String> columnNames = new HashSet<>();
while (tables.next()) {
String tableName = tables.getString("TABLE_NAME");
tableName = toUpperCase(tableName, Locale.US);

String tableType = tables.getString("TABLE_TYPE");
if ("SYSTEM TABLE".equalsIgnoreCase(tableType)) {
continue;
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getTables(null, null, null, null);

Set<String> columnNames = new HashSet<>();
while (tables.next()) {
String tableName = tables.getString("TABLE_NAME");
tableName = toUpperCase(tableName, Locale.US);

String tableType = tables.getString("TABLE_TYPE");
if ("SYSTEM TABLE".equalsIgnoreCase(tableType)) {
continue;
}

columnNames.add(tableName);
}

columnNames.add(tableName);
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}

return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
});
}
}

public static boolean isColumnNullable(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theColumnName) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
//noinspection ConstantConditions
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getColumns(null, null, null, null);

while (tables.next()) {
String tableName = toUpperCase(tables.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
try (Connection connection = dataSource.getConnection()) {
//noinspection ConstantConditions
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getColumns(null, null, null, null);

if (theColumnName.equalsIgnoreCase(tables.getString("COLUMN_NAME"))) {
String nullable = tables.getString("IS_NULLABLE");
if ("YES".equalsIgnoreCase(nullable)) {
return true;
} else if ("NO".equalsIgnoreCase(nullable)) {
return false;
} else {
throw new IllegalStateException("Unknown nullable: " + nullable);
while (tables.next()) {
String tableName = toUpperCase(tables.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
}
}

throw new IllegalStateException("Did not find column " + theColumnName);
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
if (theColumnName.equalsIgnoreCase(tables.getString("COLUMN_NAME"))) {
String nullable = tables.getString("IS_NULLABLE");
if ("YES".equalsIgnoreCase(nullable)) {
return true;
} else if ("NO".equalsIgnoreCase(nullable)) {
return false;
} else {
throw new IllegalStateException("Unknown nullable: " + nullable);
}
}
}

throw new IllegalStateException("Did not find column " + theColumnName);
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
}
}
}
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -277,7 +277,6 @@ private void init350() {
Builder.BuilderWithTableName spp = version.onTable("HFJ_RES_PARAM_PRESENT");
version.startSectionWithMessage("Starting work on table: " + spp.getTableName());
spp.dropIndex("IDX_RESPARMPRESENT_SPID_RESID");
spp.dropColumn("SP_ID");
spp
.addColumn("HASH_PRESENCE")
.nullable()
Expand Down Expand Up @@ -307,6 +306,9 @@ private void init350() {
});
version.addTask(consolidateSearchParamPresenceIndexesTask);

// SP_ID is no longer needed
spp.dropColumn("SP_ID");

// Concept
Builder.BuilderWithTableName trmConcept = version.onTable("TRM_CONCEPT");
version.startSectionWithMessage("Starting work on table: " + trmConcept.getTableName());
Expand Down

0 comments on commit 9906243

Please sign in to comment.