org.eclipse.persistence.sequencing.UnaryTableSequence Maven / Gradle / Ivy
Show all versions of eclipselink Show documentation
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.sequencing;
import java.io.StringWriter;
import org.eclipse.persistence.queries.*;
/**
*
* Purpose: Defines sequencing through using a singleton sequence table.
*
* Description
* This is similar to the TableSequence but the sequence table only stores a single
* row defining a single sequence.
*/
public class UnaryTableSequence extends QuerySequence {
/** Hold the name of the only column in multiple single-column tables */
protected String counterFieldName = "SEQUENCE";
protected String updateString1;
protected String updateString2;
protected String selectString1;
protected String selectString2;
protected int updateStringBufferSize;
protected int selectStringBufferSize;
public UnaryTableSequence() {
super(false, true);
}
/**
* Create a new sequence with the name.
*/
public UnaryTableSequence(String name) {
super(name, false, true);
}
/**
* Create a new sequence with the name and sequence pre-allocation size.
*/
public UnaryTableSequence(String name, int size) {
super(name, size, false, true);
}
public UnaryTableSequence(String name, String counterFieldName) {
this(name);
setCounterFieldName(counterFieldName);
}
public UnaryTableSequence(String name, int size, String counterFieldName) {
this(name, size);
setCounterFieldName(counterFieldName);
}
@Override
public boolean isUnaryTable() {
return true;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof UnaryTableSequence) {
UnaryTableSequence other = (UnaryTableSequence)obj;
if (equalNameAndSize(this, other)) {
return getCounterFieldName().equals(other.getCounterFieldName());
} else {
return false;
}
} else {
return false;
}
}
@Override
public int hashCode() {
int result = super.hashCode();
String counterFieldName = getCounterFieldName();
result = 31 * result + (counterFieldName != null ? counterFieldName.hashCode() : 0);
return result;
}
public void setCounterFieldName(String name) {
this.counterFieldName = name;
}
public String getCounterFieldName() {
return counterFieldName;
}
/**
* INTERNAL:
*/
@Override
public void onConnect() {
super.onConnect();
initialize();
}
/**
* INTERNAL:
*/
@Override
public void onDisconnect() {
clear();
super.onDisconnect();
}
@Override
protected ValueReadQuery buildSelectQuery(String seqName, Integer size) {
ValueReadQuery selectQuery = new ValueReadQuery();
selectQuery.dontBindAllParameters();
StringWriter writer = new StringWriter(selectStringBufferSize + seqName.length());
writer.write(selectString1);
writer.write(seqName);
selectQuery.setSQLString(writer.toString());
return selectQuery;
}
@Override
protected DataModifyQuery buildUpdateQuery(String seqName, Number size) {
DataModifyQuery updateQuery = new DataModifyQuery();
updateQuery.dontBindAllParameters();
String sizeString = size.toString();
StringWriter writer = new StringWriter(updateStringBufferSize + seqName.length() + sizeString.length());
writer.write(updateString1);
writer.write(seqName);
writer.write(updateString2);
writer.write(sizeString);
updateQuery.setSQLString(writer.toString());
return updateQuery;
}
protected void initialize() {
if (getSelectQuery() == null) {
buildSelectString1();
selectStringBufferSize = selectString1.length();
}
if ((getUpdateQuery() == null) && !shouldSkipUpdate()) {
buildUpdateString1();
buildUpdateString2();
updateStringBufferSize = updateString1.length() + updateString2.length();
}
}
protected void buildUpdateString1() {
updateString1 = "UPDATE ";
if (!qualifier.isEmpty()) {
updateString1 = updateString1 + qualifier + '.';
}
}
protected void buildUpdateString2() {
StringWriter writer = new StringWriter();
writer.write(" SET ");
writer.write(getCounterFieldName());
writer.write(" = ");
writer.write(getCounterFieldName());
writer.write(" + ");
updateString2 = writer.toString();
}
protected void buildSelectString1() {
selectString1 = "SELECT * FROM ";
if (!qualifier.isEmpty()) {
selectString1 = selectString1 + qualifier + '.';
}
}
protected void clear() {
updateString1 = null;
updateString2 = null;
selectString1 = null;
selectString2 = null;
updateStringBufferSize = 0;
selectStringBufferSize = 0;
}
}