提交 59e254f4 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 308: Statement.getGeneratedKeys() now returns an empty result set if no key was generated.

上级 a32eff44
......@@ -11,6 +11,7 @@ import org.h2.expression.Parameter;
import org.h2.expression.ParameterInterface;
import org.h2.result.ResultInterface;
import org.h2.value.Value;
import org.h2.value.ValueNull;
/**
* Represents a single SQL statements.
......@@ -65,6 +66,7 @@ class CommandContainer extends Command {
public int update() {
recompileIfRequired();
start();
session.setLastIdentity(ValueNull.INSTANCE);
prepared.checkParameters();
int updateCount = prepared.update();
prepared.trace(startTime, updateCount);
......
......@@ -26,7 +26,6 @@ import org.h2.table.Table;
import org.h2.util.New;
import org.h2.util.StatementBuilder;
import org.h2.value.Value;
import org.h2.value.ValueLong;
/**
* This class represents the statement
......@@ -82,7 +81,6 @@ public class Merge extends Prepared {
session.getUser().checkRight(table, Right.INSERT);
session.getUser().checkRight(table, Right.UPDATE);
setCurrentRowNumber(0);
session.setLastIdentity(ValueLong.get(0));
if (list.size() > 0) {
count = 0;
for (int x = 0, size = list.size(); x < size; x++) {
......
......@@ -1440,7 +1440,7 @@ public class JdbcConnection extends TraceObject implements Connection {
* INTERNAL
*/
ResultSet getGeneratedKeys(JdbcStatement stat, int id) {
getGeneratedKeys = prepareCommand("CALL SCOPE_IDENTITY()", getGeneratedKeys);
getGeneratedKeys = prepareCommand("SELECT SCOPE_IDENTITY() WHERE SCOPE_IDENTITY() IS NOT NULL", getGeneratedKeys);
ResultInterface result = getGeneratedKeys.executeQuery(0, false);
ResultSet rs = new JdbcResultSet(this, stat, result, id, false, true, false);
return rs;
......
......@@ -657,8 +657,9 @@ public class JdbcStatement extends TraceObject implements Statement {
}
/**
* Return a result set that contains the last generated autoincrement key
* for this connection.
* Return a result set that contains the last generated auto-increment key
* for this connection, if there was one. If no key was generated by the
* last modification statement, then an empty result set is returned.
*
* @return the result set with one row and one column containing the key
* @throws SQLException if this object is closed
......@@ -710,7 +711,7 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* Executes a statement and returns the update count.
* This method just calls executeUpdate(String sql) internally.
* The method getGeneratedKeys only supports one column.
* The method getGeneratedKeys supports at most one columns and row.
*
* @param sql the SQL statement
* @param autoGeneratedKeys ignored
......@@ -734,7 +735,7 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* Executes a statement and returns the update count.
* This method just calls executeUpdate(String sql) internally.
* The method getGeneratedKeys only supports one column.
* The method getGeneratedKeys supports at most one columns and row.
*
* @param sql the SQL statement
* @param columnIndexes ignored
......@@ -758,7 +759,7 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* Executes a statement and returns the update count.
* This method just calls executeUpdate(String sql) internally.
* The method getGeneratedKeys only supports one column.
* The method getGeneratedKeys supports at most one columns and row.
*
* @param sql the SQL statement
* @param columnNames ignored
......@@ -782,7 +783,7 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* Executes a statement and returns the update count.
* This method just calls execute(String sql) internally.
* The method getGeneratedKeys only supports one column.
* The method getGeneratedKeys supports at most one columns and row.
*
* @param sql the SQL statement
* @param autoGeneratedKeys ignored
......@@ -806,7 +807,7 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* Executes a statement and returns the update count.
* This method just calls execute(String sql) internally.
* The method getGeneratedKeys only supports one column.
* The method getGeneratedKeys supports at most one columns and row.
*
* @param sql the SQL statement
* @param columnIndexes ignored
......@@ -830,7 +831,7 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* Executes a statement and returns the update count.
* This method just calls execute(String sql) internally.
* The method getGeneratedKeys only supports one column.
* The method getGeneratedKeys supports at most one columns and row.
*
* @param sql the SQL statement
* @param columnNames ignored
......
......@@ -356,6 +356,9 @@ public class TestPreparedStatement extends TestBase {
rs.next();
byte[] data = rs.getBytes(1);
assertEquals(16, data.length);
stat.execute("INSERT INTO TEST_UUID VALUES(random_UUID())");
rs = stat.getGeneratedKeys();
assertFalse(rs.next());
stat.execute("DROP TABLE TEST_UUID");
}
......@@ -394,6 +397,10 @@ public class TestPreparedStatement extends TestBase {
ResultSet rs = stat.getGeneratedKeys();
rs.next();
assertEquals(1, rs.getLong(1));
stat.execute("insert into test values(100)");
rs = stat.getGeneratedKeys();
rs.next();
assertEquals(100, rs.getLong(1));
stat.execute("drop sequence seq");
stat.execute("drop table test");
}
......
......@@ -337,8 +337,7 @@ public class TestStatement extends TestBase {
stat.execute("insert into test2(x) values(10), (11), (12)");
stat.execute("merge into test1(x) key(x) values(5)");
keys = stat.getGeneratedKeys();
keys.next();
assertEquals(0, keys.getInt(1));
assertFalse(keys.next());
stat.execute("merge into test1(x) key(x) values(6)");
keys = stat.getGeneratedKeys();
keys.next();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论