1 45 package org.exolab.jms.tools.db; 46 47 import java.io.FileInputStream ; 48 import java.io.FileNotFoundException ; 49 import java.io.InputStream ; 50 import java.io.InputStreamReader ; 51 import java.sql.Connection ; 52 import java.sql.PreparedStatement ; 53 import java.sql.ResultSet ; 54 import java.sql.SQLException ; 55 56 import org.exolab.castor.xml.MarshalException; 57 import org.exolab.castor.xml.ValidationException; 58 import org.exolab.jms.persistence.PersistenceException; 59 import org.exolab.jms.persistence.SQLHelper; 60 61 62 68 public class SchemaHelper { 69 70 73 private static final String SCHEMA = "/org/exolab/jms/tools/db/schema.xml"; 74 75 76 83 public static String getSchemaVersion(Connection connection) 84 throws PersistenceException { 85 String version = null; 86 PreparedStatement query = null; 87 ResultSet result = null; 88 try { 89 query = connection.prepareStatement( 90 "select version from system_data where id = 1"); 91 result = query.executeQuery(); 92 if (result.next()) { 93 version = result.getString(1); 94 } 95 } catch (SQLException exception) { 96 throw new PersistenceException( 97 "Failed to get the schema version", exception); 98 } finally { 99 SQLHelper.close(result); 100 SQLHelper.close(query); 101 } 102 return version; 103 } 104 105 public static void setVersion(Connection connection, String version) 106 throws PersistenceException { 107 PreparedStatement update = null; 108 try { 109 update = connection.prepareStatement( 110 "update system_data set version=? where id = 1"); 111 update.setString(1, version); 112 if (update.executeUpdate() != 1) { 113 throw new PersistenceException( 114 "Failed to update system_data.version"); 115 } 116 } catch (SQLException exception) { 117 throw new PersistenceException( 118 "Failed to update system_data.version", exception); 119 } finally { 120 SQLHelper.close(update); 121 } 122 } 123 124 public static Table getTable(Database schema, String name) { 125 Table result = null; 126 Table[] tables = schema.getTable(); 127 for (int i = 0; i < tables.length; ++i) { 128 if (tables[i].getName().equalsIgnoreCase(name)) { 129 result = tables[i]; 130 break; 131 } 132 } 133 return result; 134 } 135 136 public static Attribute getAttribute(Table table, String name) { 137 Attribute result = null; 138 Attribute[] attributes = table.getAttribute(); 139 for (int i = 0; i < attributes.length; ++i) { 140 if (attributes[i].getName().equalsIgnoreCase(name)) { 141 result = attributes[i]; 142 break; 143 } 144 } 145 return result; 146 } 147 148 public static Database getSchema() throws PersistenceException { 149 return getSchemaFromResource(SCHEMA); 150 } 151 152 public static Database getSchemaFromResource(String path) 153 throws PersistenceException { 154 Database schema = null; 155 InputStream stream = SchemaHelper.class.getResourceAsStream(path); 156 if (stream == null) { 157 throw new PersistenceException("Cannot locate resource: " + 158 path); 159 } 160 try { 161 schema = Database.unmarshal(new InputStreamReader (stream)); 162 } catch (MarshalException exception) { 163 throw new PersistenceException(exception.getMessage()); 164 } catch (ValidationException exception) { 165 throw new PersistenceException(exception.getMessage()); 166 } 167 return schema; 168 } 169 170 public static Database getSchema(String path) throws PersistenceException { 171 Database schema = null; 172 InputStream stream = null; 173 try { 174 stream = new FileInputStream (path); 175 } catch (FileNotFoundException exception) { 176 throw new PersistenceException(exception.getMessage(), exception); 177 } 178 179 try { 180 schema = Database.unmarshal(new InputStreamReader (stream)); 181 } catch (MarshalException exception) { 182 throw new PersistenceException(exception.getMessage()); 183 } catch (ValidationException exception) { 184 throw new PersistenceException(exception.getMessage()); 185 } 186 return schema; 187 } 188 189 } 190 | Popular Tags |