Skip to content

Commit e37afa1

Browse files
committed
Ignore drop schema failure, refactor to single code path
1 parent 0c9efec commit e37afa1

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

src/test/org/firebirdsql/common/DdlHelper.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.sql.Statement;
1010
import java.util.Arrays;
1111
import java.util.Collection;
12+
import java.util.List;
1213

1314
import static org.firebirdsql.util.FirebirdSupportInfo.supportInfoFor;
1415

@@ -17,7 +18,6 @@
1718
*
1819
* @author Mark Rotteveel
1920
*/
20-
@SuppressWarnings("SqlSourceToSinkFlow")
2121
public final class DdlHelper {
2222

2323
private DdlHelper() {
@@ -69,9 +69,7 @@ public static void executeCreateTable(final Statement statement, final String sq
6969
*/
7070
public static void executeDDL(final Connection connection, final String sql, final int... ignoreErrors)
7171
throws SQLException {
72-
try (Statement stmt = connection.createStatement()) {
73-
executeDDL(stmt, sql, ignoreErrors);
74-
}
72+
executeDDL(connection, List.of(sql), ignoreErrors);
7573
}
7674

7775
/**
@@ -111,10 +109,7 @@ public static void executeDDL(final Connection connection, final Collection<Stri
111109
*/
112110
public static void executeDDL(final Statement statement, final String sql, final int... ignoreErrors)
113111
throws SQLException {
114-
if (ignoreErrors != null) {
115-
Arrays.sort(ignoreErrors);
116-
}
117-
executeDDL0(statement, sql, ignoreErrors);
112+
executeDDL(statement, List.of(sql), ignoreErrors);
118113
}
119114

120115
/**
@@ -136,26 +131,41 @@ public static void executeDDL(final Statement statement, final Collection<String
136131
if (ignoreErrors != null) {
137132
Arrays.sort(ignoreErrors);
138133
}
139-
for (String currentSql : sql) {
140-
executeDDL0(statement, currentSql, ignoreErrors);
134+
Connection connection = statement.getConnection();
135+
final boolean autoCommitAtStart = connection.getAutoCommit();
136+
if (autoCommitAtStart && sql.size() > 1) {
137+
connection.setAutoCommit(false);
138+
}
139+
try {
140+
for (String currentSql : sql) {
141+
executeDDL0(statement, currentSql, ignoreErrors);
142+
}
143+
if (!autoCommitAtStart) {
144+
connection.commit();
145+
}
146+
} finally {
147+
// if we were not in auto commit at start and an exception occurred, the transaction will still be pending
148+
if (autoCommitAtStart) {
149+
connection.setAutoCommit(true);
150+
}
141151
}
142152
}
143153

144154
private static void executeDDL0(Statement statement, String sql, int[] ignoreErrors) throws SQLException {
145155
try {
146156
statement.execute(sql);
147-
} catch (SQLException ex) {
157+
} catch (SQLException e) {
148158
if (ignoreErrors == null || ignoreErrors.length == 0)
149-
throw ex;
159+
throw e;
150160

151-
for (Throwable current : ex) {
152-
if (current instanceof SQLException
153-
&& Arrays.binarySearch(ignoreErrors, ((SQLException) current).getErrorCode()) >= 0) {
161+
for (Throwable current : e) {
162+
if (current instanceof SQLException currentSqle
163+
&& Arrays.binarySearch(ignoreErrors, currentSqle.getErrorCode()) >= 0) {
154164
return;
155165
}
156166
}
157167

158-
throw ex;
168+
throw e;
159169
}
160170
}
161171

0 commit comments

Comments
 (0)