KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > tools > schemaframework > OracleSequenceDefinition


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.tools.schemaframework;
23
24 import java.io.*;
25 import oracle.toplink.essentials.exceptions.*;
26 import oracle.toplink.essentials.sequencing.NativeSequence;
27 import oracle.toplink.essentials.internal.sessions.AbstractSession;
28
29 /**
30  * <p>
31  * <b>Purpose</b>: Support Oracle native sequence creation.
32  * Oracle has custom support for sequences.
33  * <p>
34  */

35 public class OracleSequenceDefinition extends SequenceDefinition {
36
37     /** The increment can be used to support pre-allocation. */
38     protected int increment;
39
40     /** The start is the first sequence value that will be available for TopLink to use. */
41     protected int start = 1;
42
43     public OracleSequenceDefinition(String JavaDoc name, int preallocationSize) {
44         super(name);
45         setIncrement(preallocationSize);
46     }
47
48     public OracleSequenceDefinition(String JavaDoc name, int preallocationSize, int start) {
49         super(name);
50         // sequence value should be positive
51
if(start <= 0) {
52             start = 1;
53         }
54         setIncrement(preallocationSize);
55         setStart(start);
56     }
57     
58     public OracleSequenceDefinition(String JavaDoc name) {
59         this(name, 1);
60     }
61
62     public OracleSequenceDefinition(NativeSequence sequence) {
63         this(sequence.getName(), sequence.getPreallocationSize(), sequence.getInitialValue());
64     }
65
66     /**
67      * INTERNAL:
68      * Return the SQL required to create the Oracle sequence object.
69      */

70     public Writer buildCreationWriter(AbstractSession session, Writer writer) {
71         try {
72             writer.write("CREATE SEQUENCE ");
73             writer.write(getFullName());
74             if (getIncrement() != 1) {
75                 writer.write(" INCREMENT BY " + getIncrement());
76             }
77             // startWith value calculated using the initial value and increment.
78
// The first time TopLink calls select nextval, the value equal to startWith is returned.
79
// The first sequence value TopLink may assign is startWith - getIncrement() + 1.
80
int startWith = getStart() + getIncrement() - 1;
81             writer.write(" START WITH " + startWith);
82         } catch (IOException ioException) {
83             throw ValidationException.fileError(ioException);
84         }
85         return writer;
86     }
87
88     /**
89      * INTERNAL:
90      * Return the SQL required to drop the Oracle sequence object.
91      */

92     public Writer buildDeletionWriter(AbstractSession session, Writer writer) {
93         try {
94             writer.write("DROP SEQUENCE ");
95             writer.write(getFullName());
96         } catch (IOException ioException) {
97             throw ValidationException.fileError(ioException);
98         }
99         return writer;
100     }
101
102     /**
103      * INTERNAL:
104      * Return the SQL required to alter INCREMENT BY
105      */

106     public Writer buildAlterIncrementWriter(AbstractSession session, Writer writer) {
107         try {
108             writer.write("ALTER SEQUENCE ");
109             writer.write(getFullName());
110             writer.write(" INCREMENT BY " + getIncrement());
111         } catch (IOException ioException) {
112             throw ValidationException.fileError(ioException);
113         }
114         return writer;
115     }
116
117     /**
118      * INTERNAL:
119      * Check if the sequence object already exists, in which case dont create it.
120      */

121     public boolean checkIfExist(AbstractSession session) throws DatabaseException {
122         try {
123             session.executeSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall("SELECT " + getName() + ".NEXTVAL FROM DUAL"));
124         } catch (DatabaseException exception) {
125             return false;
126         }
127         return true;
128     }
129
130     /**
131      * The increment can be used to support pre-allocation.
132      */

133     public int getIncrement() {
134         return increment;
135     }
136
137     /**
138      * The increment can be used to support pre-allocation.
139      */

140     public void setIncrement(int increment) {
141         this.increment = increment;
142     }
143
144     /**
145      * The start used as a starting value for sequence
146      */

147     public int getStart() {
148         return start;
149     }
150
151     /**
152      * The start used as a starting value for sequence
153      */

154     public void setStart(int start) {
155         this.start = start;
156     }
157
158     /**
159      * The start used as a starting value for sequence
160      */

161     public void setStartAndIncrement(int value) {
162         setStart(1);
163         setIncrement(value);
164     }
165
166     /**
167      * INTERNAL:
168      * Indicates whether alterIncrement is supported
169      */

170     public boolean isAlterSupported() {
171         return true;
172     }
173
174     /**
175      * INTERNAL:
176      * Execute the SQL required to alter sequence increment.
177      * Assume that the sequence exists.
178      */

179     public void alterOnDatabase(AbstractSession session) throws TopLinkException {
180         try {
181             session.executeNonSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall(buildAlterIncrementWriter(session, new StringWriter()).toString()));
182         } catch (DatabaseException exception) {
183             createOnDatabase(session);
184         }
185     }
186
187     /**
188      * INTERNAL:
189      * Execute the SQL required to alter sequence increment.
190      * Assume that the sequence exists.
191      */

192     public void alterIncrement(AbstractSession session, Writer schemaWriter) throws ValidationException {
193         if (schemaWriter == null) {
194             this.alterOnDatabase(session);
195         } else {
196             this.buildAlterIncrementWriter(session, schemaWriter);
197         }
198     }
199 }
200
Popular Tags