1 45 package org.exolab.jms.tools.db; 46 47 import java.sql.Connection ; 48 import java.sql.SQLException ; 49 import java.sql.Statement ; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 54 import org.exolab.jms.persistence.PersistenceException; 55 import org.exolab.jms.persistence.SQLHelper; 56 57 58 65 public class RDBMSTool { 66 67 70 private Connection _connection = null; 71 72 75 private SchemaBrowser _browser = null; 76 77 80 private static final Log _log = LogFactory.getLog(RDBMSTool.class); 81 82 83 89 public RDBMSTool(Connection connection) throws PersistenceException { 90 _connection = connection; 91 try { 92 _connection.setAutoCommit(true); 93 } catch (SQLException exception) { 94 throw new PersistenceException("Failed to set auto-commit on", 95 exception); 96 } 97 _browser = new SchemaBrowser(_connection); 98 } 99 100 107 public void create(Database schema) throws PersistenceException { 108 Table[] tables = schema.getTable(); 109 for (int i = 0; i < tables.length; ++i) { 110 create(tables[i]); 111 } 112 } 113 114 121 public void drop(Database schema) throws PersistenceException { 122 Table[] tables = schema.getTable(); 123 for (int i = 0; i < tables.length; ++i) { 124 drop(tables[i]); 125 } 126 Deprecated [] redundant = schema.getDeprecated(); 127 for (int i = 0; i < redundant.length; ++i) { 128 dropTable(redundant[i].getName()); 129 } 130 } 131 132 135 public void close() { 136 SQLHelper.close(_connection); 137 } 138 139 145 public void create(Table table) throws PersistenceException { 146 String name = table.getName(); 147 if (_browser.getTableExists(name)) { 148 throw new PersistenceException( 149 "An object already exists in the database named " + name); 150 } 151 152 StringBuffer sql = new StringBuffer ("create table "); 153 sql.append(name); 154 sql.append(" ("); 155 156 _log.debug("Creating table: " + name); 157 Attribute[] attributes = table.getAttribute(); 158 for (int i = 0; i < attributes.length; ++i) { 159 if (i > 0) { 160 sql.append(", "); 161 } 162 sql.append(attributes[i].getName()); 163 sql.append(" "); 164 sql.append(getSQLType(attributes[i])); 165 } 166 sql.append(")"); 167 168 _log.debug("SQL=" + sql); 169 Statement statement = null; 170 try { 171 statement = _connection.createStatement(); 172 statement.executeUpdate(sql.toString()); 173 } catch (SQLException exception) { 174 throw new PersistenceException("Failed to create table=" + name, 175 exception); 176 } finally { 177 SQLHelper.close(statement); 178 } 179 createIndexes(table); 180 } 181 182 189 public void drop(Table table) throws PersistenceException { 190 dropTable(table.getName()); 191 } 192 193 198 public SchemaBrowser getSchemaBrowser() { 199 return _browser; 200 } 201 202 208 private void createIndexes(Table table) throws PersistenceException { 209 Index[] indexes = table.getIndex(); 210 for (int i = 0; i < indexes.length; ++i) { 211 Index index = indexes[i]; 212 StringBuffer sql = new StringBuffer ("create "); 213 if (index.getUnique()) { 214 sql.append("unique "); 215 } 216 sql.append("index "); 217 sql.append(index.getName()); 218 sql.append(" on "); 219 sql.append(table.getName()); 220 sql.append("("); 221 Column[] columns = index.getColumn(); 222 for (int j = 0; j < columns.length; ++j) { 223 if (j > 0) { 224 sql.append(", "); 225 } 226 sql.append(columns[j].getName()); 227 } 228 sql.append(")"); 229 _log.debug("SQL=" + sql); 230 Statement statement = null; 231 try { 232 statement = _connection.createStatement(); 233 statement.executeUpdate(sql.toString()); 234 } catch (SQLException exception) { 235 throw new PersistenceException( 236 "Failed to create index=" + index.getName() + " on table " 237 + table.getName()); 238 } finally { 239 SQLHelper.close(statement); 240 } 241 } 242 } 243 244 250 private void dropTable(String name) throws PersistenceException { 251 if (_browser.getTableExists(name)) { 252 String sql = "drop table " + name; 253 _log.debug("SQL=" + sql); 254 Statement statement = null; 255 try { 256 statement = _connection.createStatement(); 257 statement.executeUpdate(sql); 258 } catch (SQLException exception) { 259 throw new PersistenceException("Failed to drop table=" + name, 260 exception); 261 } finally { 262 SQLHelper.close(statement); 263 } 264 } 265 } 266 267 275 private String getSQLType(Attribute attribute) 276 throws PersistenceException { 277 Type result = _browser.getType(attribute); 278 _log.debug("attribute=" + attribute.getName() + "->" + result); 279 return result.getSQL(); 280 } 281 282 } 283 | Popular Tags |