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.AdapterFactory;
15 import com.quantum.adapters.DatabaseAdapter;
18 * This class models a table or view.
22 abstract class EntityImpl implements Entity {
23 private String schema;
26 private Bookmark bookmark;
27 private Boolean exists = Boolean.TRUE;
29 public EntityImpl(Bookmark bookmark, String schema, String name, String type) {
33 this.bookmark = bookmark;
35 public Bookmark getBookmark() {
38 public String getName() {
41 public String getSchema() {
44 public String getType() {
47 public String getCondQualifiedName() {
48 return (this.schema == null || this.schema.length() == 0) ?
49 this.name : this.schema + "." + this.name;
51 public Column getColumn(String columnName) {
53 Column[] columns = getColumns();
54 for (int i = 0, length = (columns == null) ? 0 : columns.length;
55 column == null && i < length;
57 if (columnName != null && columnName.equals(columns[i].getName())) {
63 public Column[] getColumns() {
65 Column[] columns = new Column[0];
67 // TODO: Some DBs (like DB2) don't support metadata
68 Map temp = new HashMap();
69 Connection connection = this.bookmark.getConnection();
70 DatabaseMetaData metaData = connection.getMetaData();
71 ResultSet resultSet = metaData.getColumns(null, getSchema(), getName(), null);
73 while (resultSet.next()) {
74 ColumnImpl column = new ColumnImpl(
76 resultSet.getString("COLUMN_NAME"),
77 resultSet.getString("TYPE_NAME"),
78 resultSet.getInt("DATA_TYPE"),
79 resultSet.getInt("COLUMN_SIZE"),
80 resultSet.getInt("DECIMAL_DIGITS"),
81 "YES".equalsIgnoreCase(resultSet.getString("IS_NULLABLE")),
82 resultSet.getInt("ORDINAL_POSITION"),
83 getComments(resultSet.getString("REMARKS"),getCondQualifiedName(), resultSet.getString("COLUMN_NAME"))
85 temp.put(column.getName(), column);
89 resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
90 while (resultSet.next()) {
91 String name = resultSet.getString("COLUMN_NAME");
92 short keySequence = resultSet.getShort("KEY_SEQ");
93 ColumnImpl column = (ColumnImpl) temp.get(name);
95 column.setPrimaryKeyOrder(keySequence);
100 List columnList = Collections.synchronizedList(
101 new ArrayList(temp.values()));
102 Collections.sort(columnList);
103 columns = (Column[]) columnList.toArray(new Column[columnList.size()]);
105 } catch (NotConnectedException e) {
106 } catch (SQLException e) {
112 * Some JDBC drivers (Oracle for example) won't return the comments
113 * We recheck with a custom query, if it's defined
114 * @param iniComment The already got comment
115 * @param tableName The fully qualified table name
116 * @param columnName The column name
118 private String getComments( String iniComment, String tableName, String columnName) {
119 if (iniComment != null && iniComment.length() > 0)
123 Connection con = this.bookmark.getConnection();
124 Statement stmt = con.createStatement();
125 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(this.bookmark.getType());
126 if (adapter != null && stmt != null && adapter.getCommentsQuery(tableName, columnName) != null) {
128 stmt.execute(adapter.getCommentsQuery(tableName, columnName));
129 ResultSet set = stmt.getResultSet();
131 comment = set.getString(1);
133 } catch (NotConnectedException e) {
134 } catch (SQLException e) {
139 public Index[] getIndexes() {
141 List indexList = new ArrayList();
142 Map temp = new HashMap();
144 Connection connection = this.bookmark.getConnection();
145 DatabaseMetaData metaData = connection.getMetaData();
146 ResultSet resultSet = metaData.getIndexInfo(null, getSchema(), getName(), false, false);
148 while (resultSet.next()) {
149 String indexName = resultSet.getString("INDEX_NAME");
150 IndexImpl index = (IndexImpl) temp.get(indexName);
152 index = new IndexImpl(this, indexName);
153 temp.put(indexName, index);
155 String columnName = resultSet.getString("COLUMN_NAME");
156 String ascending = resultSet.getString("ASC_OR_DESC");
157 index.addColumn(columnName, ascending == null
158 ? null : (ascending.toUpperCase().startsWith("A")
159 ? Boolean.TRUE : Boolean.FALSE));
162 indexList.addAll(temp.values());
164 } catch (NotConnectedException e) {
165 } catch (SQLException e) {
167 return (Index[]) indexList.toArray(new Index[indexList.size()]);
170 public Boolean exists() {
176 * @see com.quantum.model.Entity#getQuotedTableName()
178 public String getQuotedTableName() {
179 return getBookmark().getAdapter().filterTableName(getCondQualifiedName());