Browse Source

made comment prefix configurable (SPR-6667)

pull/23217/head
Juergen Hoeller 15 years ago
parent
commit
5c41e2c6e1
  1. 41
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java

41
org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,11 +41,12 @@ import org.springframework.util.StringUtils;
* *
* @author Keith Donald * @author Keith Donald
* @author Dave Syer * @author Dave Syer
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
public class ResourceDatabasePopulator implements DatabasePopulator { public class ResourceDatabasePopulator implements DatabasePopulator {
private static String COMMENT_PREFIX = "--"; private static String DEFAULT_COMMENT_PREFIX = "--";
private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class); private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class);
@ -53,7 +54,9 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private List<Resource> scripts = new ArrayList<Resource>(); private List<Resource> scripts = new ArrayList<Resource>();
private String sqlScriptEncoding; private String sqlScriptEncoding;
private String commentPrefix = DEFAULT_COMMENT_PREFIX;
private boolean continueOnError = false; private boolean continueOnError = false;
private boolean ignoreFailedDrops = false; private boolean ignoreFailedDrops = false;
@ -85,10 +88,17 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
this.sqlScriptEncoding = sqlScriptEncoding; this.sqlScriptEncoding = sqlScriptEncoding;
} }
/**
* Set the line prefix that identifies comments in the SQL script.
* Default is "--".
*/
public void setCommentPrefix(String commentPrefix) {
this.commentPrefix = commentPrefix;
}
/** /**
* Flag to indicate that all failures in SQL should be logged but not cause a failure. * Flag to indicate that all failures in SQL should be logged but not cause a failure.
* Defaults to false. * Defaults to false.
* @param continueOnError the flag value to set
*/ */
public void setContinueOnError(boolean continueOnError) { public void setContinueOnError(boolean continueOnError) {
this.continueOnError = continueOnError; this.continueOnError = continueOnError;
@ -96,9 +106,9 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/** /**
* Flag to indicate that a failed SQL <code>DROP</code> statement can be ignored. * Flag to indicate that a failed SQL <code>DROP</code> statement can be ignored.
* This is useful for non-embedded databases whose SQL dialect does not support an <code>IF EXISTS</code> clause in a <code>DROP</code>. * <p>This is useful for non-embedded databases whose SQL dialect does not support an
* The default is false so that if it the populator runs accidentally, it will failfast when the script starts with a <code>DROP</code>. * <code>IF EXISTS</code> clause in a <code>DROP</code>. The default is false so that if the
* @param ignoreFailedDrops the flag value to set * populator runs accidentally, it will fail fast when the script starts with a <code>DROP</code>.
*/ */
public void setIgnoreFailedDrops(boolean ignoreFailedDrops) { public void setIgnoreFailedDrops(boolean ignoreFailedDrops) {
this.ignoreFailedDrops = ignoreFailedDrops; this.ignoreFailedDrops = ignoreFailedDrops;
@ -123,7 +133,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/** /**
* Execute the given SQL script. <p>The script will normally be loaded by classpath. There should be one statement * Execute the given SQL script. <p>The script will normally be loaded by classpath. There should be one statement
* per line. Any semicolons will be removed. <b>Do not use this method to execute DDL if you expect rollback.</b> * per line. Any semicolons will be removed. <b>Do not use this method to execute DDL if you expect rollback.</b>
* @param template the SimpleJdbcTemplate with which to perform JDBC operations * @param connection the JDBC Connection with which to perform JDBC operations
* @param resource the resource (potentially associated with a specific encoding) to load the SQL script from. * @param resource the resource (potentially associated with a specific encoding) to load the SQL script from.
* @param continueOnError whether or not to continue without throwing an exception in the event of an error. * @param continueOnError whether or not to continue without throwing an exception in the event of an error.
* @param ignoreFailedDrops whether of not to continue in thw event of specifically an error on a <code>DROP</code>. * @param ignoreFailedDrops whether of not to continue in thw event of specifically an error on a <code>DROP</code>.
@ -187,17 +197,18 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
} }
/** /**
* Read a script from the LineNumberReader and build a String containing the lines. * Read a script from the given resource and build a String containing the lines.
* @param lineNumberReader the <code>LineNumberReader</> containing the script to be processed * @param resource the resource to be read
* @return <code>String</code> containing the script lines * @return <code>String</code> containing the script lines
* @throws IOException * @throws IOException in case of I/O errors
*/ */
private static String readScript(EncodedResource resource) throws IOException { private String readScript(EncodedResource resource) throws IOException {
LineNumberReader lnr = new LineNumberReader(resource.getReader()); LineNumberReader lnr = new LineNumberReader(resource.getReader());
String currentStatement = lnr.readLine(); String currentStatement = lnr.readLine();
StringBuilder scriptBuilder = new StringBuilder(); StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) { while (currentStatement != null) {
if (StringUtils.hasText(currentStatement) && !currentStatement.startsWith(COMMENT_PREFIX)) { if (StringUtils.hasText(currentStatement) &&
(this.commentPrefix != null && !currentStatement.startsWith(this.commentPrefix))) {
if (scriptBuilder.length() > 0) { if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n'); scriptBuilder.append('\n');
} }
@ -213,7 +224,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* @param script the SQL script * @param script the SQL script
* @param delim character delimiting each statement - typically a ';' character * @param delim character delimiting each statement - typically a ';' character
*/ */
private static boolean containsSqlScriptDelimiters(String script, char delim) { private boolean containsSqlScriptDelimiters(String script, char delim) {
boolean inLiteral = false; boolean inLiteral = false;
char[] content = script.toCharArray(); char[] content = script.toCharArray();
for (int i = 0; i < script.length(); i++) { for (int i = 0; i < script.length(); i++) {
@ -234,7 +245,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* @param delim character delimiting each statement - typically a ';' character * @param delim character delimiting each statement - typically a ';' character
* @param statements the List that will contain the individual statements * @param statements the List that will contain the individual statements
*/ */
private static void splitSqlScript(String script, char delim, List<String> statements) { private void splitSqlScript(String script, char delim, List<String> statements) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
boolean inLiteral = false; boolean inLiteral = false;
char[] content = script.toCharArray(); char[] content = script.toCharArray();

Loading…
Cancel
Save