KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > AutoincrementCounter


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.AutoincrementCounter
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23 import org.apache.derby.iapi.services.sanity.SanityManager;
24 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
25 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
26 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
27 import org.apache.derby.iapi.store.access.TransactionController;
28 import org.apache.derby.iapi.error.StandardException;
29 import org.apache.derby.catalog.UUID;
30 import org.apache.derby.iapi.reference.SQLState;
31
32
33 /**
34  * AutoincrementCounter is a not so general counter for the specific purposes
35  * of autoincrement columns. It can be thought of as an in-memory autoincrement
36  * column.
37  * The counting or incrementing is done in fashion identical to the
38  * AUTOINCREMENTVALUE in SYSCOLUMNS.
39  * <p>
40  * To create a counter, the user must call the constructor with a start value,
41  * increment and optionally a final value. In addition the caller must specify
42  * the schema name, table name and column name uniquely identifying the
43  * counter.
44  * <p>
45  * When a counter is created it is in an invalid state-- to initialize it, the
46  * user must call either <i>update</i> or <i>reset(false)</i>. The value of a
47  * counter can be changed by either calling reset or update.
48
49  * @author manish
50  */

51 public class AutoincrementCounter
52 {
53
54     private Long JavaDoc start;
55     private long increment;
56     private String JavaDoc identity;
57     private long finalValue;
58     private String JavaDoc schemaName;
59     private String JavaDoc tableName;
60     private String JavaDoc columnName;
61     // maintains state.
62
private long counter;
63     private int columnPosition;
64     private boolean initialized = false;
65
66     /**
67      * constructor
68      * @param start The start value of the counter; is a java object as
69      * it can also be null.
70      * @param increment how much to increment the counter by.
71      * @param finalValue the finalvalue of the counter. used by reset
72      * @param s
73      * @param t
74      * @param c
75      */

76     public AutoincrementCounter(Long JavaDoc start, long increment, long finalValue,
77                                 String JavaDoc s, String JavaDoc t, String JavaDoc c, int position)
78     {
79         this.increment = increment;
80         this.start = start;
81         this.initialized = false;
82         this.identity = makeIdentity(s,t,c);
83         this.finalValue = finalValue;
84         this.schemaName = s;
85         this.tableName = t;
86         this.columnName = c;
87         this.columnPosition = position;
88         // System.out.println("aic created with " + this);
89
}
90
91     /**
92      * make a unique key for the counter.
93      */

94     public static String JavaDoc makeIdentity(String JavaDoc s, String JavaDoc t, String JavaDoc c)
95     {
96         return s + "." + t + "." + c;
97     }
98
99     /**
100      * make a unique key for the counter.
101      */

102     public static String JavaDoc makeIdentity(TableDescriptor td, ColumnDescriptor cd)
103     {
104         return td.getSchemaName() + "." + td.getName() +
105                 "." + cd.getColumnName();
106     }
107
108     /**
109      * reset to the counter to the beginning or the end.
110      *
111      * @param begin if TRUE reset to beginning and mark it uninitialized.
112      */

113     public void reset(boolean begin)
114     {
115         if (begin == true)
116             initialized = false;
117         else
118         {
119             counter = finalValue;
120             initialized = true;
121         }
122         // System.out.println("counter reset to " + this);
123

124     }
125
126     /**
127      * update the counter.
128      *
129      * @param t update the counter to this value.
130      */

131     public long update(long t)
132     {
133         counter = t;
134         // System.out.println("counter updated to " + this);
135
initialized = true;
136         return counter;
137     }
138
139     /**
140      * update the counter to its next value.
141      *
142      * @exception StandardException if the counter has not yet been
143      * initialized and the Start value is NULL.
144      */

145     public long update() throws StandardException
146     {
147         long counterVal;
148
149         if (initialized == false)
150         {
151             // The first time around, counter simply gets the start
152
// value.
153
initialized = true;
154             
155             if (start == null)
156             {
157                 throw StandardException.newException(
158                                             SQLState.LANG_AI_COUNTER_ERROR);
159             }
160             counter = start.longValue();
161         }
162         else
163         {
164             counter = counter + increment;
165         }
166         // System.out.println("counter updated to " + this);
167
return counter;
168     }
169
170     /**
171      * get the current value of the counter. An uninitialized counter means the
172      * current value is NULL.
173      */

174     public Long JavaDoc getCurrentValue()
175     {
176         if (initialized == false)
177             return null;
178         return new Long JavaDoc(counter);
179     }
180     
181     /**
182      * return the identity of the counter.
183      */

184     public String JavaDoc getIdentity()
185     {
186         return identity;
187     }
188
189     /**
190      * flush a counter to disk; i.e write the current value of the counter into
191      * the row in SYSCOLUMNS.
192      *
193      * @param tc TransactionController to use
194      * @param dd DataDictionary to use.
195      * @param tableUUID I might have the table name but I need more
196      * information
197      * @exception StandardException standard cloudscape exception.
198      */

199     public void flushToDisk(TransactionController tc, DataDictionary dd,
200                             UUID tableUUID)
201            throws StandardException
202     {
203         dd.setAutoincrementValue(tc, tableUUID, columnName, counter, true);
204     }
205
206     /**
207      * get the column position in the table for which this counter has been
208      * created.
209      * @return the position of the corresponding column in the table (1-based)
210      */

211     public int getColumnPosition()
212     {
213         return columnPosition;
214     }
215
216     /**
217      * get the start value
218      * @return the initial value of the counter
219      */

220     public Long JavaDoc getStartValue()
221     {
222         return start;
223     }
224
225     public String JavaDoc toString()
226     {
227         return "counter: " + identity + " current: " + counter
228             + " start: " + start +
229             " increment: " + increment + " final: " + finalValue;
230     }
231 }
232
233
234
Popular Tags