public Connection openConnection() throws SQLException { return getConnection(); }
public void executeDdl(String ddl) { try (Connection connection = getConnection(); Statement stmt = connection.createStatement()) { stmt.execute(ddl); } catch (SQLException e) { throw new IllegalStateException("Failed to execute DDL: " + ddl, e); } }
public void assertTableDoesNotExist(String table) { try (Connection connection = getConnection()) { boolean tableExists = DatabaseUtils.tableExists(table, connection); assertThat(tableExists).isFalse(); } catch (Exception e) { throw new IllegalStateException("Fail to check if table exists", e); } }
/** * Verify that index with name {@code indexName} does not exist on the table {@code tableName} */ public void assertIndexDoesNotExist(String tableName, String indexName) { try (Connection connection = getConnection(); ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) { List<String> indices = new ArrayList<>(); while (rs.next()) { indices.add(rs.getString("INDEX_NAME").toLowerCase(Locale.ENGLISH)); } assertThat(indices).doesNotContain(indexName); } catch (SQLException e) { throw new IllegalStateException("Fail to check existence of index", e); } }
private void assertIndexImpl(String tableName, String indexName, boolean expectedUnique, String expectedColumn, String... expectedSecondaryColumns) { try (Connection connection = getConnection(); ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) { List<String> onColumns = new ArrayList<>(); while (rs.next()) { if (indexName.equalsIgnoreCase(rs.getString("INDEX_NAME"))) { assertThat(rs.getBoolean("NON_UNIQUE")).isEqualTo(!expectedUnique); int position = rs.getInt("ORDINAL_POSITION"); onColumns.add(position - 1, rs.getString("COLUMN_NAME").toLowerCase(Locale.ENGLISH)); } } assertThat(asList(expectedColumn, expectedSecondaryColumns)).isEqualTo(onColumns); } catch (SQLException e) { throw new IllegalStateException("Fail to check index", e); } }
public void assertColumnDoesNotExist(String table, String column) throws SQLException { try (Connection connection = getConnection(); PreparedStatement stmt = connection.prepareStatement("select * from " + table); ResultSet res = stmt.executeQuery()) { assertThat(getColumnNames(res)).doesNotContain(column); } }
public void executeUpdateSql(String sql, Object... params) { try (Connection connection = getConnection()) { new QueryRunner().update(connection, sql, params); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { SQLException nextException = e.getNextException(); if (nextException != null) { throw new IllegalStateException("Fail to execute sql: " + sql, new SQLException(e.getMessage(), nextException.getSQLState(), nextException.getErrorCode(), nextException)); } throw new IllegalStateException("Fail to execute sql: " + sql, e); } catch (Exception e) { throw new IllegalStateException("Fail to execute sql: " + sql, e); } }
public void assertColumnDefinition(String table, String column, int expectedType, @Nullable Integer expectedSize, @Nullable Boolean isNullable) { try (Connection connection = getConnection(); PreparedStatement stmt = connection.prepareStatement("select * from " + table); ResultSet res = stmt.executeQuery()) { Integer columnIndex = getColumnIndex(res, column); if (columnIndex == null) { fail("The column '" + column + "' does not exist"); } assertThat(res.getMetaData().getColumnType(columnIndex)).isEqualTo(expectedType); if (expectedSize != null) { assertThat(res.getMetaData().getColumnDisplaySize(columnIndex)).isEqualTo(expectedSize); } if (isNullable != null) { assertThat(res.getMetaData().isNullable(columnIndex)).isEqualTo(isNullable ? columnNullable : columnNoNulls); } } catch (Exception e) { throw new IllegalStateException("Fail to check column", e); } }
public void assertPrimaryKey(String tableName, @Nullable String expectedPkName, String columnName, String... otherColumnNames) { try (Connection connection = getConnection()) { PK pk = pkOf(connection, tableName.toUpperCase(Locale.ENGLISH)); if (pk == null) { pkOf(connection, tableName.toLowerCase(Locale.ENGLISH)); } assertThat(pk).as("No primary key is defined on table %s", tableName).isNotNull(); if (expectedPkName != null) { assertThat(pk.getName()).isEqualToIgnoringCase(expectedPkName); } List<String> expectedColumns = ImmutableList.copyOf(Iterables.concat(Collections.singletonList(columnName), Arrays.asList(otherColumnNames))); assertThat(pk.getColumns()).as("Primary key does not have the '%s' expected columns", expectedColumns.size()).hasSize(expectedColumns.size()); Iterator<String> expectedColumnsIt = expectedColumns.iterator(); Iterator<String> actualColumnsIt = pk.getColumns().iterator(); while (expectedColumnsIt.hasNext() && actualColumnsIt.hasNext()) { assertThat(actualColumnsIt.next()).isEqualToIgnoringCase(expectedColumnsIt.next()); } } catch (SQLException e) { throw new IllegalStateException("Fail to check primary key", e); } }