KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > ddl > schemadefinition > checkconstraintdefinition


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
6 import com.daffodilwoods.daffodildb.server.serversystem.*;
7 import com.daffodilwoods.daffodildb.server.sql99.common.*;
8 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.semanticchecker.*;
10 import com.daffodilwoods.daffodildb.server.sql99.token.*;
11 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
12 import com.daffodilwoods.database.general.*;
13 import com.daffodilwoods.database.resource.*;
14 import com.daffodilwoods.database.utility.*;
15
16 public class checkconstraintdefinition implements tableconstraint, columnconstraint {
17    public Srightparen_1874859514 _Srightparen_18748595140;
18    public searchcondition _searchcondition1;
19    public Sleftparen653880241 _Sleftparen6538802412;
20    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223;
21
22    private TableConstraintDescriptor tableConsDes;
23    private DomainConstraintDescriptor domainConsDes;
24    public _Reference[] ref;
25
26    public void setTableConstraintDescriptor(_Descriptor tableConsDes0) throws DException {
27       tableConsDes = (TableConstraintDescriptor) tableConsDes0;
28    }
29
30    public void setDomainConstraintDescriptor(_Descriptor domainConsDes0) throws DException {
31       domainConsDes = (DomainConstraintDescriptor) domainConsDes0;
32    }
33
34    public Object JavaDoc run(Object JavaDoc object) throws DException {
35       _ServerSession currentSession = (_ServerSession) object;
36       /** @todo
37        * to review this part with domain constraint defintion
38        * */

39       CheckConstraintDescriptor checkConstraint = new CheckConstraintDescriptor();
40       checkConstraint.check_clause = _searchcondition1._searchcondition0.toString();
41       verifiyLengthOfCheckClause(checkConstraint.check_clause.length());
42       /** @todo
43        * optimize this by shiftiing the common part of DCD and TCD in CD abstract class
44        * */

45       if (tableConsDes == null) { // if check constraint is for domain
46
checkConstraint.domainConstraintDescriptor = domainConsDes;
47          checkConstraint.constraint_catalog = domainConsDes.constraint_catalog;
48          checkConstraint.constraint_schema = domainConsDes.constraint_schema;
49          checkConstraint.constraint_name = domainConsDes.constraint_name;
50          ColumnDetails[] cds = _searchcondition1.getColumnDetails();
51          for (int i = 0; i < cds.length; i++) {
52             String JavaDoc[] namePresent = cds[i].getColumnName();
53             if (namePresent[0].equalsIgnoreCase(SqlKeywords.VALUE)) {
54                cds[i].setDatatype(domainConsDes.domainDes.dataTypeDescriptor.getType());
55             }
56          }
57          _Reference[] ref = null;
58          try {
59             ref = _searchcondition1.checkSemantic(currentSession);
60             if (_searchcondition1.hasContainClause()) {
61                throw new DException("DSE8138", null);
62             }
63             for (int i = 0; i < ref.length; i++) {
64                if (ref[i].getReferenceType() == SimpleConstants.VARIABLECOLUMN)
65                   throw new DException("DSE8172", null);
66             }
67          } catch (Exception JavaDoc ex) {
68             throw new DException("DSE8108", new Object JavaDoc[] {ex.getMessage()});
69          }
70          if (ref != null && ref.length != 0) {
71             throw new DException("DSE256", new Object JavaDoc[] {StaticClass.printArray(ref)});
72          }
73          storeTablesIncluded(checkConstraint);
74          storeColumnsIncluded(checkConstraint);
75          checkConstraint.saveTableAndColumnIncluded = true;
76       } else {
77          tableConsDes.constraint_type = "Check";
78          if (tableConsDes.constraint_name == null) {
79             tableConsDes.setGeneratedConstraintName(currentSession, "CHECKC_");
80          }
81          checkConstraint.constraint_catalog = tableConsDes.constraint_catalog;
82          checkConstraint.constraint_schema = tableConsDes.constraint_schema;
83          checkConstraint.constraint_name = tableConsDes.constraint_name;
84          tableConsDes.setConstraintDescriptor(checkConstraint);
85          checkConstraint.tableConstraintDescriptor = tableConsDes;
86          /*
87                   DataDictionary dd = (DataDictionary) currentSession.getDataDictionary();
88                   QualifiedIdentifier qu = new QualifiedIdentifier(tableConsDes.table_catalog, tableConsDes.table_schema, tableConsDes.table_name);
89                                     TableDetails tds = new TableDetails();
90                                     tds.setTableName(new String[] {tableConsDes.table_catalog, tableConsDes.table_schema, tableConsDes.table_name});
91                                     _ColumnCharacteristics cc = dd.getColumnCharacteristics(qu, true);
92                                     tds.cc = cc;*/

93          ColumnDetails[] cds = _searchcondition1.getColumnDetails();
94          /*
95           */

96          setDataTypeAndTableDetails(cds);
97          _Reference[] ref = null;
98          try {
99             ref = _searchcondition1.checkSemantic(currentSession);
100          } catch (DException ex) {
101             throw new DException("DSE8108", new Object JavaDoc[] {ex.getMessage()});
102          }
103          if (ref != null && ref.length != 0) {
104             ref = (_Reference[]) GeneralPurposeStaticClass.getNonClonedReferences(ref).toArray(new _Reference[0]);
105             Object JavaDoc[] actualReferences = new Object JavaDoc[ref.length];
106             for (int i = 0; i < ref.length; i++) {
107                if (ref[i].getReferenceType() == SimpleConstants.VARIABLECOLUMN) {
108                   throw new DException("DSE8209", null);
109                }
110                actualReferences[i] = ref[i].getColumn();
111             }
112             throw new DException("DSE256", new Object JavaDoc[] {StaticClass.printArray(actualReferences)});
113          }
114          storeTablesIncluded(checkConstraint);
115          storeColumnsIncluded(checkConstraint, currentSession);
116          checkConstraint.saveTableAndColumnIncluded = true;
117       }
118       return checkConstraint;
119    }
120
121    private void verifiyLengthOfCheckClause(int len) throws DException {
122       int premmitedLen = SystemTablesCreator.serializeObjectDataSize.hashCode();
123       if (len > premmitedLen) {
124          throw new DException("DSE1087", new Object JavaDoc[] {new Integer JavaDoc(len), new Integer JavaDoc(premmitedLen)});
125       }
126    }
127
128    /*private void setDataTypeAndTableDetails(ColumnDetails[] cds, TableDetails tds, _ColumnCharacteristics cc) throws DException {
129       for (int i = 0; i < cds.length; i++) {
130          String[] namePresent = cds[i].getColumnName();
131          if (cds[i].getType() == cds[i].CONSTANT) {
132             continue;
133          }
134          if (cds[i].getType() == cds[i].REFERENCE) {
135             ColumnDescriptor cd = tableConsDes.tableDescriptor.getColumnDescriptor(namePresent[0]);
136             cds[i].setDatatype(cc.getColumnType(cc.getColumnIndex(namePresent[0])));
137             cds[i].setTableDetails(tds);
138          } else {
139             ColumnDetails[] asd = cds[i].getExistingColumnDetails();
140             if (asd != null) {
141                setDataTypeAndTableDetails(asd, tds, cc);
142             }
143          }
144       }
145        }*/

146
147    private void setDataTypeAndTableDetails(ColumnDetails[] cds) throws DException {
148       for (int i = 0; i < cds.length; i++) {
149          String JavaDoc[] namePresent = cds[i].getColumnName();
150          if (cds[i].getType() == cds[i].CONSTANT) {
151             continue;
152          }
153          if (cds[i].getType() == cds[i].REFERENCE) {
154             ColumnDescriptor cd = tableConsDes.tableDescriptor.getColumnDescriptor(namePresent[0]);
155             cds[i].setDatatype(cd.getType());
156          } else {
157             ColumnDetails[] asd = cds[i].getExistingColumnDetails();
158             if (asd != null) {
159                setDataTypeAndTableDetails(asd);
160             }
161          }
162       }
163    }
164
165    public void validateCheckConstraintSemantic(_ServerSession serverSession) throws
166        DException {
167       QualifiedIdentifier tableName = tableConsDes.tableDescriptor.getQualifiedTableName();
168
169       TableDetails tableDetails = new TableDetails();
170       tableDetails.setTableName(tableName.getTableName());
171       SemanticChecker semanticChecker = new SemanticChecker(new TableDetails[] {tableDetails}
172           , _searchcondition1.getColumnDetails());
173       ArrayList syntaxResult = new ArrayList();
174       semanticChecker.semanticCheckingUpdate(serverSession, syntaxResult);
175       if (syntaxResult.size() != 0) {
176          throw new DException("DSE7005",
177                               new Object JavaDoc[] {StaticClass.printArray(syntaxResult.
178              toArray()), tableConsDes.getQualifiedTable().getIdentifier()});
179       }
180       _Reference[] ref = null;
181       try {
182          ref = _searchcondition1.checkSemantic(serverSession);
183       } catch (Exception JavaDoc ex) {
184          throw new DException("DSE8108", new Object JavaDoc[] {ex.getMessage()});
185       }
186       if (ref != null && ref.length != 0) {
187          _ColumnCharacteristics cc = serverSession.getColumnCharacteristics(tableName);
188          String JavaDoc[] columns = cc.getColumnNames();
189          ArrayList refrencesList = new ArrayList(ref.length);
190          for (int j = 0; j < ref.length; j++) {
191             if (ref[j].getReferenceType() == SimpleConstants.VARIABLECOLUMN)
192                refrencesList.add(ref[j]);
193             else {
194                ColumnDetails cd = (ColumnDetails) ref[j];
195                String JavaDoc[] refTableName = cd.getColumnName();
196                int len = refTableName.length;
197                if (P.indexOfIgnoreCase(columns, refTableName[len - 1]) == -1)
198                   refrencesList.add(ref[j]);
199                else if (len > 1 &&
200                         !tableName.name.equalsIgnoreCase(refTableName[len - 2]))
201                   refrencesList.add(ref[j]);
202                else if (len > 2 &&
203                         !tableName.schema.equalsIgnoreCase(refTableName[len - 3]))
204                   refrencesList.add(ref[j]);
205                else if (len > 3 &&
206                         !tableName.catalog.equalsIgnoreCase(refTableName[len - 4]))
207                   refrencesList.add(ref[j]);
208             }
209          }
210          /* Next Line :: Done by Kaushik on 10/09/2004 to solve Bug no. 11173 */
211          refrencesList = GeneralPurposeStaticClass.getNonClonedReferences( (_Reference[]) refrencesList.toArray(new _Reference[0]));
212
213          if (refrencesList.size() != 0)
214             throw new DException("DSE256",
215                                  new Object JavaDoc[] {StaticClass.printArray(refrencesList.
216                 toArray())});
217       }
218
219       CheckConstraintDescriptor checkConDes = (CheckConstraintDescriptor) tableConsDes.constraintDescriptor;
220       if (!checkConDes.saveTableAndColumnIncluded) {
221          storeTablesIncluded(checkConDes);
222          storeColumnsIncluded(checkConDes);
223          checkConDes.saveTablesAndColumnsIncluded(serverSession);
224       }
225    }
226
227    public void validateCheckConstraintSemanticForAddColumn(_ServerSession serverSession, ColumnDescriptor colDes) throws
228        DException {
229       QualifiedIdentifier tableName = tableConsDes.tableDescriptor.getQualifiedTableName();
230       TableDetails tableDetails = new TableDetails();
231       tableDetails.setTableName(tableName.getTableName());
232       SemanticChecker semanticChecker = new SemanticChecker(new TableDetails[] {tableDetails}
233           , _searchcondition1.getColumnDetails());
234       _ColumnCharacteristics cc = serverSession.getColumnCharacteristics(tableName);
235       String JavaDoc[] columns = cc.getColumnNames();
236       ArrayList syntaxResult = new ArrayList();
237       semanticChecker.semanticCheckingUpdate(serverSession, syntaxResult);
238       if (syntaxResult.size() != 0) {
239          ArrayList refrencesList = new ArrayList(syntaxResult.size());
240          for (int j = 0; j < syntaxResult.size(); j++) {
241             if ( ( (_Reference) syntaxResult.get(j)).getReferenceType() == SimpleConstants.VARIABLECOLUMN)
242                refrencesList.add(syntaxResult.get(j));
243             else {
244                ColumnDetails cd = (ColumnDetails) syntaxResult.get(j);
245                String JavaDoc[] refColName = cd.getColumnName();
246                int len = refColName.length;
247                if (!colDes.column_name.equalsIgnoreCase(refColName[len - 1]))
248                   refrencesList.add(syntaxResult.get(j));
249                else if (len > 1 &&
250                         !tableName.name.equalsIgnoreCase(refColName[len - 2]))
251                   refrencesList.add(syntaxResult.get(j));
252                else if (len > 2 &&
253                         !tableName.schema.equalsIgnoreCase(refColName[len - 3]))
254                   refrencesList.add(syntaxResult.get(j));
255                else if (len > 3 &&
256                         !tableName.catalog.equalsIgnoreCase(refColName[len - 4]))
257                   refrencesList.add(syntaxResult.get(j));
258                else
259                   cd.setDatatype(colDes.dataTypeDescriptor.getType());
260             }
261          }
262          if (refrencesList.size() != 0)
263             throw new DException("DSE7005",
264                                  new Object JavaDoc[] {StaticClass.printArray(refrencesList.
265                 toArray()), tableConsDes.getQualifiedTable().getIdentifier()});
266       }
267       try {
268          ref = _searchcondition1.checkSemantic(serverSession);
269       } catch (Exception JavaDoc ex) {
270          throw new DException("DSE8108", new Object JavaDoc[] {ex.getMessage()});
271       }
272       if (ref != null && ref.length != 0) {
273          ArrayList refrencesList = new ArrayList(ref.length);
274          for (int j = 0; j < ref.length; j++) {
275             if (ref[j].getReferenceType() == SimpleConstants.VARIABLECOLUMN)
276                refrencesList.add(ref[j]);
277             else {
278                ColumnDetails cd = (ColumnDetails) ref[j];
279                String JavaDoc[] refColName = cd.getColumnName();
280                int len = refColName.length;
281                if (! (P.indexOfIgnoreCase(columns, refColName[len - 1]) != -1
282                       || colDes.column_name.equalsIgnoreCase(refColName[len - 1])))
283                   refrencesList.add(ref[j]);
284                else if (len > 1 &&
285                         !tableName.name.equalsIgnoreCase(refColName[len - 2]))
286                   refrencesList.add(ref[j]);
287                else if (len > 2 &&
288                         !tableName.schema.equalsIgnoreCase(refColName[len - 3]))
289                   refrencesList.add(ref[j]);
290                else if (len > 3 &&
291                         !tableName.catalog.equalsIgnoreCase(refColName[len - 4]))
292                   refrencesList.add(ref[j]);
293                else if (colDes.column_name.equalsIgnoreCase(refColName[len - 1])) {
294                   cd.setDatatype(colDes.dataTypeDescriptor.getType());
295                }
296             }
297          }
298          if (refrencesList.size() != 0)
299             throw new DException("DSE256",
300                                  new Object JavaDoc[] {StaticClass.printArray(refrencesList.
301                 toArray())});
302       }
303       CheckConstraintDescriptor checkConDes = (CheckConstraintDescriptor) tableConsDes.constraintDescriptor;
304       if (!checkConDes.saveTableAndColumnIncluded) {
305          storeTablesIncluded(checkConDes);
306          storeColumnsIncluded(checkConDes);
307          checkConDes.saveTablesAndColumnsIncluded(serverSession);
308       }
309    }
310
311    private void storeTablesIncluded(CheckConstraintDescriptor checkConstraint) throws
312        DException {
313       ArrayList tablesIncluded = new ArrayList();
314       _searchcondition1.getTablesIncluded(tablesIncluded);
315       checkConstraint.setTablesIncluded(tablesIncluded);
316    }
317
318    private void storeColumnsIncluded(CheckConstraintDescriptor checkConstraint) throws
319        DException {
320       ArrayList columnsIncluded = new ArrayList();
321       _searchcondition1.getColumnsIncluded(columnsIncluded);
322       checkConstraint.setColumnsIncluded(columnsIncluded);
323    }
324
325    private void storeColumnsIncluded(CheckConstraintDescriptor checkConstraint, _ServerSession currentSession) throws
326        DException {
327       ArrayList columnsIncluded = new ArrayList();
328       _searchcondition1.getColumnsIncluded(columnsIncluded);
329       checkReferencePrivilegesOnAllColumnsUsedInSearchCondition(columnsIncluded, currentSession);
330       checkConstraint.setColumnsIncluded(columnsIncluded);
331    }
332
333    private void checkReferencePrivilegesOnAllColumnsUsedInSearchCondition(ArrayList columnsIncluded, _ServerSession currentSession) throws DException {
334       int size = columnsIncluded.size();
335       for (int i = 0; i < size; i++) {
336          String JavaDoc[] columnNamePresent = (String JavaDoc[]) columnsIncluded.get(i);
337          if (columnNamePresent.length > 1) {
338             if (!(tableConsDes.table_catalog.equalsIgnoreCase(columnNamePresent[0]) &&
339                 tableConsDes.table_schema.equalsIgnoreCase(columnNamePresent[1]) &&
340                 tableConsDes.table_name.equalsIgnoreCase(columnNamePresent[2]))) {
341                int columnIndex = currentSession.getColumnCharacteristics(new QualifiedIdentifier(columnNamePresent[0], columnNamePresent[1], columnNamePresent[2])).getColumnIndex(columnNamePresent[3]);
342                _PrivilegeTable privilegeTable = currentSession.getUserSession().getPrivilegeCharacteristics().getPrivilegeTable(new QualifiedIdentifier(columnNamePresent[0], columnNamePresent[1], columnNamePresent[2]));
343                boolean referenceRight = privilegeTable.hasColumnPrivileges(privilegeTable.REFERENCES, new int[] {columnIndex});
344                if (!referenceRight)
345                   throw new DException("DSE8176", new Object JavaDoc[] {currentSession.getCurrentUser(), columnNamePresent[3], new QualifiedIdentifier(columnNamePresent[0], columnNamePresent[1], columnNamePresent[2])});
346             }
347          }
348       }
349    }
350
351    public String JavaDoc toString() {
352       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
353       sb.append(" ");
354       sb.append(_SRESERVEDWORD12065439223);
355       sb.append(" ");
356       sb.append(_Sleftparen6538802412);
357       sb.append(" ");
358       sb.append(_searchcondition1);
359       sb.append(" ");
360       sb.append(_Srightparen_18748595140);
361       return sb.toString();
362    }
363
364    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
365       return this;
366    }
367 }
368
Popular Tags