KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
10 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause.*;
13 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
14 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.*;
15 import com.daffodilwoods.daffodildb.server.sql99.token.*;
16 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
17 import com.daffodilwoods.daffodildb.utils.*;
18 import com.daffodilwoods.daffodildb.utils.field.*;
19 import com.daffodilwoods.database.resource.*;
20 import com.daffodilwoods.database.utility.*;
21
22 public class columndefinition implements tableelement {
23    public dummyrule _Optdummyrule0;
24    public columnconstraintdefinition[] _OptRepcolumnconstraintdefinition1;
25    public SNONRESERVEDWORD136444255 _OptSNONRESERVEDWORD1364442552;
26    public defaultclause _Optdefaultclause3;
27    public dummyrule _Optdummyrule4;
28    public datatypedomainname _datatypedomainname5;
29
30    public identifier _identifier6;
31
32    private TableDescriptor tableDescriptor;
33    private ColumnDescriptor columnDescriptor;
34    private ArrayList referentialConstraintList;
35
36
37    public void setTableDescriptor(_Descriptor tableDes) throws DException {
38       tableDescriptor = (TableDescriptor) tableDes;
39    }
40
41    /** @todo algo for column definition
42     * initialize descriptor, currentsession and globalSession
43     * setColumnName()
44     * a. setTable()
45     * b. checkIfSystemFieldName()
46     * setColumnProperties()
47     * a. setDataTypeDescriptor()
48     * b. setDefaultValue()
49     * c. setAutoIncrement() -- call if autoincrement specified.
50     * add column in table descriptor;
51     * setConstraints()
52     * a. setNotNullConstraint()
53     * b. set non referential constraint and adding referential type of
54     * constraints for setting later.
55     * save the column descriptor;
56     */

57
58    /** @todo in case of domain copy of datatype descriptor is being made
59     * we should use domain in colum descriptor and changes sccordingly in
60     * columncharacterstics or where ever required.*/

61    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.
62
       DException {
63       _ServerSession currentSession = (_ServerSession) object;
64
65       String JavaDoc columnName = (String JavaDoc) _identifier6.run(null);
66       columnDescriptor = new ColumnDescriptor(
67           tableDescriptor.table_catalog, tableDescriptor.table_schema,
68           tableDescriptor.table_name, columnName);
69       /** @todo temporary */
70       columnDescriptor.tableDescriptor = tableDescriptor;
71
72       checkIsSystemField(columnName);
73       setColumnProperties(object, currentSession, columnDescriptor);
74       if (_OptRepcolumnconstraintdefinition1 != null) {
75          setIsNullableColumnValue(columnDescriptor);
76       }
77       columnDescriptor.save(currentSession);
78       if (_OptRepcolumnconstraintdefinition1 != null) {
79          checkConstraintsValidityOnColumnLevel();
80          storeConstraints(currentSession, columnDescriptor);
81       }
82       return columnDescriptor;
83    }
84
85    private void setColumnProperties(Object JavaDoc object, _ServerSession currentSession,
86                                     ColumnDescriptor columnDescriptor) throws
87        DException {
88       setColumnTypeDescriptor(currentSession, columnDescriptor);
89       if (_Optdefaultclause3 != null) {
90          setDefaultClause(object, columnDescriptor);
91       }
92       if (_OptSNONRESERVEDWORD1364442552 != null) {
93          setAutoIncrement(columnDescriptor);
94       }
95       tableDescriptor.addColumnDescriptor(columnDescriptor);
96    }
97
98    public String JavaDoc getColumnName() throws DException {
99       return (String JavaDoc) _identifier6.run(null);
100    }
101
102    /** @todo algo
103     * initialize data type descriptor;
104     * initDataTypeDescriptor() -- catalog, schema, name, type, dtd-identifier
105     * setDataTypeProperties() -- if predefined data type
106     * a. set collate sequence if required
107     * setDomainProperties() -- if domain specified.
108     * a. getDomainDescriptor()
109     * b. validateUserPrivileges()
110     * c. set Data Type as of Domain.
111     * d. set collate sequence if required
112     * */

113    /*
114       getting the type of the column i.e. type of the data
115     */

116    private void setColumnTypeDescriptor(_ServerSession currentSession,
117                                         ColumnDescriptor columnDescriptor) throws
118        DException {
119       DataTypeDescriptor columnTypeDescriptor = new DataTypeDescriptor();
120       columnTypeDescriptor.object_catalog = columnDescriptor.table_catalog;
121       columnTypeDescriptor.object_schema = columnDescriptor.table_schema;
122       columnTypeDescriptor.object_name = columnDescriptor.table_name;
123       columnTypeDescriptor.object_type = SqlKeywords.TABLE;
124       columnTypeDescriptor.dtd_identifier = columnDescriptor.table_name + "."
125           + columnDescriptor.column_name;
126
127       if (_datatypedomainname5 instanceof predefinedtype) {
128          /**
129           * setting data type properties in data type descriptor
130           */

131          _datatypedomainname5.setDescriptor(columnTypeDescriptor);
132          _datatypedomainname5.run(currentSession);
133       } else if (_datatypedomainname5 instanceof domainname) {
134          try {
135             setDomainName(columnDescriptor);
136             DomainDescriptor domainDescriptor = checkDomainPrivilege(currentSession, columnDescriptor);
137             setColumnPropertyFromDomain(columnDescriptor,
138                                         columnTypeDescriptor, domainDescriptor);
139          } catch (DException ex) {
140             throw ex;
141          }
142       }
143       columnDescriptor.dataTypeDescriptor = columnTypeDescriptor;
144    }
145
146    private void setDomainName(ColumnDescriptor columnDescriptor) throws
147        DException {
148       domainname domainName = (domainname) _datatypedomainname5;
149       columnDescriptor.domain_catalog = domainName.getCatalogName();
150       columnDescriptor.domain_schema = domainName.getSchemaName();
151       columnDescriptor.domain_name = domainName.getDomainName();
152       if (columnDescriptor.domain_catalog == null) {
153          columnDescriptor.domain_catalog = columnDescriptor.table_catalog;
154       }
155       if (columnDescriptor.domain_schema == null) {
156          columnDescriptor.domain_schema = columnDescriptor.table_schema;
157       }
158    }
159
160    private DomainDescriptor checkDomainPrivilege(_ServerSession currentSession, ColumnDescriptor columnDescriptor) throws
161        DException {
162       DomainDescriptor domainDescriptor = getDomainDescriptor(columnDescriptor, currentSession);
163       if (! (currentSession.getCurrentUser().equalsIgnoreCase(ServerSystem.browserUser))) {
164          _Executer domainExecuter = ( (DataDictionary) currentSession.getDataDictionary()).getPreparedStatementGetter().getExecuterForDomainValidity();
165          _SelectQueryIterator iter = (_SelectQueryIterator) domainExecuter.executeForFresh(new Object JavaDoc[] {domainDescriptor.catalog_name, domainDescriptor.schema_name, domainDescriptor.domain_name, SqlKeywords.DOMAIN, currentSession.getCurrentUser()});
166          if (!iter.first()) {
167             throw new DException("DSE8188", new Object JavaDoc[] {domainDescriptor.getQualifiedIdentifier()});
168          }
169       }
170       return domainDescriptor;
171    }
172
173    private DomainDescriptor getDomainDescriptor(ColumnDescriptor
174                                                 columnDescriptor,
175                                                 _ServerSession serverSession) throws DException {
176       DomainDescriptor domainDescriptor = new DomainDescriptor();
177       domainDescriptor.catalog_name = columnDescriptor.domain_catalog;
178       domainDescriptor.schema_name = columnDescriptor.domain_schema;
179       domainDescriptor.domain_name = columnDescriptor.domain_name;
180       domainDescriptor.loadWithoutConstraints(serverSession);
181       return domainDescriptor;
182    }
183
184    private void setDefaultClause(Object JavaDoc object, ColumnDescriptor
185                                  columnDescriptor) throws DException {
186       _Optdefaultclause3.setDataTypeDescriptor(columnDescriptor.
187                                                dataTypeDescriptor);
188       columnDescriptor.column_default = (String JavaDoc) _Optdefaultclause3.run(object);
189    }
190
191    private void checkIsSystemField(String JavaDoc columnName) throws DException {
192       if (P.indexOfIgnoreCase(com.daffodilwoods.database.general.SystemFields.
193                               systemFields, columnName) != -1) {
194          throw new DException("DSE1088", new Object JavaDoc[] {columnName});
195       }
196    }
197
198    private void setColumnPropertyFromDomain(ColumnDescriptor columnDescriptor, DataTypeDescriptor columnDTD,
199                                             DomainDescriptor domainDescriptor) throws DException {
200       DataTypeDescriptor domainDTD = domainDescriptor.dataTypeDescriptor;
201       columnDescriptor.column_default = domainDescriptor.default_clause;
202       columnDTD.data_Type = domainDTD.data_Type;
203       columnDTD.character_maximum_length = domainDTD.character_maximum_length;
204       columnDTD.character_octet_length = domainDTD.character_octet_length;
205       columnDTD.interval_precision = domainDTD.interval_precision;
206       columnDTD.interval_type = domainDTD.interval_type;
207       columnDTD.maximum_cardinality = domainDTD.maximum_cardinality;
208       columnDTD.numeric_precision = domainDTD.numeric_precision;
209       columnDTD.numeric_precision_radix = domainDTD.numeric_precision_radix;
210       columnDTD.numeric_scal = domainDTD.numeric_scal;
211       columnDTD.datatime_precision = domainDTD.datatime_precision;
212    }
213
214    private void setAutoIncrement(ColumnDescriptor columnDescriptor) throws
215        DException {
216       if (tableDescriptor.isAutoIncrementColumnPresent()) {
217          throw new DException("DSE5005", null);
218       }
219       if (_Optdefaultclause3 != null) {
220          throw new DException("DSE5006", null);
221       }
222       GeneralUtility.checkValidDataTypeForAutoIncrement(columnDescriptor.getType());
223       columnDescriptor.isAutoIncrement = SqlSchemaConstants.YES;
224       columnDescriptor.isNullable = SqlSchemaConstants.NO;
225       tableDescriptor.setAutoIncrement();
226    }
227
228
229    public String JavaDoc toString() {
230       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
231       sb.append(" ");
232       sb.append(_identifier6);
233       sb.append(" ");
234       sb.append(_datatypedomainname5);
235       sb.append(" ");
236       if (_Optdummyrule4 != null) {
237          sb.append(_Optdummyrule4);
238       }
239       sb.append(" ");
240       if (_Optdefaultclause3 != null) {
241          sb.append(_Optdefaultclause3);
242       }
243       sb.append(" ");
244       if (_OptSNONRESERVEDWORD1364442552 != null) {
245          sb.append(_OptSNONRESERVEDWORD1364442552);
246       }
247       sb.append(" ");
248       if (_OptRepcolumnconstraintdefinition1 != null) {
249          for (int i = 0; i < _OptRepcolumnconstraintdefinition1.length; i++)
250             sb.append("").append(_OptRepcolumnconstraintdefinition1[i]);
251       }
252       sb.append(" ");
253       if (_Optdummyrule0 != null) {
254          sb.append(_Optdummyrule0);
255       }
256       return sb.toString();
257    }
258
259    public void validateCheckConstraintSemantic(_ServerSession currentSession) throws
260        DException {
261       if (_OptRepcolumnconstraintdefinition1 != null) {
262          for (int i = 0, size = _OptRepcolumnconstraintdefinition1.length;
263               i < size; i++) {
264             columnconstraint temp = _OptRepcolumnconstraintdefinition1[i].
265                 _columnconstraint1;
266             if (temp instanceof checkconstraintdefinition)
267                ( (checkconstraintdefinition) temp).validateCheckConstraintSemantic(
268                    currentSession);
269          }
270       }
271    }
272
273    /**
274     * @param currentSession
275     * @param columnDescriptor descriptor of the newly addded column
276     * @throws DException
277     * Used when a column is added .Check the semantic of the checkconstraint applied and verifes it
278     * acc. to data present the existing table
279     */

280
281    public void validateCheckConstraintSemanticAndVerify(_ServerSession
282        currentSession, ColumnDescriptor columnDescriptor) throws DException {
283       if (_OptRepcolumnconstraintdefinition1 != null) {
284          for (int i = 0, size = _OptRepcolumnconstraintdefinition1.length;
285               i < size; i++) {
286             columnconstraint temp = _OptRepcolumnconstraintdefinition1[i].
287                 _columnconstraint1;
288             if (temp instanceof checkconstraintdefinition) {
289                checkconstraintdefinition checkCons = (checkconstraintdefinition)temp;
290                checkCons.validateCheckConstraintSemanticForAddColumn(currentSession, columnDescriptor);
291                TableDetails tableDetail = new TableDetails();
292                tableDetail.setTableName(tableDescriptor.getQualifiedTableName().getTableName());
293                _Iterator iter = SqlSchemaConstants.getIterator(currentSession,
294                    " select * from " +
295                    tableDescriptor.getQualifiedTableName().getIdentifier(), null);
296                if (iter.first()) {
297                   booleanvalueexpression condition = checkCons._searchcondition1.
298                       _searchcondition0;
299                   _Reference[] ref = checkCons.ref;
300                   _Reference[] upperRef = condition.getReferences(new TableDetails[] {
301                       tableDetail});
302                   if (columnDescriptor.column_default != null&&!columnDescriptor.column_default.equalsIgnoreCase("null")) {
303 /**
304                       * Add following three lines by harvinder related to bug 12292. */

305                      Object JavaDoc defaultOption = _Optdefaultclause3._defaultoption0.run( (Object JavaDoc) currentSession);
306                      Object JavaDoc obj = ( (FieldBase) defaultOption).getObject();
307                      FieldBase fb1 = FieldUtility.getFieldForVariable(obj.toString());
308
309                      FieldBase fb = FieldUtility.convertToAppropriateType(fb1, columnDescriptor.dataTypeDescriptor.getType(),
310                          columnDescriptor.dataTypeDescriptor.getPrecision(), null);
311                      ArrayList allRefs = new ArrayList();
312                      if (upperRef != null)
313                         for (int j = 0; j < upperRef.length; j++)
314                            if (upperRef[j].getReferenceType() ==
315                                SimpleConstants.SUBQUERY) {
316                               _Iterator selectIterator = ( (subquery) upperRef[j]).
317                                   getSelectIterator(currentSession);
318                               allRefs.add(new Object JavaDoc[] {upperRef[j], selectIterator});
319                            }
320                      boolean refFound = allRefs.size() > 0;
321                      do {
322                         _VariableValues vv = null;
323                         if (refFound) {
324                            vv = new CheckConstraintVariableValues(iter, currentSession,
325                                (Object JavaDoc[][]) allRefs.toArray(new Object JavaDoc[0][0]), fb);
326                            Object JavaDoc[] values = new Object JavaDoc[ref.length];
327                            for (int j = 0; j < values.length; j++)
328                               values[j] = iter.getColumnValues(ref[j]);
329                            vv.setConditionVariableValue(ref, values, 1);
330                         } else {
331                            vv = new CheckConstraintVariableValues(iter, currentSession, null,
332                                fb);
333                         }
334                         int eval = condition.run(vv).hashCode();
335                         if (eval != 0) {
336                            throw new DException("DSE8115", null);
337                         }
338                      } while (iter.next());
339                   }
340                }
341             }
342          }
343       }
344    }
345
346    private void storeConstraints(_ServerSession currentSession,
347                                  ColumnDescriptor columnDescriptor) throws
348        DException {
349       /** @todo change variable name -- referentialConstraint
350        * verify if a column can reference more than one table;
351        * if not then throw exception if references more than one table.
352        */

353       referentialConstraintList = new ArrayList(2);
354       for (int i = 0, size = _OptRepcolumnconstraintdefinition1.length; i < size;
355            i++) {
356          _OptRepcolumnconstraintdefinition1[i].setColumnDescriptor(
357              columnDescriptor);
358          if (! (_OptRepcolumnconstraintdefinition1[i]._columnconstraint1 instanceof
359                 referencesspecification))
360             _OptRepcolumnconstraintdefinition1[i].run(currentSession);
361          else
362             referentialConstraintList.add(_OptRepcolumnconstraintdefinition1[i]);
363       }
364    }
365
366    private void checkConstraintsValidityOnColumnLevel() throws DException {
367       int primaryConstraint, uniqueConstraint, notNullConstraint;
368       primaryConstraint = uniqueConstraint = notNullConstraint = 0;
369       for (int i = 0, size = _OptRepcolumnconstraintdefinition1.length; i < size; i++) {
370          if (_OptRepcolumnconstraintdefinition1[i]._columnconstraint1 instanceof
371              SRESERVEDWORD1206543922SRESERVEDWORD1206543922) {
372             String JavaDoc type = (String JavaDoc) _OptRepcolumnconstraintdefinition1[i]._columnconstraint1.run(null);
373             if (type.equalsIgnoreCase(SqlKeywords.PRIMARY + " " + SqlKeywords.KEY)) {
374                primaryConstraint++;
375             } else {
376                notNullConstraint++;
377             }
378          } else if (_OptRepcolumnconstraintdefinition1[i]._columnconstraint1 instanceof uniquespecification) {
379             uniqueConstraint++;
380          }
381       }
382       if (primaryConstraint > 1)
383          throw new DException("DSE8154", new Object JavaDoc[] {columnDescriptor.column_name});
384       if (uniqueConstraint > 1)
385          throw new DException("DSE8155", new Object JavaDoc[] {columnDescriptor.column_name});
386       if (notNullConstraint > 1)
387          throw new DException("DSE8156", new Object JavaDoc[] {columnDescriptor.column_name});
388       if ( (primaryConstraint == 1 & uniqueConstraint > 0) || (uniqueConstraint == 1 & primaryConstraint > 0))
389          throw new DException("DSE8157", new Object JavaDoc[] {columnDescriptor.column_name});
390    }
391
392    /** @todo
393     * if a column can reference a single table then no need for loop.
394     * 1. simply store the foreign constaint.
395     * */

396    /* for storing foreign constraints */
397    public void setReferentialConstraints(_ServerSession serverSession) throws
398        DException {
399       if (referentialConstraintList != null)
400          for (int i = 0, size = referentialConstraintList.size(); i < size; i++) {
401             columnconstraintdefinition columnConstraint = (
402                 columnconstraintdefinition) referentialConstraintList.get(i);
403             columnConstraint.run(serverSession);
404          }
405    }
406
407    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
408       return this;
409    }
410
411    private void setIsNullableColumnValue(ColumnDescriptor columnDescriptor) throws
412        DException {
413       for (int i = 0, size = _OptRepcolumnconstraintdefinition1.length; i < size;
414            i++) {
415          columnconstraintdefinition columnConstraint = (columnconstraintdefinition)
416              _OptRepcolumnconstraintdefinition1[i];
417          if (columnConstraint._columnconstraint1 instanceof
418              SRESERVEDWORD1206543922SRESERVEDWORD1206543922) {
419             columnDescriptor.isNullable = SqlKeywords.NO;
420          }
421       }
422    }
423 }
424
Popular Tags