initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / EntityImpl.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/model/EntityImpl.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/model/EntityImpl.java
new file mode 100644 (file)
index 0000000..a960fbb
--- /dev/null
@@ -0,0 +1,182 @@
+       package com.quantum.model;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.quantum.adapters.AdapterFactory;
+import com.quantum.adapters.DatabaseAdapter;
+
+/**
+ * This class models a table or view.
+ * 
+ * @author bcholmes
+ */
+abstract class EntityImpl implements Entity {
+    private String schema;
+    private String name;
+    private String type;
+    private Bookmark bookmark;
+    private Boolean exists = Boolean.TRUE;
+    
+    public EntityImpl(Bookmark bookmark, String schema, String name, String type) {
+        this.schema = schema;
+        this.name = name;
+        this.type = type;
+        this.bookmark = bookmark;
+    }
+    public Bookmark getBookmark() {
+        return this.bookmark;
+    }
+    public String getName() {
+        return this.name;
+    }
+    public String getSchema() {
+        return this.schema;
+    }
+    public String getType() {
+        return this.type;
+    }
+    public String getCondQualifiedName() {
+        return (this.schema == null || this.schema.length() == 0) ?
+            this.name : this.schema + "." + this.name;
+    }
+    public Column getColumn(String columnName) {
+        Column column = null;
+        Column[] columns = getColumns();
+        for (int i = 0, length = (columns == null) ? 0 : columns.length;
+            column == null && i < length;
+            i++) {
+            if (columnName != null && columnName.equals(columns[i].getName())) {
+                column = columns[i];
+            }
+        }
+        return column;
+    }
+    public Column[] getColumns() {
+        
+        Column[] columns = new Column[0];
+        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();
+
+            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) {
+        }
+        return columns;
+    }
+    
+    /**
+     * 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
+        * @param tableName The fully qualified table name
+        * @param columnName The column name
+        */
+       private String getComments( String iniComment, String tableName, String columnName) {
+               if (iniComment != null && iniComment.length() > 0) 
+                       return iniComment;
+               String comment = "";
+               try {
+                       Connection con = this.bookmark.getConnection();
+                       Statement stmt = con.createStatement();
+                       DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(this.bookmark.getType());
+                       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);
+                       }
+               } catch (NotConnectedException e) {
+               } catch (SQLException e) {
+               }
+            
+               return comment;
+       }
+       public Index[] getIndexes() {
+        
+        List indexList = new ArrayList();
+        Map temp = new HashMap();
+        try {
+            Connection connection = this.bookmark.getConnection();
+            DatabaseMetaData metaData = connection.getMetaData();
+            ResultSet resultSet = metaData.getIndexInfo(null, getSchema(), getName(), false, false);
+            
+            while (resultSet.next()) {
+                String indexName = resultSet.getString("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");
+                index.addColumn(columnName, ascending == null 
+                    ? null : (ascending.toUpperCase().startsWith("A") 
+                        ? Boolean.TRUE : Boolean.FALSE));
+            }
+            resultSet.close();
+            indexList.addAll(temp.values());
+            
+        } catch (NotConnectedException e) {
+        } catch (SQLException e) {
+        }
+        return (Index[]) indexList.toArray(new Index[indexList.size()]);
+    }
+    
+    public Boolean exists() {
+        return this.exists;
+    }
+    
+    
+    /**
+     * @see com.quantum.model.Entity#getQuotedTableName()
+     */
+    public String getQuotedTableName() {
+        return getBookmark().getAdapter().filterTableName(getCondQualifiedName());
+    }
+
+}
\ No newline at end of file