1 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 76 public class V061toV072SchemaConverter implements SchemaConverter { 77 78 81 private Connection _connection; 82 83 86 private RDBMSTool _tool; 87 88 91 private static final String DESTINATIONS_TABLE = "destinations"; 92 93 96 private static final String ISQUEUE_COLUMN = "isQueue"; 97 98 101 private static final Log _log = 102 LogFactory.getLog(V061toV072SchemaConverter.class); 103 104 105 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 Table table = SchemaHelper.getTable(schema, DESTINATIONS_TABLE); 144 Attribute column = SchemaHelper.getAttribute(table, ISQUEUE_COLUMN); 145 Type expected = browser.getType(column); 146 147 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 _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 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 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 _tool.drop(table); 200 _tool.create(table); 201 202 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 _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 } | Popular Tags |