FileDocCategorySizeDatePackage
SchemaValidator.javaAPI DocHibernate 3.2.53493Thu Feb 09 20:48:38 GMT 2006org.hibernate.tool.hbm2ddl

SchemaValidator

public class SchemaValidator extends Object
A commandline tool to update a database schema. May also be called from inside an application.
author
Christoph Sturm

Fields Summary
private static final Log
log
private ConnectionHelper
connectionHelper
private org.hibernate.cfg.Configuration
configuration
private org.hibernate.dialect.Dialect
dialect
Constructors Summary
public SchemaValidator(org.hibernate.cfg.Configuration cfg)


	     
		this( cfg, cfg.getProperties() );
	
public SchemaValidator(org.hibernate.cfg.Configuration cfg, Properties connectionProperties)

		this.configuration = cfg;
		dialect = Dialect.getDialect( connectionProperties );
		Properties props = new Properties();
		props.putAll( dialect.getDefaultProperties() );
		props.putAll( connectionProperties );
		connectionHelper = new ManagedProviderConnectionHelper( props );
	
public SchemaValidator(org.hibernate.cfg.Configuration cfg, org.hibernate.cfg.Settings settings)

		this.configuration = cfg;
		dialect = settings.getDialect();
		connectionHelper = new SuppliedConnectionProviderConnectionHelper(
				settings.getConnectionProvider()
		);
	
Methods Summary
public static voidmain(java.lang.String[] args)

		try {
			Configuration cfg = new Configuration();

			String propFile = null;

			for ( int i = 0; i < args.length; i++ ) {
				if ( args[i].startsWith( "--" ) ) {
					if ( args[i].startsWith( "--properties=" ) ) {
						propFile = args[i].substring( 13 );
					}
					else if ( args[i].startsWith( "--config=" ) ) {
						cfg.configure( args[i].substring( 9 ) );
					}
					else if ( args[i].startsWith( "--naming=" ) ) {
						cfg.setNamingStrategy(
								( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
						);
					}
				}
				else {
					cfg.addFile( args[i] );
				}

			}

			if ( propFile != null ) {
				Properties props = new Properties();
				props.putAll( cfg.getProperties() );
				props.load( new FileInputStream( propFile ) );
				cfg.setProperties( props );
			}

			new SchemaValidator( cfg ).validate();
		}
		catch ( Exception e ) {
			log.error( "Error running schema update", e );
			e.printStackTrace();
		}
	
public voidvalidate()
Perform the validations.


		log.info( "Running schema validator" );

		Connection connection = null;

		try {

			DatabaseMetadata meta;
			try {
				log.info( "fetching database metadata" );
				connectionHelper.prepare( false );
				connection = connectionHelper.getConnection();
				meta = new DatabaseMetadata( connection, dialect, false );
			}
			catch ( SQLException sqle ) {
				log.error( "could not get database metadata", sqle );
				throw sqle;
			}

			configuration.validateSchema( dialect, meta );

		}
		catch ( SQLException e ) {
			log.error( "could not complete schema validation", e );
		}
		finally {

			try {
				connectionHelper.release();
			}
			catch ( Exception e ) {
				log.error( "Error closing connection", e );
			}

		}