99import java .sql .Statement ;
1010import java .util .Arrays ;
1111import java .util .Collection ;
12+ import java .util .List ;
1213
1314import static org .firebirdsql .util .FirebirdSupportInfo .supportInfoFor ;
1415
1718 *
1819 * @author Mark Rotteveel
1920 */
20- @ SuppressWarnings ("SqlSourceToSinkFlow" )
2121public 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