1 45 46 package org.exolab.jms.tools.db; 47 48 import java.sql.Connection ; 49 import java.sql.DatabaseMetaData ; 50 import java.sql.PreparedStatement ; 51 import java.sql.ResultSet ; 52 import java.sql.ResultSetMetaData ; 53 import java.sql.SQLException ; 54 55 import org.apache.commons.logging.Log; 56 import org.apache.commons.logging.LogFactory; 57 58 import org.exolab.jms.persistence.PersistenceException; 59 import org.exolab.jms.persistence.SQLHelper; 60 61 62 68 public class SchemaBrowser { 69 70 73 private Connection _connection = null; 74 75 78 private TypeSet _types = null; 79 80 84 private TypeMapper _mapper = null; 85 86 89 private static final Log _log = LogFactory.getLog(SchemaBrowser.class); 90 91 92 98 public SchemaBrowser(Connection connection) throws PersistenceException { 99 _connection = connection; 100 101 _types = new TypeSet(_connection); 103 _mapper = new TypeMapper(_types); 104 } 105 106 114 public Table getTable(String name) throws PersistenceException { 115 Table result = new Table(); 116 117 result.setName(name); 118 119 PreparedStatement select = null; 123 try { 124 select = _connection.prepareStatement( 125 "select * from " + name + " where 1 = 0"); 126 ResultSet set = select.executeQuery(); 127 ResultSetMetaData metaData = set.getMetaData(); 128 129 for (int i = 1; i <= metaData.getColumnCount(); ++i) { 130 String columnName = metaData.getColumnName(i); 131 int dataType = metaData.getColumnType(i); 132 long precision = metaData.getPrecision(i); 133 int nullable = metaData.isNullable(i); 134 String typeName = metaData.getColumnTypeName(i); 135 Type type = _mapper.getType(dataType, precision); 136 if (type == null) { 137 type = _types.getNearestType(dataType, precision); 142 if (type == null) { 143 throw new InvalidTypeException( 146 "JDBC driver error. Type=" + dataType 147 + ", precision=" + precision + "(SQL type=" 148 + typeName + ") isn't supported by " 149 + "Connection.getMetaData().getTypeInfo(), but is " 150 + "referred to by " 151 + "Connection.getMetaData().getColumns()"); 152 } 153 } 154 155 Attribute attribute = new Attribute(); 156 attribute.setName(columnName); 157 attribute.setType(type.getSymbolicType()); 158 if (nullable == DatabaseMetaData.columnNoNulls) { 159 attribute.setNot_null(true); 160 } else { 161 attribute.setNot_null(false); 162 } 163 result.addAttribute(attribute); 164 } 165 } catch (SQLException exception) { 166 throw new PersistenceException( 167 "Failed to determine the schema of table=" + name, 168 exception); 169 } finally { 170 SQLHelper.close(select); 171 } 172 return result; 173 } 174 175 183 public Type getType(Attribute attribute) throws PersistenceException { 184 Type result = null; 185 Type type = Type.getType(attribute.getType()); 186 Type map = _mapper.getType(type.getType(), type.getPrecision()); 187 if (map == null) { 188 throw new PersistenceException( 189 "Database does not support type=" + attribute.getType()); 190 } 191 192 if (type.getType() != map.getType()) { 193 result = map; 194 } else { 195 boolean parameters = type.getParameters(); 196 long precision = type.getPrecision(); 197 if (precision <= map.getPrecision()) { 198 if (precision == -1) { 199 precision = map.getPrecision(); 200 parameters = map.getParameters(); 201 } 202 result = new Type(map.getType(), map.getName(), 203 precision, parameters); 204 } else { 205 throw new PersistenceException( 206 attribute.getName() + type + " exceeds precision for " 207 + map + " precision=" + map.getPrecision()); 208 } 209 } 210 return result; 211 } 212 213 218 public boolean getTableExists(String table) throws PersistenceException { 219 boolean result = false; 220 221 String name = table; 222 ResultSet set = null; 223 try { 224 DatabaseMetaData metaData = _connection.getMetaData(); 225 if (metaData.storesUpperCaseIdentifiers()) { 226 name = name.toUpperCase(); 227 } 228 229 set = metaData.getTables(_connection.getCatalog(), null, 230 name, null); 231 if (set.next()) { 232 result = true; 233 } 234 } catch (SQLException exception) { 235 throw new PersistenceException("Failed to determine if table=" 236 + table + " exists", exception); 237 } finally { 238 SQLHelper.close(set); 239 } 240 return result; 241 } 242 243 } | Popular Tags |