Checkpoint: Update @GeneratedValue to handle long, not yet compiled.

My installation is outdated?
This commit is contained in:
Ray J. Greenwell
2024-11-13 16:18:05 -08:00
parent 9f3ece627e
commit a136a8f19b
10 changed files with 36 additions and 36 deletions
@@ -30,7 +30,7 @@ public @interface GeneratedValue
* initial value is 1. <em>Note:</em> this default differs from the value used by the EJB3
* persistence framework.
*/
int initialValue () default 1;
long initialValue () default 1;
/**
* If there are rows in our corresponding table, this boolean determines whether or not to
@@ -35,7 +35,7 @@ public class IdentityValueGenerator extends ValueGenerator
}
@Override // from ValueGenerator
public int nextGeneratedValue (Connection conn, DatabaseLiaison liaison, Statement stmt)
public long nextGeneratedValue (Connection conn, DatabaseLiaison liaison, Statement stmt)
throws SQLException
{
return liaison.lastInsertedId(conn, stmt, _dm.getTableName(), _fm.getColumnName());
@@ -46,7 +46,7 @@ public class TableValueGenerator extends ValueGenerator
conn, _valueTable,
Arrays.asList(_pkColumnName, _valueColumnName),
Arrays.asList(new ColumnDefinition("VARCHAR(255)", true, false, null),
new ColumnDefinition("INTEGER")),
new ColumnDefinition("BIGINT")),
Arrays.asList(_pkColumnName));
// and also that there's a row in it for us
@@ -58,11 +58,11 @@ public class TableValueGenerator extends ValueGenerator
return;
}
int initialValue = _initialValue;
long initialValue = _initialValue;
if (_migrateIfExists) {
Integer max = getFieldMaximum(conn, liaison);
Long max = getFieldMaximum(conn, liaison);
if (max != null) {
initialValue = 1 + max.intValue();
initialValue = 1 + max.longValue();
}
}
@@ -71,7 +71,7 @@ public class TableValueGenerator extends ValueGenerator
liaison.columnSQL(_pkColumnName) + ", " + liaison.columnSQL(_valueColumnName) +
") VALUES (?, ?)");
stmt.setString(1, _pkColumnValue);
stmt.setInt(2, initialValue);
stmt.setLong(2, initialValue);
stmt.executeUpdate();
}
@@ -86,7 +86,7 @@ public class TableValueGenerator extends ValueGenerator
}
@Override // from ValueGenerator
public int nextGeneratedValue (Connection conn, DatabaseLiaison liaison, Statement stmt)
public long nextGeneratedValue (Connection conn, DatabaseLiaison liaison, Statement stmt)
throws SQLException
{
// TODO: Make this lockless!
@@ -111,12 +111,12 @@ public class TableValueGenerator extends ValueGenerator
", column=" + _valueColumnName + "]");
}
// fetch the next available value
int val = rs.getInt(1);
int val = rs.getLong(1);
// claim this value locklessly
writeStatement.setInt(1, val + _allocationSize);
writeStatement.setLong(1, val + _allocationSize);
writeStatement.setString(2, _pkColumnValue);
writeStatement.setInt(3, val);
writeStatement.setLong(3, val);
// if we modified a row, we know we and nobody else got this particular value!
if (writeStatement.executeUpdate() == 1) {
@@ -50,7 +50,7 @@ public abstract class ValueGenerator
* @param stmt if post-factum, the statement that was used to perform the insert which resulted
* in the auto-generation of a value, otherwise null.
*/
public abstract int nextGeneratedValue (Connection conn, DatabaseLiaison liaison, Statement stmt)
public abstract long nextGeneratedValue (Connection conn, DatabaseLiaison liaison, Statement stmt)
throws SQLException;
/**
@@ -61,10 +61,10 @@ public abstract class ValueGenerator
/**
* Scans the table associated with this {@link ValueGenerator} and returns either null, if
* there are no rows, or an {@link Integer} containing the largest numerical value our
* there are no rows, or a {@link Long} containing the largest numerical value our
* field attains.
*/
protected Integer getFieldMaximum (Connection conn, DatabaseLiaison liaison)
protected Long getFieldMaximum (Connection conn, DatabaseLiaison liaison)
throws SQLException
{
String column = _fm.getColumnName();
@@ -79,9 +79,9 @@ public abstract class ValueGenerator
return null;
}
int cnt = rs.getInt(1);
long cnt = rs.getLong(1);
if (cnt > 0) {
return Integer.valueOf(rs.getInt(2));
return Long.valueOf(rs.getLong(2));
}
return null;
}
@@ -96,7 +96,7 @@ public abstract class ValueGenerator
return _fm;
}
protected int _initialValue;
protected long _initialValue;
protected int _allocationSize;
protected boolean _migrateIfExists;
@@ -41,18 +41,18 @@ public abstract class BaseLiaison implements DatabaseLiaison
@Deprecated
public int lastInsertedId (Connection conn, String table, String column) throws SQLException {
return lastInsertedId(conn, null, table, column);
return (int)lastInsertedId(conn, null, table, column);
}
// from DatabaseLiaison
public int lastInsertedId (Connection conn, Statement istmt, String table, String column)
public long lastInsertedId (Connection conn, Statement istmt, String table, String column)
throws SQLException
{
// if this JDBC driver supports getGeneratedKeys, use it!
if (istmt != null && conn.getMetaData().supportsGetGeneratedKeys()) {
ResultSet rs = istmt.getGeneratedKeys();
if (rs.next()) {
return rs.getInt(column);
return rs.getLong(column);
}
}
return fetchLastInsertedId(conn, table, column);
@@ -62,7 +62,7 @@ public abstract class BaseLiaison implements DatabaseLiaison
* Requests the last inserted id for the specified table and column. This is used if a JDBC
* driver does not support {@code getGeneratedKeys} or an attempt to use that failed.
*/
protected int fetchLastInsertedId (Connection conn, String table, String column)
protected long fetchLastInsertedId (Connection conn, String table, String column)
throws SQLException
{
throw new SQLException(
@@ -58,7 +58,7 @@ public interface DatabaseLiaison
* @return the requested inserted id.
* @throws SQLException if we are unable to obtain the last inserted id.
*/
public int lastInsertedId (Connection conn, Statement istmt, String table, String column)
public long lastInsertedId (Connection conn, Statement istmt, String table, String column)
throws SQLException;
/**
@@ -75,7 +75,7 @@ public class HsqldbLiaison extends BaseLiaison
}
@Override
protected int fetchLastInsertedId (Connection conn, String table, String column)
protected long fetchLastInsertedId (Connection conn, String table, String column)
throws SQLException
{
// HSQL does not keep track of per-table-and-column insertion data, so we are pretty much
@@ -85,7 +85,7 @@ public class HsqldbLiaison extends BaseLiaison
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("call IDENTITY()");
return rs.next() ? rs.getInt(1) : super.fetchLastInsertedId(conn, table, column);
return rs.next() ? rs.getLong(1) : super.fetchLastInsertedId(conn, table, column);
} finally {
JDBCUtil.close(stmt);
}
@@ -50,7 +50,7 @@ public class MySQLLiaison extends BaseLiaison
}
@Override // from BaseLiaison
public int lastInsertedId (Connection conn, Statement istmt, String table, String column)
public long lastInsertedId (Connection conn, Statement istmt, String table, String column)
throws SQLException
{
// MySQL uses "GENERATED_KEY" as the column name for the last inserted key, so we have to
@@ -59,7 +59,7 @@ public class MySQLLiaison extends BaseLiaison
}
@Override
protected int fetchLastInsertedId (Connection conn, String table, String column)
protected long fetchLastInsertedId (Connection conn, String table, String column)
throws SQLException
{
// MySQL does not keep track of per-table-and-column insertion data, so we are pretty much
@@ -69,7 +69,7 @@ public class MySQLLiaison extends BaseLiaison
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select LAST_INSERT_ID()");
return rs.next() ? rs.getInt(1) : super.fetchLastInsertedId(conn, table, column);
return rs.next() ? rs.getLong(1) : super.fetchLastInsertedId(conn, table, column);
} finally {
JDBCUtil.close(stmt);
}
@@ -35,7 +35,7 @@ public class PostgreSQLLiaison extends BaseLiaison
}
@Override
protected int fetchLastInsertedId (Connection conn, String table, String column)
protected long fetchLastInsertedId (Connection conn, String table, String column)
throws SQLException
{
// PostgreSQL's support for auto-generated ID's comes in the form of appropriately named
@@ -46,14 +46,14 @@ public class PostgreSQLLiaison extends BaseLiaison
try {
ResultSet rs = stmt.executeQuery(
"select currval('\"" + table + "_" + column + "_seq\"')");
return rs.next() ? rs.getInt(1) : super.fetchLastInsertedId(conn, table, column);
return rs.next() ? rs.getLong(1) : super.fetchLastInsertedId(conn, table, column);
} finally {
JDBCUtil.close(stmt);
}
}
// from DatabaseLiaison
public void createGenerator (Connection conn, String tableName, String columnName, int initValue)
public void createGenerator (Connection conn, String tableName, String columnName, long initValue)
throws SQLException
{
if (initValue == 1) {
@@ -12,23 +12,23 @@ public class GeneratedValueRecord extends PersistentRecord
{
// AUTO-GENERATED: FIELDS START
public static final Class<GeneratedValueRecord> _R = GeneratedValueRecord.class;
public static final ColumnExp<Integer> RECORD_ID = colexp(_R, "recordId");
public static final ColumnExp<Integer> VALUE = colexp(_R, "value");
public static final ColumnExp<Long> RECORD_ID = colexp(_R, "recordId");
public static final ColumnExp<Long> VALUE = colexp(_R, "value");
// AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 1;
public static final int SCHEMA_VERSION = 2;
@Id @GeneratedValue
public int recordId;
public long recordId;
public int value;
public long value;
// AUTO-GENERATED: METHODS START
/**
* Create and return a primary {@link Key} to identify a {@link GeneratedValueRecord}
* with the supplied key values.
*/
public static Key<GeneratedValueRecord> getKey (int recordId)
public static Key<GeneratedValueRecord> getKey (long recordId)
{
return newKey(_R, recordId);
}