KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tool > hbm2ddl > SchemaUpdate


1 //$Id: SchemaUpdate.java,v 1.4 2005/07/18 22:36:43 epbernard Exp $
2
package org.hibernate.tool.hbm2ddl;
3
4 import java.io.FileInputStream JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Statement JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.hibernate.HibernateException;
15 import org.hibernate.cfg.Configuration;
16 import org.hibernate.cfg.NamingStrategy;
17 import org.hibernate.cfg.Settings;
18 import org.hibernate.connection.ConnectionProvider;
19 import org.hibernate.connection.ConnectionProviderFactory;
20 import org.hibernate.dialect.Dialect;
21 import org.hibernate.util.ReflectHelper;
22
23 /**
24  * A commandline tool to update a database schema. May also be called from
25  * inside an application.
26  *
27  * @author Christoph Sturm
28  */

29 public class SchemaUpdate {
30
31     private static final Log log = LogFactory.getLog(SchemaUpdate.class);
32     private ConnectionProvider connectionProvider;
33     private Configuration configuration;
34     private Dialect dialect;
35     private List JavaDoc exceptions;
36
37     public SchemaUpdate(Configuration cfg) throws HibernateException {
38         this( cfg, cfg.getProperties() );
39     }
40
41     public SchemaUpdate(Configuration cfg, Properties JavaDoc connectionProperties) throws HibernateException {
42         this.configuration = cfg;
43         dialect = Dialect.getDialect(connectionProperties);
44         Properties JavaDoc props = new Properties JavaDoc();
45         props.putAll( dialect.getDefaultProperties() );
46         props.putAll(connectionProperties);
47         connectionProvider = ConnectionProviderFactory.newConnectionProvider(props);
48         exceptions = new ArrayList JavaDoc();
49     }
50
51     public SchemaUpdate(Configuration cfg, Settings settings) throws HibernateException {
52         this.configuration = cfg;
53         dialect = settings.getDialect();
54         connectionProvider = settings.getConnectionProvider();
55         exceptions = new ArrayList JavaDoc();
56     }
57
58     public static void main(String JavaDoc[] args) {
59         try {
60             Configuration cfg = new Configuration();
61
62             boolean script = true;
63             // If true then execute db updates, otherwise just generate and display updates
64
boolean doUpdate = true;
65             String JavaDoc propFile = null;
66
67             for ( int i=0; i<args.length; i++ ) {
68                 if( args[i].startsWith("--") ) {
69                     if( args[i].equals("--quiet") ) {
70                         script = false;
71                     }
72                     else if( args[i].startsWith("--properties=") ) {
73                         propFile = args[i].substring(13);
74                     }
75                     else if ( args[i].startsWith("--config=") ) {
76                         cfg.configure( args[i].substring(9) );
77                     }
78                     else if ( args[i].startsWith("--text") ) {
79                         doUpdate = false;
80                     }
81                     else if ( args[i].startsWith("--naming=") ) {
82                         cfg.setNamingStrategy(
83                             (NamingStrategy) ReflectHelper.classForName( args[i].substring(9) ).newInstance()
84                         );
85                     }
86                 }
87                 else {
88                     cfg.addFile(args[i]);
89                 }
90
91             }
92             if (propFile!=null) {
93                 Properties JavaDoc props = new Properties JavaDoc();
94                 props.load( new FileInputStream JavaDoc(propFile) );
95                 new SchemaUpdate(cfg, props).execute(script, doUpdate);
96             }
97             else {
98                 new SchemaUpdate(cfg).execute(script, doUpdate);
99             }
100         }
101         catch (Exception JavaDoc e) {
102             log.error( "Error running schema update", e );
103             e.printStackTrace();
104         }
105     }
106
107     /**
108      * Execute the schema updates
109      * @param script print all DDL to the console
110      */

111     public void execute(boolean script, boolean doUpdate) {
112
113         log.info("Running hbm2ddl schema update");
114
115         Connection JavaDoc connection=null;
116         DatabaseMetadata meta;
117         Statement JavaDoc stmt=null;
118         boolean autoCommitWasEnabled = true;
119
120         exceptions.clear();
121
122         try {
123
124             try {
125                 log.info("fetching database metadata");
126                 connection = connectionProvider.getConnection();
127                 if ( !connection.getAutoCommit() ) {
128                     connection.commit();
129                     connection.setAutoCommit(true);
130                     autoCommitWasEnabled = false;
131                 }
132                 meta = new DatabaseMetadata(connection, dialect);
133                 stmt = connection.createStatement();
134             }
135             catch (SQLException JavaDoc sqle) {
136                 exceptions.add(sqle);
137                 log.error("could not get database metadata", sqle);
138                 throw sqle;
139             }
140
141             log.info("updating schema");
142
143             String JavaDoc[] createSQL = configuration.generateSchemaUpdateScript(dialect, meta);
144             for (int j = 0; j < createSQL.length; j++) {
145
146                 final String JavaDoc sql = createSQL[j];
147                 try {
148                     if (script) System.out.println(sql);
149                     if (doUpdate) {
150                         log.debug(sql);
151                         stmt.executeUpdate(sql);
152                     }
153                 }
154                 catch (SQLException JavaDoc e) {
155                     exceptions.add(e);
156                     log.error( "Unsuccessful: " + sql );
157                     log.error( e.getMessage() );
158                 }
159             }
160
161             log.info("schema update complete");
162
163         }
164         catch (Exception JavaDoc e) {
165             exceptions.add(e);
166             log.error("could not complete schema update", e);
167         }
168         finally {
169
170             try {
171                 if (stmt!=null) stmt.close();
172                 if (!autoCommitWasEnabled) connection.setAutoCommit(false);
173                 if (connection!=null) connection.close();
174                 if (connectionProvider!=null) connectionProvider.close();
175             }
176             catch (Exception JavaDoc e) {
177                 exceptions.add(e);
178                 log.error("Error closing connection", e);
179             }
180
181         }
182     }
183
184     /**
185      * Returns a List of all Exceptions which occured during the export.
186      * @return A List containig the Exceptions occured during the export
187      */

188     public List JavaDoc getExceptions() {
189         return exceptions;
190     }
191 }
192
193
194
195
196
197
198
Popular Tags