KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > resultsetmetadata > SelectColumnCharacteristics


1 package com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata;
2
3 import java.text.*;
4 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
7 import com.daffodilwoods.database.general.*;
8 import com.daffodilwoods.database.resource.*;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression;
10 import com.daffodilwoods.database.utility.P;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryspecification.selectsublist;
12
13 /**
14  *
15  * <p>Title: SelectColumnCharacteristics</p>
16  * <p>Description:
17  * This class represents the characteristics of selected columns of Select query.
18  * It also provides the functionality for supporting modifications through
19  * resultset.
20  * </p>
21  * <p>Copyright: Copyright (c) 2003</p>
22  * <p>Company: Daffodil S/W Ltd.</p>
23  * @author SelectTeam
24  * @version 1.0
25  */

26
27 public class SelectColumnCharacteristics implements _ColumnCharacteristics, _SelectColumnCharacteristics, TypeConstants {
28
29    /**
30     * Represents the mapping of table and its column characteristics.
31     */

32
33    private Object JavaDoc[][] tableDetailsCCMapping;
34
35    /**
36     * Represents the selected columns.
37     */

38
39    private ColumnDetails[] columnDetails;
40
41    /**
42     * Represents the columns of group by which are also primary key.
43     */

44
45    private String JavaDoc[] groupByPrimaryColumns;
46
47    /**
48     * Represents the reader for rows returned by resultset.
49     */

50
51    private _RowReader rowReader;
52
53    /**
54     * boolean flag represents whether distinct or group by is present in select
55     * query.
56     */

57
58    private boolean isDistinctOrGroup;
59
60    /**
61     * Represents the indexes of columns of primary condition.
62     */

63
64    private int[] indexes;
65
66    /**
67     * boolean flag represents that whether rows of resultset contains values of
68     * only selected columns or it contains the values of all columns of all
69     * tables.
70     */

71
72    private boolean allOrSelected;
73
74    /**
75     * Represents the chained columns.
76     */

77
78    private ForeignKeyColumns[] foreignKeyColumns = null;
79
80    /**
81     * This constructor is initiated in case of SCROLLABLE/NON SCROLLABLE type
82     * @param columnDetails1
83     * @param tableDetailsCCMapping1
84     * @param allOrSelected1
85     */

86    public SelectColumnCharacteristics(ColumnDetails[] columnDetails1, Object JavaDoc[][] tableDetailsCCMapping1, boolean allOrSelected1) {
87       tableDetailsCCMapping = tableDetailsCCMapping1;
88       columnDetails = columnDetails1;
89       allOrSelected = allOrSelected1;
90       try {
91          for (int i = 0; i < columnDetails.length; i++) {
92             int type = columnDetails[i].getType();
93             if ( (columnDetails[i].getAliasName() == null) &&
94                 (type == SCALARFUNCTION || type == CASEEXPRESSION ||
95                  type == FUNCTIONAL || type == GROUPING || type == USERFUNCTION)) {
96                /* Done by Kaushik on 22/07/2004 to solve some Failed Test Cases */
97                columnDetails[i].setOriginalColumnName("Expr" + i);
98             }
99          }
100       } catch (DException de) {
101       }
102
103    }
104
105    /**
106     * This constructor is initiated in case of UPDATABLE type
107     * @param columnDetails1
108     * @param tableDetailsCCMapping1
109     * @param groupByPrimaryColumns1
110     * @param isDistinctOrGroup1
111     * @param allOrSelected1
112     */

113    public SelectColumnCharacteristics(ColumnDetails[] columnDetails1, Object JavaDoc[][] tableDetailsCCMapping1, String JavaDoc[] groupByPrimaryColumns1, boolean isDistinctOrGroup1, boolean allOrSelected1) {
114       tableDetailsCCMapping = tableDetailsCCMapping1;
115       columnDetails = columnDetails1;
116       groupByPrimaryColumns = groupByPrimaryColumns1;
117       allOrSelected = allOrSelected1;
118       isDistinctOrGroup = isDistinctOrGroup1;
119       try {
120          for (int i = 0; i < columnDetails.length; i++) {
121             int type = columnDetails[i].getType();
122             if ( (columnDetails[i].getAliasName() == null) && (type == SCALARFUNCTION || type == CASEEXPRESSION || type == FUNCTIONAL || type == GROUPING || type == USERFUNCTION)) {
123                columnDetails[i].setAliasName("Expr" + i);
124             }
125          }
126          getPrimaryConditionColumns();
127       } catch (DException de) {
128       }
129    }
130
131    /**
132     * This method returns the DataType of the column represented by passed index.
133     * The index is one based, means index of first SelectedColumn is one.
134     * @param index
135     * @return
136     * @throws DException
137     */

138
139    public int getColumnType(int index) throws DException {
140       return columnDetails[index - 1].getDatatype();
141    }
142
143    /**
144     * This method returns the Index of the column represented by passed columnName.
145     * @param columnName
146     * @return
147     * @throws DException
148     */

149
150    public int getColumnIndex(String JavaDoc columnName) throws DException {
151       int length = columnDetails.length;
152       for (int i = 0; i < length; i++) {
153          if (columnDetails[i].getAppropriateColumn().equalsIgnoreCase(columnName)) {
154             return i + 1;
155          }
156       }
157       throw new DException("DSE3517", new Object JavaDoc[] {columnName});
158    }
159
160    /**
161     * Returns the qualified name of column represented by passed index.
162     * @param index
163     * @return
164     * @throws DException
165     */

166
167    private String JavaDoc getQualifiedColumnName(int index) throws DException {
168       return columnDetails[index - 1].getQualifiedColumnName();
169    }
170
171    /**
172     * This method returns the Name of the column represented by passed index. It
173     * also takes care of alias name.
174     * @param index
175     * @return
176     * @throws DException
177     */

178
179    public String JavaDoc getColumnName(int index) throws DException {
180       String JavaDoc aliasName = columnDetails[index - 1].getAliasName();
181       return (aliasName == null || aliasName.length() == 0) ? columnDetails[index - 1].getOriginalColumn() : aliasName;
182    }
183
184    /**
185     * Returns the characteristics of passed table by using mapping of table and
186     * characteristics.
187     * @param tableDetails
188     * @return
189     * @throws DException
190     */

191
192    public _ColumnCharacteristics getColumnCharacteristics(TableDetails tableDetails) throws DException {
193       if (tableDetails == null) {
194          return null;
195       }
196       int length = tableDetailsCCMapping.length;
197       for (int i = 0; i < length; i++) {
198          if (tableDetails == (TableDetails) tableDetailsCCMapping[i][0]) {
199             return (_ColumnCharacteristics) tableDetailsCCMapping[i][1];
200          }
201       }
202       throw new DException("DSE3516", new Object JavaDoc[] {tableDetails.getNameOfTable()});
203    }
204
205    /**
206     * This method is used to get related table of column represented by passed
207     * index.
208     * @param index
209     * @return
210     * @throws DatabaseException
211     * @throws DException
212     */

213
214    public String JavaDoc getRelatedTable(int index) throws DatabaseException, DException {
215       TableDetails tableDetails = columnDetails[index - 1].getTable();
216       if (tableDetails == null || columnDetails[index - 1].getType() == ColumnDetails.HAS_RECORD) {
217          return null;
218       }
219       _ColumnCharacteristics cc = columnDetails[index - 1].getColumnCharacteristics();
220       return cc.getRelatedTable(getIndex(cc, columnDetails[index - 1]));
221    }
222
223    /**
224     * This method returns the Number of Selected Columns taking part in this
225     * ColumnCharacteristics.
226     * @return
227     * @throws DException
228     */

229
230    public int getColumnCount() throws DException {
231       return columnDetails.length;
232    }
233
234    /**
235     * This method returns the name of all selected columns.
236     * @return
237     * @throws DException
238     */

239
240    public String JavaDoc[] getColumnNames() throws DException {
241       int length = columnDetails.length;
242       String JavaDoc[] names = new String JavaDoc[length];
243       for (int i = 0; i < length; i++) {
244          String JavaDoc aliasName = columnDetails[i].getAliasName();
245          names[i] = (aliasName == null || aliasName.length() == 0) ? columnDetails[i].getOriginalColumn() : aliasName;
246          names[i] = names[i] == null ? names[i] : columnDetails[i].getAppropriateColumn();
247       }
248       return names;
249    }
250
251    /**
252     * This method returns the size of the column represented by passed index.
253     * @param index
254     * @return
255     * @throws DException
256     */

257
258    public int getSize(int index) throws DException {
259       return columnDetails[index - 1].getSize();
260    }
261
262    /**
263     * This method is used to get relation of column represented by passed
264     * index.
265     * @param index
266     * @return
267     * @throws DatabaseException
268     * @throws DException
269     */

270
271    public String JavaDoc getRelation(int index) throws DatabaseException, DException {
272       ColumnDetails cd = columnDetails[index - 1];
273       TableDetails tableDetails = cd.getTable();
274       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
275          return null;
276       }
277       _ColumnCharacteristics cc = cd.getColumnCharacteristics();
278       return cc.getRelation(getIndex(cc, cd));
279    }
280
281    /**
282     * This method returns the Indexes of the columns represented by passed columnNames.
283     * @param parm1
284     * @return
285     * @throws DException
286     */

287
288    public int[] getColumnIndexes(String JavaDoc[] parm1) throws DException {
289       int length = parm1.length;
290       int[] result = new int[length];
291       for (int i = 0; i < length; i++) {
292          result[i] = getColumnIndex(parm1[i]);
293       }
294       return result;
295    }
296
297    /**
298     * This method is used to obtain the indexes of columns involved in primary
299     * condition. It is allowed only when resultset type is UPDATABLE.
300     * @return
301     * @throws DException
302     */

303
304    public int[] getPrimaryConditionColumns() throws DException {
305       if (!allOrSelected) {
306          throw new DException("DSE3513", null);
307       }
308       if (indexes == null) {
309          int length = tableDetailsCCMapping.length;
310          for (int i = 0; i < length; i++) {
311             _ColumnCharacteristics columnCharacteristics = (_ColumnCharacteristics) tableDetailsCCMapping[i][1];
312             int[] indexesOfTable = columnCharacteristics.getPrimaryConditionColumns();
313             int startingIndex = -1;
314             startingIndex = tableDetailsCCMapping[i][2].hashCode();
315             indexes = indexes == null ? addStartingIndex(indexesOfTable, startingIndex) :
316                 constructNewArray(indexes, addStartingIndex(indexesOfTable, startingIndex));
317          }
318       }
319       return indexes;
320    }
321
322    /**
323     * This method is used to merge passed two arrays into one array.
324     * @param OldIndexes
325     * @param newTableIndexes
326     * @return
327     * @throws DException
328     */

329
330    private int[] constructNewArray(int[] OldIndexes, int[] newTableIndexes) throws DException {
331       int lengthOfOld = OldIndexes.length, lengthOfNew = newTableIndexes.length;
332       int[] newIndexes = new int[lengthOfOld + lengthOfNew];
333       System.arraycopy(OldIndexes, 0, newIndexes, 0, lengthOfOld);
334       System.arraycopy(newTableIndexes, 0, newIndexes, lengthOfOld, lengthOfNew);
335       return newIndexes;
336    }
337
338    /**
339     * This method is used to add passed starting index to all the indexes
340     * respresented by passed array.
341     * @param indexes
342     * @param startingIndex
343     * @return
344     * @throws DException
345     */

346
347    private int[] addStartingIndex(int[] indexes, int startingIndex) throws DException {
348       int length = indexes.length;
349       int[] indexesTemp = new int[length];
350       for (int i = 0; i < length; i++) {
351          indexesTemp[i] = indexes[i] + startingIndex;
352       }
353       return indexesTemp;
354    }
355
356    /**
357     * This method returns the table name of the column represented by passed
358     * index. If the column on the passed index is a functional column then in
359     * that case null is returned.
360     * @param index
361     * @return
362     * @throws DatabaseException
363     * @throws DException
364     */

365
366    public String JavaDoc getTableName(int index) throws DatabaseException, DException {
367       ColumnDetails cd = columnDetails[index - 1];
368       TableDetails tableDetails = cd.getTableForDisplay();
369       return tableDetails == null ? null : tableDetails.getName();
370    }
371
372    /**
373     * Returns the type of ColumnCharacteristics. It can be any one of two -
374     * 1. TABLE - when characteristics is of table.
375     * 2. VIEW - when characteristics of select query.
376     * @return
377     * @throws DException
378     */

379
380    public short getTableType() throws DException {
381       return VIEW;
382    }
383
384    /**
385     * This method returns the precision of the column represented by passed
386     * index. If the column on the passed index is a functional column then in
387     * that case -1 is returned.
388     * @param index
389     * @return
390     * @throws DException
391     */

392
393    public int getPrecision(int index) throws DException {
394       ColumnDetails cd = columnDetails[index - 1];
395       TableDetails tableDetails = cd.getTable();
396       if (cd.getType() == TypeConstants.GROUPING) {
397          if (cd.toString().trim().toLowerCase().startsWith("max") || cd.toString().trim().toLowerCase().startsWith("min")) {
398             int l = ( (valueexpression) cd.getObject()).getColumnDetails()[0].getSize();
399             return l;
400          }
401       }
402       if (cd.getType() == TypeConstants.SCALARFUNCTION) {
403          int l = ( (selectsublist) cd.getObject()).getColumnDetails()[0].getSize();
404          return l;
405       }
406
407       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
408     return GeneralPurposeStaticClass.isCharacterType(cd.getType()) ?cd.getSize(): GeneralPurposeStaticClass.getPrecision(cd.getDatatype());
409
410       }
411       _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics();
412       return columnCharacteristics.getPrecision(getIndex(columnCharacteristics, cd));
413    }
414
415    /**
416     * This method returns the scale of the column represented by passed
417     * index. If the column on the passed index is a functional column then in
418     * that case -1 is returned.
419     * @param index
420     * @return
421     * @throws DException
422     */

423
424    public int getScale(int index) throws DException {
425       ColumnDetails cd = columnDetails[index - 1];
426       TableDetails tableDetails = cd.getTable();
427       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
428         return 0; //'done by Sandeep as suggested by Parveen Sir for handle the problem in compiere
429
}
430       _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics();
431       return columnCharacteristics.getScale(getIndex(columnCharacteristics, cd));
432    }
433
434    /**
435     * This method returns the schema name of the column represented by passed
436     * index. If the column on the passed index is a functional column then in
437     * that case null is returned.
438     * @param index
439     * @return
440     * @throws DException
441     */

442
443    public String JavaDoc getSchemaName(int index) throws DException {
444       ColumnDetails cd = columnDetails[index - 1];
445       TableDetails tableDetails = cd.getTable();
446       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
447          return null;
448       }
449       _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics();
450       return columnCharacteristics.getSchemaName(getIndex(columnCharacteristics, cd));
451    }
452
453    /**
454     * This method returns the catalog name of the column represented by passed
455     * index. If the column on the passed index is a functional column then in
456     * that case null is returned.
457     * @param index
458     * @return
459     * @throws DException
460     */

461
462    public String JavaDoc getCatalogName(int index) throws DException {
463       ColumnDetails cd = columnDetails[index - 1];
464       TableDetails tableDetails = cd.getTable();
465       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
466          return null;
467       }
468       _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics();
469       return columnCharacteristics.getCatalogName(getIndex(columnCharacteristics, cd));
470    }
471
472    /**
473     * This method is used to obtain the nullability of column represented by
474     * passed index.
475     * @param index
476     * @return
477     * @throws DException
478     */

479
480    public int isNullable(int index) throws DException {
481       ColumnDetails cd = columnDetails[index - 1];
482       TableDetails tableDetails = cd.getTable();
483       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
484          return -1;
485       }
486       _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics();
487       return columnCharacteristics.isNullable(getIndex(columnCharacteristics, cd));
488    }
489
490    /**
491     * This method is used to check whether the column represented by passed
492     * index is auto increment or not.
493     * @param index
494     * @return
495     * @throws DException
496     */

497
498    public boolean isAutoIncrement(int index) throws DException {
499       ColumnDetails cd = columnDetails[index - 1];
500       TableDetails tableDetails = cd.getTable();
501       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
502          return false;
503       }
504       _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics();
505       return columnCharacteristics.isAutoIncrement(getIndex(columnCharacteristics, cd));
506    }
507
508    /**
509     * This method returns the label of the column represented by passed
510     * index. The label name is appropriate name of the column, means if alias
511     * is specified then alias is appropriate name otherwise actual columnName is
512     * appropriate name.
513     * @param index
514     * @return
515     * @throws DException
516     */

517
518    public String JavaDoc getColumnLabel(int index) throws DException {
519       return columnDetails[index - 1].getAppropriateColumn();
520    }
521
522    /**
523     * This method returns the Qualified table name of the column represented by
524     * passed index. If the column on the passed index is a functional column then in
525     * that case null is returned.
526     * @param index
527     * @return
528     * @throws DException
529     */

530
531    public String JavaDoc getQualifiedTableName(int index) throws DException {
532       ColumnDetails cd = columnDetails[index - 1];
533       TableDetails tableDetails = cd.getTable();
534       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
535          return null;
536       }
537       return cd.getQualifiedChainedTableName();
538    }
539
540    /**
541     * This method is needed while updation on a column are made through resultSet.
542     * This method tells whether the column represented by passed index can be updated
543     * or not. The criteria in which a column is updatable are -
544     * 1. Columns must be of Reference type.
545     * 2. if group by is specified then the column must be primary key.
546     * @param columnIndex
547     * @return
548     * @throws DException
549     */

550
551    public boolean isColumnUpdatable(int columnIndex) throws DException {
552       int type = columnDetails[columnIndex - 1].getType();
553       if (type == SCALARFUNCTION || type == FUNCTIONAL || type == DERIVEDCOLUMN || type == CONSTANT || type == CASEEXPRESSION || type == USERFUNCTION) {
554          return false;
555       }
556       if (columnDetails[columnIndex - 1].getTableAliasArray() != null) {
557          return true;
558       }
559       String JavaDoc columnName = getQualifiedColumnName(columnIndex);
560       if (exists(columnName, type)) {
561          return true;
562       }
563       return false;
564    }
565
566    /**
567     * This method tells whether the resultSet can be used for modification. It
568     * can be used for modification, if any one of selected columns is updatable.
569     * @return
570     * @throws DException
571     */

572
573    public boolean isUpdatable() throws DException {
574       int length = columnDetails.length;
575       for (int i = 0; i < length; i++) {
576          int type = columnDetails[i].getType();
577          if (type == TypeConstants.REFERENCE || type == TypeConstants.HAS_RECORD) {
578             String JavaDoc columnName = columnDetails[i].getStringOfColumn();
579             if (exists(columnName, type)) {
580                return true;
581             }
582          }
583       }
584       return false;
585    }
586
587    /**
588     * This method is used to check whether any updation on passed column can be
589     * performed or not. It is true when
590     * 1. If groupby with primary columns is present, then it should belong to
591     * those columns.
592     * 2. If groupby with primary columns, distinct and group by is not
593     * present, then it should be of Reference, constant, hasRecord type.
594     * @param columnName
595     * @param type
596     * @return
597     * @throws DException
598     */

599
600    private boolean exists(String JavaDoc columnName, int type) throws DException {
601       if (groupByPrimaryColumns == null) {
602          if (isDistinctOrGroup) {
603             return false;
604          }
605          return type == REFERENCE || type == HAS_RECORD || type == CONSTANT; // true;
606
}
607       int length = groupByPrimaryColumns.length;
608       for (int i = 0; i < length; i++) {
609          if (groupByPrimaryColumns[i].equalsIgnoreCase(columnName)) {
610             return true;
611          }
612       }
613       return false;
614    }
615
616    /**
617     * Sets the row reader for the rows returned by resultset.
618     * @param rowReader0
619     * @throws DException
620     */

621
622    public void setRowReader(_RowReader rowReader0) throws DException {
623       rowReader = rowReader0;
624    }
625
626    /**
627     * Returns the expression associated with the column represented by passed
628     * index.
629     * @param index
630     * @return
631     * @throws com.daffodilwoods.database.resource.DException
632     */

633
634    public String JavaDoc getExpression(int index) throws com.daffodilwoods.database.resource.DException {
635       return columnDetails[index - 1].getExpression();
636    }
637
638    /**
639     * Sets the mapping for Foreign key columns.
640     * @param foreignKeyCol
641     * @throws com.daffodilwoods.database.resource.DException
642     */

643
644    public void setForeignKeyMapping(ForeignKeyColumns[] foreignKeyCol) throws com.daffodilwoods.database.resource.DException {
645       this.foreignKeyColumns = foreignKeyCol;
646    }
647
648    /**
649     * This method is used to find the related columns of column represented by
650     * passed index.
651     * @param index
652     * @return
653     * @throws DException
654     */

655
656    public String JavaDoc[] getRelatedColumns(int index) throws DException {
657       ColumnDetails cd = columnDetails[index - 1];
658       TableDetails tableDetails = cd.getTable();
659       if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) {
660          return null;
661       }
662       _ColumnCharacteristics cc = cd.getColumnCharacteristics();
663       return cc.getRelatedColumns(getIndex(cc, cd));
664    }
665
666    /**
667     * Returns the collator for columncharacteristics.
668     * @return
669     * @throws DException
670     */

671
672    public Collator getCollator() throws DException {
673       return null;
674    }
675
676    /**
677     * Returns the index of passed column through the help of passed
678     * characteristics.
679     * @param columnCharacteristics
680     * @param column
681     * @return
682     * @throws DException
683     */

684
685    private int getIndex(_ColumnCharacteristics columnCharacteristics, ColumnDetails column) throws DException {
686       int index = -1;
687       try {
688          index = column.getIndex();
689       } catch (DException ex) {
690          index = columnCharacteristics.getColumnIndex(column.getColumn());
691          column.setIndex(index);
692       }
693       return index;
694    }
695
696    /**
697     * Method returns actual table for the column passed.
698     * @usage - DDL used it while creation of view for select query which
699     * involves 'From Subquery'.
700     * @param column belongs to from subquery.
701     * @return actual column which is used in definition of 'from subquery' and
702     * its table.
703     * @throws DException
704     */

705
706    public Object JavaDoc[] getCorresspondingColumnsForFromSubQueryColumn(Object JavaDoc column) throws DException {
707       for (int i = 0, length = columnDetails.length; i < length; i++) {
708          if (columnDetails[i].getAppropriateColumn().equalsIgnoreCase( ( (ColumnDetails) column).getColumn())) {
709             return columnDetails[i].getCorresspondingColumnsInvolved();
710          }
711       }
712       throw new DException("DSE3517", new Object JavaDoc[] { ( (ColumnDetails) column).getTable().getQualifiedTableName() + "." + ( (ColumnDetails) column).getAppropriateColumn()});
713    }
714
715    /**
716     * This method is used to obtain the characteristics for the actual table
717     * of foreign columns. It includes all the columns of that table which are
718     * referred in select query.
719     * @param parm1
720     * @return
721     * @throws com.daffodilwoods.database.resource.DException
722     */

723
724    public _SelectColumnCharacteristics getColumnCharacteristics(int parm1) throws com.daffodilwoods.database.resource.DException {
725       if (foreignKeyColumns == null) {
726          throw new DException("DSE3513", (Object JavaDoc[])null);
727       }
728       int relatedIndex = getCorrespondingIndexInMapping(parm1);
729       ForeignTableColumnCharacteristics foreignTableCharacteristics = null;
730       if (relatedIndex != -1) {
731          foreignTableCharacteristics = new ForeignTableColumnCharacteristics(foreignKeyColumns[relatedIndex].getColumns(), foreignKeyColumns[relatedIndex].getIndexes());
732          foreignTableCharacteristics.setParentSelectColumnCharacteristics(this);
733          foreignTableCharacteristics.setColumnDetails(columnDetails[parm1 - 1]);
734       }
735       return foreignTableCharacteristics;
736    }
737
738    /**
739     * All the following private methods are used in obtaining foreign columns
740     * characteristics.
741     */

742
743    private int getCorrespondingIndexInMapping(int parm1) throws DException {
744       ColumnDetails column = columnDetails[parm1 - 1];
745       String JavaDoc[] columnToSearch = null;
746
747       if (column.getTableAliasArray() == null) {
748          columnToSearch = new String JavaDoc[1];
749          columnToSearch[0] = column.getColumn();
750          return checkForeignRecord(columnToSearch);
751       }
752
753       String JavaDoc[] columnName = column.getTableAliasArray();
754       int length = columnName.length;
755       columnToSearch = new String JavaDoc[length + 1];
756       System.arraycopy(columnName, 0, columnToSearch, 0, length);
757       columnToSearch[length] = column.getColumn();
758       return checkForeignRecord(columnToSearch);
759    }
760
761    public boolean isForeignTableRecordFetched(int parm1) throws com.daffodilwoods.database.resource.DException {
762       ColumnDetails column = columnDetails[parm1 - 1];
763       String JavaDoc[] columnToSearch = null;
764       if (column.getTableAliasArray() == null) {
765          columnToSearch = new String JavaDoc[1];
766          columnToSearch[0] = column.getColumn();
767          return checkForeignRecord(columnToSearch) >= 0;
768       }
769       String JavaDoc[] columnName = column.getTableAliasArray();
770       int length = columnName.length;
771       columnToSearch = new String JavaDoc[length + 1];
772       System.arraycopy(columnName, 0, columnToSearch, 0, length);
773       columnToSearch[length] = column.getColumn();
774       return checkForeignRecord(columnToSearch) >= 0;
775    }
776
777    public int checkForeignRecord(String JavaDoc[] columns) throws DException {
778       if (foreignKeyColumns != null) {
779          for (int i = 0, length = foreignKeyColumns.length; i < length; i++) {
780             String JavaDoc[] columnNames = foreignKeyColumns[i].getColumns();
781             int lengthOfFKC = columnNames.length, lengthOFPassed = columns.length;
782             boolean found = true;
783             if (lengthOfFKC == lengthOFPassed) {
784                for (int k = 0; k < lengthOfFKC; k++) {
785                   if (!columns[k].equalsIgnoreCase(columnNames[k])) {
786                      found = false;
787                      break;
788                   }
789                }
790                if (found) {
791                   return i;
792                }
793             }
794          }
795       }
796       return -1;
797    }
798
799    /**
800     * The following methods are never called. They are just place holders.
801     * @return
802     * @throws DatabaseException
803     * @throws DException
804     */

805
806    public String JavaDoc[] getPrimaryKeys() throws DatabaseException, DException {
807       throw new UnsupportedOperationException JavaDoc();
808    }
809
810    public boolean isColumnSelectable(int columnIndex) throws DException {
811       /**@todo: Implement this com.daffodilwoods.daffodildb.server.sql99.dql.interfaces._SelectColumnCharacteristics method*/
812       throw new java.lang.UnsupportedOperationException JavaDoc("Method isColumnSelectable() not yet implemented.");
813    }
814
815    /*public void writeExternal(ObjectOutput out) throws IOException{
816          try{
817             if(allOrSelected)
818                initialiseRelatedTables();
819          }catch(DException de){
820            throw new RuntimeException("Problem in getting related table ");
821          }
822         out.writeObject(tableDetailsCCMapping);
823         out.writeObject(columnDetails);
824         out.writeObject(groupByPrimaryColumns);
825         out.writeObject(rowReader);
826         out.writeObject(functionalColumn);
827         out.writeObject(new Boolean(isDistinctOrGroup));
828         out.writeObject(indexes);
829         out.writeObject(new Boolean(allOrSelected));
830         out.writeObject(foreignKeyColumns);
831       }
832     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException{
833       tableDetailsCCMapping = (Object[][])in.readObject();
834       columnDetails = (ColumnDetails[])in.readObject();
835       groupByPrimaryColumns = (String[])in.readObject();
836       rowReader = (_RowReader)in.readObject();
837       functionalColumn = (_FunctionalColumn[])in.readObject();
838       isDistinctOrGroup = ((Boolean)in.readObject()).booleanValue();
839       indexes = (int[])in.readObject();
840       allOrSelected = ((Boolean)in.readObject()).booleanValue();
841       foreignKeyColumns = (ForeignKeyColumns[])in.readObject();
842     }*/

843    public _ColumnCharacteristics getCCFromIndexes(_ColumnCharacteristics cc, int offset, int[] columnIndexes) throws DException {
844       throw new UnsupportedOperationException JavaDoc("getCCFromIndexes(cc, offset, columnIndexes) method not implemented yet");
845    }
846 }
847
Popular Tags