View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2002-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: V061toV072SchemaConverter.java,v 1.1 2004/11/26 01:51:16 tanderson Exp $
44   */
45  package org.exolab.jms.tools.db.migration;
46  
47  import java.sql.Connection;
48  import java.sql.PreparedStatement;
49  import java.sql.ResultSet;
50  import java.sql.SQLException;
51  
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  
55  import org.exolab.castor.xml.MarshalException;
56  import org.exolab.castor.xml.ValidationException;
57  import org.exolab.jms.persistence.PersistenceException;
58  import org.exolab.jms.persistence.SQLHelper;
59  import org.exolab.jms.tools.db.Attribute;
60  import org.exolab.jms.tools.db.Database;
61  import org.exolab.jms.tools.db.InvalidTypeException;
62  import org.exolab.jms.tools.db.RDBMSTool;
63  import org.exolab.jms.tools.db.SchemaBrowser;
64  import org.exolab.jms.tools.db.SchemaConverter;
65  import org.exolab.jms.tools.db.SchemaHelper;
66  import org.exolab.jms.tools.db.Table;
67  import org.exolab.jms.tools.db.Type;
68  
69  
70  /***
71   * A schema converter for converting from the 0.6.1 schema to the 0.7.2 schema
72   *
73   * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:51:16 $
74   * @author      <a href="mailto:tima@intalio.com">Tim Anderson</a>
75   */
76  public class V061toV072SchemaConverter implements SchemaConverter {
77  
78      /***
79       * The database connection
80       */
81      private Connection _connection;
82  
83      /***
84       * The RDBMS tool
85       */
86      private RDBMSTool _tool;
87  
88      /***
89       * The name of the destinations table
90       */
91      private static final String DESTINATIONS_TABLE = "destinations";
92  
93      /***
94       * The name of the isQueue column
95       */
96      private static final String ISQUEUE_COLUMN = "isQueue";
97  
98      /***
99       * The logger
100      */
101     private static final Log _log =
102         LogFactory.getLog(V061toV072SchemaConverter.class);
103 
104 
105     /***
106      * Construct a new <code>V061toV072SchemaConverter</code>
107      *
108      * @param connection the connection to use
109      */
110     public V061toV072SchemaConverter(Connection connection) {
111         _connection = connection;
112     }
113 
114     public void convert() throws PersistenceException {
115         Database schema = SchemaHelper.getSchema();
116         try {
117             if (_connection.getAutoCommit()) {
118                 _connection.setAutoCommit(false);
119             }
120             _tool = new RDBMSTool(_connection);
121         } catch (SQLException exception) {
122             throw new PersistenceException(exception.getMessage());
123         }
124 
125         try {
126             if (needsConversion(schema)) {
127                 doConvert(schema);
128             }
129             SchemaHelper.setVersion(_connection, "V0.7.2");
130             _connection.commit();
131         } catch (SQLException exception) {
132             SQLHelper.rollback(_connection);
133             throw new PersistenceException(exception);
134         }
135     }
136 
137     private boolean needsConversion(Database schema)
138         throws PersistenceException {
139         boolean result = false;
140         SchemaBrowser browser = _tool.getSchemaBrowser();
141 
142         // get the expected type of the isQueue column
143         Table table = SchemaHelper.getTable(schema, DESTINATIONS_TABLE);
144         Attribute column = SchemaHelper.getAttribute(table, ISQUEUE_COLUMN);
145         Type expected = browser.getType(column);
146 
147         // get the actual type of the isQueue column
148         try {
149             Table currentTable = browser.getTable(DESTINATIONS_TABLE);
150             Attribute currentColumn =
151                 SchemaHelper.getAttribute(currentTable, ISQUEUE_COLUMN);
152             Type currentType = browser.getType(currentColumn);
153             result = (currentType.getType() != expected.getType());
154         } catch (InvalidTypeException exception) {
155             // this will only occur if the JDBC driver is buggy (its amazing
156             // home many are - MM.MySQL 2.0.x and Oracle are 2 examples)
157             // Try and perform a conversion anyway - and hope for the best...
158             _log.warn(exception);
159             result = true;
160         }
161         return result;
162     }
163 
164     private void doConvert(Database schema) throws PersistenceException {
165         Table table = SchemaHelper.getTable(schema, DESTINATIONS_TABLE);
166 
167         // create a temporary table to perform conversion
168         Table tmpTable = new Table();
169         String tmpName = "openjms_tmp_" + DESTINATIONS_TABLE;
170         tmpTable.setName(tmpName);
171         tmpTable.setAttribute(table.getAttribute());
172 
173         _tool.drop(tmpTable);
174         _tool.create(tmpTable);
175 
176         // convert the destinations table, inserting converted records into
177         // the temporary table
178         PreparedStatement select = null;
179         ResultSet set = null;
180         try {
181             select = _connection.prepareStatement(
182                 "select * from " + DESTINATIONS_TABLE);
183             set = select.executeQuery();
184             while (set.next()) {
185                 String name = set.getString(1);
186                 boolean isQueue = (set.getInt(2) > 0);
187                 long id = set.getLong(3);
188                 insert(tmpName, name, isQueue, id);
189             }
190         } catch (SQLException exception) {
191             throw new PersistenceException("Failed to convert destinations",
192                 exception);
193         } finally {
194             SQLHelper.close(set);
195             SQLHelper.close(select);
196         }
197 
198         // recreate the destinations table
199         _tool.drop(table);
200         _tool.create(table);
201 
202         // copy the data from the temporary table into the destinations table
203         PreparedStatement insert = null;
204         try {
205             insert = _connection.prepareStatement(
206                 "insert into " + DESTINATIONS_TABLE + " select * from " +
207                 tmpName);
208             insert.executeQuery();
209         } catch (SQLException exception) {
210             throw new PersistenceException(
211                 "Failed to copy converted destinations", exception);
212         } finally {
213             SQLHelper.close(insert);
214         }
215 
216         // drop the temporary table
217         _tool.drop(tmpTable);
218     }
219 
220     private void insert(String table, String name, boolean isQueue, long id)
221         throws SQLException {
222         PreparedStatement insert = _connection.prepareStatement(
223             "insert into " + table + " values (?, ?, ?)");
224         insert.setString(1, name);
225         insert.setBoolean(2, isQueue);
226         insert.setLong(3, id);
227         insert.executeUpdate();
228     }
229 
230 
231 } //-- V061toV072SchemaConverter