Author: anthonyHib
Date: 2008-08-25 04:48:44 -0400 (Mon, 25 Aug 2008)
New Revision: 15140
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java
Log:
HBX-757 fix
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java
===================================================================
---
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java 2008-08-22
14:24:25 UTC (rev 15139)
+++
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java 2008-08-25
08:48:44 UTC (rev 15140)
@@ -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.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 @@
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 @@
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 @@
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 @@
Connection connection = null;
Statement stmt = null;
+ Writer outputFileWriter = null;
exceptions.clear();
@@ -160,20 +178,36 @@
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 @@
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 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;
+ }
+
}
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java
===================================================================
---
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java 2008-08-22
14:24:25 UTC (rev 15139)
+++
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java 2008-08-25
08:48:44 UTC (rev 15140)
@@ -70,9 +70,13 @@
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 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,40 @@
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;
+ }
+
}
+