KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > ddl > schemamanipulation > droptableconstraintdefinition


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemamanipulation;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.serversystem.*;
6 import com.daffodilwoods.daffodildb.server.sql99.common.*;
7 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
8 import com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
10 import com.daffodilwoods.daffodildb.server.sql99.token.*;
11 import com.daffodilwoods.database.general.*;
12 import com.daffodilwoods.database.resource.*;
13
14 public class droptableconstraintdefinition implements altertableaction {
15    public dropbehavior _dropbehavior0;
16    public constraintname _constraintname1;
17    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439222;
18    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223;
19
20    private TableDescriptor tableDescriptor;
21
22    public void setTableDescriptor(_Descriptor tableDes) throws DException {
23       tableDescriptor = (TableDescriptor) tableDes;
24    }
25
26    public Object JavaDoc run(Object JavaDoc object) throws DException {
27       _ServerSession currentSession = (_ServerSession) object;
28       TableConstraintDescriptor tableConsDes = new
29           TableConstraintDescriptor();
30       setConstraintName(tableConsDes, currentSession);
31       String JavaDoc dropBehavior = (String JavaDoc) _dropbehavior0.run(null);
32       String JavaDoc constraint_type = tableConsDes.constraint_type;
33       dropConstraint(tableConsDes, currentSession, dropBehavior);
34       if (constraint_type.equalsIgnoreCase(SqlKeywords.PRIMARY + " " + SqlKeywords.KEY))
35          setIsNullableColumnProperty(currentSession, tableConsDes);
36       return null;
37    }
38
39    private void setConstraintName(TableConstraintDescriptor tableConsDes,
40                                   _ServerSession currentSession) throws
41        DException {
42       tableConsDes.constraint_catalog = _constraintname1.getCatalogName();
43       tableConsDes.constraint_schema = _constraintname1.getSchemaName();
44       tableConsDes.constraint_name = _constraintname1.getConstraintName();
45       if (tableConsDes.constraint_schema == null) {
46          tableConsDes.constraint_schema = tableDescriptor.table_schema;
47       }
48       if (tableConsDes.constraint_catalog == null) {
49          tableConsDes.constraint_catalog = tableDescriptor.table_catalog;
50       }
51       tableConsDes.load(currentSession);
52       tableConsDes.setTableDescriptor(tableDescriptor);
53    }
54
55    private void dropConstraint(TableConstraintDescriptor tableConsDes,
56                                _ServerSession currentSession, String JavaDoc dropBehavior) throws DException {
57       if (dropBehavior.equalsIgnoreCase(SqlKeywords.RESTRICT)) {
58          checkDependencies(tableConsDes, currentSession);
59       } else {
60          dropDependents(tableConsDes, currentSession);
61       }
62       tableConsDes.delete(currentSession);
63    }
64
65    private void checkDependencies(TableConstraintDescriptor tableConsDes,
66                                   _ServerSession serverSession) throws
67        DException {
68       QualifiedIdentifier obb = tableConsDes.areDependentConstraints(serverSession);
69       if (obb != null) {
70          throw new DException("DSE288", new Object JavaDoc[] {tableConsDes.getQualifiedConstraintName(), ( (QualifiedIdentifier) obb).getIdentifier()});
71       }
72    }
73
74    private void dropDependents(TableConstraintDescriptor
75                                constraintDescriptor,
76                                _ServerSession currentSession) throws DException {
77       dropDependentReferentialConstraints(constraintDescriptor, currentSession);
78    }
79
80    private void dropDependentReferentialConstraints(
81        TableConstraintDescriptor constraintDescriptor,
82        _ServerSession currentSession) throws DException {
83       String JavaDoc clause = "select * from " +
84           SqlSchemaConstants.referential_constraints_TableName
85           + " where unique_constraint_catalog = ? "
86           + " and unique_constraint_schema = ? "
87           + " and unique_constraint_name = ? ";
88       _SelectQueryIterator referentialConstraints =
89           SqlSchemaConstants.getIterator(currentSession, clause,
90                                          new Object JavaDoc[] {constraintDescriptor.
91                                          constraint_catalog,
92                                          constraintDescriptor.constraint_schema,
93                                          constraintDescriptor.constraint_name});
94       if (referentialConstraints.first()) {
95          do {
96             TableConstraintDescriptor referentialConstraintDescriptor = new
97                 TableConstraintDescriptor();
98             Object JavaDoc[] obj = (Object JavaDoc[]) referentialConstraints.getObject();
99             referentialConstraintDescriptor.constraint_catalog = (String JavaDoc) obj[
100                 SystemTablesFields.referential_constraints_constraint_catalog];
101             referentialConstraintDescriptor.constraint_schema = (String JavaDoc) obj[
102                 SystemTablesFields.referential_constraints_constraint_schema];
103             referentialConstraintDescriptor.constraint_name = (String JavaDoc) obj[
104                 SystemTablesFields.referential_constraints_constraint_name];
105             referentialConstraintDescriptor.load(currentSession);
106             referentialConstraintDescriptor.delete(currentSession);
107          } while (referentialConstraints.next());
108       }
109    }
110
111    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
112       return this;
113    }
114
115    public String JavaDoc toString() {
116       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117       sb.append(" ");
118       sb.append(_SRESERVEDWORD12065439223);
119       sb.append(" ");
120       sb.append(_SRESERVEDWORD12065439222);
121       sb.append(" ");
122       sb.append(_constraintname1);
123       sb.append(" ");
124       sb.append(_dropbehavior0);
125       return sb.toString();
126    }
127
128    private void setIsNullableColumnProperty(_ServerSession currentSession, TableConstraintDescriptor tableConsDes) throws DException {
129       ArrayList primaryKeysColumns = tableConsDes.getConstraintColumns();
130       Object JavaDoc[] values = new Object JavaDoc[] {SqlSchemaConstants.YES};
131
132       for (int i = 0; i < primaryKeysColumns.size(); i++) {
133          ColumnDescriptor columnDescriptor = tableConsDes.
134              tableDescriptor.getColumnDescriptor( (String JavaDoc) primaryKeysColumns.
135                                                  get(i));
136          int[] columns = new int[] {SystemTablesFields.columns_is_nullable};
137          if (columnDescriptor.isNullable.equalsIgnoreCase(SqlSchemaConstants.NO)) {
138             columnDescriptor.update(currentSession, columns, values);
139          }
140       }
141    }
142 }
143
Popular Tags