HBX-757 fix

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@15139 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Anthony Patricio 2008-08-22 14:24:25 +00:00
parent 39123a97fc
commit 879d8900d9
2 changed files with 99 additions and 6 deletions

View File

@ -25,6 +25,8 @@
package org.hibernate.tool.hbm2ddl;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
@ -32,14 +34,20 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.Settings;
import org.hibernate.dialect.Dialect;
import org.hibernate.jdbc.util.FormatStyle;
import org.hibernate.jdbc.util.Formatter;
import org.hibernate.jdbc.util.SQLStatementLogger;
import org.hibernate.util.PropertiesHelper;
import org.hibernate.util.ReflectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A commandline tool to update a database schema. May also be called from
@ -54,6 +62,12 @@ public class SchemaUpdate {
private Configuration configuration;
private Dialect dialect;
private List exceptions;
private boolean haltOnError = false;
private boolean format = true;
private String outputFile = null;
private String delimiter;
private Formatter formatter;
private SQLStatementLogger sqlStatementLogger;
public SchemaUpdate(Configuration cfg) throws HibernateException {
this( cfg, cfg.getProperties() );
@ -67,6 +81,7 @@ public class SchemaUpdate {
props.putAll( connectionProperties );
connectionHelper = new ManagedProviderConnectionHelper( props );
exceptions = new ArrayList();
formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, props ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
}
public SchemaUpdate(Configuration cfg, Settings settings) throws HibernateException {
@ -76,6 +91,8 @@ public class SchemaUpdate {
settings.getConnectionProvider()
);
exceptions = new ArrayList();
sqlStatementLogger = settings.getSqlStatementLogger();
formatter = ( sqlStatementLogger.isFormatSql() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
}
public static void main(String[] args) {
@ -139,6 +156,7 @@ public class SchemaUpdate {
Connection connection = null;
Statement stmt = null;
Writer outputFileWriter = null;
exceptions.clear();
@ -160,20 +178,36 @@ public class SchemaUpdate {
log.info( "updating schema" );
if ( outputFile != null ) {
log.info( "writing generated schema to file: " + outputFile );
outputFileWriter = new FileWriter( outputFile );
}
String[] createSQL = configuration.generateSchemaUpdateScript( dialect, meta );
for ( int j = 0; j < createSQL.length; j++ ) {
final String sql = createSQL[j];
String formatted = formatter.format( sql );
try {
if ( delimiter != null ) {
formatted += delimiter;
}
if ( script ) {
System.out.println( sql );
System.out.println( formatted );
}
if ( outputFile != null ) {
outputFileWriter.write( formatted + "\n" );
}
if ( doUpdate ) {
log.debug( sql );
stmt.executeUpdate( sql );
stmt.executeUpdate( formatted );
}
}
catch ( SQLException e ) {
if ( haltOnError ) {
throw new JDBCException( "Error during DDL export", e );
}
exceptions.add( e );
log.error( "Unsuccessful: " + sql );
log.error( e.getMessage() );
@ -199,7 +233,15 @@ public class SchemaUpdate {
exceptions.add( e );
log.error( "Error closing connection", e );
}
try {
if( outputFileWriter != null ) {
outputFileWriter.close();
}
}
catch(Exception e) {
exceptions.add(e);
log.error( "Error closing connection", e );
}
}
}
@ -211,4 +253,21 @@ public class SchemaUpdate {
public List getExceptions() {
return exceptions;
}
public void setHaltOnError(boolean haltOnError) {
this.haltOnError = haltOnError;
}
public void setFormat(boolean format) {
this.formatter = ( format ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
}
public void setOutputFile(String outputFile) {
this.outputFile = outputFile;
}
public void setDelimiter(String delimiter) {
this.delimiter = delimiter;
}
}

View File

@ -70,9 +70,13 @@ public class SchemaUpdateTask extends MatchingTask {
private List fileSets = new LinkedList();
private File propertiesFile = null;
private File configurationFile = null;
private File outputFile = null;
private boolean quiet = false;
private boolean text = true;
private boolean haltOnError = false;
private String delimiter = null;
private String namingStrategy = null;
public void addFileset(FileSet set) {
fileSets.add(set);
@ -122,6 +126,8 @@ public class SchemaUpdateTask extends MatchingTask {
*/
public void execute() throws BuildException {
try {
log("Running Hibernate Core SchemaUpdate.");
log("This is an Ant task supporting only mapping files, if you want to use annotations see http://tools.hibernate.org.");
Configuration cfg = getConfiguration();
getSchemaUpdate(cfg).execute(!quiet, !text);
}
@ -195,11 +201,39 @@ public class SchemaUpdateTask extends MatchingTask {
properties.load( new FileInputStream(propertiesFile) );
}
cfg.setProperties(properties);
return new SchemaUpdate(cfg);
SchemaUpdate su = new SchemaUpdate(cfg);
su.setOutputFile( outputFile.getPath() );
su.setDelimiter(delimiter);
su.setHaltOnError(haltOnError);
return su;
}
public void setNamingStrategy(String namingStrategy) {
this.namingStrategy = namingStrategy;
}
public File getOutputFile() {
return outputFile;
}
public void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}
public boolean isHaltOnError() {
return haltOnError;
}
public void setHaltOnError(boolean haltOnError) {
this.haltOnError = haltOnError;
}
public String getDelimiter() {
return delimiter;
}
public void setDelimiter(String delimiter) {
this.delimiter = delimiter;
}
}