KickJava   Java API By Example, From Geeks To Geeks.

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


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, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.tools.schemaframework;
23
24 import java.util.Vector JavaDoc;
25 import java.io.*;
26 import java.math.BigDecimal JavaDoc;
27 import oracle.toplink.essentials.internal.sessions.AbstractSession;
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.sequencing.TableSequence;
30
31 /**
32  * <p>
33  * <b>Purpose</b>: Allow a generic way of creating sequences on the different platforms,
34  * and allow optional parameters to be specified.
35  * <p>
36  */

37 public class TableSequenceDefinition extends SequenceDefinition {
38
39     /** Hold the name of the sequence table */
40     public String JavaDoc sequenceTableName;
41
42     /** Hold the name of the column in the sequence table which specifies the sequence name */
43     public String JavaDoc sequenceNameFieldName;
44
45     /** Hold the name of the column in the sequence table which specifies the sequence numeric value */
46     public String JavaDoc sequenceCounterFieldName;
47
48     public TableSequenceDefinition(String JavaDoc name, String JavaDoc sequenceTableName, String JavaDoc sequenceNameFieldName, String JavaDoc sequenceCounterFieldName) {
49         super(name);
50         setSequenceTableName(sequenceTableName);
51         setSequenceCounterFieldName(sequenceCounterFieldName);
52         setSequenceNameFieldName(sequenceNameFieldName);
53     }
54
55     public TableSequenceDefinition(TableSequence sequence) {
56         this(sequence.getName(), sequence.getTableName(), sequence.getNameFieldName(), sequence.getCounterFieldName());
57     }
58
59     public TableSequenceDefinition(String JavaDoc name, TableSequence sequence) {
60         this(name, sequence.getTableName(), sequence.getNameFieldName(), sequence.getCounterFieldName());
61     }
62
63     /**
64      * INTERNAL:
65      * Return the SQL required to insert the sequence row into the sequence table.
66      * Assume that the sequence table exists.
67      */

68     public Writer buildCreationWriter(AbstractSession session, Writer writer) throws ValidationException {
69         try {
70             writer.write("INSERT INTO ");
71             writer.write(getSequenceTableName());
72             writer.write("(" + getSequenceNameFieldName());
73             writer.write(", " + getSequenceCounterFieldName());
74             writer.write(") values (");
75             writer.write("'" + getName() + "', 0)");
76         } catch (IOException ioException) {
77             throw ValidationException.fileError(ioException);
78         }
79         return writer;
80     }
81
82     /**
83      * INTERNAL:
84      * Return the SQL to delete the row from the sequence table.
85      */

86     public Writer buildDeletionWriter(AbstractSession session, Writer writer) throws ValidationException {
87         try {
88             writer.write("DELETE FROM ");
89             writer.write(getSequenceTableName());
90             writer.write(" WHERE " + getSequenceNameFieldName());
91             writer.write(" = '" + getName() + "'");
92         } catch (IOException ioException) {
93             throw ValidationException.fileError(ioException);
94         }
95         return writer;
96     }
97
98     /**
99      * INTERAL:
100      * Execute the SQL required to insert the sequence row into the sequence table.
101      * Assume that the sequence table exists.
102      */

103     public boolean checkIfExist(AbstractSession session) throws DatabaseException {
104         Vector JavaDoc results = session.executeSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall("SELECT * FROM " + getSequenceTableName() + " WHERE " + getSequenceNameFieldName() + " = '" + getName() + "'"));
105         return !results.isEmpty();
106     }
107
108     /**
109      * PUBLIC:
110      */

111     public void setSequenceTableName(String JavaDoc sequenceTableName) {
112         this.sequenceTableName = sequenceTableName;
113     }
114
115     /**
116      * PUBLIC:
117      */

118     public String JavaDoc getSequenceTableName() {
119         return sequenceTableName;
120     }
121
122     /**
123      * PUBLIC:
124      */

125     public void setSequenceCounterFieldName(String JavaDoc sequenceCounterFieldName) {
126         this.sequenceCounterFieldName = sequenceCounterFieldName;
127     }
128
129     /**
130      * PUBLIC:
131      */

132     public String JavaDoc getSequenceCounterFieldName() {
133         return sequenceCounterFieldName;
134     }
135
136     /**
137      * PUBLIC:
138      */

139     public void setSequenceNameFieldName(String JavaDoc sequenceNameFieldName) {
140         this.sequenceNameFieldName = sequenceNameFieldName;
141     }
142
143     /**
144      * PUBLIC:
145      */

146     public String JavaDoc getSequenceNameFieldName() {
147         return sequenceNameFieldName;
148     }
149
150     /**
151      * INTERNAL:
152      * Return a TableDefinition specifying sequence table.
153      */

154     public TableDefinition buildTableDefinition() {
155         TableDefinition definition = new TableDefinition();
156         definition.setName(getSequenceTableName());
157         definition.addPrimaryKeyField(getSequenceNameFieldName(), String JavaDoc.class, 50);
158         definition.addField(getSequenceCounterFieldName(), BigDecimal JavaDoc.class);
159         return definition;
160     }
161 }
162
Popular Tags