oracle.toplink.essentials.sequencing.TableSequence Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2007, Oracle. All rights reserved.
package oracle.toplink.essentials.sequencing;
import java.io.StringWriter;
import oracle.toplink.essentials.internal.helper.DatabaseTable;
import oracle.toplink.essentials.queryframework.*;
/**
*
* Purpose:
*
*/
public class TableSequence extends QuerySequence {
/** Default sequence table name */
protected static final String defaultTableName = "SEQUENCE";
/** Hold the database table */
protected DatabaseTable table;
/** Hold the name of the column in the sequence table which specifies the sequence numeric value */
protected String counterFieldName = "SEQ_COUNT";
/** Hold the name of the column in the sequence table which specifies the sequence name */
protected String nameFieldName = "SEQ_NAME";
protected String qualifier = "";
public TableSequence() {
super(false, true);
setTableName(defaultTableName);
}
public TableSequence(String name) {
super(name, false, true);
setTableName(defaultTableName);
}
public TableSequence(String name, int size) {
super(name, size, false, true);
setTableName(defaultTableName);
}
public TableSequence(String name, int size, int initialValue) {
super(name, size, initialValue, false, true);
setTableName(defaultTableName);
}
public TableSequence(String name, String tableName) {
this(name);
setTableName(tableName);
}
public TableSequence(String name, String tableName, String nameFieldName, String counterFieldName) {
this(name);
setTableName(tableName);
setNameFieldName(nameFieldName);
setCounterFieldName(counterFieldName);
}
public TableSequence(String name, int size, String tableName) {
this(name, size);
setTableName(tableName);
}
public TableSequence(String name, int size, String tableName, String nameFieldName, String counterFieldName) {
this(name, size);
setTableName(tableName);
setNameFieldName(nameFieldName);
setCounterFieldName(counterFieldName);
}
public boolean equals(Object obj) {
if (obj instanceof TableSequence) {
TableSequence other = (TableSequence)obj;
if (equalNameAndSize(this, other)) {
return getTableName().equals(other.getTableName()) && getCounterFieldName().equals(other.getCounterFieldName()) && getNameFieldName().equals(other.getNameFieldName());
} else {
return false;
}
} else {
return false;
}
}
public String getCounterFieldName() {
return counterFieldName;
}
public void setCounterFieldName(String name) {
counterFieldName = name;
}
public String getNameFieldName() {
return nameFieldName;
}
public void setNameFieldName(String name) {
nameFieldName = name;
}
public DatabaseTable getTable() {
return table;
}
public String getTableName() {
return getTable().getQualifiedName();
}
public String getQualifiedTableName() {
if (qualifier.equals("")) {
return getTableName();
} else {
return qualifier + "." + getTableName();
}
}
public void setTable(DatabaseTable table) {
this.table = table;
}
public void setTableName(String name) {
table = new DatabaseTable(name);
}
protected ValueReadQuery buildSelectQuery() {
ValueReadQuery query = new ValueReadQuery();
query.addArgument(getNameFieldName());
StringWriter writer = new StringWriter();
writer.write("SELECT " + getCounterFieldName());
writer.write(" FROM " + getQualifiedTableName());
writer.write(" WHERE " + getNameFieldName());
writer.write(" = #" + getNameFieldName());
query.setSQLString(writer.toString());
return query;
}
protected DataModifyQuery buildUpdateQuery() {
DataModifyQuery query = new DataModifyQuery();
query.addArgument(getNameFieldName());
query.addArgument("PREALLOC_SIZE");
StringWriter writer = new StringWriter();
writer.write("UPDATE " + getQualifiedTableName());
writer.write(" SET " + getCounterFieldName());
writer.write(" = " + getCounterFieldName());
writer.write(" + #PREALLOC_SIZE");
writer.write(" WHERE " + getNameFieldName() + " = #" + getNameFieldName());
query.setSQLString(writer.toString());
return query;
}
/**
* INTERNAL:
*/
public void onConnect() {
super.onConnect();
qualifier = getDatasourcePlatform().getTableQualifier();
}
/**
* INTERNAL:
*/
public void onDisconnect() {
qualifier = "";
super.onDisconnect();
}
}