KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > PentahoSchemaUpdate


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Nov 7, 2005
14  * @author mbatchel
15  *
16  * 99% of this file is directly copied from Hibernate. The problem with the hibernate
17  * version is that it won't work in under JTA transactions.
18  *
19  */

20 package org.pentaho.repository;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Properties JavaDoc;
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 /**
39  * A commandline tool to update a database schema. May also be called from
40  * inside an application.
41  *
42  * @author Christoph Sturm
43  */

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 JavaDoc exceptions;
55
56     public PentahoSchemaUpdate(Configuration cfg) throws HibernateException {
57         this(cfg, cfg.getProperties());
58     }
59
60     public PentahoSchemaUpdate(Configuration cfg, Properties JavaDoc connectionProperties) throws HibernateException {
61         this.configuration = cfg;
62         dialect = Dialect.getDialect(connectionProperties);
63         Properties JavaDoc props = new Properties JavaDoc();
64         props.putAll(dialect.getDefaultProperties());
65         props.putAll(connectionProperties);
66         connectionProvider = ConnectionProviderFactory.newConnectionProvider(props);
67         exceptions = new ArrayList JavaDoc();
68     }
69
70     /**
71      * Execute the schema updates
72      *
73      * @param script
74      * print all DDL to the console
75      */

76     public void execute(boolean script, boolean doUpdate) {
77
78         log.info(Messages.getString("PentahoSchemaUpdate.USER_RUNNING_SCHEMA_UPDATE")); //$NON-NLS-1$
79

80         Connection JavaDoc connection = null;
81         DatabaseMetadata meta;
82         Statement JavaDoc stmt = null;
83         exceptions.clear();
84
85         try {
86
87             try {
88                 log.info(Messages.getString("PentahoSchemaUpdate.USER_FETCHING_DBMETADATA")); //$NON-NLS-1$
89
connection = connectionProvider.getConnection();
90                 HibernateUtil.beginTransaction();
91                 meta = new DatabaseMetadata(connection, dialect);
92                 stmt = connection.createStatement();
93             } catch (SQLException JavaDoc sqle) {
94                 exceptions.add(sqle);
95                 log.error(Messages.getErrorString("PentahoSchemaUpdate.ERROR_0001_CANNOT_GET_DBMETADATA"), sqle); //$NON-NLS-1$
96
throw sqle;
97             }
98
99             log.info(Messages.getString("PentahoSchemaUpdate.USER_UPDATING_SCHEMA")); //$NON-NLS-1$
100

101             String JavaDoc[] createSQL = configuration.generateSchemaUpdateScript(dialect, meta);
102             for (int j = 0; j < createSQL.length; j++) {
103
104                 final String JavaDoc 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 JavaDoc e) {
113                     exceptions.add(e);
114                     log.error(Messages.getString("PentahoSchemaUpdate.USER_UNSUCCESSFUL") + sql); //$NON-NLS-1$
115
log.error(e.getMessage());
116                 }
117             }
118
119             log.info(Messages.getString("PentahoSchemaUpdate.USER_UPDATE_COMPLETE")); //$NON-NLS-1$
120

121         } catch (Exception JavaDoc e) {
122             exceptions.add(e);
123             log.error(Messages.getErrorString("PentahoSchemaUpdate.ERROR_0002_COULD_NOT_UPDATE"), e); //$NON-NLS-1$
124
} finally {
125
126             try {
127                 if (stmt != null)
128                     stmt.close();
129                 // if (!autoCommitWasEnabled) connection.setAutoCommit(false);
130
if (connection != null)
131                     connection.close();
132                 if (connectionProvider != null)
133                     connectionProvider.close();
134             } catch (Exception JavaDoc e) {
135                 exceptions.add(e);
136                 log.error(Messages.getErrorString("PentahoSchemaUpdate.ERROR_0003_CLOSING_CONNECTION"), e); //$NON-NLS-1$
137
}
138
139         }
140     }
141
142     /**
143      * Returns a List of all Exceptions which occured during the export.
144      *
145      * @return A List containig the Exceptions occured during the export
146      */

147     public List JavaDoc getExceptions() {
148         return exceptions;
149     }
150 }
151
Popular Tags