Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/tool/hbm2ddl/SchemaUpdate.java
//$Id: SchemaUpdate.java 14783 2008-06-19 09:54:03Z anthonyHib $
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;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

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;

/**
 * A commandline tool to update a database schema. May also be called from
 * inside an application.
 *
 @author Christoph Sturm
 */
public class SchemaUpdate {

  private static final Log log = LogFactory.getLogSchemaUpdate.class );
  private ConnectionHelper connectionHelper;
  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 cfgthrows HibernateException {
    thiscfg, cfg.getProperties() );
  }

  public SchemaUpdate(Configuration cfg, Properties connectionPropertiesthrows HibernateException {
    this.configuration = cfg;
    dialect = Dialect.getDialectconnectionProperties );
    Properties props = new Properties();
    props.putAlldialect.getDefaultProperties() );
    props.putAllconnectionProperties );
    connectionHelper = new ManagedProviderConnectionHelperprops );
    exceptions = new ArrayList();
  }

  public SchemaUpdate(Configuration cfg, Settings settingsthrows HibernateException {
    this.configuration = cfg;
    dialect = settings.getDialect();
    connectionHelper = new SuppliedConnectionProviderConnectionHelper(
        settings.getConnectionProvider()
    );
    exceptions = new ArrayList();
  }

  public static void main(String[] args) {
    try {
      Configuration cfg = new Configuration();

      boolean script = true;
      // If true then execute db updates, otherwise just generate and display updates
      boolean doUpdate = true;
      String propFile = null;

      for int i = 0; i < args.length; i++ ) {
        if args[i].startsWith"--" ) ) {
          if args[i].equals"--quiet" ) ) {
            script = false;
          }
          else if args[i].startsWith"--properties=" ) ) {
            propFile = args[i].substring13 );
          }
          else if args[i].startsWith"--config=" ) ) {
            cfg.configureargs[i].substring) );
          }
          else if args[i].startsWith"--text" ) ) {
            doUpdate = false;
          }
          else if args[i].startsWith"--naming=" ) ) {
            cfg.setNamingStrategy(
                NamingStrategy ReflectHelper.classForNameargs[i].substring) ).newInstance()
            );
          }
        }
        else {
          cfg.addFileargs[i] );
        }

      }

      if propFile != null ) {
        Properties props = new Properties();
        props.putAllcfg.getProperties() );
        props.loadnew FileInputStreampropFile ) );
        cfg.setPropertiesprops );
      }

      new SchemaUpdatecfg ).executescript, doUpdate );
    }
    catch Exception e ) {
      log.error"Error running schema update", e );
      e.printStackTrace();
    }
  }

  /**
   * Execute the schema updates
   *
   @param script print all DDL to the console
   */
  public void execute(boolean script, boolean doUpdate) {

    log.info"Running hbm2ddl schema update" );

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

    exceptions.clear();

    try {

      DatabaseMetadata meta;
      try {
        log.info"fetching database metadata" );
        connectionHelper.preparetrue );
        connection = connectionHelper.getConnection();
        meta = new DatabaseMetadataconnection, dialect );
        stmt = connection.createStatement();
      }
      catch SQLException sqle ) {
        exceptions.addsqle );
        log.error"could not get database metadata", sqle );
        throw sqle;
      }

      log.info"updating schema" );

      
      if outputFile != null ) {
        log.info"writing generated schema to file: " + outputFile );
        outputFileWriter = new FileWriteroutputFile );
      }
       
      String[] createSQL = configuration.generateSchemaUpdateScriptdialect, meta );
      for int j = 0; j < createSQL.length; j++ ) {

        final String sql = createSQL[j];
        String formatted = formatsql );
        try {
          if delimiter != null ) {
            formatted += delimiter;
          }
          if script ) {
            System.out.printlnformatted );
          }
          if outputFile != null ) {
            outputFileWriter.writeformatted + "\n" );
          }
          if doUpdate ) {
            log.debugsql );
            stmt.executeUpdateformatted );
          }
        }
        catch SQLException e ) {
          if haltOnError ) {
            throw new JDBCException"Error during DDL export", e );
          }
          exceptions.add);
          log.error"Unsuccessful: " + sql );
          log.errore.getMessage() );
        }
      }

      log.info"schema update complete" );

    }
    catch Exception e ) {
      exceptions.add);
      log.error"could not complete schema update", e );
    }
    finally {

      try {
        if stmt != null ) {
          stmt.close();
        }
        connectionHelper.release();
      }
      catch Exception e ) {
        exceptions.add);
        log.error"Error closing connection", e );
      }
      try {
        ifoutputFileWriter != null ) {
          outputFileWriter.close();
        }
      }
      catch(Exception e) {
        exceptions.add(e);
        log.error"Error closing connection", e );
      }
    }
  }

  /**
   * Returns a List of all Exceptions which occured during the export.
   *
   @return A List containig the Exceptions occured during the export
   */
  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 DDLFormattersql ).format() :
           sql;
  }
}