|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.testing.cleaner; |
| 8 | + |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.function.Function; |
| 18 | +import java.util.logging.Level; |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Christian Beikov |
| 23 | + */ |
| 24 | +public abstract class AbstractMySQLDatabaseCleaner implements DatabaseCleaner { |
| 25 | + private static final Logger LOG = Logger.getLogger( AbstractMySQLDatabaseCleaner.class.getName() ); |
| 26 | + private static final String SYSTEM_SCHEMAS = "'information_schema'," + |
| 27 | + "'mysql'," + |
| 28 | + "'sys'," + |
| 29 | + "'performance_schema'"; |
| 30 | + private final List<String> ignoredTables = new ArrayList<>(); |
| 31 | + private final Map<String, List<String>> clearingSqlsPerSchema = new HashMap<>(); |
| 32 | + |
| 33 | + @Override |
| 34 | + public void addIgnoredTable(String tableName) { |
| 35 | + ignoredTables.add( tableName ); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void clearAllSchemas(Connection connection) { |
| 40 | + try { |
| 41 | + clearSchema( connection, connection.getSchema() ); |
| 42 | + } |
| 43 | + catch (SQLException e) { |
| 44 | + throw new RuntimeException( e ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void clearSchema(Connection connection, String schemaName) { |
| 50 | + clearSchema0( connection, schemaName ); |
| 51 | + } |
| 52 | + |
| 53 | + private void clearSchema0(Connection c, String schemaName) { |
| 54 | + clearingSqlsPerSchema.remove( schemaName ); |
| 55 | + try (Statement s = c.createStatement()) { |
| 56 | + // Collect schema names |
| 57 | + LOG.log( Level.FINEST, "Collect table names: START" ); |
| 58 | + ResultSet rs = s.executeQuery( |
| 59 | + "SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '" + schemaName + "'" ); |
| 60 | + |
| 61 | + StringBuilder sb = new StringBuilder( "DROP TABLE " ); |
| 62 | + if ( rs.next() ) { |
| 63 | + do { |
| 64 | + String tableSchema = rs.getString( 1 ); |
| 65 | + String tableName = rs.getString( 2 ); |
| 66 | + sb.append( tableSchema ); |
| 67 | + sb.append( '.' ); |
| 68 | + sb.append( tableName ); |
| 69 | + sb.append( ',' ); |
| 70 | + } while ( rs.next() ); |
| 71 | + } |
| 72 | + else { |
| 73 | + // Nothing to clear since there are no tables |
| 74 | + return; |
| 75 | + } |
| 76 | + sb.setCharAt( sb.length() - 1, ' ' ); |
| 77 | + LOG.log( Level.FINEST, "Collect table names: END" ); |
| 78 | + |
| 79 | + // Disable foreign keys |
| 80 | + LOG.log( Level.FINEST, "Disable foreign keys: START" ); |
| 81 | + s.execute( "SET FOREIGN_KEY_CHECKS = 0" ); |
| 82 | + LOG.log( Level.FINEST, "Disable foreign keys: END" ); |
| 83 | + |
| 84 | + LOG.log( Level.FINEST, "Dropping tables: START" ); |
| 85 | + String sql = sb.toString(); |
| 86 | + s.execute( sql ); |
| 87 | + LOG.log( Level.FINEST, "Dropping tables: END" ); |
| 88 | + |
| 89 | + // Enable foreign keys |
| 90 | + LOG.log( Level.FINEST, "Enabling foreign keys: START" ); |
| 91 | + s.execute( "SET FOREIGN_KEY_CHECKS = 1" ); |
| 92 | + LOG.log( Level.FINEST, "Enabling foreign keys: END" ); |
| 93 | + |
| 94 | + LOG.log( Level.FINEST, "Committing: START" ); |
| 95 | + c.commit(); |
| 96 | + LOG.log( Level.FINEST, "Committing: END" ); |
| 97 | + } |
| 98 | + catch (SQLException e) { |
| 99 | + try { |
| 100 | + c.rollback(); |
| 101 | + } |
| 102 | + catch (SQLException e1) { |
| 103 | + e.addSuppressed( e1 ); |
| 104 | + } |
| 105 | + |
| 106 | + throw new RuntimeException( e ); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void clearAllData(Connection connection) { |
| 112 | + clearData0( |
| 113 | + connection, |
| 114 | + null, |
| 115 | + statement -> { |
| 116 | + try { |
| 117 | + return statement.executeQuery( |
| 118 | + "SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA NOT IN (" + SYSTEM_SCHEMAS + ")" ); |
| 119 | + } |
| 120 | + catch (SQLException e) { |
| 121 | + throw new RuntimeException( e ); |
| 122 | + } |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void clearData(Connection connection, String schemaName) { |
| 129 | + clearData0( |
| 130 | + connection, |
| 131 | + schemaName, |
| 132 | + statement -> { |
| 133 | + try { |
| 134 | + return statement.executeQuery( |
| 135 | + "SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '" + schemaName + "'" ); |
| 136 | + } |
| 137 | + catch (SQLException e) { |
| 138 | + throw new RuntimeException( e ); |
| 139 | + } |
| 140 | + } |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + private void clearData0(Connection connection, String schemaName, Function<Statement, ResultSet> tablesProvider) { |
| 145 | + try (Statement s = connection.createStatement()) { |
| 146 | + // Disable foreign keys |
| 147 | + LOG.log( Level.FINEST, "Disable foreign keys: START" ); |
| 148 | + s.execute( "SET FOREIGN_KEY_CHECKS = 0" ); |
| 149 | + LOG.log( Level.FINEST, "Disable foreign keys: END" ); |
| 150 | + |
| 151 | + // Delete data |
| 152 | + LOG.log( Level.FINEST, "Deleting data: START" ); |
| 153 | + List<String> clearingSqls = clearingSqlsPerSchema.get( schemaName ); |
| 154 | + if ( clearingSqls == null ) { |
| 155 | + clearingSqls = new ArrayList<>(); |
| 156 | + ResultSet rs = tablesProvider.apply( s ); |
| 157 | + while ( rs.next() ) { |
| 158 | + String tableSchema = rs.getString( 1 ); |
| 159 | + String tableName = rs.getString( 2 ); |
| 160 | + if ( !ignoredTables.contains( tableName ) ) { |
| 161 | + clearingSqls.add( createClearingStatementForTable( tableSchema, tableName ) ); |
| 162 | + } |
| 163 | + } |
| 164 | + clearingSqlsPerSchema.put( schemaName, clearingSqls ); |
| 165 | + } |
| 166 | + for ( String clearingSql : clearingSqls ) { |
| 167 | + s.execute( clearingSql ); |
| 168 | + } |
| 169 | + LOG.log( Level.FINEST, "Deleting data: END" ); |
| 170 | + |
| 171 | + // Enable foreign keys |
| 172 | + LOG.log( Level.FINEST, "Enabling foreign keys: START" ); |
| 173 | + s.execute( "SET FOREIGN_KEY_CHECKS = 1" ); |
| 174 | + LOG.log( Level.FINEST, "Enabling foreign keys: END" ); |
| 175 | + |
| 176 | + LOG.log( Level.FINEST, "Committing: START" ); |
| 177 | + connection.commit(); |
| 178 | + LOG.log( Level.FINEST, "Committing: END" ); |
| 179 | + } |
| 180 | + catch (SQLException e) { |
| 181 | + try { |
| 182 | + connection.rollback(); |
| 183 | + } |
| 184 | + catch (SQLException e1) { |
| 185 | + e.addSuppressed( e1 ); |
| 186 | + } |
| 187 | + |
| 188 | + throw new RuntimeException( e ); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + protected abstract String createClearingStatementForTable(String tableSchema, String tableName); |
| 193 | + |
| 194 | +} |
0 commit comments