提交 48d40b2f authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add support for generated keys from INSERT ... SELECT

上级 816461dd
...@@ -144,11 +144,11 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -144,11 +144,11 @@ public class Insert extends Prepared implements ResultTarget {
setCurrentRowNumber(0); setCurrentRowNumber(0);
table.fire(session, Trigger.INSERT, true); table.fire(session, Trigger.INSERT, true);
rowNumber = 0; rowNumber = 0;
GeneratedKeys generatedKeys = session.getGeneratedKeys();
generatedKeys.initialize(table);
int listSize = list.size(); int listSize = list.size();
if (listSize > 0) { if (listSize > 0) {
int columnLen = columns.length; int columnLen = columns.length;
GeneratedKeys generatedKeys = session.getGeneratedKeys();
generatedKeys.initialize(table);
for (int x = 0; x < listSize; x++) { for (int x = 0; x < listSize; x++) {
session.startStatementWithinTransaction(); session.startStatementWithinTransaction();
generatedKeys.nextRow(); generatedKeys.nextRow();
...@@ -199,8 +199,12 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -199,8 +199,12 @@ public class Insert extends Prepared implements ResultTarget {
} else { } else {
ResultInterface rows = query.query(0); ResultInterface rows = query.query(0);
while (rows.next()) { while (rows.next()) {
generatedKeys.nextRow();
Value[] r = rows.currentRow(); Value[] r = rows.currentRow();
addRow(r); Row newRow = addRowImpl(r);
if (newRow != null) {
generatedKeys.confirmRow(newRow);
}
} }
rows.close(); rows.close();
} }
...@@ -211,6 +215,10 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -211,6 +215,10 @@ public class Insert extends Prepared implements ResultTarget {
@Override @Override
public void addRow(Value[] values) { public void addRow(Value[] values) {
addRowImpl(values);
}
private Row addRowImpl(Value[] values) {
Row newRow = table.getTemplateRow(); Row newRow = table.getTemplateRow();
setCurrentRowNumber(++rowNumber); setCurrentRowNumber(++rowNumber);
for (int j = 0, len = columns.length; j < len; j++) { for (int j = 0, len = columns.length; j < len; j++) {
...@@ -229,7 +237,9 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -229,7 +237,9 @@ public class Insert extends Prepared implements ResultTarget {
table.addRow(session, newRow); table.addRow(session, newRow);
session.log(table, UndoLogRecord.INSERT, newRow); session.log(table, UndoLogRecord.INSERT, newRow);
table.fireAfterRow(session, null, newRow, false); table.fireAfterRow(session, null, newRow, false);
return newRow;
} }
return null;
} }
@Override @Override
......
...@@ -62,6 +62,7 @@ public class TestGetGeneratedKeys extends TestBase { ...@@ -62,6 +62,7 @@ public class TestGetGeneratedKeys extends TestBase {
Connection conn = getConnection("getGeneratedKeys"); Connection conn = getConnection("getGeneratedKeys");
testBatchAndMergeInto(conn); testBatchAndMergeInto(conn);
testCalledSequenses(conn); testCalledSequenses(conn);
testInsertWithSelect(conn);
testMergeUsing(conn); testMergeUsing(conn);
testMultithreaded(conn); testMultithreaded(conn);
testNameCase(conn); testNameCase(conn);
...@@ -231,6 +232,29 @@ public class TestGetGeneratedKeys extends TestBase { ...@@ -231,6 +232,29 @@ public class TestGetGeneratedKeys extends TestBase {
stat.execute("DROP SEQUENCE SEQ"); stat.execute("DROP SEQUENCE SEQ");
} }
/**
* Test method for INSERT ... SELECT operator.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testInsertWithSelect(Connection conn) throws Exception {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT, VALUE INT NOT NULL)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(VALUE) SELECT 10",
Statement.RETURN_GENERATED_KEYS);
prep.executeUpdate();
ResultSet rs = prep.getGeneratedKeys();
assertTrue(rs.next());
assertEquals(1, rs.getLong(1));
rs.close();
stat.execute("DROP TABLE TEST");
}
/** /**
* Test method for MERGE USING operator. * Test method for MERGE USING operator.
* *
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论