KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition;
2
3 import com.daffodilwoods.daffodildb.server.sql99.common.*;
4 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
5 import com.daffodilwoods.daffodildb.server.sql99.expression.datetimevalueexpression.*;
6 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.*;
7 import com.daffodilwoods.daffodildb.server.sql99.token.*;
8 import com.daffodilwoods.daffodildb.utils.*;
9 import com.daffodilwoods.daffodildb.utils.field.*;
10 import com.daffodilwoods.database.resource.*;
11
12 /**
13  * default clause can be contained in
14  * 1. <column definition>,
15  * 2. <domain definition>,
16  * 3. <attribute definition>,
17  * 4. <alter column definition>, or
18  * 5. <alter domain statement>
19  */

20
21 public class defaultclause {
22    public defaultoption _defaultoption0;
23    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439221;
24    private DataTypeDescriptor dataTypeDescriptor;
25
26    public void setDataTypeDescriptor(_Descriptor dtd) throws DException {
27       dataTypeDescriptor = (DataTypeDescriptor) dtd;
28    }
29
30    public Object JavaDoc run(Object JavaDoc object) throws DException {
31       String JavaDoc dataType = dataTypeDescriptor.data_Type;
32       if (_defaultoption0 instanceof SRESERVEDWORD1206543922) {
33          checkKeyWords(dataType);
34       } else if (_defaultoption0 instanceof literal) {
35          checkLiteralType(getdefaultExpression(object));
36       } else if (_defaultoption0 instanceof datetimevaluefunction) {
37          checkDateTimeValueFunction();
38       }
39       return _defaultoption0.toString();
40    }
41
42    private String JavaDoc getdefaultExpression(Object JavaDoc object) throws DException {
43       Object JavaDoc defaultOption = null;
44       try {
45          defaultOption = _defaultoption0.run(object);
46       } catch (DException ex) {
47          throw new DException("DSE8149", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier});
48       }
49       Object JavaDoc obj = ( (FieldBase) defaultOption).getObject();
50       return defaultOption == null ? "null" :
51           obj == null ? "null" : obj.toString();
52    }
53
54    private void checkKeyWords(String JavaDoc dataType) throws DException {
55       String JavaDoc defaultExpression = _defaultoption0.toString();
56       if (defaultExpression.equalsIgnoreCase(SqlKeywords.SESSION_USER)) {
57          throw new DException("DSE16", new Object JavaDoc[] {defaultExpression});
58       }
59       int character_length = dataTypeDescriptor.getCharacterLength();
60       if (defaultExpression.equalsIgnoreCase(SqlKeywords.CURRENT_USER) ||
61           defaultExpression.equalsIgnoreCase(SqlKeywords.CURRENT_ROLE) ||
62           defaultExpression.equalsIgnoreCase(SqlKeywords.SESSION_USER) ||
63           defaultExpression.equalsIgnoreCase(SqlKeywords.SYSTEM_USER) ||
64           defaultExpression.equalsIgnoreCase(SqlKeywords.USER)) {
65          if (!dataTypeDescriptor.isCharacterType()) { // with character set SQl_Identifier
66
throw new DException("DSE973", new Object JavaDoc[] {dataType});
67          }
68          if (character_length < 128) {
69             throw new DException("DSE975", new Object JavaDoc[] {
70                                  dataTypeDescriptor.character_maximum_length, dataTypeDescriptor.dtd_identifier});
71          }
72       } else if (defaultExpression.equalsIgnoreCase(SqlKeywords.CURRENT_PATH)) {
73          if (!dataTypeDescriptor.isCharacterType()) { // with character set SQl_Identifier
74
throw new DException("DSE973", new Object JavaDoc[] {dataType});
75          }
76          if (character_length < 1031) {
77             throw new DException("DSE8144", new Object JavaDoc[] {
78                                  dataTypeDescriptor.dtd_identifier});
79          }
80       } else if (defaultExpression.equalsIgnoreCase(SqlKeywords.CURRENT_DATE)) {
81          if (!dataTypeDescriptor.isDateType()) {
82             throw new DException("DSE973", new Object JavaDoc[] {dataType});
83          }
84       }
85    }
86
87    private void checkLiteralType(String JavaDoc defaultExpression) throws
88        DException {
89       int literalLength = defaultExpression.length();
90       if (dataTypeDescriptor.isBitType() || dataTypeDescriptor.isBitVaryingType()) {
91          checkCharacterType(literalLength);
92       } else if (dataTypeDescriptor.isBinaryType()) {
93          checkCharacterType(literalLength);
94       } else if (dataTypeDescriptor.isCharacterType()) {
95          checkCharacterType(literalLength);
96       } else if (dataTypeDescriptor.isNumericType()) {
97          checkNumericType();
98       } else if (dataTypeDescriptor.isDateTimeType()) {
99          checkDateTimeType();
100       } else if (dataTypeDescriptor.isBooleanType()) {
101          if (! (_defaultoption0 instanceof booleanliteral)) {
102             throw new DException("DSE8018", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier});
103          }
104       } else {
105          throw new DException("DSE8029", new Object JavaDoc[] {dataTypeDescriptor.data_Type});
106       }
107       try {
108          FieldUtility.convertToAppropriateType(FieldUtility.setFieldLiteralBufferRange(_defaultoption0.run(null)), dataTypeDescriptor.getType(), dataTypeDescriptor.getPrecision(), dataTypeDescriptor.getScale(), null);
109       } catch (DException ex) {
110          if (ex.getDseCode().equalsIgnoreCase("DSE5546")) {
111             throw new DException("DSE8148", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier});
112          }
113          throw ex;
114       }
115    }
116
117    private void checkDateTimeType() throws DException {
118       if (dataTypeDescriptor.isDateType()) {
119          if (! (_defaultoption0 instanceof dateliteral)) {
120             throw new DException("DSE8017", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier}); //Invalid date value
121
}
122       }
123       if (dataTypeDescriptor.isTimeType()) {
124          if (! (_defaultoption0 instanceof timeliteral)) {
125             throw new DException("DSE8146", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier}); //Invalid time value
126
}
127       }
128       if (dataTypeDescriptor.isTimestampType()) {
129          if (! (_defaultoption0 instanceof timestampliteral)) {
130             throw new DException("DSE8147", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier}); //Invalid timestamp value
131
}
132       }
133       if (! (_defaultoption0 instanceof datetimeliteral)) {
134          throw new DException("DSE8017", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier}); //Invalid date time value
135
}
136    }
137
138    private void checkNumericType() throws DException {
139       if (dataTypeDescriptor.isApproximateNumericType()) {
140          if (! (_defaultoption0 instanceof signednumericliteral ||
141
142                   _defaultoption0 instanceof unsignednumericliteral)) {
143
144             throw new DException("DSE8009", null); //Invalid number
145
}
146       } else if (! (_defaultoption0 instanceof signednumericliteral ||
147                     _defaultoption0 instanceof unsignedinteger ||
148                     _defaultoption0 instanceof exactnumericliteral)) {
149          if (_defaultoption0 instanceof SRESERVEDWORD1206543922) {
150             new Exception JavaDoc("[" + _defaultoption0 + "] [" + _defaultoption0.run(null) + "]").printStackTrace();
151          }
152          throw new DException("DSE8009", null); //Invalid number
153
}
154    }
155
156    private void checkCharacterType(int literalLength) throws DException {
157       if (! (_defaultoption0 instanceof characterstringliteral)) {
158          throw new DException("DSE8016", null);
159       }
160       checkCharacterStringLength(literalLength);
161    }
162
163    private void checkCharacterStringLength(int literalLength) throws DException {
164       if (literalLength > dataTypeDescriptor.character_maximum_length.intValue()) {
165          throw new DException("DSE8019", new Object JavaDoc[] {dataTypeDescriptor.dtd_identifier});
166       } else if (! (literalLength > dataTypeDescriptor.character_maximum_length.intValue())) {
167          if (literalLength > 1024) {
168             throw new DException("DSE8165", null);
169          }
170       } // Changes done by harvinder over
171
}
172
173    private void checkDateTimeValueFunction() throws DException {
174       datetimevaluefunction _dateTimeValueFunction = (datetimevaluefunction)
175           _defaultoption0;
176       if (dataTypeDescriptor.isDateType()) {
177          if (_dateTimeValueFunction.getFunctionType() !=
178              datetimevaluefunction.DATEFUNCTION) {
179             throw new DException("DSE8029", new Object JavaDoc[] {dataTypeDescriptor.data_Type});
180          }
181       } else if (dataTypeDescriptor.isTimeType()) {
182          if (_dateTimeValueFunction.getFunctionType() !=
183              datetimevaluefunction.TIMEFUNCTION) {
184             throw new DException("DSE8029", new Object JavaDoc[] {dataTypeDescriptor.data_Type});
185          }
186       } else if (dataTypeDescriptor.isTimestampType()) {
187          if (_dateTimeValueFunction.getFunctionType() !=
188              datetimevaluefunction.TIMESTAMPFUNCTION) {
189             throw new DException("DSE8029", new Object JavaDoc[] {dataTypeDescriptor.data_Type});
190          }
191       } else if (dataTypeDescriptor.isCharacterType()) {
192          if (_dateTimeValueFunction.getFunctionType() !=
193              datetimevaluefunction.VARCHARFUNCTION) {
194             throw new DException("DSE8029", new Object JavaDoc[] {dataTypeDescriptor.data_Type});
195          }
196       } else if (dataTypeDescriptor.isNumericType()) {
197          if (_dateTimeValueFunction.getFunctionType() !=
198              datetimevaluefunction.INTEGERFUNCTION) {
199             throw new DException("DSE8029", new Object JavaDoc[] {dataTypeDescriptor.data_Type});
200          }
201       } else {
202          throw new DException("DSE8029", new Object JavaDoc[] {dataTypeDescriptor.data_Type});
203       }
204    }
205
206    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
207       return this;
208    }
209
210    public String JavaDoc toString() {
211       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
212       sb.append(" ");
213       sb.append(_SRESERVEDWORD12065439221);
214       sb.append(" ");
215       sb.append(_defaultoption0);
216       return sb.toString();
217    }
218 }
219
Popular Tags