1 package com.quantum.model;
3 import java.sql.Connection;
4 import java.sql.DatabaseMetaData;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.List;
14 import com.quantum.adapters.DatabaseAdapter;
17 * This class models a table or view.
21 abstract class EntityImpl implements Entity {
23 // The JDBC-ODBC Driver is more happy if you look up metadata values
24 // using the column number than if you use the column name
26 private static final int INDEX_METADATA_INDEX_NAME = 6;
27 private static final int INDEX_METADATA_COLUMN_NAME = 9;
28 private static final int INDEX_METADATA_ASC_OR_DESC = 10;
30 private static final int PRIMARY_KEYS_METADATA_COLUMN_NAME = 4;
31 private static final int PRIMARY_KEYS_METADATA_KEY_SEQ = 5;
33 private static final int COLUMN_METADATA_COLUMN_NAME = 4;
34 private static final int COLUMN_METATDATA_DATA_TYPE = 5;
35 private static final int COLUMN_METATDATA_TYPE_NAME = 6;
36 private static final int COLUMN_METADATA_COLUMN_SIZE = 7;
37 private static final int COLUMN_METADATA_DECIMAL_DIGITS = 9;
38 private static final int COLUMN_METADATA_REMARKS = 12;
39 private static final int COLUMN_METADATA_ORDINAL_POSITION = 17;
40 private static final int COLUMN_METADATA_IS_NULLABLE = 18;
42 private String schema;
45 private Bookmark bookmark;
46 private Boolean exists = Boolean.TRUE;
48 public EntityImpl(Bookmark bookmark, String schema, String name, String type) {
52 this.bookmark = bookmark;
54 public Bookmark getBookmark() {
57 public String getName() {
60 public String getSchema() {
63 public String getType() {
66 public String getQualifiedName() {
67 return (this.schema == null || this.schema.length() == 0) ?
68 this.name : this.schema + "." + this.name;
70 public Column getColumn(String columnName) throws NotConnectedException, SQLException {
72 Column[] columns = getColumns();
73 for (int i = 0, length = (columns == null) ? 0 : columns.length;
74 column == null && i < length;
76 if (columnName != null && columnName.equals(columns[i].getName())) {
82 public Column[] getColumns() throws NotConnectedException, SQLException {
84 Map temp = new HashMap();
85 Connection connection = this.bookmark.getConnection();
86 DatabaseMetaData metaData = connection.getMetaData();
87 ResultSet resultSet = metaData.getColumns(null, getSchema(), getName(), null);
89 while (resultSet.next()) {
90 ColumnImpl column = new ColumnImpl(
92 resultSet.getString(COLUMN_METADATA_COLUMN_NAME),
93 resultSet.getString(COLUMN_METATDATA_TYPE_NAME),
94 resultSet.getInt(COLUMN_METATDATA_DATA_TYPE),
95 resultSet.getInt(COLUMN_METADATA_COLUMN_SIZE),
96 resultSet.getInt(COLUMN_METADATA_DECIMAL_DIGITS),
97 "YES".equalsIgnoreCase(resultSet.getString(COLUMN_METADATA_IS_NULLABLE)),
98 resultSet.getInt(COLUMN_METADATA_ORDINAL_POSITION),
100 resultSet.getString(COLUMN_METADATA_REMARKS),
102 resultSet.getString(COLUMN_METADATA_COLUMN_NAME))
104 temp.put(column.getName(), column);
110 resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
112 while (resultSet.next()) {
113 String name = resultSet.getString(PRIMARY_KEYS_METADATA_COLUMN_NAME);
114 short keySequence = resultSet.getShort(PRIMARY_KEYS_METADATA_KEY_SEQ);
115 ColumnImpl column = (ColumnImpl) temp.get(name);
116 if (column != null) {
117 column.setPrimaryKeyOrder(keySequence);
122 List columnList = Collections.synchronizedList(
123 new ArrayList(temp.values()));
124 Collections.sort(columnList);
125 return (Column[]) columnList.toArray(new Column[columnList.size()]);
132 * Some JDBC drivers (Oracle for example) won't return the comments
133 * We recheck with a custom query, if it's defined
134 * @param iniComment The already got comment
135 * @param tableName The fully qualified table name
136 * @param columnName The column name
138 private String getComments( String iniComment, String tableName, String columnName) {
139 if (iniComment != null && iniComment.length() > 0)
143 Connection con = this.bookmark.getConnection();
144 DatabaseAdapter adapter = this.bookmark.getAdapter();
145 Statement stmt = con.createStatement();
147 if (adapter != null && stmt != null
148 && adapter.getCommentsQuery(tableName, columnName) != null) {
150 stmt.execute(adapter.getCommentsQuery(tableName, columnName));
151 ResultSet set = stmt.getResultSet();
154 comment = set.getString(1);
163 } catch (NotConnectedException e) {
164 } catch (SQLException e) {
170 public Index[] getIndexes() {
172 List indexList = new ArrayList();
173 Map temp = new HashMap();
175 Connection connection = this.bookmark.getConnection();
176 DatabaseMetaData metaData = connection.getMetaData();
177 ResultSet resultSet = metaData.getIndexInfo(null, getSchema(), getName(), false, false);
179 while (resultSet.next()) {
180 String indexName = resultSet.getString(INDEX_METADATA_INDEX_NAME);
181 IndexImpl index = (IndexImpl) temp.get(indexName);
183 index = new IndexImpl(this, indexName);
184 temp.put(indexName, index);
186 String columnName = resultSet.getString(INDEX_METADATA_COLUMN_NAME);
187 String ascending = resultSet.getString(INDEX_METADATA_ASC_OR_DESC);
188 index.addColumn(columnName, ascending == null
189 ? null : (ascending.toUpperCase().startsWith("A")
190 ? Boolean.TRUE : Boolean.FALSE));
193 indexList.addAll(temp.values());
195 } catch (NotConnectedException e) {
196 } catch (SQLException e) {
198 return (Index[]) indexList.toArray(new Index[indexList.size()]);
201 public Boolean exists() {
207 * @see com.quantum.model.Entity#getQuotedTableName()
209 public String getQuotedTableName() {
210 return getBookmark().getAdapter().filterTableName(getQualifiedName());
213 public ForeignKey[] getExportedKeys() throws SQLException, NotConnectedException {
214 return this.bookmark.getDatabase().getExportedKeys(getSchema(), getName());
217 public ForeignKey[] getImportedKeys() throws SQLException, NotConnectedException {
218 return this.bookmark.getDatabase().getImportedKeys(getSchema(), getName());
220 public ForeignKey[] getReferences() throws SQLException, NotConnectedException {
221 ForeignKey[] importedKeys = getImportedKeys();
222 ForeignKey[] exportedKeys = getExportedKeys();
224 List list = new ArrayList(); // if we could guarantee JDK 1.4, we'd use LinkedHashSet
225 for (int i = 0, length = importedKeys == null ? 0 : importedKeys.length; i < length; i++) {
226 list.add(importedKeys[i]);
228 for (int i = 0, length = exportedKeys == null ? 0 : exportedKeys.length; i < length; i++) {
229 if (!list.contains(exportedKeys[i])) {
230 list.add(exportedKeys[i]);
233 return (ForeignKey[]) list.toArray(new ForeignKey[list.size()]);
236 public int compareTo(Object object) {
237 Entity that = (Entity) object;
238 if (that.getQualifiedName() == null && this.getQualifiedName() != null) {
240 } else if (this.getQualifiedName() == null && that.getQualifiedName() != null) {
242 } else if (this.getQualifiedName() == null && that.getQualifiedName() == null) {
245 return this.getQualifiedName().compareTo(that.getQualifiedName());