KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > sequencing > TableSequence


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.sequencing;
23
24 import java.io.StringWriter JavaDoc;
25 import oracle.toplink.essentials.internal.helper.DatabaseTable;
26 import oracle.toplink.essentials.queryframework.*;
27
28 /**
29  * <p>
30  * <b>Purpose</b>:
31  * <p>
32  */

33 public class TableSequence extends QuerySequence {
34     /** Default sequence table name */
35     protected static final String JavaDoc defaultTableName = "SEQUENCE";
36     
37     /** Hold the database table */
38     protected DatabaseTable table;
39
40     /** Hold the name of the column in the sequence table which specifies the sequence numeric value */
41     protected String JavaDoc counterFieldName = "SEQ_COUNT";
42
43     /** Hold the name of the column in the sequence table which specifies the sequence name */
44     protected String JavaDoc nameFieldName = "SEQ_NAME";
45     protected String JavaDoc qualifier = "";
46
47     public TableSequence() {
48         super(false, true);
49         setTableName(defaultTableName);
50     }
51
52     public TableSequence(String JavaDoc name) {
53         super(name, false, true);
54         setTableName(defaultTableName);
55     }
56
57     public TableSequence(String JavaDoc name, int size) {
58         super(name, size, false, true);
59         setTableName(defaultTableName);
60     }
61     
62     public TableSequence(String JavaDoc name, int size, int initialValue) {
63         super(name, size, initialValue, false, true);
64         setTableName(defaultTableName);
65     }
66
67     public TableSequence(String JavaDoc name, String JavaDoc tableName) {
68         this(name);
69         setTableName(tableName);
70     }
71
72     public TableSequence(String JavaDoc name, String JavaDoc tableName, String JavaDoc nameFieldName, String JavaDoc counterFieldName) {
73         this(name);
74         setTableName(tableName);
75         setNameFieldName(nameFieldName);
76         setCounterFieldName(counterFieldName);
77     }
78
79     public TableSequence(String JavaDoc name, int size, String JavaDoc tableName) {
80         this(name, size);
81         setTableName(tableName);
82     }
83
84     public TableSequence(String JavaDoc name, int size, String JavaDoc tableName, String JavaDoc nameFieldName, String JavaDoc counterFieldName) {
85         this(name, size);
86         setTableName(tableName);
87         setNameFieldName(nameFieldName);
88         setCounterFieldName(counterFieldName);
89     }
90
91     public boolean equals(Object JavaDoc obj) {
92         if (obj instanceof TableSequence) {
93             TableSequence other = (TableSequence)obj;
94             if (equalNameAndSize(this, other)) {
95                 return getTableName().equals(other.getTableName()) && getCounterFieldName().equals(other.getCounterFieldName()) && getNameFieldName().equals(other.getNameFieldName());
96             } else {
97                 return false;
98             }
99         } else {
100             return false;
101         }
102     }
103
104     public String JavaDoc getCounterFieldName() {
105         return counterFieldName;
106     }
107
108     public void setCounterFieldName(String JavaDoc name) {
109         counterFieldName = name;
110     }
111
112     public String JavaDoc getNameFieldName() {
113         return nameFieldName;
114     }
115
116     public void setNameFieldName(String JavaDoc name) {
117         nameFieldName = name;
118     }
119
120     public DatabaseTable getTable() {
121         return table;
122     }
123     
124     public String JavaDoc getTableName() {
125         return getTable().getQualifiedName();
126     }
127
128     public String JavaDoc getQualifiedTableName() {
129         if (qualifier.equals("")) {
130             return getTableName();
131         } else {
132             return qualifier + "." + getTableName();
133         }
134     }
135
136     public void setTable(DatabaseTable table) {
137         this.table = table;
138     }
139     
140     public void setTableName(String JavaDoc name) {
141         table = new DatabaseTable(name);
142     }
143
144     protected ValueReadQuery buildSelectQuery() {
145         ValueReadQuery query = new ValueReadQuery();
146         query.addArgument(getNameFieldName());
147         StringWriter JavaDoc writer = new StringWriter JavaDoc();
148         writer.write("SELECT " + getCounterFieldName());
149         writer.write(" FROM " + getQualifiedTableName());
150         writer.write(" WHERE " + getNameFieldName());
151         writer.write(" = #" + getNameFieldName());
152         query.setSQLString(writer.toString());
153
154         return query;
155     }
156
157     protected DataModifyQuery buildUpdateQuery() {
158         DataModifyQuery query = new DataModifyQuery();
159         query.addArgument(getNameFieldName());
160         query.addArgument("PREALLOC_SIZE");
161         StringWriter JavaDoc writer = new StringWriter JavaDoc();
162         writer.write("UPDATE " + getQualifiedTableName());
163         writer.write(" SET " + getCounterFieldName());
164         writer.write(" = " + getCounterFieldName());
165         writer.write(" + #PREALLOC_SIZE");
166         writer.write(" WHERE " + getNameFieldName() + " = #" + getNameFieldName());
167         query.setSQLString(writer.toString());
168
169         return query;
170     }
171
172     /**
173     * INTERNAL:
174     */

175     public void onConnect() {
176         super.onConnect();
177         qualifier = getDatasourcePlatform().getTableQualifier();
178     }
179
180     /**
181     * INTERNAL:
182     */

183     public void onDisconnect() {
184         qualifier = "";
185         super.onDisconnect();
186     }
187 }
188
Popular Tags