11/*
2- * Copyright 2002-2014 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
4848 */
4949public abstract class ScriptUtils {
5050
51- private static final Log logger = LogFactory .getLog (ScriptUtils .class );
52-
5351 /**
5452 * Default statement separator within SQL scripts: {@code ";"}.
5553 */
@@ -89,12 +87,8 @@ public abstract class ScriptUtils {
8987 public static final String DEFAULT_BLOCK_COMMENT_END_DELIMITER = "*/" ;
9088
9189
92- /**
93- * Prevent instantiation of this utility class.
94- */
95- private ScriptUtils () {
96- /* no-op */
97- }
90+ private static final Log logger = LogFactory .getLog (ScriptUtils .class );
91+
9892
9993 /**
10094 * Split an SQL script into separate statements delimited by the provided
@@ -140,7 +134,7 @@ public static void splitSqlScript(String script, char separator, List<String> st
140134 */
141135 public static void splitSqlScript (String script , String separator , List <String > statements ) throws ScriptException {
142136 splitSqlScript (null , script , separator , DEFAULT_COMMENT_PREFIX , DEFAULT_BLOCK_COMMENT_START_DELIMITER ,
143- DEFAULT_BLOCK_COMMENT_END_DELIMITER , statements );
137+ DEFAULT_BLOCK_COMMENT_END_DELIMITER , statements );
144138 }
145139
146140 /**
@@ -276,6 +270,7 @@ static String readScript(EncodedResource resource) throws IOException {
276270 */
277271 private static String readScript (EncodedResource resource , String commentPrefix , String separator )
278272 throws IOException {
273+
279274 LineNumberReader lnr = new LineNumberReader (resource .getReader ());
280275 try {
281276 return readScript (lnr , commentPrefix , separator );
@@ -302,6 +297,7 @@ private static String readScript(EncodedResource resource, String commentPrefix,
302297 */
303298 public static String readScript (LineNumberReader lineNumberReader , String commentPrefix , String separator )
304299 throws IOException {
300+
305301 String currentStatement = lineNumberReader .readLine ();
306302 StringBuilder scriptBuilder = new StringBuilder ();
307303 while (currentStatement != null ) {
@@ -431,9 +427,8 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
431427 if (logger .isInfoEnabled ()) {
432428 logger .info ("Executing SQL script from " + resource );
433429 }
434-
435430 long startTime = System .currentTimeMillis ();
436- List < String > statements = new LinkedList < String >();
431+
437432 String script ;
438433 try {
439434 script = readScript (resource , commentPrefix , separator );
@@ -449,13 +444,15 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
449444 separator = FALLBACK_STATEMENT_SEPARATOR ;
450445 }
451446
447+ List <String > statements = new LinkedList <String >();
452448 splitSqlScript (resource , script , separator , commentPrefix , blockCommentStartDelimiter ,
453- blockCommentEndDelimiter , statements );
454- int lineNumber = 0 ;
449+ blockCommentEndDelimiter , statements );
450+
451+ int stmtNumber = 0 ;
455452 Statement stmt = connection .createStatement ();
456453 try {
457454 for (String statement : statements ) {
458- lineNumber ++;
455+ stmtNumber ++;
459456 try {
460457 stmt .execute (statement );
461458 int rowsAffected = stmt .getUpdateCount ();
@@ -467,12 +464,12 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
467464 boolean dropStatement = StringUtils .startsWithIgnoreCase (statement .trim (), "drop" );
468465 if (continueOnError || (dropStatement && ignoreFailedDrops )) {
469466 if (logger .isDebugEnabled ()) {
470- logger .debug ("Failed to execute SQL script statement at line " + lineNumber
471- + " of resource " + resource + ": " + statement , ex );
467+ logger .debug ("Failed to execute SQL script statement # " + stmtNumber +
468+ " of resource " + resource + ": " + statement , ex );
472469 }
473470 }
474471 else {
475- throw new ScriptStatementFailedException (statement , lineNumber , resource , ex );
472+ throw new ScriptStatementFailedException (statement , stmtNumber , resource , ex );
476473 }
477474 }
478475 }
@@ -495,7 +492,6 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
495492 if (ex instanceof ScriptException ) {
496493 throw (ScriptException ) ex ;
497494 }
498-
499495 throw new UncategorizedScriptException (
500496 "Failed to execute database script from resource [" + resource + "]" , ex );
501497 }
0 commit comments