KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > common > TableDetails


1 package com.daffodilwoods.daffodildb.server.sql99.common;
2
3 import java.util.*;
4 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
6 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
7 import com.daffodilwoods.daffodildb.server.sql99.expression.datetimevalueexpression.*;
8 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
9 import com.daffodilwoods.database.general.*;
10 import com.daffodilwoods.database.resource.*;
11 import com.daffodilwoods.database.utility.P;
12
13 public class TableDetails implements TypeConstants,java.io.Serializable JavaDoc {
14
15   private TableDetails[] underLyingFKeyTables; // Chained Columns Table Details used in case of conditional Columns for chained columns
16
private int tableType = TypeConstants.TABLE; // default is tableType of this table
17
private QualifiedIdentifier qualifiedIdentifier; // QualifiedIdentifier for this table
18
private ArrayList tableName; // Table Name
19
private String JavaDoc aliasName; // alias Name
20
private TreeMap columnNames; // it is used to show presence of column in table
21
private Object JavaDoc[] originalMapping; // Mapping of Column -- DataType -- Size
22
public _ColumnCharacteristics cc; // columnCharacteristics of this table
23
private String JavaDoc[] ccColumnNames; // ColumnCharacterisics Column Names
24
private long rowCount = -1; // Row Count of this table
25
private String JavaDoc appropriateTableName; // NAme of table with alias if present
26
private boolean hasRecord; // To Indicate that hasRecord column is present
27
private datetimevalueexpression datetimeValueExpression; // second argument of Dated FrameWork
28
private booleanvalueexpression bve; // Dated BooleanValueExpression
29
private _TableInfo tableInfo; // TableInfo of this object
30
private ArrayList commonColumns;//common columns in the case of natural joins
31
private boolean isFromSubQuery;
32
33   public TableDetails() throws DException {
34     tableName = new ArrayList();
35     commonColumns = new ArrayList();
36     rowCount = -1;
37   }
38
39   /**
40    * Initialises List with Array of table Name passed
41    * If it is previously initialised the reinitialised it.
42    * @param tableName
43    * @throws DException
44    */

45   public void setTableName(String JavaDoc tableName[]) throws DException {
46     if ( !this.tableName.isEmpty())
47       this.tableName.clear();
48     this.tableName.addAll(Arrays.asList(tableName));
49   }
50
51   /**
52    * return the column names of the columns of the table excluding the common columns
53    * @return
54    * @throws DException
55    */

56   public String JavaDoc[] getColumnsExcludingCommonColumns() throws DException {
57     if ( cc.getTableType() == TypeConstants.VIEW )
58       return getColumnsExcludingCommonColumnsForView();
59     if(ccColumnNames == null)
60       ccColumnNames = cc.getColumnNames();
61     int length = ccColumnNames.length;
62     int commonLength=commonColumns.size();
63     String JavaDoc[] allColumns = new String JavaDoc[length - commonLength-5];
64     for(int i = 5,j = 0; i < length; i++){
65       if(commonColumns.contains(ccColumnNames[i]))
66         continue;
67       allColumns[j++] = ccColumnNames[i];
68     }
69     return allColumns;
70   }
71
72  private String JavaDoc[] getColumnsExcludingCommonColumnsForView() throws DException {
73    if(ccColumnNames == null)
74      ccColumnNames = cc.getColumnNames();
75    int length = ccColumnNames.length;
76    int lenghtCommon = commonColumns.size();
77    String JavaDoc[] allColumns = new String JavaDoc[length-lenghtCommon];
78    for(int i = 0, j = 0 ; i < length; i++){
79      if( commonColumns.contains(ccColumnNames[i]))
80        continue;
81      allColumns[j++] = ccColumnNames[i];
82    }
83    return allColumns;
84
85  }
86
87
88   public void addCommonColumn(String JavaDoc commonColumnName) throws DException {
89     if(!commonColumns.contains(commonColumnName))
90       commonColumns.add(commonColumnName);
91   }
92
93
94   /**
95    * Returns Table Name
96    * @return
97    * @throws DException
98    */

99   public String JavaDoc[] getTableName() throws DException{
100     if ( tableName.size() == 0 )
101       throw new DException("DSE1137",null);
102     return (String JavaDoc [])tableName.toArray(new String JavaDoc[tableName.size()]);
103   }
104
105   /**
106    * Returns Qualified Dot Appended table Name
107    * @return
108    * @throws DException
109    */

110   public String JavaDoc getQualifiedTableName() throws DException {
111     String JavaDoc []nameOfTable = getAppropriateTableName();
112     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
113     int index;
114     for(index = 0; index < nameOfTable.length - 1 ; ++index){
115       buffer.append(nameOfTable[index]);
116       buffer.append(".");
117     }
118     buffer.append(nameOfTable[index]);
119     return buffer.toString();
120   }
121
122   /**
123    * Returns Actual Table Name, if alias Name is specified then alias Name otherwise
124    * only tableName is returned
125    * @return
126    * @throws DException
127    */

128   public String JavaDoc getActualName() throws DException{ // return only tableName(may be alias)
129
if ( tableName.size() == 0 )
130       throw new DException("DSE970",null);
131     if(aliasName != null)
132       return aliasName;
133     return (String JavaDoc)tableName.get(tableName.size() - 1);
134   }
135
136    public String JavaDoc getName() throws DException{ // return only tableName
137
if ( tableName.size() == 0 )
138           throw new DException("DSE970",null);
139        return (String JavaDoc)tableName.get(tableName.size() - 1);
140    }
141
142
143
144    public String JavaDoc[] getAppropriateTableName()throws DException{ // return array of tablename acc. to alias
145
String JavaDoc[] names = getTableName();
146       if(aliasName != null)
147          names[names.length - 1] = aliasName;
148       return names;
149    }
150
151    public void setAliasName(String JavaDoc aliasName) throws DException {
152       this.aliasName = aliasName;
153    }
154
155    public String JavaDoc getAliasName() throws DException {
156       return aliasName;
157    }
158
159    public String JavaDoc getCatalogName() throws DException {
160       return (String JavaDoc)tableName.get(0);
161    }
162
163    public String JavaDoc getSchemaName() throws DException {
164       return (String JavaDoc)tableName.get(1);
165    }
166
167    public ColumnDetails[] getColumnPropertiesExcludingCommonColumns() throws DException {
168      if ( cc.getTableType() == TypeConstants.VIEW )
169        return getDetailsOfAllColumnsInView();
170      if(ccColumnNames == null)
171        ccColumnNames = cc.getColumnNames();
172      int length = ccColumnNames.length;
173      int commonLength=commonColumns.size();
174      ColumnDetails[] allColumns = new ColumnDetails[length - commonLength-5];
175      for(int i = 5,j = 0; i < length; i++){
176        if( commonColumns.contains(ccColumnNames[i]))
177          continue;
178        allColumns[j++] = getDetailsOfColumn(ccColumnNames[i],cc);
179      }
180      return allColumns;
181    }
182
183    public int getDataType(String JavaDoc columnName) throws DException {
184     try {
185       return cc.getColumnType(cc.getColumnIndex(columnName));
186     }
187     catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
188       throw ex;
189     }
190    }
191
192    public int getSize(String JavaDoc columnName) throws DException {
193      return cc.getSize(cc.getColumnIndex(columnName));
194    }
195
196    private ColumnDetails getDetailsOfColumn(String JavaDoc columnName,_ColumnCharacteristics cc) throws DException {
197      ColumnDetails columnDetail = new ColumnDetails();
198      String JavaDoc []tableName = getTableName();
199      String JavaDoc []column = new String JavaDoc[4];
200      System.arraycopy(tableName,0,column,0,tableName.length);
201      column[3] = columnName;
202      columnDetail.setColumnName(column);
203      columnDetail.setTableDetails(this);
204      columnDetail.setTableForDisplay(this);
205      int index = cc.getColumnIndex(columnName);
206      columnDetail.setDatatype(cc.getColumnType(index));
207      columnDetail.setSize(cc.getSize(index));
208      columnDetail.setType(REFERENCE);
209      columnDetail.setExpression(column[2]+"."+column[3]);
210      return columnDetail;
211    }
212
213    private ColumnDetails[] getDetailsOfAllColumnsInView() throws DException{
214      if(ccColumnNames == null){
215        ccColumnNames = cc.getColumnNames();
216      }
217      int length = ccColumnNames.length;
218      int lenghtCommon = commonColumns.size();
219      ColumnDetails[] allColumns = new ColumnDetails[length-lenghtCommon];
220      for(int i = 0, j = 0 ; i < length; i++){
221        if( commonColumns.contains(ccColumnNames[i]))
222          continue;
223        allColumns[j++] = getDetailsOfColumn(ccColumnNames[i],cc);
224      }
225      return allColumns;
226    }
227
228    public int getColumnCount()throws DException{
229      return cc.getColumnCount();
230    }
231
232
233    private void getColumns()throws DException{
234       if(columnNames != null)
235          return;
236       if(ccColumnNames == null)
237          ccColumnNames = cc.getColumnNames();
238       columnNames = new TreeMap(getComparator());
239       int length = ccColumnNames.length;
240       int lenghtCommon = commonColumns.size();
241       originalMapping = new Object JavaDoc[length - lenghtCommon];
242       for(int index=0 , j = 0; index < length; ++index ){
243         if (commonColumns.contains(ccColumnNames[index]))
244           continue;
245         String JavaDoc[] nameOfColumn = getAppropriateArray(ccColumnNames[index]);
246         Integer JavaDoc in = new Integer JavaDoc(j);
247         columnNames.put(nameOfColumn, in); //to put only the unique columns
248
int ii = cc.getColumnIndex(ccColumnNames[index]);
249         originalMapping[j++] = new Object JavaDoc[] {
250             new Integer JavaDoc(cc.getColumnType(ii)), new Integer JavaDoc(cc.getSize(ii))};
251       }
252    }
253
254     private Comparator getComparator() throws DException {
255         return new TempComparator();
256     }
257
258     private class TempComparator implements Comparator,java.io.Serializable JavaDoc{
259       TempComparator(){}
260           public int compare(Object JavaDoc object1,Object JavaDoc object2) {
261             if(object1 == null)
262                if(object2 == null)
263                   return 0;
264                else
265                   return -1;
266             else
267                if(object2 == null)
268                   return 1;
269                else{
270                   Object JavaDoc[] result1 = (Object JavaDoc[])object1;
271                   Object JavaDoc[] result2 = (Object JavaDoc[])object2;
272                   int compare = 0;
273                   for(int i = 0 ; i < result1.length && compare == 0 ; i++)
274                             compare = String.CASE_INSENSITIVE_ORDER.compare(result1[i],result2[i]);
275                   return compare;
276                }
277          }
278     }
279
280     private String JavaDoc[] getAppropriateArray(String JavaDoc column)throws DException{
281       String JavaDoc []nameOfTable = getTableName();
282       String JavaDoc[] result = new String JavaDoc[nameOfTable.length + 1];
283       System.arraycopy(nameOfTable,0,result,0,nameOfTable.length);
284       result[result.length - 1] = column;
285       return result;
286     }
287
288    private String JavaDoc append()throws DException{
289       String JavaDoc []nameOfTable = getAppropriateTableName();
290       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
291       int index;
292       for(index = 0; index < nameOfTable.length - 1 ; ++index){
293         buffer.append(nameOfTable[index]);
294         buffer.append(".");
295       }
296       buffer.append(nameOfTable[index]);
297       return buffer.toString();
298    }
299
300
301    public String JavaDoc getUnderscoreTableName() throws DException{
302       String JavaDoc []nameOfTable = getAppropriateTableName();
303       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
304       int index;
305       for(index = 0; index < nameOfTable.length - 1 ; ++index){
306         buffer.append(nameOfTable[index]);
307         buffer.append("_");
308       }
309       buffer.append(nameOfTable[index]);
310       return buffer.toString();
311    }
312    public int getDatatype(int key) throws DException {
313       return ((Object JavaDoc[])originalMapping[key])[0].hashCode();
314    }
315
316    public int getSize(int key) throws DException {
317       return ((Object JavaDoc[])originalMapping[key])[1].hashCode();
318    }
319
320    public String JavaDoc getNameOfTable()throws DException{
321       if(appropriateTableName == null)
322            appropriateTableName = append();
323         return appropriateTableName;
324    }
325
326
327    public int checkForColumn(String JavaDoc[] columnName)throws DException{
328       getColumns();
329       Object JavaDoc obj = columnNames.get(columnName) ;
330       return obj == null ? -1 : obj.hashCode();
331    }
332
333
334    public String JavaDoc getAppropriateOnlyTableName(){
335       return aliasName == null ? (String JavaDoc)tableName.get(tableName.size() - 1) : aliasName;
336    }
337
338
339    public void setRowCount(long rowCount) throws DException {
340      this.rowCount = rowCount;
341    }
342
343    public QualifiedIdentifier getQualifiedIdentifier() throws DException {
344      if ( qualifiedIdentifier == null ){
345        String JavaDoc tableName[] = getTableName();
346            if(tableName.length > 1)
347        qualifiedIdentifier = new QualifiedIdentifier(tableName[0],tableName[1],tableName[2]);
348            else
349                 qualifiedIdentifier = new QualifiedIdentifier("users","users",tableName[0]);
350      }
351      return qualifiedIdentifier;
352    }
353
354    public long getRowCount() throws DException {
355      return rowCount;
356    }
357
358    public _ColumnCharacteristics getColumnCharacteristics() throws DException{
359       return cc;
360    }
361
362    public void setHasRecord(boolean hasRecord0) throws DException{
363       hasRecord = hasRecord0;
364    }
365
366    public boolean getHasRecord() throws DException{
367       return hasRecord;
368    }
369
370    public void setTableType(int tableType0){
371       tableType = tableType0;
372    }
373
374    public int getTableType() {
375       return tableType;
376    }
377
378    public void setUnderLyingFKeyTables(TableDetails[] tables0) throws DException {
379      underLyingFKeyTables = tables0;
380    }
381
382    public TableDetails[] getUnderLyingFKeyTables() throws DException {
383      return underLyingFKeyTables;
384    }
385
386    public boolean isDated() {
387        return (datetimeValueExpression != null || bve != null);
388    }
389     public java.sql.Date JavaDoc getDate(_VariableValues variableValue ) throws DException{
390         return (java.sql.Date JavaDoc)datetimeValueExpression.run(variableValue);
391     }
392     public void setDatetimeValueExpression(datetimevalueexpression datetimeValueExpression0) {
393         datetimeValueExpression = datetimeValueExpression0;
394     }
395
396     public void setDatedBooleanValueExpression(booleanvalueexpression bve) {
397         this.bve = bve;
398     }
399
400     public booleanvalueexpression getDatedBooleanValueExpression() {
401         return bve;
402     }
403
404    public _TableInfo getTableInfo() {
405       return tableInfo;
406    }
407
408    public void setTableInfo(_TableInfo t) {
409       tableInfo = t;
410    }
411
412    public String JavaDoc toString() {
413      try {
414        String JavaDoc []nameOfTable = getAppropriateTableName();
415        StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
416        int index;
417        for(index = 0; index < nameOfTable.length - 1 ; ++index){
418          buffer.append(nameOfTable[index]);
419          buffer.append(".");
420        }
421        buffer.append(nameOfTable[index]);
422        return buffer.toString();
423      }catch(DException ex){
424        throw new DRuntimeException(ex.getDseCode(),ex.getParameters());
425      }
426    }
427
428    public boolean isFromSubQuery() throws DException {
429       return isFromSubQuery;
430    }
431
432    public void setAsFromSubQuery() throws DException {
433       isFromSubQuery = true;
434    }
435 }
436
Popular Tags