added button to start tests.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / EntityImpl.java
1         package com.quantum.model;
2
3 import java.sql.Connection;
4 import java.sql.DatabaseMetaData;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import com.quantum.adapters.AdapterFactory;
15 import com.quantum.adapters.DatabaseAdapter;
16
17 /**
18  * This class models a table or view.
19  * 
20  * @author bcholmes
21  */
22 abstract class EntityImpl implements Entity {
23     private String schema;
24     private String name;
25     private String type;
26     private Bookmark bookmark;
27     private Boolean exists = Boolean.TRUE;
28     
29     public EntityImpl(Bookmark bookmark, String schema, String name, String type) {
30         this.schema = schema;
31         this.name = name;
32         this.type = type;
33         this.bookmark = bookmark;
34     }
35     public Bookmark getBookmark() {
36         return this.bookmark;
37     }
38     public String getName() {
39         return this.name;
40     }
41     public String getSchema() {
42         return this.schema;
43     }
44     public String getType() {
45         return this.type;
46     }
47     public String getCondQualifiedName() {
48         return (this.schema == null || this.schema.length() == 0) ?
49             this.name : this.schema + "." + this.name;
50     }
51     public Column getColumn(String columnName) {
52         Column column = null;
53         Column[] columns = getColumns();
54         for (int i = 0, length = (columns == null) ? 0 : columns.length;
55             column == null && i < length;
56             i++) {
57             if (columnName != null && columnName.equals(columns[i].getName())) {
58                 column = columns[i];
59             }
60         }
61         return column;
62     }
63     public Column[] getColumns() {
64         
65         Column[] columns = new Column[0];
66         try {
67             // TODO: Some DBs (like DB2) don't support metadata
68             Map temp = new HashMap();
69             Connection connection = this.bookmark.getConnection();
70             DatabaseMetaData metaData = connection.getMetaData();
71             ResultSet resultSet = metaData.getColumns(null, getSchema(), getName(), null);
72             
73             while (resultSet.next()) {
74                 ColumnImpl column = new ColumnImpl(
75                     this, 
76                     resultSet.getString("COLUMN_NAME"),
77                     resultSet.getString("TYPE_NAME"),
78                     resultSet.getInt("DATA_TYPE"),
79                     resultSet.getInt("COLUMN_SIZE"),
80                     resultSet.getInt("DECIMAL_DIGITS"),
81                     "YES".equalsIgnoreCase(resultSet.getString("IS_NULLABLE")),
82                     resultSet.getInt("ORDINAL_POSITION"),
83                                         getComments(resultSet.getString("REMARKS"),getCondQualifiedName(), resultSet.getString("COLUMN_NAME"))
84                     );
85                 temp.put(column.getName(), column);
86             }
87             resultSet.close();
88
89             resultSet = metaData.getPrimaryKeys(null, getSchema(), getName());
90             while (resultSet.next()) {
91                 String name = resultSet.getString("COLUMN_NAME");
92                 short keySequence = resultSet.getShort("KEY_SEQ");
93                 ColumnImpl column = (ColumnImpl) temp.get(name);
94                 if (column != null) {
95                     column.setPrimaryKeyOrder(keySequence);
96                 }
97             }
98             resultSet.close();
99             
100             List columnList = Collections.synchronizedList(
101                 new ArrayList(temp.values()));
102             Collections.sort(columnList);
103             columns = (Column[]) columnList.toArray(new Column[columnList.size()]);
104             
105         } catch (NotConnectedException e) {
106         } catch (SQLException e) {
107         }
108         return columns;
109     }
110     
111     /**
112      * Some JDBC drivers (Oracle for example) won't return the comments
113      * We recheck with a custom query, if it's defined
114          * @param iniComment The already got comment
115          * @param tableName The fully qualified table name
116          * @param columnName The column name
117          */
118         private String getComments( String iniComment, String tableName, String columnName) {
119                 if (iniComment != null && iniComment.length() > 0) 
120                         return iniComment;
121                 String comment = "";
122                 try {
123                         Connection con = this.bookmark.getConnection();
124                         Statement stmt = con.createStatement();
125                         DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(this.bookmark.getType());
126                         if (adapter != null && stmt != null && adapter.getCommentsQuery(tableName, columnName) != null) {
127                         
128                                 stmt.execute(adapter.getCommentsQuery(tableName, columnName));
129                                 ResultSet set = stmt.getResultSet();
130                                 if (set.next())
131                                         comment = set.getString(1);
132                         }
133                 } catch (NotConnectedException e) {
134                 } catch (SQLException e) {
135                 }
136             
137                 return comment;
138         }
139         public Index[] getIndexes() {
140         
141         List indexList = new ArrayList();
142         Map temp = new HashMap();
143         try {
144             Connection connection = this.bookmark.getConnection();
145             DatabaseMetaData metaData = connection.getMetaData();
146             ResultSet resultSet = metaData.getIndexInfo(null, getSchema(), getName(), false, false);
147             
148             while (resultSet.next()) {
149                 String indexName = resultSet.getString("INDEX_NAME");
150                 IndexImpl index = (IndexImpl) temp.get(indexName);
151                 if (index == null) {
152                     index = new IndexImpl(this, indexName);
153                     temp.put(indexName, index);
154                 }
155                 String columnName = resultSet.getString("COLUMN_NAME");
156                 String ascending = resultSet.getString("ASC_OR_DESC");
157                 index.addColumn(columnName, ascending == null 
158                     ? null : (ascending.toUpperCase().startsWith("A") 
159                         ? Boolean.TRUE : Boolean.FALSE));
160             }
161             resultSet.close();
162             indexList.addAll(temp.values());
163             
164         } catch (NotConnectedException e) {
165         } catch (SQLException e) {
166         }
167         return (Index[]) indexList.toArray(new Index[indexList.size()]);
168     }
169     
170     public Boolean exists() {
171         return this.exists;
172     }
173     
174     
175     /**
176      * @see com.quantum.model.Entity#getQuotedTableName()
177      */
178     public String getQuotedTableName() {
179         return getBookmark().getAdapter().filterTableName(getCondQualifiedName());
180     }
181
182 }