KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition;
2
3 import java.math.*;
4 import java.util.*;
5
6 import com.daffodilwoods.daffodildb.server.serversystem.*;
7 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause.*;
9 import com.daffodilwoods.daffodildb.server.sql99.token.*;
10 import com.daffodilwoods.database.resource.*;
11 import java.math.BigInteger JavaDoc;
12
13 public class sequencedefinition implements SQLschemadefinitionstatement {
14    public initializesequenceOptRepinitializesequence _OptinitializesequenceOptRepinitializesequence0;
15    public localorschemaqualifiedname _localorschemaqualifiedname1;
16    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439222;
17    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223;
18
19    /** @todo algo
20     * 1. initialise currentUserSesion and globalSession
21     * 2. getSequenceName
22     * 3. checkAccessRights
23     * 4. calculate other details e.g starting value, max value, iscycle etc.
24     * 5. save descriptor */

25
26    /** @todo AccessRights Checking is missing in this statement and
27     * what about the privileges of this object */

28    public Object JavaDoc run(Object JavaDoc object) throws DException {
29       _ServerSession currentSession = (_ServerSession) object;
30       SequenceNumberDescriptor sequenceDescriptor = new
31           SequenceNumberDescriptor();
32       getSequenceName(currentSession, sequenceDescriptor);
33       validateUserPrivilege(currentSession, sequenceDescriptor);
34       storeDetails(sequenceDescriptor);
35       checkValuesValidity(sequenceDescriptor);
36       sequenceDescriptor.save(currentSession);
37       return null;
38    }
39
40    private void getSequenceName(_ServerSession serverSession,
41                                 SequenceNumberDescriptor sequenceDescriptor) throws
42        DException {
43       sequenceDescriptor.sequence_catalog = _localorschemaqualifiedname1.
44           getCatalogName();
45       sequenceDescriptor.sequence_schema = _localorschemaqualifiedname1.
46           getSchemaName();
47       if (sequenceDescriptor.sequence_catalog == null) {
48          sequenceDescriptor.sequence_catalog = serverSession.getCurrentCatalog();
49       }
50       if (sequenceDescriptor.sequence_schema == null) {
51          sequenceDescriptor.sequence_schema = serverSession.getCurrentSchema();
52       }
53       sequenceDescriptor.sequence_name = _localorschemaqualifiedname1.
54           getIdentifierName();
55    }
56
57    private void validateUserPrivilege(_ServerSession currentSession,
58                                       SequenceNumberDescriptor seqDes) throws
59        DException {
60       SchemaDescriptor schemaDescriptor = new SchemaDescriptor();
61       schemaDescriptor.catalog_name = seqDes.sequence_catalog;
62       schemaDescriptor.schema_name = seqDes.sequence_schema;
63       schemaDescriptor.load(currentSession);
64       String JavaDoc authorizationIdentifier = schemaDescriptor.schema_owner;
65       if (!currentSession.isEnabledAuthorizationIdentifier(
66           authorizationIdentifier, true)) {
67          throw new DException("DSE12", null);
68       }
69    }
70
71    /* stores the deletails of the sequencedefinition in the SequenceNumberDescriptor*/
72    private void storeDetails(SequenceNumberDescriptor sequenceDescriptor) throws
73        DException {
74       if (_OptinitializesequenceOptRepinitializesequence0 == null) {
75          storeDefaultDetails(sequenceDescriptor);
76       } else {
77          boolean[] optionsInitialized = new boolean[6];
78          executeOptions(sequenceDescriptor, optionsInitialized);
79          intializeOtherDetailsWithDefault(sequenceDescriptor, optionsInitialized);
80       }
81    }
82
83    /* - executes the OptsequencestarterunsignedintegermaxvaluesequenceminvaluesequencecycleinsequencesequenceorderRepOptsequencestarterunsignedintegermaxvaluesequenceminvaluesequencecycleinsequencesequenceorder0
84        - gets All the options, checks for their duplicacy
85          and initializes them in the sequence Descriptor
86        - if any option reoccurs it throws exception
87     */

88    private void executeOptions(SequenceNumberDescriptor sequenceDescriptor,
89                                boolean[] optionsInitialized) throws DException {
90       ArrayList optionList = (ArrayList)
91           _OptinitializesequenceOptRepinitializesequence0.run(null);
92       for (int i = 0; i < optionList.size(); i++) {
93          initializesequence optionalValue = (initializesequence) optionList.get(
94              i);
95          if (optionalValue instanceof sequencestartersuinteger) {
96             sequencestartersuinteger _sequenceStarterUnsignedInteger = (
97                 sequencestartersuinteger) optionalValue;
98             String JavaDoc nameOfSequenceStarter = (String JavaDoc)
99                 _sequenceStarterUnsignedInteger._sequencestarter1.run(null);
100             if (nameOfSequenceStarter.equalsIgnoreCase("Start With")) {
101                if (optionsInitialized[0]) {
102                   throw new DException("DSE5507", new Object JavaDoc[] {"start with"});
103                }
104                sequenceDescriptor.start_with = ( (BigDecimal)
105                                                 _sequenceStarterUnsignedInteger.
106                                                 run(null)).longValue();
107                optionsInitialized[0] = true;
108             } else {
109                if (optionsInitialized[3]) {
110                   throw new DException("DSE5507", new Object JavaDoc[] {"increment by"});
111                }
112                sequenceDescriptor.increment_by = ( (BigDecimal)
113                                                   _sequenceStarterUnsignedInteger.
114                                                   run(null)).longValue();
115                if (sequenceDescriptor.increment_by == 0) {
116                   throw new DException("DSE5508", null);
117                }
118                optionsInitialized[3] = true;
119             }
120          } else if (optionalValue instanceof SNONRESERVEDWORD136444255suinteger) {
121             SNONRESERVEDWORD136444255suinteger _maxMinValueSequence = (
122                 SNONRESERVEDWORD136444255suinteger) optionalValue;
123             String JavaDoc maxMinSequence = (String JavaDoc) _maxMinValueSequence.
124                 _SNONRESERVEDWORD1364442551.run(null);
125             BigDecimal maxMinVal = (BigDecimal) _maxMinValueSequence.run(null);
126             if (maxMinSequence.equalsIgnoreCase("MINVALUE")) {
127                if (optionsInitialized[1]) {
128                   throw new DException("DSE5507", new Object JavaDoc[] {"minvalue/nominvalue"});
129                }
130                BigDecimal minLimit = new BigDecimal("" + Long.MIN_VALUE);
131
132                if (maxMinVal.compareTo(minLimit) == -1) {
133                   throw new DException("DSE5509", new Object JavaDoc[] {minLimit});
134                }
135                sequenceDescriptor.min_value = maxMinVal.longValue();
136                optionsInitialized[1] = true;
137             } else {
138                if (optionsInitialized[2]) {
139                   throw new DException("DSE5507", new Object JavaDoc[] {"maxvalue/nomaxvalue"});
140                }
141                BigDecimal maxLimit = new BigDecimal("" + Long.MAX_VALUE);
142                if (maxMinVal.compareTo(maxLimit) == 1) {
143                   throw new DException("DSE5510", new Object JavaDoc[] {maxLimit});
144                }
145                sequenceDescriptor.max_value = maxMinVal.longValue();
146                optionsInitialized[2] = true;
147             }
148          } else if (optionalValue instanceof SNONRESERVEDWORD136444255) {
149             String JavaDoc optionNonReservedWord = (String JavaDoc) ( (SNONRESERVEDWORD136444255)
150                 optionalValue).run(null);
151             if (optionNonReservedWord.equalsIgnoreCase("NOMINVALUE")) {
152                if (optionsInitialized[1]) {
153                   throw new DException("DSE5507", new Object JavaDoc[] {"minvalue/nominvalue"});
154                }
155                sequenceDescriptor.min_value = Long.MIN_VALUE;
156                optionsInitialized[1] = true;
157             } else if (optionNonReservedWord.equalsIgnoreCase("NOMAXVALUE")) {
158                if (optionsInitialized[2]) {
159                   throw new DException("DSE5507", new Object JavaDoc[] {"maxvalue/nomaxvalue"});
160                }
161                sequenceDescriptor.max_value = Long.MAX_VALUE;
162                optionsInitialized[2] = true;
163             } else if (optionNonReservedWord.equalsIgnoreCase("NOCYCLE")) {
164                if (optionsInitialized[4]) {
165                   throw new DException("DSE5507", new Object JavaDoc[] {"cycle/nocycle"});
166                }
167                sequenceDescriptor.cycle_in_sequence = false;
168                optionsInitialized[4] = true;
169             } else {
170                throw new DException("DSE565", new Object JavaDoc[] {"noorder"});
171                /** @todo */
172                /* if(optionsInitialized[5])
173                  throw new DException ("DSE5507",new Object[]{"ORDER"}) ;
174                                     sequenceDescriptor.sequence_order = false;
175                                     optionsInitialized[5] = true;*/

176             }
177          } else if (optionalValue instanceof SRESERVEDWORD1206543922) {
178             String JavaDoc optReservedWord = (String JavaDoc) ( (SRESERVEDWORD1206543922)
179                                                optionalValue).run(null);
180             if (optReservedWord.equalsIgnoreCase("CYCLE")) {
181                if (optionsInitialized[4]) {
182                   throw new DException("DSE5507", new Object JavaDoc[] {"cycle/nocycle"});
183                }
184
185                sequenceDescriptor.cycle_in_sequence = true;
186                optionsInitialized[4] = true;
187             } else {
188                throw new DException("DSE565", new Object JavaDoc[] {"order"});
189                /** @todo */
190                /*if(optionsInitialized[5])
191                     throw new DException ("DSE5507",new Object[]{"ORDER"});
192                   sequenceDescriptor.sequence_order = true;
193                   optionsInitialized[5] = true;*/

194             }
195          }
196       }
197    }
198
199    private void intializeOtherDetailsWithDefault(SequenceNumberDescriptor
200                                                  sequenceDescriptor,
201                                                  boolean[] optionsInitialized) throws
202        DException {
203       if (!optionsInitialized[1]) {
204          sequenceDescriptor.min_value = 1;
205       }
206       if (!optionsInitialized[2]) {
207          sequenceDescriptor.max_value = Long.MAX_VALUE;
208       }
209       if (!optionsInitialized[5]) {
210          sequenceDescriptor.sequence_order = true;
211       }
212       if (!optionsInitialized[3]) {
213          sequenceDescriptor.increment_by = 1;
214       }
215       if (!optionsInitialized[0]) {
216          sequenceDescriptor.start_with = sequenceDescriptor.increment_by > 0 ?
217              sequenceDescriptor.min_value
218              : sequenceDescriptor.max_value;
219       }
220       if (!optionsInitialized[4]) {
221          sequenceDescriptor.cycle_in_sequence = false;
222       }
223    }
224
225    private void checkValuesValidity(SequenceNumberDescriptor sequenceDescriptor) throws
226        DException {
227       if (sequenceDescriptor.max_value < sequenceDescriptor.min_value) {
228          throw new DException("DSE5512", null);
229       }
230       if (! (sequenceDescriptor.min_value <= sequenceDescriptor.start_with)) {
231          throw new DException("DSE5525", null);
232       }
233       if (! (sequenceDescriptor.max_value >= sequenceDescriptor.start_with)) {
234          throw new DException("DSE5526", null);
235       }
236       if ( (new BigInteger JavaDoc(String.valueOf(sequenceDescriptor.increment_by)).abs()).compareTo(
237           (new BigInteger JavaDoc(String.valueOf(sequenceDescriptor.max_value)).subtract(new BigInteger JavaDoc(String.valueOf(sequenceDescriptor.min_value))))) > 0) {
238          throw new DException("DSE5514", null);
239       }
240    }
241
242    /* intializes the sequenceDescriptor details with default values */
243    private void storeDefaultDetails(SequenceNumberDescriptor sequenceDescriptor) throws
244        DException {
245       sequenceDescriptor.start_with = 1;
246       sequenceDescriptor.min_value = 1;
247       sequenceDescriptor.max_value = Long.MAX_VALUE;
248       sequenceDescriptor.increment_by = 1;
249       sequenceDescriptor.sequence_order = true;
250       sequenceDescriptor.cycle_in_sequence = false;
251    }
252
253    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
254       return this;
255    }
256
257    public String JavaDoc toString() {
258       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
259       sb.append(" ");
260       sb.append(_SRESERVEDWORD12065439223);
261       sb.append(" ");
262       sb.append(_SRESERVEDWORD12065439222);
263       sb.append(" ");
264       sb.append(_localorschemaqualifiedname1);
265       sb.append(" ");
266       if (_OptinitializesequenceOptRepinitializesequence0 != null) {
267          sb.append(_OptinitializesequenceOptRepinitializesequence0);
268       }
269       return sb.toString();
270    }
271 }
272
Popular Tags