@@ -157,23 +157,24 @@ public static ResultSet execute(Connection conn, Reader reader)
157157 // can not close the statement because we return a result set from it
158158 Statement stat = conn .createStatement ();
159159 ResultSet rs = null ;
160- ScriptReader r = new ScriptReader (reader );
161- while (true ) {
162- String sql = r .readStatement ();
163- if (sql == null ) {
164- break ;
165- }
166- if (StringUtils .isWhitespaceOrEmpty (sql )) {
167- continue ;
168- }
169- boolean resultSet = stat .execute (sql );
170- if (resultSet ) {
171- if (rs != null ) {
172- rs .close ();
173- rs = null ;
174- }
175- rs = stat .getResultSet ();
176- }
160+ try (ScriptReader r = new ScriptReader (reader )) {
161+ while (true ) {
162+ String sql = r .readStatement ();
163+ if (sql == null ) {
164+ break ;
165+ }
166+ if (StringUtils .isWhitespaceOrEmpty (sql )) {
167+ continue ;
168+ }
169+ boolean resultSet = stat .execute (sql );
170+ if (resultSet ) {
171+ if (rs != null ) {
172+ rs .close ();
173+ rs = null ;
174+ }
175+ rs = stat .getResultSet ();
176+ }
177+ }
177178 }
178179 return rs ;
179180 }
@@ -192,76 +193,77 @@ private void process(Connection conn, String fileName,
192193 private void process (Connection conn , boolean continueOnError , String path ,
193194 Reader reader , Charset charset ) throws SQLException , IOException {
194195 Statement stat = conn .createStatement ();
195- ScriptReader r = new ScriptReader (reader );
196- while (true ) {
197- String sql = r .readStatement ();
198- if (sql == null ) {
199- break ;
200- }
201- String trim = sql .trim ();
202- if (trim .isEmpty ()) {
203- continue ;
204- }
205- if (trim .startsWith ("@" ) && StringUtils .toUpperEnglish (trim ).
206- startsWith ("@INCLUDE" )) {
207- sql = StringUtils .trimSubstring (sql , "@INCLUDE" .length ());
208- if (!FileUtils .isAbsolute (sql )) {
209- sql = path + File .separatorChar + sql ;
210- }
211- process (conn , sql , continueOnError , charset );
212- } else {
213- try {
214- if (showResults && !trim .startsWith ("-->" )) {
215- out .print (sql + ";" );
216- }
217- if (showResults || checkResults ) {
218- boolean query = stat .execute (sql );
219- if (query ) {
220- ResultSet rs = stat .getResultSet ();
221- int columns = rs .getMetaData ().getColumnCount ();
222- StringBuilder buff = new StringBuilder ();
223- while (rs .next ()) {
224- buff .append ("\n -->" );
225- for (int i = 0 ; i < columns ; i ++) {
226- String s = rs .getString (i + 1 );
227- if (s != null ) {
228- s = StringUtils .replaceAll (s , "\r \n " , "\n " );
229- s = StringUtils .replaceAll (s , "\n " , "\n --> " );
230- s = StringUtils .replaceAll (s , "\r " , "\r --> " );
231- }
232- buff .append (' ' ).append (s );
233- }
234- }
235- buff .append ("\n ;" );
236- String result = buff .toString ();
237- if (showResults ) {
238- out .print (result );
239- }
240- if (checkResults ) {
241- String expected = r .readStatement () + ";" ;
242- expected = StringUtils .replaceAll (expected , "\r \n " , "\n " );
243- expected = StringUtils .replaceAll (expected , "\r " , "\n " );
244- if (!expected .equals (result )) {
245- expected = StringUtils .replaceAll (expected , " " , "+" );
246- result = StringUtils .replaceAll (result , " " , "+" );
247- throw new SQLException (
248- "Unexpected output for:\n " + sql .trim () +
249- "\n Got:\n " + result + "\n Expected:\n " + expected );
250- }
251- }
252-
253- }
254- } else {
255- stat .execute (sql );
256- }
257- } catch (Exception e ) {
258- if (continueOnError ) {
259- e .printStackTrace (out );
260- } else {
261- throw DbException .toSQLException (e );
262- }
263- }
264- }
196+ try (ScriptReader r = new ScriptReader (reader )) {
197+ while (true ) {
198+ String sql = r .readStatement ();
199+ if (sql == null ) {
200+ break ;
201+ }
202+ String trim = sql .trim ();
203+ if (trim .isEmpty ()) {
204+ continue ;
205+ }
206+ if (trim .startsWith ("@" ) && StringUtils .toUpperEnglish (trim ).
207+ startsWith ("@INCLUDE" )) {
208+ sql = StringUtils .trimSubstring (sql , "@INCLUDE" .length ());
209+ if (!FileUtils .isAbsolute (sql )) {
210+ sql = path + File .separatorChar + sql ;
211+ }
212+ process (conn , sql , continueOnError , charset );
213+ } else {
214+ try {
215+ if (showResults && !trim .startsWith ("-->" )) {
216+ out .print (sql + ";" );
217+ }
218+ if (showResults || checkResults ) {
219+ boolean query = stat .execute (sql );
220+ if (query ) {
221+ ResultSet rs = stat .getResultSet ();
222+ int columns = rs .getMetaData ().getColumnCount ();
223+ StringBuilder buff = new StringBuilder ();
224+ while (rs .next ()) {
225+ buff .append ("\n -->" );
226+ for (int i = 0 ; i < columns ; i ++) {
227+ String s = rs .getString (i + 1 );
228+ if (s != null ) {
229+ s = StringUtils .replaceAll (s , "\r \n " , "\n " );
230+ s = StringUtils .replaceAll (s , "\n " , "\n --> " );
231+ s = StringUtils .replaceAll (s , "\r " , "\r --> " );
232+ }
233+ buff .append (' ' ).append (s );
234+ }
235+ }
236+ buff .append ("\n ;" );
237+ String result = buff .toString ();
238+ if (showResults ) {
239+ out .print (result );
240+ }
241+ if (checkResults ) {
242+ String expected = r .readStatement () + ";" ;
243+ expected = StringUtils .replaceAll (expected , "\r \n " , "\n " );
244+ expected = StringUtils .replaceAll (expected , "\r " , "\n " );
245+ if (!expected .equals (result )) {
246+ expected = StringUtils .replaceAll (expected , " " , "+" );
247+ result = StringUtils .replaceAll (result , " " , "+" );
248+ throw new SQLException (
249+ "Unexpected output for:\n " + sql .trim () +
250+ "\n Got:\n " + result + "\n Expected:\n " + expected );
251+ }
252+ }
253+
254+ }
255+ } else {
256+ stat .execute (sql );
257+ }
258+ } catch (Exception e ) {
259+ if (continueOnError ) {
260+ e .printStackTrace (out );
261+ } else {
262+ throw DbException .toSQLException (e );
263+ }
264+ }
265+ }
266+ }
265267 }
266268 }
267269
0 commit comments