1 package org.hibernate.tool.hbm2ddl; 3 4 import java.io.FileInputStream ; 5 import java.sql.Connection ; 6 import java.sql.SQLException ; 7 import java.sql.Statement ; 8 import java.util.ArrayList ; 9 import java.util.List ; 10 import java.util.Properties ; 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 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 exceptions; 36 37 public SchemaUpdate(Configuration cfg) throws HibernateException { 38 this( cfg, cfg.getProperties() ); 39 } 40 41 public SchemaUpdate(Configuration cfg, Properties connectionProperties) throws HibernateException { 42 this.configuration = cfg; 43 dialect = Dialect.getDialect(connectionProperties); 44 Properties props = new Properties (); 45 props.putAll( dialect.getDefaultProperties() ); 46 props.putAll(connectionProperties); 47 connectionProvider = ConnectionProviderFactory.newConnectionProvider(props); 48 exceptions = new ArrayList (); 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 (); 56 } 57 58 public static void main(String [] args) { 59 try { 60 Configuration cfg = new Configuration(); 61 62 boolean script = true; 63 boolean doUpdate = true; 65 String 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 props = new Properties (); 94 props.load( new FileInputStream (propFile) ); 95 new SchemaUpdate(cfg, props).execute(script, doUpdate); 96 } 97 else { 98 new SchemaUpdate(cfg).execute(script, doUpdate); 99 } 100 } 101 catch (Exception e) { 102 log.error( "Error running schema update", e ); 103 e.printStackTrace(); 104 } 105 } 106 107 111 public void execute(boolean script, boolean doUpdate) { 112 113 log.info("Running hbm2ddl schema update"); 114 115 Connection connection=null; 116 DatabaseMetadata meta; 117 Statement 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 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 [] createSQL = configuration.generateSchemaUpdateScript(dialect, meta); 144 for (int j = 0; j < createSQL.length; j++) { 145 146 final String 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 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 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 e) { 177 exceptions.add(e); 178 log.error("Error closing connection", e); 179 } 180 181 } 182 } 183 184 188 public List getExceptions() { 189 return exceptions; 190 } 191 } 192 193 194 195 196 197 198 | Popular Tags |