1 20 package org.pentaho.repository; 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.Properties ; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.hibernate.HibernateException; 31 import org.hibernate.cfg.Configuration; 32 import org.hibernate.connection.ConnectionProvider; 33 import org.hibernate.connection.ConnectionProviderFactory; 34 import org.hibernate.dialect.Dialect; 35 import org.hibernate.tool.hbm2ddl.DatabaseMetadata; 36 import org.pentaho.messages.Messages; 37 38 44 public class PentahoSchemaUpdate { 45 46 private static final Log log = LogFactory.getLog(PentahoSchemaUpdate.class); 47 48 private ConnectionProvider connectionProvider; 49 50 private Configuration configuration; 51 52 private Dialect dialect; 53 54 private List exceptions; 55 56 public PentahoSchemaUpdate(Configuration cfg) throws HibernateException { 57 this(cfg, cfg.getProperties()); 58 } 59 60 public PentahoSchemaUpdate(Configuration cfg, Properties connectionProperties) throws HibernateException { 61 this.configuration = cfg; 62 dialect = Dialect.getDialect(connectionProperties); 63 Properties props = new Properties (); 64 props.putAll(dialect.getDefaultProperties()); 65 props.putAll(connectionProperties); 66 connectionProvider = ConnectionProviderFactory.newConnectionProvider(props); 67 exceptions = new ArrayList (); 68 } 69 70 76 public void execute(boolean script, boolean doUpdate) { 77 78 log.info(Messages.getString("PentahoSchemaUpdate.USER_RUNNING_SCHEMA_UPDATE")); 80 Connection connection = null; 81 DatabaseMetadata meta; 82 Statement stmt = null; 83 exceptions.clear(); 84 85 try { 86 87 try { 88 log.info(Messages.getString("PentahoSchemaUpdate.USER_FETCHING_DBMETADATA")); connection = connectionProvider.getConnection(); 90 HibernateUtil.beginTransaction(); 91 meta = new DatabaseMetadata(connection, dialect); 92 stmt = connection.createStatement(); 93 } catch (SQLException sqle) { 94 exceptions.add(sqle); 95 log.error(Messages.getErrorString("PentahoSchemaUpdate.ERROR_0001_CANNOT_GET_DBMETADATA"), sqle); throw sqle; 97 } 98 99 log.info(Messages.getString("PentahoSchemaUpdate.USER_UPDATING_SCHEMA")); 101 String [] createSQL = configuration.generateSchemaUpdateScript(dialect, meta); 102 for (int j = 0; j < createSQL.length; j++) { 103 104 final String sql = createSQL[j]; 105 try { 106 if (script) 107 System.out.println(sql); 108 if (doUpdate) { 109 log.debug(sql); 110 stmt.executeUpdate(sql); 111 } 112 } catch (SQLException e) { 113 exceptions.add(e); 114 log.error(Messages.getString("PentahoSchemaUpdate.USER_UNSUCCESSFUL") + sql); log.error(e.getMessage()); 116 } 117 } 118 119 log.info(Messages.getString("PentahoSchemaUpdate.USER_UPDATE_COMPLETE")); 121 } catch (Exception e) { 122 exceptions.add(e); 123 log.error(Messages.getErrorString("PentahoSchemaUpdate.ERROR_0002_COULD_NOT_UPDATE"), e); } finally { 125 126 try { 127 if (stmt != null) 128 stmt.close(); 129 if (connection != null) 131 connection.close(); 132 if (connectionProvider != null) 133 connectionProvider.close(); 134 } catch (Exception e) { 135 exceptions.add(e); 136 log.error(Messages.getErrorString("PentahoSchemaUpdate.ERROR_0003_CLOSING_CONNECTION"), e); } 138 139 } 140 } 141 142 147 public List getExceptions() { 148 return exceptions; 149 } 150 } 151
| Popular Tags
|