1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
/*
* Copyright (c) 1998, 2018 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
// 11/22/2012-2.5 Guy Pelletier
// - 389090: JPA 2.1 DDL Generation Support (index metadata support)
package org.eclipse.persistence.sequencing;
import java.io.StringWriter;
import java.util.List;
import org.eclipse.persistence.queries.*;
import org.eclipse.persistence.tools.schemaframework.IndexDefinition;
import org.eclipse.persistence.internal.databaseaccess.DatabasePlatform;
import org.eclipse.persistence.internal.helper.DatabaseTable;
/**
* <p>
* <b>Purpose</b>: Defines sequencing through using a SEQUENCE table.
* <p>
* <b>Description</b>
* This is the default sequencing mechanism.
* A table defaulting to SEQUENCE is used to generate unique ids.
* The table has a name field (SEQ_NAME) storing each sequences name,
* and a counter (SEQ_COUNT) storing the last sequence id generated.
* There will be a row in the table for each sequence object.
*/
public class TableSequence extends QuerySequence {
/** Default sequence table name
* @deprecated
* Use an empty string as a default sequence table name instead,
* that triggers usage of platform.getDefaultSequenceTableName() when the sequence is connected.
*/
public 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";
public TableSequence() {
super(false, true);
setTableName("");
}
/**
* Create a new sequence with the name.
*/
public TableSequence(String name) {
super(name, false, true);
setTableName("");
}
/**
* Create a new sequence with the name and sequence pre-allocation size.
*/
public TableSequence(String name, int size) {
super(name, size, false, true);
setTableName("");
}
public TableSequence(String name, int size, int initialValue) {
super(name, size, initialValue, false, true);
setTableName("");
}
/**
* Create a new sequence with the name, and the sequence table name.
*/
public TableSequence(String name, String tableName) {
this(name);
setTableName(tableName);
}
/**
* Create a new sequence with the name, and the sequence table information.
*/
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 isTable() {
return true;
}
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;
}
}
@Override
public int hashCode() {
int result = super.hashCode();
String tableName = getTableName();
String counterFieldName = getCounterFieldName();
String nameFieldName = getNameFieldName();
result = 31 * result + (tableName != null ? tableName.hashCode() : 0);
result = 31 * result + (counterFieldName != null ? counterFieldName.hashCode() : 0);
result = 31 * result + (nameFieldName != null ? nameFieldName.hashCode() : 0);
return result;
}
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 List<IndexDefinition> getTableIndexes() {
return getTable().getIndexes();
}
public String getTableName() {
return getTable().getQualifiedName();
}
public String getQualifiedTableName() {
return getQualified(getTable().getName());
}
public void setTable(DatabaseTable table) {
this.table = table;
}
public void setTableName(String name) {
table = new DatabaseTable(name);
}
public void onConnect() {
if(this.table.getName().length() == 0) {
this.table.setName(((DatabasePlatform)getDatasourcePlatform()).getDefaultSequenceTableName());
}
if ((this.qualifier == null || this.qualifier.length() == 0) &&
(this.table.getTableQualifier() != null && this.table.getTableQualifier().length() != 0)) {
this.qualifier = this.table.getTableQualifier();
}
super.onConnect();
}
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;
}
}
|