1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.tool.hbm2ddl;
26
27 import java.io.FileInputStream;
28 import java.sql.Connection;
29 import java.sql.SQLException;
30 import java.util.Properties;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.hibernate.HibernateException;
35 import org.hibernate.cfg.Configuration;
36 import org.hibernate.cfg.NamingStrategy;
37 import org.hibernate.cfg.Settings;
38 import org.hibernate.dialect.Dialect;
39 import org.hibernate.util.ReflectHelper;
40
41 /**
42 * A commandline tool to update a database schema. May also be called from
43 * inside an application.
44 *
45 * @author Christoph Sturm
46 */
47 public class SchemaValidator {
48
49 private static final Logger log = LoggerFactory.getLogger( SchemaValidator.class );
50 private ConnectionHelper connectionHelper;
51 private Configuration configuration;
52 private Dialect dialect;
53
54 public SchemaValidator(Configuration cfg) throws HibernateException {
55 this( cfg, cfg.getProperties() );
56 }
57
58 public SchemaValidator(Configuration cfg, Properties connectionProperties) throws HibernateException {
59 this.configuration = cfg;
60 dialect = Dialect.getDialect( connectionProperties );
61 Properties props = new Properties();
62 props.putAll( dialect.getDefaultProperties() );
63 props.putAll( connectionProperties );
64 connectionHelper = new ManagedProviderConnectionHelper( props );
65 }
66
67 public SchemaValidator(Configuration cfg, Settings settings) throws HibernateException {
68 this.configuration = cfg;
69 dialect = settings.getDialect();
70 connectionHelper = new SuppliedConnectionProviderConnectionHelper(
71 settings.getConnectionProvider()
72 );
73 }
74
75 public static void main(String[] args) {
76 try {
77 Configuration cfg = new Configuration();
78
79 String propFile = null;
80
81 for ( int i = 0; i < args.length; i++ ) {
82 if ( args[i].startsWith( "--" ) ) {
83 if ( args[i].startsWith( "--properties=" ) ) {
84 propFile = args[i].substring( 13 );
85 }
86 else if ( args[i].startsWith( "--config=" ) ) {
87 cfg.configure( args[i].substring( 9 ) );
88 }
89 else if ( args[i].startsWith( "--naming=" ) ) {
90 cfg.setNamingStrategy(
91 ( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
92 );
93 }
94 }
95 else {
96 cfg.addFile( args[i] );
97 }
98
99 }
100
101 if ( propFile != null ) {
102 Properties props = new Properties();
103 props.putAll( cfg.getProperties() );
104 props.load( new FileInputStream( propFile ) );
105 cfg.setProperties( props );
106 }
107
108 new SchemaValidator( cfg ).validate();
109 }
110 catch ( Exception e ) {
111 log.error( "Error running schema update", e );
112 e.printStackTrace();
113 }
114 }
115
116 /**
117 * Perform the validations.
118 */
119 public void validate() {
120
121 log.info( "Running schema validator" );
122
123 Connection connection = null;
124
125 try {
126
127 DatabaseMetadata meta;
128 try {
129 log.info( "fetching database metadata" );
130 connectionHelper.prepare( false );
131 connection = connectionHelper.getConnection();
132 meta = new DatabaseMetadata( connection, dialect, false );
133 }
134 catch ( SQLException sqle ) {
135 log.error( "could not get database metadata", sqle );
136 throw sqle;
137 }
138
139 configuration.validateSchema( dialect, meta );
140
141 }
142 catch ( SQLException e ) {
143 log.error( "could not complete schema validation", e );
144 }
145 finally {
146
147 try {
148 connectionHelper.release();
149 }
150 catch ( Exception e ) {
151 log.error( "Error closing connection", e );
152 }
153
154 }
155 }
156
157 }