improved syntax highlighting (php tag/return keyword/brackets and operators)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / Database.java
index b6dee05..5637b55 100644 (file)
@@ -10,7 +10,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import com.quantum.IQuantumConstants;
 import com.quantum.adapters.DatabaseAdapter;
 import com.quantum.sql.MultiSQLServer;
 import com.quantum.sql.SQLResults;
@@ -28,25 +27,29 @@ public class Database {
         this.databaseAdapter = bookmark.getAdapter();
     }
 
-    private static final String[] ALL_TYPES = { 
-        IQuantumConstants.Table, 
-        IQuantumConstants.View, 
-        IQuantumConstants.Sequence };
+    private static final String[] ALL_TABLE_TYPES = { 
+        Entity.TABLE_TYPE, 
+        Entity.VIEW_TYPE, 
+        Entity.SEQUENCE_TYPE };
         
     private static final List STANDARD_TABLE_TYPES = 
         Collections.synchronizedList(new ArrayList());
         
     static {
-        for (int i = 0, length = (ALL_TYPES == null) ? 0 : ALL_TYPES.length;
+        for (int i = 0, length = (ALL_TABLE_TYPES == null) ? 0 : ALL_TABLE_TYPES.length;
             i < length;
             i++) {
-            STANDARD_TABLE_TYPES.add(ALL_TYPES[i]);
+            STANDARD_TABLE_TYPES.add(ALL_TABLE_TYPES[i]);
         }
     }
     
     public String[] getEntityTypes() 
         throws NotConnectedException, SQLException {
-        return getEntityTypes(this.bookmark.getConnection(), this.bookmark.getSchemas()[0]);
+        return getEntityTypes(this.bookmark.getConnection());
+    }
+    
+    public String getUsername() throws NotConnectedException, SQLException {
+       return getMetaData().getUserName();
     }
 
 
@@ -60,23 +63,18 @@ public class Database {
      * Tables, Views and Sequences.</p>
      * 
      * @param connection
-     * @param schema -
-     *      This parameter is somewhat bogus.  It is used to determine if the
-     *      adapter defines an SQL statement for finding entities of a 
-     *      particular types.  
      * @return
      * @throws SQLException
      */
-    public String[] getEntityTypes(Connection connection, Schema schema) 
-        throws SQLException {
+    public String[] getEntityTypes(Connection connection) throws SQLException {
         
         Set set = new HashSet();
-        if (this.databaseAdapter.getShowTableQuery(schema.getName(), false) != null) {
-            set.add(IQuantumConstants.Table);
-        } else if (this.databaseAdapter.getShowViewQuery(schema.getName(), false) != null) {
-            set.add(IQuantumConstants.View);
-        } else if (this.databaseAdapter.getShowSequenceQuery(schema.getName(), false) != null) {
-            set.add(IQuantumConstants.Sequence);
+        if (this.databaseAdapter.getShowTableQuery(this.bookmark.getUsername()) != null) {
+            set.add(Entity.TABLE_TYPE);
+        } else if (this.databaseAdapter.getShowViewQuery(this.bookmark.getUsername()) != null) {
+            set.add(Entity.VIEW_TYPE);
+        } else if (this.databaseAdapter.getShowSequenceQuery(this.bookmark.getUsername()) != null) {
+            set.add(Entity.SEQUENCE_TYPE);
         }
         
         DatabaseMetaData metaData = connection.getMetaData();
@@ -84,6 +82,7 @@ public class Database {
         while (resultSet.next()) {
             String type = resultSet.getString("TABLE_TYPE");
             if (type != null) {
+               // Informix, in particular, pads this with extra spaces
                 type = type.trim();
             }
             if (STANDARD_TABLE_TYPES.contains(type)) {
@@ -96,8 +95,7 @@ public class Database {
 
     public String getInformation() throws SQLException {
         try {
-            Connection connection = this.bookmark.getConnection();
-            DatabaseMetaData metaData = connection.getMetaData();
+            DatabaseMetaData metaData = getMetaData();
         
             return metaData == null ? null : metaData.getDatabaseProductName() + " " 
                     + metaData.getDatabaseProductVersion();
@@ -137,7 +135,7 @@ public class Database {
         throws SQLException {
         
         List list = new ArrayList();
-        String[] types = (type == null) ? ALL_TYPES : new String[] { type };
+        String[] types = (type == null) ? ALL_TABLE_TYPES : new String[] { type };
             
         for (int i = 0; i < types.length; i++) {
             list.addAll(getEntitiesList(bookmark, connection, types[i], schema));
@@ -196,19 +194,114 @@ public class Database {
         set.close();
         return list;
     }
+    
+    public DataType[] getTypes() throws NotConnectedException, SQLException {
+       DatabaseMetaData metaData = getMetaData();
+       List list = new ArrayList();
+       ResultSet results = metaData.getTypeInfo();
+       try {
+               while (results.next()) {
+                       String name = results.getString("TYPE_NAME");
+                       int type = results.getInt("DATA_TYPE");
+                       list.add(new DataType(type, name));
+               }
+       } finally {
+               results.close();
+       }
+       return (DataType[]) list.toArray(new DataType[list.size()]);
+    }
 
 
-    private String getSQL(Bookmark bookmark, String type, Schema schema) {
+    /**
+        * @return
+        * @throws NotConnectedException
+        * @throws SQLException
+        */
+       private DatabaseMetaData getMetaData() throws NotConnectedException, SQLException {
+               Connection connection = this.bookmark.getConnection();
+       DatabaseMetaData metaData = connection.getMetaData();
+               return metaData;
+       }
+
+       private String getSQL(Bookmark bookmark, String type, Schema schema) {
         if (Entity.TABLE_TYPE.equals(type)) {
-            return this.databaseAdapter.getShowTableQuery(schema.getName(), schema.isDefault());
+            return this.databaseAdapter.getShowTableQuery(schema.getName());
         } else if (Entity.VIEW_TYPE.equals(type)) {
-            return this.databaseAdapter.getShowViewQuery(schema.getName(), schema.isDefault());
+            return this.databaseAdapter.getShowViewQuery(schema.getName());
         } else if (Entity.SEQUENCE_TYPE.equals(type)) {
-            return this.databaseAdapter.getShowSequenceQuery(schema.getName(), schema.isDefault());
+            return this.databaseAdapter.getShowSequenceQuery(schema.getName());
         } else {
             return null;
         }
     }
-    
+       
+       public ForeignKey[] getExportedKeys(String schema, String entityName) 
+                       throws NotConnectedException, SQLException {
+               DatabaseMetaData metaData = getMetaData();
+               List list = new ArrayList();
+               return getForeignKeys(list, metaData.getExportedKeys(null, schema, entityName));
+       }
+
+       public ForeignKey[] getImportedKeys(String schema, String entityName) 
+                       throws NotConnectedException, SQLException {
+               DatabaseMetaData metaData = getMetaData();
+               List list = new ArrayList();
+               return getForeignKeys(list, metaData.getImportedKeys(null, schema, entityName));
+       }
+
+       /**
+        * @param list
+        * @param resultSet
+        * @return
+        * @throws SQLException
+        */
+       private ForeignKey[] getForeignKeys(List list, ResultSet resultSet) throws SQLException {
+               ForeignKeyImpl foreignKey = null;
+               
+               int lowestKeySequence = Integer.MAX_VALUE;
+               try {
+                       while (resultSet.next()) {
+                               int keySequence = resultSet.getInt("KEY_SEQ");
+                               lowestKeySequence = Math.min(lowestKeySequence, keySequence);
+                               
+                               if (keySequence == lowestKeySequence || foreignKey == null) {
+                                       foreignKey = new ForeignKeyImpl();
+                                       list.add(foreignKey);
+                                       foreignKey.setName(resultSet.getString("FK_NAME"));
+                                       foreignKey.setDeleteRule(resultSet.getShort("DELETE_RULE"));
+                                       foreignKey.setForeignEntitySchema(resultSet.getString("FKTABLE_SCHEM"));
+                                       foreignKey.setForeignEntityName(resultSet.getString("FKTABLE_NAME"));
+                                       foreignKey.setLocalEntitySchema(resultSet.getString("PKTABLE_SCHEM"));
+                                       foreignKey.setLocalEntityName(resultSet.getString("PKTABLE_NAME"));
+                               }
+                               
+                               foreignKey.addColumns(
+                                               resultSet.getString("PKCOLUMN_NAME"), 
+                                               resultSet.getString("FKCOLUMN_NAME"));
+                       }
+                       return (ForeignKey[]) list.toArray(new ForeignKey[list.size()]);
+               } finally {
+                       resultSet.close();
+               }
+       }
 
+       /**
+        * @return
+        * @throws SQLException
+        * @throws NotConnectedException
+        */
+       public Schema[] getSchemas() throws NotConnectedException, SQLException {
+               DatabaseMetaData metaData = getMetaData();
+               List list = new ArrayList();
+               ResultSet resultSet = metaData.getSchemas();
+               try {
+                       while (resultSet.next()) {
+                               String schemaName = resultSet.getString("TABLE_SCHEM");
+                               list.add(new Schema(schemaName));
+                       }
+                       return (Schema[]) list.toArray(new Schema[list.size()]);
+               } finally {
+                       resultSet.close();
+               }
+       }
 }