import java.util.List;
import java.util.Map;
-import com.quantum.adapters.AdapterFactory;
import com.quantum.adapters.DatabaseAdapter;
/**
public String getType() {
return this.type;
}
- public String getCondQualifiedName() {
+ public String getQualifiedName() {
return (this.schema == null || this.schema.length() == 0) ?
this.name : this.schema + "." + this.name;
}
- public Column getColumn(String columnName) {
+ public Column getColumn(String columnName) throws NotConnectedException, SQLException {
Column column = null;
Column[] columns = getColumns();
for (int i = 0, length = (columns == null) ? 0 : columns.length;
}
return column;
}
- public Column[] getColumns() {
+ public Column[] getColumns() throws NotConnectedException, SQLException {
- Column[] columns = new Column[0];
+ Map temp = new HashMap();
+ Connection connection = this.bookmark.getConnection();
+ DatabaseMetaData metaData = connection.getMetaData();
+ ResultSet resultSet = metaData.getColumns(null, getSchema(), getName(), null);
try {
- // TODO: Some DBs (like DB2) don't support metadata
- Map temp = new HashMap();
- Connection connection = this.bookmark.getConnection();
- DatabaseMetaData metaData = connection.getMetaData();
- ResultSet resultSet = metaData.getColumns(null, getSchema(), getName(), null);
-
- while (resultSet.next()) {
- ColumnImpl column = new ColumnImpl(
- this,
- resultSet.getString("COLUMN_NAME"),
- resultSet.getString("TYPE_NAME"),
- resultSet.getInt("DATA_TYPE"),
- resultSet.getInt("COLUMN_SIZE"),
- resultSet.getInt("DECIMAL_DIGITS"),
- "YES".equalsIgnoreCase(resultSet.getString("IS_NULLABLE")),
- resultSet.getInt("ORDINAL_POSITION"),
- getComments(resultSet.getString("REMARKS"),getCondQualifiedName(), resultSet.getString("COLUMN_NAME"))
- );
- temp.put(column.getName(), column);
- }
- resultSet.close();
+ while (resultSet.next()) {
+ ColumnImpl column = new ColumnImpl(
+ this,
+ resultSet.getString("COLUMN_NAME"),
+ resultSet.getString("TYPE_NAME"),
+ resultSet.getInt("DATA_TYPE"),
+ resultSet.getInt("COLUMN_SIZE"),
+ resultSet.getInt("DECIMAL_DIGITS"),
+ "YES".equalsIgnoreCase(resultSet.getString("IS_NULLABLE")),
+ resultSet.getInt("ORDINAL_POSITION"),
+ getComments(resultSet.getString("REMARKS"),getQualifiedName(), resultSet.getString("COLUMN_NAME"))
+ );
+ temp.put(column.getName(), column);
+ }
+ } finally {
+ resultSet.close();
+ }
- resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
- while (resultSet.next()) {
- String name = resultSet.getString("COLUMN_NAME");
- short keySequence = resultSet.getShort("KEY_SEQ");
- ColumnImpl column = (ColumnImpl) temp.get(name);
- if (column != null) {
- column.setPrimaryKeyOrder(keySequence);
- }
- }
- resultSet.close();
-
- List columnList = Collections.synchronizedList(
- new ArrayList(temp.values()));
- Collections.sort(columnList);
- columns = (Column[]) columnList.toArray(new Column[columnList.size()]);
-
- } catch (NotConnectedException e) {
- } catch (SQLException e) {
+ resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
+ try {
+ while (resultSet.next()) {
+ String name = resultSet.getString("COLUMN_NAME");
+ short keySequence = resultSet.getShort("KEY_SEQ");
+ ColumnImpl column = (ColumnImpl) temp.get(name);
+ if (column != null) {
+ column.setPrimaryKeyOrder(keySequence);
+ }
+ }
+ resultSet.close();
+
+ List columnList = Collections.synchronizedList(
+ new ArrayList(temp.values()));
+ Collections.sort(columnList);
+ return (Column[]) columnList.toArray(new Column[columnList.size()]);
+ } finally {
+ resultSet.close();
}
- return columns;
}
/**
try {
Connection con = this.bookmark.getConnection();
Statement stmt = con.createStatement();
- DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(this.bookmark.getType());
+ DatabaseAdapter adapter = this.bookmark.getAdapter();
if (adapter != null && stmt != null && adapter.getCommentsQuery(tableName, columnName) != null) {
stmt.execute(adapter.getCommentsQuery(tableName, columnName));
* @see com.quantum.model.Entity#getQuotedTableName()
*/
public String getQuotedTableName() {
- return getBookmark().getAdapter().filterTableName(getCondQualifiedName());
+ return getBookmark().getAdapter().filterTableName(getQualifiedName());
}
+ public ForeignKey[] getExportedKeys() throws SQLException, NotConnectedException {
+ return this.bookmark.getDatabase().getExportedKeys(getSchema(), getName());
+ }
+
+ public ForeignKey[] getImportedKeys() throws SQLException, NotConnectedException {
+ return this.bookmark.getDatabase().getImportedKeys(getSchema(), getName());
+ }
+ public ForeignKey[] getReferences() throws SQLException, NotConnectedException {
+ ForeignKey[] importedKeys = getImportedKeys();
+ ForeignKey[] exportedKeys = getExportedKeys();
+
+ List list = new ArrayList(); // if we could guarantee JDK 1.4, we'd use LinkedHashSet
+ for (int i = 0, length = importedKeys == null ? 0 : importedKeys.length; i < length; i++) {
+ list.add(importedKeys[i]);
+ }
+ for (int i = 0, length = exportedKeys == null ? 0 : exportedKeys.length; i < length; i++) {
+ if (!list.contains(exportedKeys[i])) {
+ list.add(exportedKeys[i]);
+ }
+ }
+ return (ForeignKey[]) list.toArray(new ForeignKey[list.size()]);
+ }
+
+ public int compareTo(Object object) {
+ Entity that = (Entity) object;
+ if (that.getQualifiedName() == null && this.getQualifiedName() != null) {
+ return 1;
+ } else if (this.getQualifiedName() == null && that.getQualifiedName() != null) {
+ return -1;
+ } else if (this.getQualifiedName() == null && that.getQualifiedName() == null) {
+ return 0;
+ } else {
+ return this.getQualifiedName().compareTo(that.getQualifiedName());
+ }
+ }
}
\ No newline at end of file