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.prepare( true );
connection = connectionHelper.getConnection();
meta = new DatabaseMetadata( connection, dialect );
stmt = connection.createStatement();
}
catch ( SQLException sqle ) {
exceptions.add( sqle );
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 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( formatted );
}
if ( outputFile != null ) {
outputFileWriter.write( formatted + "\n" );
}
if ( doUpdate ) {
log.debug( 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() );
}
}
log.info( "schema update complete" );
}
catch ( Exception e ) {
exceptions.add( e );
log.error( "could not complete schema update", e );
}
finally {
try {
if ( stmt != null ) {
stmt.close();
}
connectionHelper.release();
}
catch ( Exception e ) {
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 );
}
}
}
Execute the schema updates |