1 22 23 package org.xquark.mapper.mapping; 24 25 import java.sql.DatabaseMetaData ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.util.*; 29 30 import org.xquark.jdbc.typing.ColumnMetaData; 31 import org.xquark.jdbc.typing.ColumnMetaDataImpl; 32 import org.xquark.mapper.dbms.AbstractConnection; 33 34 40 public class TableMetaDataImpl implements TableMetaData 41 { 42 private static final String RCSRevision = "$Revision: 1.3 $"; 43 private static final String RCSName = "$Name: $"; 44 45 46 private String name; 47 48 private Map columnsMetaData; 49 50 private Map uniqueConstraints = new HashMap(); 51 52 private boolean statementsAreShareable = true; 53 private boolean shared = false; 54 55 61 public TableMetaDataImpl(String name, AbstractConnection connection) 62 throws SQLException 63 { 64 DatabaseMetaData metadata = connection.getConnection().getMetaData(); 65 this.name = name; 66 String catalog = connection.getConnection().getCatalog(); 67 String schemaName = connection.getSchemaName(); 68 ResultSet rs = null; 69 try 70 { 71 columnsMetaData = connection.getTableMetadata(name); 74 75 rs = metadata.getPrimaryKeys(catalog, schemaName, name); 77 while (rs.next()) 78 { 79 String cname = rs.getString(4); 80 short seq = rs.getShort(5); 81 ColumnMetaDataImpl cmeta = 82 (ColumnMetaDataImpl) columnsMetaData.get(cname); 83 cmeta.setPrimaryKeySeq(seq); 84 } 85 rs.close(); 86 87 rs = metadata.getImportedKeys(catalog, schemaName, name); 89 while (rs.next()) 90 { 91 String pkTableName = rs.getString(3); 92 String pkColumnName = rs.getString(4); 93 String cname = rs.getString(8); 94 ColumnMetaDataImpl cmeta = 95 (ColumnMetaDataImpl) columnsMetaData.get(cname); 96 cmeta.setRefTable(pkTableName); 97 cmeta.setRefColumn(pkColumnName); 98 } 99 rs.close(); 100 101 try { 106 rs = metadata.getIndexInfo(catalog, schemaName, name, true, true); 107 while (rs.next()) 108 { 109 String indexName = rs.getString(6); 110 String indexColumnName = rs.getString(9); 111 112 ArrayList columns = 113 (ArrayList) uniqueConstraints.get(indexName); 114 if (columns == null) 115 { 116 columns = new ArrayList(); 117 uniqueConstraints.put(indexName, columns); 118 } 119 columns.add(columnsMetaData.get(indexColumnName)); 121 } 122 123 rs.close(); 124 } 125 catch (SQLException e) { if (connection.getUniversalError(e) != AbstractConnection.OBJECT_DOES_NOT_EXISTS) 128 throw e; 129 } 130 } 131 finally 132 { 133 if (rs != null) 134 rs.close(); 135 } 136 } 137 138 public boolean areStatementsShareable() 142 { 143 return shared && statementsAreShareable; 144 } 145 146 public void setStatementsNotShareable() 147 { 148 statementsAreShareable = false; 149 } 150 151 public String getTableName() 152 { 153 return name; 154 } 155 156 public ColumnMetaData getColumnMetaData(String cname) 157 { 158 ColumnMetaData ret = (ColumnMetaData) columnsMetaData.get(cname); 159 return ret; 166 } 167 168 public Collection getColumnsMetaData() 169 { 170 return columnsMetaData.values(); 171 } 172 173 public int getColumnCount() 174 { 175 return columnsMetaData.size(); 176 } 177 178 public Collection getUniqueConstraints() 179 { 180 return uniqueConstraints.values(); 181 } 182 183 public boolean isShared() 184 { 185 return shared; 186 } 187 188 public void setShared() 189 { 190 shared = true; 191 } 192 193 } 194 | Popular Tags |