Java Mailing List Archive

http://www.gg3721.com/

Home » Hibernate Commits List »

[hibernate-commits] Hibernate SVN: r15140 -
 core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl.

hibernate-commits

2008-08-25


Author LoginPost Reply
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)
@@(protected) @@
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;
@@(protected) @@
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
@@(protected) @@
 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() );
@@(protected) @@
   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 {
@@(protected) @@
       settings.getConnectionProvider()
   );
   exceptions = new ArrayList();
+    sqlStatementLogger = settings.getSqlStatementLogger();
+    formatter = ( sqlStatementLogger.isFormatSql() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
 }

 public static void main(String[] args) {
@@(protected) @@

   Connection connection = null;
   Statement stmt = null;
+    Writer outputFileWriter = null;

   exceptions.clear();

@@(protected) @@

     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() );
@@(protected) @@
       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 );
+      }
   }
 }

@@(protected) @@
 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)
@@(protected) @@
 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);
@@(protected) @@
  */
 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);
   }
@@(protected) @@
     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;
+  }
+
}
+

_______________________________________________
hibernate-commits mailing list
hibernate-commits@(protected)
https://lists.jboss.org/mailman/listinfo/hibernate-commits
©2008 gg3721.com - Jax Systems, LLC, U.S.A.