1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
43
43
* @author Dave Syer
44
44
* @author Juergen Hoeller
45
45
* @author Chris Beams
46
+ * @author Oliver Gierke
46
47
* @since 3.0
47
48
*/
48
49
public class ResourceDatabasePopulator implements DatabasePopulator {
@@ -265,16 +266,37 @@ private boolean containsSqlScriptDelimiters(String script, String delim) {
265
266
if (content [i ] == '\'' ) {
266
267
inLiteral = !inLiteral ;
267
268
}
268
- if (!inLiteral && script . substring ( i ). startsWith ( delim )) {
269
+ if (!inLiteral && startsWithDelimiter ( script , i , delim )) {
269
270
return true ;
270
271
}
271
272
}
272
273
return false ;
273
274
}
274
275
275
276
/**
276
- * Split an SQL script into separate statements delimited with the provided delimiter character.
277
- * Each individual statement will be added to the provided <code>List</code>.
277
+ * Return whether the substring of a given source {@link String} starting at the
278
+ * given index starts with the given delimiter.
279
+ *
280
+ * @param source the source {@link String} to inspect
281
+ * @param startIndex the index to look for the delimiter
282
+ * @param delim the delimiter to look for
283
+ */
284
+ private boolean startsWithDelimiter (String source , int startIndex , String delim ) {
285
+
286
+ int endIndex = startIndex + delim .length ();
287
+
288
+ if (source .length () < endIndex ) {
289
+ // String is too short to contain the delimiter
290
+ return false ;
291
+ }
292
+
293
+ return source .substring (startIndex , endIndex ).equals (delim );
294
+ }
295
+
296
+ /**
297
+ * Split an SQL script into separate statements delimited with the provided delimiter
298
+ * character. Each individual statement will be added to the provided {@code List}.
299
+ *
278
300
* @param script the SQL script
279
301
* @param delim character delimiting each statement (typically a ';' character)
280
302
* @param statements the List that will contain the individual statements
@@ -301,7 +323,7 @@ private void splitSqlScript(String script, String delim, List<String> statements
301
323
inLiteral = !inLiteral ;
302
324
}
303
325
if (!inLiteral ) {
304
- if (script . substring ( i ). startsWith ( delim )) {
326
+ if (startsWithDelimiter ( script , i , delim )) {
305
327
if (sb .length () > 0 ) {
306
328
statements .add (sb .toString ());
307
329
sb = new StringBuilder ();
0 commit comments