KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datadictionarysystem > SequenceGenerator


1 package com.daffodilwoods.daffodildb.server.datadictionarysystem;
2
3 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator;
4
5 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.predicate;
6
7
8 import com.daffodilwoods.daffodildb.server.sql99.utils.parser.ConditionParser;
9
10  import com.daffodilwoods.database.resource.*;
11  import com.daffodilwoods.daffodildb.server.serversystem.*;
12  import com.daffodilwoods.database.sqlinitiator.*;
13  import com.daffodilwoods.database.general.*;
14  import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
15  import java.util.*;
16 import com.daffodilwoods.daffodildb.server.sql99.utils.VariableValues;
17 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.booleanvalueexpression;
18 import com.daffodilwoods.daffodildb.server.sql99.common.TableDetails;
19 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
20 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.ConditionSingleTableExecuter;
21 import com.daffodilwoods.daffodildb.utils.FieldUtility;
22 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
23 public class SequenceGenerator implements _SequenceGenerator{
24
25   private _ServerSession connection;
26   private boolean cycle;
27   private boolean order;
28   private long minValue, maxValue;
29   private long startWith, increment;
30   private Long JavaDoc currentValue;
31   private String JavaDoc sequence_catalog ;
32   private String JavaDoc sequence_schema ;
33   private String JavaDoc sequence_name ;
34   _Iterator iterator;
35
36
37   public SequenceGenerator(_Sequence sequenceInformation, QualifiedIdentifier sequenceIdentifier) {
38       cycle = sequenceInformation.getCycleInSequence();
39       order = sequenceInformation.getSequenceOrder();
40       maxValue = sequenceInformation.getMaxValue();
41       minValue = sequenceInformation.getMinValue();
42       startWith = sequenceInformation.getStartWith();
43       increment = sequenceInformation.getIncrementBy();
44       currentValue = sequenceInformation.getLastValue();
45       sequence_catalog = sequenceIdentifier.catalog;
46       sequence_schema = sequenceIdentifier.schema;
47       sequence_name = sequenceIdentifier.name;
48
49  }
50   public FieldBase getCurrent()throws DException{
51     if(currentValue == null){
52       currentValue = new Long JavaDoc(startWith) ;
53       return FieldUtility.getField( new Long JavaDoc(startWith));
54     }else{
55
56    return FieldUtility.getField( currentValue);
57     }
58
59   }
60   public synchronized FieldBase getNext()throws DException{
61         currentValue = new Long JavaDoc(ascendingValue());
62         _ServerTable serverTable = connection.getServerTable(SystemTables.sequence_number_TableName);
63         if(iterator == null){
64             String JavaDoc bve = "sequence_catalog = ? and sequence_schema = ? and sequence_name = ?";
65             booleanvalueexpression condition = ConditionParser.parseCondition(bve,connection, SqlSchemaConstants.sequence_number_TableName);
66             TableDetails tableDetail = new TableDetails();
67             tableDetail.setTableName(SqlSchemaConstants.sequence_number_TableName.getTableName());
68             ConditionSingleTableExecuter conSingTE = new ConditionSingleTableExecuter(null,tableDetail,connection,condition,null);
69             iterator = connection.getInternalIterator(SqlSchemaConstants.sequence_number_TableName,conSingTE );
70            _Reference[] ref = null;
71             Object JavaDoc[] val = null;
72             ref = condition.getReferences(new TableDetails[]{tableDetail});
73             val = FieldUtility.getFields( new Object JavaDoc[]{sequence_catalog, sequence_schema, sequence_name});
74             iterator.setConditionVariableValue(ref, val, 1);
75         }
76         iterator.first();
77         FieldBase fb = FieldUtility.getField( currentValue);
78         serverTable.update( iterator , new int[]{SystemFields.systemFieldsCount + SystemTablesFields.sequence_number_last_value}, new FieldBase[]{fb });
79         connection.commit();
80         return fb;
81   }
82
83   void setConenction(_Connection connection){
84     this.connection = ((Connection)connection).getServerSession();
85   }
86
87
88
89   private long ascendingValue() throws DException {
90
91     return increment > 0 ? checkForAscending() : checkForDescending();
92
93       }
94
95 /*method ends*/
96
97       private long checkForAscending() throws DException {
98         long temp = 0;
99         if (currentValue == null)
100           return startWith;
101         else
102           temp = currentValue.longValue();
103
104         if((temp + increment > maxValue || temp + increment < startWith) && !cycle){
105            throw new DException("DSE5504", new Object JavaDoc[] {sequence_name});
106
107          }
108
109          if((temp + increment > maxValue || temp + increment < temp) && cycle){
110            return minValue;
111         }
112
113
114         long newVal = currentValue == null ? startWith :currentValue.longValue() + increment;
115            return newVal;
116       }
117
118       private long checkForDescending() throws DException {
119         long temp = 0;
120         if (currentValue == null)
121           return startWith;
122         else
123           temp = currentValue.longValue();
124
125         if((temp + increment < minValue || temp + increment > startWith) && !cycle){
126         throw new DException("DSE5505", new Object JavaDoc[] {sequence_name});
127
128       }
129
130       if((temp + increment < minValue || temp + increment > temp ) && cycle){
131         return maxValue;
132      }
133
134
135         long newVal = currentValue == null ? startWith :currentValue.longValue() + increment;
136
137          return newVal;
138
139       }
140 }
141
142   /*method for getting the incremented value when order is ascending*/
143   /* private long ascendingValue() throws DException{
144
145                   long newVal = currentValue == null ? startWith : currentValue.longValue() + increment;
146                   return increment > 0 ? checkForAscending(newVal) : checkForDescending(newVal);
147
148          } */

149
150
151
152
153
154
Popular Tags