new sql.gif
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / EntityImpl.java
index 8973ffd..f16a1e0 100644 (file)
@@ -1,4 +1,4 @@
-       package com.quantum.model;
+package com.quantum.model;
 
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
@@ -19,7 +19,27 @@ import com.quantum.adapters.DatabaseAdapter;
  * @author bcholmes
  */
 abstract class EntityImpl implements Entity {
-    private String schema;
+       
+       // The JDBC-ODBC Driver is more happy if you look up metadata values
+       // using the column number than if you use the column name
+       
+       private static final int INDEX_METADATA_INDEX_NAME = 6;
+       private static final int INDEX_METADATA_COLUMN_NAME = 9;
+       private static final int INDEX_METADATA_ASC_OR_DESC = 10;
+       
+       private static final int PRIMARY_KEYS_METADATA_COLUMN_NAME = 4;
+       private static final int PRIMARY_KEYS_METADATA_KEY_SEQ = 5;
+       
+       private static final int COLUMN_METADATA_COLUMN_NAME = 4;
+       private static final int COLUMN_METATDATA_DATA_TYPE = 5;
+       private static final int COLUMN_METATDATA_TYPE_NAME = 6;
+       private static final int COLUMN_METADATA_COLUMN_SIZE = 7;
+       private static final int COLUMN_METADATA_DECIMAL_DIGITS = 9;
+       private static final int COLUMN_METADATA_REMARKS = 12;
+       private static final int COLUMN_METADATA_ORDINAL_POSITION = 17;
+       private static final int COLUMN_METADATA_IS_NULLABLE = 18;
+       
+       private String schema;
     private String name;
     private String type;
     private Bookmark bookmark;
@@ -69,14 +89,17 @@ abstract class EntityImpl implements Entity {
                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"))
+                       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);
                }
@@ -87,8 +110,8 @@ abstract class EntityImpl implements Entity {
         resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
         try {
                while (resultSet.next()) {
-                   String name = resultSet.getString("COLUMN_NAME");
-                   short keySequence = resultSet.getShort("KEY_SEQ");
+                   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);
@@ -118,14 +141,24 @@ abstract class EntityImpl implements Entity {
                String comment = "";
                try {
                        Connection con = this.bookmark.getConnection();
-                       Statement stmt = con.createStatement();
                        DatabaseAdapter adapter = this.bookmark.getAdapter();
-                       if (adapter != null && stmt != null && adapter.getCommentsQuery(tableName, columnName) != null) {
-                       
-                               stmt.execute(adapter.getCommentsQuery(tableName, columnName));
-                               ResultSet set = stmt.getResultSet();
-                               if (set.next())
-                                       comment = set.getString(1);
+                       Statement stmt = con.createStatement();
+                       try {
+                               if (adapter != null && stmt != null 
+                                               && adapter.getCommentsQuery(tableName, columnName) != null) {
+                               
+                                       stmt.execute(adapter.getCommentsQuery(tableName, columnName));
+                                       ResultSet set = stmt.getResultSet();
+                                       try {
+                                               if (set.next()) {
+                                                       comment = set.getString(1);
+                                               }
+                                       } finally {
+                                               set.close();
+                                       }
+                               }
+                       } finally {
+                               stmt.close();
                        }
                } catch (NotConnectedException e) {
                } catch (SQLException e) {
@@ -133,6 +166,7 @@ abstract class EntityImpl implements Entity {
             
                return comment;
        }
+       
        public Index[] getIndexes() {
         
         List indexList = new ArrayList();
@@ -143,14 +177,14 @@ abstract class EntityImpl implements Entity {
             ResultSet resultSet = metaData.getIndexInfo(null, getSchema(), getName(), false, false);
             
             while (resultSet.next()) {
-                String indexName = resultSet.getString("INDEX_NAME");
+                String indexName = resultSet.getString(INDEX_METADATA_INDEX_NAME);
                 IndexImpl index = (IndexImpl) temp.get(indexName);
                 if (index == null) {
                     index = new IndexImpl(this, indexName);
                     temp.put(indexName, index);
                 }
-                String columnName = resultSet.getString("COLUMN_NAME");
-                String ascending = resultSet.getString("ASC_OR_DESC");
+                String columnName = resultSet.getString(INDEX_METADATA_COLUMN_NAME);
+                String ascending = resultSet.getString(INDEX_METADATA_ASC_OR_DESC);
                 index.addColumn(columnName, ascending == null 
                     ? null : (ascending.toUpperCase().startsWith("A") 
                         ? Boolean.TRUE : Boolean.FALSE));