KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.common;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
6 import com.daffodilwoods.daffodildb.server.serversystem.*;
7 import com.daffodilwoods.database.general.*;
8 import com.daffodilwoods.database.resource.*;
9 import com.daffodilwoods.database.utility.P;
10
11 public class SetColumnProperties implements TypeConstants {
12
13    /**
14     * Performs Various functions Like
15     * Checks Whether columns passed in the function belongs to tables passed
16     * If belongs to tables passed
17     * checks whether column specfiied is ambiguous, that present in two tables
18     * Examples
19     * select * from country,state where countryid > 1
20     * Here countryid column specified in where condition is ambiguous as it
21     * is present in both the tables.
22     * Initialises the data type, size , Qualified columnName of column and
23     * tableDetails in reference to which it belongs.
24     * else
25     * returns columns as unKnown reference
26     * If * is present then setCount method is called to initialise ColumnDetails
27     * as columns belonging to all tables specified.
28     * If A.* is present then ColumnDetails are initialised of this particular table A
29     * Also checks for Chained Columns present, if present then checks the validity
30     * of chained column
31     * Examples
32     * select S.Cid.CountryNAme from state S
33     * Here, Cid column must present in State Table and CountryNAme column must present in Country Table
34     * which Cid is referring.
35     * called by checkSelectListColumnSemantic(),checkWhereColumnSemantic(), etc.
36     * @param serverSession
37     * @param columnDetails
38     * @param tableDetails
39     * @param conditionalColumns
40     * @return _Reference[]
41     * @throws com.daffodilwoods.database.resource.DException
42     */

43
44    public static void setTableNamesAndDatatypesOfAllColumns(_ServerSession serverSession, ColumnDetails[] columnDetails, TableDetails[] tableDetails, ArrayList listOfUnderLyingReferences, ArrayList listOfColumnsInJoin) throws com.daffodilwoods.database.resource.DException {
45       ArrayList aList = new ArrayList(3);
46       for (int indexOfColumnDetails = 0; indexOfColumnDetails < columnDetails.length; ++indexOfColumnDetails) {
47          int type = columnDetails[indexOfColumnDetails].getType();
48          if (type == CONSTANT && !columnDetails[indexOfColumnDetails].getUnderLyingReference()) {
49             if ( ( (String JavaDoc[]) columnDetails[indexOfColumnDetails].getColumnName()).length == 1) {
50                if ( ( (String JavaDoc) columnDetails[indexOfColumnDetails].getColumn()).equals("*")) {
51                   setCount(serverSession, tableDetails, columnDetails[indexOfColumnDetails]);
52                }
53             }
54             continue;
55          }
56          if (type == SCALARFUNCTION || type == GROUPING || type == CASEEXPRESSION || type==FUNCTIONAL||type == USERFUNCTION ) {
57             setTableNamesAndDatatypesOfAllColumns(serverSession, columnDetails[indexOfColumnDetails].getExistingColumnDetails(), tableDetails, listOfUnderLyingReferences, listOfColumnsInJoin);
58             continue;
59          }
60          checkForEachTable(serverSession, columnDetails[indexOfColumnDetails], tableDetails, listOfUnderLyingReferences, listOfColumnsInJoin);
61       }
62       if ( listOfUnderLyingReferences != null ){
63         for (int i = 0; i < columnDetails.length; i++) {
64           if (columnDetails[i].getTable() != null &&
65               P.indexOf(tableDetails, columnDetails[i].getTable()) > -1) {
66             boolean o = listOfUnderLyingReferences.remove(columnDetails[i]);
67
68           }
69         }
70       }
71   }
72
73
74
75    private static void setCount(_ServerSession serverSession, TableDetails[] tableDetail, ColumnDetails columnDetails) throws com.daffodilwoods.database.resource.DException {
76       ArrayList aList = new ArrayList(); // in case select *
77
for (int i = 0; i < tableDetail.length; ++i) {
78          aList.addAll(Arrays.asList(tableDetail[i].getColumnPropertiesExcludingCommonColumns()));
79       }
80       columnDetails.setObject( (ColumnDetails[]) aList.toArray(new ColumnDetails[0]));
81    }
82
83    private static void checkForEachTable(_ServerSession serverSession, ColumnDetails columnDetail, TableDetails[] tableDetails, ArrayList listOfUnderLyingReferences, ArrayList listOfColumnsInJoin) throws com.daffodilwoods.database.resource.DException {
84       String JavaDoc[] keyArray;
85       boolean check = false; // already checked
86
Object JavaDoc[] result = new Object JavaDoc[4];
87       for (int indexOfTableDetails = 0; indexOfTableDetails < tableDetails.length; ++indexOfTableDetails) {
88          TableDetails tempTableDetails = tableDetails[indexOfTableDetails];
89          String JavaDoc columnName[] = columnDetail.getColumnName();
90          if (columnDetail.getType() == GROUPING) {
91             return;
92          }
93          keyArray = getDetailedColumn(columnName, tempTableDetails, columnDetail, serverSession, listOfColumnsInJoin);
94          if (keyArray == null) {
95             continue; // Means Not From This Table, Check For Next Table.
96
}
97          if (columnDetail.getTableAliasArray() != null) {
98             columnDetail.setColumn(keyArray);
99             return;
100          }
101          if (keyArray[keyArray.length - 1].equals("*")) { // for A.*
102
columnDetail.setColumn(keyArray);
103             columnDetail.setTableDetails(tempTableDetails);
104             columnDetail.setObject(tempTableDetails.getColumnPropertiesExcludingCommonColumns());
105             columnDetail.setTableForDisplay(tempTableDetails);
106             return;
107          }
108          int key = tempTableDetails.checkForColumn(keyArray);
109          if (key != -1) {
110             if (check) { // Column Found in Duplicate TableDetails
111
throw new DException("DSE17", new Object JavaDoc[] {columnDetail.getColumn()}); // Column Duplicacy
112
}
113             columnDetail.setTableDetails(tempTableDetails); // setting TableDetails in ColumnDetails
114
columnDetail.setTableForDisplay(tempTableDetails);
115             check = true;
116             result[0] = columnDetail;
117             result[1] = keyArray;
118             result[2] = new Integer JavaDoc(tempTableDetails.getDatatype(key));
119             result[3] = new Integer JavaDoc(tempTableDetails.getSize(key));
120          }
121       }
122       if (!check) { // Column Not Found in All TableDetails
123
listOfUnderLyingReferences.add(columnDetail); // Column Not Found in All Tables Means Either it can be Outer Query Col or InValid Column.
124
return;
125       }
126       /*Work done to check and prevent the BLOB/CLOB/IMAGE type columns in the table expression*/
127       if (! ( ( (ColumnDetails) result[0]).ifBelongToSelectList() || ( (ColumnDetails) result[0]).ifBelongToAllowedPredicate())) {
128          Check.checkForAllowedDatatype(result[2].hashCode());
129       }
130       ( (ColumnDetails) result[0]).setColumn( (String JavaDoc[]) result[1]);
131       ( (ColumnDetails) result[0]).setDatatype(result[2].hashCode());
132       ( (ColumnDetails) result[0]).setSize(result[3].hashCode());
133    }
134
135
136    private static String JavaDoc[] getDetailedColumn(String JavaDoc columnName[], TableDetails tableDetails, ColumnDetails columnDetails, _ServerSession serverSession, ArrayList listOfColumnsInJoin) throws DException {
137       String JavaDoc[] appropriateTableName = tableDetails.getAppropriateTableName();
138       String JavaDoc[] originalTableName = tableDetails.getTableName(); // alias name is needed for checking purpose
139
if (check(appropriateTableName, columnName, tableDetails, columnDetails, serverSession, listOfColumnsInJoin)) {
140          return append(originalTableName, columnName);
141       }
142       return null;
143    }
144
145    private static boolean check(String JavaDoc[] tableName, String JavaDoc[] columnName, TableDetails tableDetails, ColumnDetails columnDetail, _ServerSession serverSession, ArrayList listOfColumnsInJoin) throws DException {
146       for (int i = columnName.length - 2, j = tableName.length - 1, length = columnName.length; i >= 0 && j >= 0; i--, j--) {
147          if (!tableName[j].equalsIgnoreCase(columnName[i])) {
148             if (length - 2 != 0 && i == length - 2 && j == tableName.length - 1) {
149                String JavaDoc aliasName = tableDetails.getAliasName();
150                if (aliasName != null && aliasName.equalsIgnoreCase(columnName[0])) {
151                   checkForeignKeyCC(tableDetails, columnDetail, serverSession, columnName, listOfColumnsInJoin);
152                   return true;
153                }
154             }
155             return false;
156          }
157       }
158       return true;
159    }
160
161    private static void checkForeignKeyCC(TableDetails tableDetails, ColumnDetails columnDetail, _ServerSession serverSession, String JavaDoc[] columnName, ArrayList listOfColumnsInJoin) throws DException {
162       int length = columnName.length;
163       QualifiedIdentifier identifier = tableDetails.getQualifiedIdentifier();
164       String JavaDoc[] tableAliasArray = new String JavaDoc[length - 2];
165       _ColumnCharacteristics columnCharacteristics = serverSession.getColumnCharacteristics(identifier);
166       ArrayList listOfFKeyTables = new ArrayList();
167       TableDetails tempTable = tableDetails;
168       for (int i = 1, j = 0; i < length - 1; i++, j++) {
169          if (!ifExistsColumnInCC(columnName[i], columnCharacteristics.getColumnNames())) {
170             throw new DException("DSE255", new Object JavaDoc[] {columnName[i], identifier.getIdentifier()}); // Column Not Present
171
}
172          Object JavaDoc[] object = serverSession.getForeignConstraintCharacteritics(identifier, columnName[i]);
173
174          identifier = (QualifiedIdentifier) object[0];
175          columnCharacteristics = (_ColumnCharacteristics) object[1];
176
177          TableDetails table = new TableDetails();
178          table.setTableName(identifier.getTableName());
179          table.cc = columnCharacteristics;
180          listOfFKeyTables.add(table);
181
182          tempTable = table;
183
184          String JavaDoc[] referencedColumns = (String JavaDoc[]) object[2];
185          tableAliasArray[j] = columnName[i];
186       }
187       columnDetail.setTableAliasArray(tableAliasArray);
188       tableDetails.setUnderLyingFKeyTables( (TableDetails[]) listOfFKeyTables.toArray(new TableDetails[listOfFKeyTables.size()]));
189       if (columnName[length - 1].equalsIgnoreCase("*")) {
190          initializeAllColumnsOfForeign(columnDetail, columnCharacteristics, tableDetails, tableAliasArray);
191       } else {
192          if (!ifExistsColumnInCC(columnName[length - 1], columnCharacteristics.getColumnNames())) {
193             throw new DException("DSE255", new Object JavaDoc[] {columnName[length - 1], identifier.getIdentifier()}); // Column Not Present
194
}
195          columnDetail.setDatatype(columnCharacteristics.getColumnType(columnCharacteristics.getColumnIndex(columnName[length - 1])));
196          columnDetail.setSize(columnCharacteristics.getSize(columnCharacteristics.getColumnIndex(columnName[length - 1])));
197          columnDetail.setTableDetails(tableDetails);
198          columnDetail.setQualifiedIdentifier(new QualifiedIdentifier(columnCharacteristics.getCatalogName(0), columnCharacteristics.getSchemaName(0), columnCharacteristics.getTableName(0)));
199          columnDetail.setChainedColumnCC(columnCharacteristics);
200       }
201    }
202
203    private static void initializeAllColumnsOfForeign(ColumnDetails columnDetail, _ColumnCharacteristics columnCharacteristics, TableDetails table, String JavaDoc[] tableAliasArray) throws DException {
204       String JavaDoc[] ccColumnNames = columnCharacteristics.getColumnNames();
205       int length = ccColumnNames.length;
206       QualifiedIdentifier identifier = new QualifiedIdentifier(columnCharacteristics.getCatalogName(0), columnCharacteristics.getSchemaName(0), columnCharacteristics.getTableName(0));
207       ColumnDetails[] newColumns = new ColumnDetails[length - 5];
208       for (int i = 5, j = 0; i < length; i++, j++) {
209          ColumnDetails columnDetails = new ColumnDetails();
210          String JavaDoc[] tableName = table.getTableName();
211          String JavaDoc[] column = new String JavaDoc[4];
212          System.arraycopy(tableName, 0, column, 0, tableName.length);
213          column[3] = ccColumnNames[i];
214          columnDetails.setColumn(column);
215          columnDetails.setChainedColumnCC(columnCharacteristics);
216          columnDetails.setDatatype(columnCharacteristics.getColumnType(i));
217          columnDetails.setSize(columnCharacteristics.getSize(i));
218          columnDetails.setTableDetails(table);
219          columnDetails.setTableForDisplay(table);
220          columnDetails.setType(REFERENCE);
221          columnDetails.setTableAliasArray(tableAliasArray);
222          columnDetails.setQualifiedIdentifier(identifier);
223          newColumns[j] = columnDetails;
224       }
225       columnDetail.setObject(newColumns);
226       columnDetail.setTableDetails(table);
227    }
228
229    private static boolean ifExistsColumnInCC(String JavaDoc columnName, String JavaDoc[] columns) {
230       int length = columns.length;
231       for (int i = 0; i < length; i++) {
232          if (columns[i].equalsIgnoreCase(columnName)) {
233             return true;
234          }
235       }
236       return false;
237    }
238
239    private static String JavaDoc[] append(String JavaDoc[] tableName, String JavaDoc[] columnName) throws DException {
240       String JavaDoc[] result = new String JavaDoc[tableName.length + 1];
241       System.arraycopy(tableName, 0, result, 0, tableName.length);
242       result[result.length - 1] = columnName[columnName.length - 1];
243       return result;
244    }
245
246    private static Object JavaDoc[] getForeignConstraintCharacteritics(QualifiedIdentifier identifier, String JavaDoc columnName) {
247       return null;
248    }
249
250    public static void checkingForAmbuityForTableNamesWithCC(_ServerSession serverSession, TableDetails[] tableDetails) throws com.daffodilwoods.database.resource.DException {
251       int length = tableDetails.length;
252       for (int index1 = 0; index1 < length - 1; ++index1) {
253          TableDetails tableDetailSource = tableDetails[index1];
254          tableDetailSource.cc = serverSession.getColumnCharacteristics(tableDetailSource.getQualifiedIdentifier());
255          for (int index2 = index1 + 1; index2 < length; ++index2) {
256             TableDetails tableDetailTarget = tableDetails[index2];
257             if (tableDetailSource.toString().equalsIgnoreCase(tableDetailTarget.toString())) {
258                throw new DException("DSE954", new Object JavaDoc[] {tableDetailSource.getQualifiedIdentifier().getIdentifier()});
259             }
260          }
261       }
262       tableDetails[length - 1].cc = serverSession.getColumnCharacteristics(tableDetails[length - 1].getQualifiedIdentifier());
263    }
264
265 }
266
Popular Tags