Quantum version 2.4.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / EntityImpl.java
index f16a1e0..966329a 100644 (file)
@@ -11,7 +11,11 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import com.quantum.adapters.AdapterFactory;
 import com.quantum.adapters.DatabaseAdapter;
+import com.quantum.sql.MultiSQLServer;
+import com.quantum.sql.SQLMetaDataResults;
+import com.quantum.util.sql.SQLStates;
 
 /**
  * This class models a table or view.
@@ -81,54 +85,103 @@ abstract class EntityImpl implements Entity {
     }
     public Column[] getColumns() throws NotConnectedException, SQLException {
         
-        Map temp = new HashMap();
         Connection connection = this.bookmark.getConnection();
-        DatabaseMetaData metaData = connection.getMetaData();
-        ResultSet resultSet = metaData.getColumns(null, getSchema(), getName(), null);
         try {
-               while (resultSet.next()) {
-                   ColumnImpl column = new ColumnImpl(
-                       this, 
-                       resultSet.getString(COLUMN_METADATA_COLUMN_NAME),
-                       resultSet.getString(COLUMN_METATDATA_TYPE_NAME),
-                       resultSet.getInt(COLUMN_METATDATA_DATA_TYPE),
-                       resultSet.getInt(COLUMN_METADATA_COLUMN_SIZE),
-                       resultSet.getInt(COLUMN_METADATA_DECIMAL_DIGITS),
-                       "YES".equalsIgnoreCase(resultSet.getString(COLUMN_METADATA_IS_NULLABLE)),
-                       resultSet.getInt(COLUMN_METADATA_ORDINAL_POSITION),
+               return getColumnsFromMetaData(connection);
+        } catch (SQLException e) {
+               if (SQLStates.ODBC_DRIVER_NOT_CAPABLE.equals(e.getSQLState()) 
+                               && AdapterFactory.JDBC_ODBC_BRIDGE.equals(
+                                               getBookmark().getJDBCDriver().getType())) {
+                       return getColumnsFromQuery(connection);
+               } else {
+                       throw e;
+               }
+        }
+        
+    }
+    
+    /**
+        * @param connection
+        * @return
+        * @throws SQLException
+        */
+       private Column[] getColumnsFromMetaData(Connection connection) throws SQLException {
+               Map temp = new HashMap();
+               DatabaseMetaData metaData = connection.getMetaData();
+               ResultSet resultSet = metaData.getColumns(null, getSchema(), getName(), null);
+               try {
+                   while (resultSet.next()) {
+                       ColumnImpl column = new ColumnImpl(
+                           this, 
+                           resultSet.getString(COLUMN_METADATA_COLUMN_NAME),
+                           resultSet.getString(COLUMN_METATDATA_TYPE_NAME),
+                           resultSet.getInt(COLUMN_METATDATA_DATA_TYPE),
+                           resultSet.getInt(COLUMN_METADATA_COLUMN_SIZE),
+                           resultSet.getInt(COLUMN_METADATA_DECIMAL_DIGITS),
+                           "YES".equalsIgnoreCase(resultSet.getString(COLUMN_METADATA_IS_NULLABLE)),
+                           resultSet.getInt(COLUMN_METADATA_ORDINAL_POSITION),
                                        getComments(
                                                        resultSet.getString(COLUMN_METADATA_REMARKS),
                                                        getQualifiedName(), 
                                                        resultSet.getString(COLUMN_METADATA_COLUMN_NAME))
-                       );
-                   temp.put(column.getName(), column);
-               }
-        } finally {
-               resultSet.close();
-        }
+                           );
+                       temp.put(column.getName(), column);
+                   }
+               } finally {
+                       resultSet.close();
+               }
 
-        resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
-        try {
-               while (resultSet.next()) {
-                   String name = resultSet.getString(PRIMARY_KEYS_METADATA_COLUMN_NAME);
-                   short keySequence = resultSet.getShort(PRIMARY_KEYS_METADATA_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();
-        }
-    }
-    
-    /**
+               resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
+               try {
+                   while (resultSet.next()) {
+                       String name = resultSet.getString(PRIMARY_KEYS_METADATA_COLUMN_NAME);
+                       short keySequence = resultSet.getShort(PRIMARY_KEYS_METADATA_KEY_SEQ);
+                       ColumnImpl column = (ColumnImpl) temp.get(name);
+                       if (column != null) {
+                           column.setPrimaryKeyOrder(keySequence);
+                       }
+                       
+                   }
+                   
+                   List columnList = Collections.synchronizedList(
+                       new ArrayList(temp.values()));
+                   Collections.sort(columnList);
+                   return (Column[]) columnList.toArray(new Column[columnList.size()]);
+                   
+               } finally {
+                       resultSet.close();
+               }
+       }
+       
+       /**
+        * Some databases, (in particular, MS Access under ODBC) aren't terribly friendly
+        * about supporting metadata.  This method scrapes out the data the old-fashioned way.
+        * 
+        * @param temp
+        * @param connection
+        * @throws SQLException
+        */
+       private Column[] getColumnsFromQuery(Connection connection) throws SQLException {
+               List temp = new ArrayList();
+               SQLMetaDataResults results = 
+                               (SQLMetaDataResults) MultiSQLServer.getInstance().getMetaData(
+                                       this, connection);
+               SQLMetaDataResults.Row[] rows = results.getRows();
+               for (int i = 0, length = results.getRowCount(); i < length; i++) {
+                   ColumnImpl column = new ColumnImpl(
+                           this, 
+                           (String) rows[i].get(1),
+                           (String) rows[i].get(2),
+                           ((Integer) rows[i].get(7)).intValue(),
+                           ((Integer) rows[i].get(3)).intValue(),
+                           ((Integer) rows[i].get(4)).intValue(),
+                           "Nullable".equalsIgnoreCase((String) rows[i].get(5)),
+                           i+1, "");
+                       temp.add(column);
+               }
+               return (Column[]) temp.toArray(new Column[temp.size()]);
+       }
+       /**
      * Some JDBC drivers (Oracle for example) won't return the comments
      * We recheck with a custom query, if it's defined
         * @param iniComment The already got comment