Skip to content

Commit

Permalink
Merge 822af7c into 2b920e1
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaCN committed Sep 4, 2020
2 parents 2b920e1 + 822af7c commit 752dc4e
Showing 1 changed file with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,37 +95,44 @@ private void initTablesAndIndexes() throws SQLException {
}

private void createJobExecutionTableAndIndexIfNeeded(final Connection connection) throws SQLException {
DatabaseMetaData dbMetaData = connection.getMetaData();
try (ResultSet resultSet = dbMetaData.getTables(connection.getCatalog(), null, TABLE_JOB_EXECUTION_LOG, new String[]{"TABLE"})) {
if (!resultSet.next()) {
createJobExecutionTable(connection);
}
if (existsTable(connection, TABLE_JOB_EXECUTION_LOG) || existsTable(connection, TABLE_JOB_EXECUTION_LOG.toLowerCase())) {
return;
}
createJobExecutionTable(connection);
}

private void createJobStatusTraceTableAndIndexIfNeeded(final Connection connection) throws SQLException {
DatabaseMetaData dbMetaData = connection.getMetaData();
try (ResultSet resultSet = dbMetaData.getTables(connection.getCatalog(), null, TABLE_JOB_STATUS_TRACE_LOG, new String[]{"TABLE"})) {
if (!resultSet.next()) {
createJobStatusTraceTable(connection);
}
if (existsTable(connection, TABLE_JOB_STATUS_TRACE_LOG) || existsTable(connection, TABLE_JOB_STATUS_TRACE_LOG.toLowerCase())) {
return;
}
createJobStatusTraceTable(connection);
createTaskIdIndexIfNeeded(connection);
}

private boolean existsTable(final Connection connection, final String tableName) throws SQLException {
DatabaseMetaData dbMetaData = connection.getMetaData();
try (ResultSet resultSet = dbMetaData.getTables(connection.getCatalog(), null, tableName, new String[]{"TABLE"})) {
return resultSet.next();
}
}

private void createTaskIdIndexIfNeeded(final Connection connection) throws SQLException {
if (existsIndex(connection, TABLE_JOB_STATUS_TRACE_LOG, TASK_ID_STATE_INDEX) || existsIndex(connection, TABLE_JOB_STATUS_TRACE_LOG.toLowerCase(), TASK_ID_STATE_INDEX.toLowerCase())) {
return;
}
createTaskIdAndStateIndex(connection);
}

private boolean existsIndex(final Connection connection, final String tableName, final String indexName) throws SQLException {
DatabaseMetaData dbMetaData = connection.getMetaData();
try (ResultSet resultSet = dbMetaData.getIndexInfo(connection.getCatalog(), null, TABLE_JOB_STATUS_TRACE_LOG, false, false)) {
boolean hasTaskIdIndex = false;
try (ResultSet resultSet = dbMetaData.getIndexInfo(connection.getCatalog(), null, tableName, false, false)) {
while (resultSet.next()) {
if (TASK_ID_STATE_INDEX.equals(resultSet.getString("INDEX_NAME"))) {
hasTaskIdIndex = true;
if (indexName.equals(resultSet.getString("INDEX_NAME"))) {
return true;
}
}
if (!hasTaskIdIndex) {
createTaskIdAndStateIndex(connection);
}
}
return false;
}

private void createJobExecutionTable(final Connection connection) throws SQLException {
Expand Down

0 comments on commit 752dc4e

Please sign in to comment.