[hibernate-commits] Hibernate SVN: r14775 - core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jun 18 02:04:17 EDT 2008


Author: anthonyHib
Date: 2008-06-18 02:04:17 -0400 (Wed, 18 Jun 2008)
New Revision: 14775

Modified:
   core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdate.java
   core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java
Log:
JBPAPP-859

Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdate.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdate.java	2008-06-17 13:36:42 UTC (rev 14774)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdate.java	2008-06-18 06:04:17 UTC (rev 14775)
@@ -2,6 +2,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;
@@ -12,10 +14,12 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.HibernateException;
+import org.hibernate.JDBCException;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.NamingStrategy;
 import org.hibernate.cfg.Settings;
 import org.hibernate.dialect.Dialect;
+import org.hibernate.pretty.DDLFormatter;
 import org.hibernate.util.ReflectHelper;
 
 /**
@@ -31,6 +35,10 @@
 	private Configuration configuration;
 	private Dialect dialect;
 	private List exceptions;
+	private boolean haltOnError = false;
+	private boolean format = true;
+	private String outputFile = null;
+	private String delimiter;
 
 	public SchemaUpdate(Configuration cfg) throws HibernateException {
 		this( cfg, cfg.getProperties() );
@@ -116,6 +124,7 @@
 
 		Connection connection = null;
 		Statement stmt = null;
+		Writer outputFileWriter = null;
 
 		exceptions.clear();
 
@@ -137,20 +146,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 = 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() );
@@ -176,7 +201,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 );
+			}
 		}
 	}
 
@@ -188,4 +221,26 @@
 	public List getExceptions() {
 		return exceptions;
 	}
+
+	public void setHaltOnError(boolean haltOnError) {
+		this.haltOnError = haltOnError;
+	}
+
+	public void setFormat(boolean format) {
+		this.format = format;
+	}
+
+	public void setOutputFile(String outputFile) {
+		this.outputFile = outputFile;
+	}
+
+	public void setDelimiter(String delimiter) {
+		this.delimiter = delimiter;
+	}
+	
+	private String format(String sql) {
+		return format ?
+		       new DDLFormatter( sql ).format() :
+		       sql;
+	}
 }

Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java	2008-06-17 13:36:42 UTC (rev 14774)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java	2008-06-18 06:04:17 UTC (rev 14775)
@@ -47,9 +47,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);
@@ -174,11 +178,39 @@
 			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;
+	}
+
 }




More information about the hibernate-commits mailing list