show HTML files as local resources (no 'http://' url prefix)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / EntityImpl.java
index a960fbb..8973ffd 100644 (file)
@@ -11,7 +11,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import com.quantum.adapters.AdapterFactory;
 import com.quantum.adapters.DatabaseAdapter;
 
 /**
@@ -44,11 +43,11 @@ abstract class EntityImpl implements Entity {
     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;
@@ -60,52 +59,50 @@ abstract class EntityImpl implements Entity {
         }
         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;
     }
     
     /**
@@ -122,7 +119,7 @@ abstract class EntityImpl implements Entity {
                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));
@@ -176,7 +173,42 @@ abstract class EntityImpl implements Entity {
      * @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