001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Nov 7, 2005
014: * @author mbatchel
015: *
016: * 99% of this file is directly copied from Hibernate. The problem with the hibernate
017: * version is that it won't work in under JTA transactions.
018: *
019: */
020: package org.pentaho.repository;
021:
022: import java.sql.Connection;
023: import java.sql.SQLException;
024: import java.sql.Statement;
025: import java.util.ArrayList;
026: import java.util.List;
027: import java.util.Properties;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.hibernate.HibernateException;
031: import org.hibernate.cfg.Configuration;
032: import org.hibernate.connection.ConnectionProvider;
033: import org.hibernate.connection.ConnectionProviderFactory;
034: import org.hibernate.dialect.Dialect;
035: import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
036: import org.pentaho.messages.Messages;
037:
038: /**
039: * A commandline tool to update a database schema. May also be called from
040: * inside an application.
041: *
042: * @author Christoph Sturm
043: */
044: public class PentahoSchemaUpdate {
045:
046: private static final Log log = LogFactory
047: .getLog(PentahoSchemaUpdate.class);
048:
049: private ConnectionProvider connectionProvider;
050:
051: private Configuration configuration;
052:
053: private Dialect dialect;
054:
055: private List exceptions;
056:
057: public PentahoSchemaUpdate(Configuration cfg)
058: throws HibernateException {
059: this (cfg, cfg.getProperties());
060: }
061:
062: public PentahoSchemaUpdate(Configuration cfg,
063: Properties connectionProperties) throws HibernateException {
064: this .configuration = cfg;
065: dialect = Dialect.getDialect(connectionProperties);
066: Properties props = new Properties();
067: props.putAll(dialect.getDefaultProperties());
068: props.putAll(connectionProperties);
069: connectionProvider = ConnectionProviderFactory
070: .newConnectionProvider(props);
071: exceptions = new ArrayList();
072: }
073:
074: /**
075: * Execute the schema updates
076: *
077: * @param script
078: * print all DDL to the console
079: */
080: public void execute(boolean script, boolean doUpdate) {
081:
082: log
083: .info(Messages
084: .getString("PentahoSchemaUpdate.USER_RUNNING_SCHEMA_UPDATE")); //$NON-NLS-1$
085:
086: Connection connection = null;
087: DatabaseMetadata meta;
088: Statement stmt = null;
089: exceptions.clear();
090:
091: try {
092:
093: try {
094: log
095: .info(Messages
096: .getString("PentahoSchemaUpdate.USER_FETCHING_DBMETADATA")); //$NON-NLS-1$
097: connection = connectionProvider.getConnection();
098: meta = new DatabaseMetadata(connection, dialect);
099: stmt = connection.createStatement();
100: } catch (SQLException sqle) {
101: exceptions.add(sqle);
102: log
103: .error(
104: Messages
105: .getErrorString("PentahoSchemaUpdate.ERROR_0001_CANNOT_GET_DBMETADATA"), sqle); //$NON-NLS-1$
106: throw sqle;
107: }
108:
109: log
110: .info(Messages
111: .getString("PentahoSchemaUpdate.USER_UPDATING_SCHEMA")); //$NON-NLS-1$
112:
113: String[] createSQL = configuration
114: .generateSchemaUpdateScript(dialect, meta);
115: for (int j = 0; j < createSQL.length; j++) {
116: final String sql = createSQL[j];
117: try {
118: if (script)
119: System.out.println(sql);
120: if (doUpdate) {
121: log.debug(sql);
122: stmt.executeUpdate(sql);
123: }
124: } catch (SQLException e) {
125: exceptions.add(e);
126: log
127: .error(Messages
128: .getString("PentahoSchemaUpdate.USER_UNSUCCESSFUL") + sql); //$NON-NLS-1$
129: log.error(e.getMessage());
130: }
131: }
132:
133: log
134: .info(Messages
135: .getString("PentahoSchemaUpdate.USER_UPDATE_COMPLETE")); //$NON-NLS-1$
136:
137: } catch (Exception e) {
138: exceptions.add(e);
139: log
140: .error(
141: Messages
142: .getErrorString("PentahoSchemaUpdate.ERROR_0002_COULD_NOT_UPDATE"), e); //$NON-NLS-1$
143: } finally {
144:
145: try {
146: if (stmt != null) {
147: stmt.close();
148: }
149: connection.commit();
150: connection.close();
151: } catch (Exception e) {
152: exceptions.add(e);
153: log
154: .error(
155: Messages
156: .getErrorString("PentahoSchemaUpdate.ERROR_0003_CLOSING_CONNECTION"), e); //$NON-NLS-1$
157: }
158:
159: }
160: }
161:
162: /**
163: * Returns a List of all Exceptions which occured during the export.
164: *
165: * @return A List containig the Exceptions occured during the export
166: */
167: public List getExceptions() {
168: return exceptions;
169: }
170: }
|