From 5c41e2c6e1f06b4b0d55527a683054a93d334c6e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 12 Jan 2010 10:15:04 +0000 Subject: [PATCH] made comment prefix configurable (SPR-6667) --- .../init/ResourceDatabasePopulator.java | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java index d5f716700b..f9151ef90c 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java +++ b/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"); * 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 Dave Syer + * @author Juergen Hoeller * @since 3.0 */ 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); @@ -53,7 +54,9 @@ public class ResourceDatabasePopulator implements DatabasePopulator { private List scripts = new ArrayList(); private String sqlScriptEncoding; - + + private String commentPrefix = DEFAULT_COMMENT_PREFIX; + private boolean continueOnError = false; private boolean ignoreFailedDrops = false; @@ -85,10 +88,17 @@ public class ResourceDatabasePopulator implements DatabasePopulator { 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. * Defaults to false. - * @param continueOnError the flag value to set */ public void setContinueOnError(boolean continueOnError) { this.continueOnError = continueOnError; @@ -96,9 +106,9 @@ public class ResourceDatabasePopulator implements DatabasePopulator { /** * Flag to indicate that a failed SQL DROP statement can be ignored. - * This is useful for non-embedded databases whose SQL dialect does not support an IF EXISTS clause in a DROP. - * The default is false so that if it the populator runs accidentally, it will failfast when the script starts with a DROP. - * @param ignoreFailedDrops the flag value to set + *

This is useful for non-embedded databases whose SQL dialect does not support an + * IF EXISTS clause in a DROP. The default is false so that if the + * populator runs accidentally, it will fail fast when the script starts with a DROP. */ public void setIgnoreFailedDrops(boolean ignoreFailedDrops) { this.ignoreFailedDrops = ignoreFailedDrops; @@ -123,7 +133,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { /** * Execute the given SQL script.

The script will normally be loaded by classpath. There should be one statement * per line. Any semicolons will be removed. Do not use this method to execute DDL if you expect rollback. - * @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 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 DROP. @@ -187,17 +197,18 @@ public class ResourceDatabasePopulator implements DatabasePopulator { } /** - * Read a script from the LineNumberReader and build a String containing the lines. - * @param lineNumberReader the LineNumberReader containing the script to be processed + * Read a script from the given resource and build a String containing the lines. + * @param resource the resource to be read * @return String 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()); String currentStatement = lnr.readLine(); StringBuilder scriptBuilder = new StringBuilder(); 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) { scriptBuilder.append('\n'); } @@ -213,7 +224,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * @param script the SQL script * @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; char[] content = script.toCharArray(); 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 statements the List that will contain the individual statements */ - private static void splitSqlScript(String script, char delim, List statements) { + private void splitSqlScript(String script, char delim, List statements) { StringBuilder sb = new StringBuilder(); boolean inLiteral = false; char[] content = script.toCharArray();