@@ -0,0 +1,2275 @@
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.stereotype.Service;%0A%0Aimport javax.sql.DataSource;%0Aimport java.sql.*;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A@Service%0Apublic class TableClearer %7B%0A    private final Logger logger = LoggerFactory.getLogger(getClass());%0A%0A    @Autowired%0A    private DataSource dataSource;%0A%0A    private Connection connection;%0A%0A    public void clearTables() %7B%0A        try %7B%0A            connection = dataSource.getConnection();%0A            tryToClearTables();%0A        %7D catch (SQLException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    private void tryToClearTables() throws SQLException %7B%0A        List%3CString%3E tableNames = getTableNames();%0A        clear(tableNames);%0A    %7D%0A%0A    private List%3CString%3E getTableNames() throws SQLException %7B%0A        List%3CString%3E tableNames = new ArrayList%3C%3E();%0A%0A        DatabaseMetaData metaData = connection.getMetaData();%0A        ResultSet rs = metaData.getTables(%0A                connection.getCatalog(), null, null, new String%5B%5D%7B%22TABLE%22%7D);%0A%0A        while (rs.next()) %7B%0A            tableNames.add(rs.getString(%22TABLE_NAME%22));%0A        %7D%0A%0A        return tableNames;%0A    %7D%0A%0A    private void clear(List%3CString%3E tableNames) throws SQLException %7B%0A        Statement statement = buildSqlStatement(tableNames);%0A%0A        logger.debug(%22Executing SQL%22);%0A        statement.executeBatch();%0A    %7D%0A%0A    private Statement buildSqlStatement(List%3CString%3E tableNames) throws SQLException %7B%0A        Statement statement = connection.createStatement();%0A%0A        statement.addBatch(sql(%22SET FOREIGN_KEY_CHECKS = 0%22));%0A        addDeleteSatements(tableNames, statement);%0A        statement.addBatch(sql(%22SET FOREIGN_KEY_CHECKS = 1%22));%0A%0A        return statement;%0A    %7D%0A%0A    private void addDeleteSatements(List%3CString%3E tableNames, Statement statement) %7B%0A        tableNames.forEach(tableName -%3E %7B%0A            try %7B%0A                statement.addBatch(sql(%22DELETE FROM %22 + tableName));%0A            %7D catch (SQLException e) %7B%0A                throw new RuntimeException(e);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private String sql(String sql) %7B%0A        logger.debug(%22Adding SQL: %7B%7D%22, sql);%0A        return sql;%0A    %7D%0A%7D%0A

Create TableClearer.java