Yet more lastInsertedId revamping. Third time's the charm.
This commit is contained in:
@@ -38,12 +38,11 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public int lastInsertedId (Connection conn, String table, String column) throws SQLException {
|
public int lastInsertedId (Connection conn, String table, String column) throws SQLException {
|
||||||
Integer id = lastInsertedId(conn, null, table, column);
|
return lastInsertedId(conn, null, table, column);
|
||||||
return (id == null) ? -1 : id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// from DatabaseLiaison
|
// from DatabaseLiaison
|
||||||
public Integer lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
public int lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// if this JDBC driver supports getGeneratedKeys, use it!
|
// if this JDBC driver supports getGeneratedKeys, use it!
|
||||||
@@ -53,7 +52,18 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
return rs.getInt(column);
|
return rs.getInt(column);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return fetchLastInsertedId(conn, table, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
throws SQLException
|
||||||
|
{
|
||||||
|
throw new SQLException(
|
||||||
|
"Unable to obtain last inserted id [table=" + table + ", column=" + column + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
// from DatabaseLiaison
|
// from DatabaseLiaison
|
||||||
|
|||||||
@@ -55,9 +55,10 @@ public interface DatabaseLiaison
|
|||||||
*
|
*
|
||||||
* @param istmt the insert statement that generated the keys. May be null if the ORM doesn't
|
* @param istmt the insert statement that generated the keys. May be null if the ORM doesn't
|
||||||
* have the statement handy.
|
* have the statement handy.
|
||||||
* @return the requested inserted id, or null if no last inserted id can be obtained.
|
* @return the requested inserted id.
|
||||||
|
* @throws SQLException if we are unable to obtain the last inserted id.
|
||||||
*/
|
*/
|
||||||
public Integer lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
public int lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
||||||
throws SQLException;
|
throws SQLException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -72,13 +72,10 @@ public class HsqldbLiaison extends BaseLiaison
|
|||||||
// HSQL's IDENTITY() does not create any database entities that we need to delete
|
// HSQL's IDENTITY() does not create any database entities that we need to delete
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from DatabaseLiaison
|
@Override
|
||||||
public Integer lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
protected int fetchLastInsertedId (Connection conn, String table, String column)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
Integer id = super.lastInsertedId(conn, istmt, table, column);
|
|
||||||
if (id != null) return id;
|
|
||||||
|
|
||||||
// HSQL does not keep track of per-table-and-column insertion data, so we are pretty much
|
// HSQL does not keep track of per-table-and-column insertion data, so we are pretty much
|
||||||
// going on blind faith here that we're fetching the right ID. In the overwhelming number
|
// going on blind faith here that we're fetching the right ID. In the overwhelming number
|
||||||
// of cases that will be so, but it's still not pretty.
|
// of cases that will be so, but it's still not pretty.
|
||||||
@@ -86,7 +83,7 @@ public class HsqldbLiaison extends BaseLiaison
|
|||||||
try {
|
try {
|
||||||
stmt = conn.createStatement();
|
stmt = conn.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery("call IDENTITY()");
|
ResultSet rs = stmt.executeQuery("call IDENTITY()");
|
||||||
return rs.next() ? rs.getInt(1) : null;
|
return rs.next() ? rs.getInt(1) : super.fetchLastInsertedId(conn, table, column);
|
||||||
} finally {
|
} finally {
|
||||||
JDBCUtil.close(stmt);
|
JDBCUtil.close(stmt);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,16 +49,19 @@ public class MySQLLiaison extends BaseLiaison
|
|||||||
// AUTO_INCREMENT does not create any database entities that we need to delete
|
// AUTO_INCREMENT does not create any database entities that we need to delete
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from DatabaseLiaison
|
@Override // from BaseLiaison
|
||||||
public Integer lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
public int lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// MySQL hackily reports the last inserted key as GENERATED_KEY, so we call super with that
|
// MySQL uses "GENERATED_KEY" as the column name for the last inserted key, so we have to
|
||||||
// "column name"; but if that doesn't work (we're using an old JDBC driver without support
|
// hackily pass that to our super method to get things to work
|
||||||
// for returning generated keys), fall back to the old old method
|
return super.lastInsertedId(conn, istmt, table, "GENERATED_KEY");
|
||||||
Integer id = super.lastInsertedId(conn, istmt, table, "GENERATED_KEY");
|
}
|
||||||
if (id != null) return id;
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int 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
|
// MySQL does not keep track of per-table-and-column insertion data, so we are pretty much
|
||||||
// going on blind faith here that we're fetching the right ID. In the overwhelming number
|
// going on blind faith here that we're fetching the right ID. In the overwhelming number
|
||||||
// of cases that will be so, but it's still not pretty.
|
// of cases that will be so, but it's still not pretty.
|
||||||
@@ -66,7 +69,7 @@ public class MySQLLiaison extends BaseLiaison
|
|||||||
try {
|
try {
|
||||||
stmt = conn.createStatement();
|
stmt = conn.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery("select LAST_INSERT_ID()");
|
ResultSet rs = stmt.executeQuery("select LAST_INSERT_ID()");
|
||||||
return rs.next() ? rs.getInt(1) : null;
|
return rs.next() ? rs.getInt(1) : super.fetchLastInsertedId(conn, table, column);
|
||||||
} finally {
|
} finally {
|
||||||
JDBCUtil.close(stmt);
|
JDBCUtil.close(stmt);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,14 +34,10 @@ public class PostgreSQLLiaison extends BaseLiaison
|
|||||||
msg.indexOf("An I/O error occured while sending to the backend") != -1);
|
msg.indexOf("An I/O error occured while sending to the backend") != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// from DatabaseLiaison
|
@Override
|
||||||
public Integer lastInsertedId (Connection conn, Statement istmt, String table, String column)
|
protected int fetchLastInsertedId (Connection conn, String table, String column)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// try the default first, which uses JDBC's getGeneratedKeys mechanism
|
|
||||||
Integer id = super.lastInsertedId(conn, istmt, table, column);
|
|
||||||
if (id != null) return id;
|
|
||||||
|
|
||||||
// PostgreSQL's support for auto-generated ID's comes in the form of appropriately named
|
// PostgreSQL's support for auto-generated ID's comes in the form of appropriately named
|
||||||
// sequences and DEFAULT nextval(sequence) modifiers in the ID columns. To get the next ID,
|
// sequences and DEFAULT nextval(sequence) modifiers in the ID columns. To get the next ID,
|
||||||
// we use the currval() method which is set in a database sessions when any given sequence
|
// we use the currval() method which is set in a database sessions when any given sequence
|
||||||
@@ -50,7 +46,7 @@ public class PostgreSQLLiaison extends BaseLiaison
|
|||||||
try {
|
try {
|
||||||
ResultSet rs = stmt.executeQuery(
|
ResultSet rs = stmt.executeQuery(
|
||||||
"select currval('\"" + table + "_" + column + "_seq\"')");
|
"select currval('\"" + table + "_" + column + "_seq\"')");
|
||||||
return rs.next() ? rs.getInt(1) : null;
|
return rs.next() ? rs.getInt(1) : super.fetchLastInsertedId(conn, table, column);
|
||||||
} finally {
|
} finally {
|
||||||
JDBCUtil.close(stmt);
|
JDBCUtil.close(stmt);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user