提交 d261c4f8 authored 作者: Thomas Mueller's avatar Thomas Mueller

Local temporary tables can now be created without having to commit a transaction.

上级 f2578a29
......@@ -96,7 +96,9 @@ public class AlterTableAddConstraint extends SchemaCommand {
* @return the update count
*/
public int tryUpdate() {
if (!transactional) {
session.commit(true);
}
Database db = session.getDatabase();
Table table = getSchema().getTableOrView(session, tableName);
if (getSchema().findConstraint(session, constraintName) != null) {
......
......@@ -7,7 +7,6 @@
package org.h2.command.ddl;
import java.util.ArrayList;
import org.h2.command.Prepared;
import org.h2.command.dml.Insert;
import org.h2.command.dml.Query;
import org.h2.constant.ErrorCode;
......@@ -30,7 +29,7 @@ import org.h2.value.DataType;
public class CreateTable extends SchemaCommand {
private CreateTableData data = new CreateTableData();
private ArrayList<Prepared> constraintCommands = New.arrayList();
private ArrayList<DefineCommand> constraintCommands = New.arrayList();
private IndexColumn[] pkColumns;
private boolean ifNotExists;
private boolean onCommitDrop;
......@@ -72,7 +71,7 @@ public class CreateTable extends SchemaCommand {
*
* @param command the statement to add
*/
public void addConstraintCommand(Prepared command) {
public void addConstraintCommand(DefineCommand command) {
if (command instanceof CreateIndex) {
constraintCommands.add(command);
} else {
......@@ -94,7 +93,9 @@ public class CreateTable extends SchemaCommand {
}
public int update() {
if (!transactional) {
session.commit(true);
}
Database db = session.getDatabase();
if (!db.isPersistent()) {
data.persistIndexes = false;
......@@ -156,7 +157,8 @@ public class CreateTable extends SchemaCommand {
for (Sequence sequence : sequences) {
table.addSequence(sequence);
}
for (Prepared command : constraintCommands) {
for (DefineCommand command : constraintCommands) {
command.setTransactional(transactional);
command.update();
}
if (asQuery != null) {
......@@ -177,7 +179,9 @@ public class CreateTable extends SchemaCommand {
} catch (DbException e) {
db.checkPowerOff();
db.removeSchemaObject(session, table);
if (!transactional) {
session.commit(true);
}
throw e;
}
return 0;
......
......@@ -16,6 +16,8 @@ import org.h2.result.ResultInterface;
*/
public abstract class DefineCommand extends Prepared {
protected boolean transactional;
/**
* Create a new command for the given session.
*
......@@ -25,10 +27,6 @@ public abstract class DefineCommand extends Prepared {
super(session);
}
public boolean isTransactional() {
return false;
}
public boolean isReadOnly() {
return false;
}
......@@ -37,4 +35,12 @@ public abstract class DefineCommand extends Prepared {
return null;
}
public void setTransactional(boolean transactional) {
this.transactional = transactional;
}
public boolean isTransactional() {
return transactional;
}
}
......@@ -421,6 +421,10 @@ public abstract class Table extends SchemaObjectBase {
}
}
public ArrayList<TableView> getViews() {
return views;
}
public void removeChildrenAndResources(Session session) {
while (views != null && views.size() > 0) {
TableView view = views.get(0);
......
......@@ -30,6 +30,7 @@ public class TestTempTables extends TestBase {
public void test() throws SQLException {
deleteDb("tempTables");
testTransactionalTemp();
testDeleteGlobalTempTableWhenClosing();
Connection c1 = getConnection("tempTables");
testAlter(c1);
......@@ -42,6 +43,27 @@ public class TestTempTables extends TestBase {
deleteDb("tempTables");
}
private void testTransactionalTemp() throws SQLException {
deleteDb("tempTables");
Connection conn = getConnection("tempTables");
conn.setAutoCommit(false);
Statement stat = conn.createStatement();
ResultSet rs;
stat.execute("create table test(id int primary key)");
stat.execute("insert into test values(1)");
stat.execute("commit");
stat.execute("insert into test values(2)");
stat.execute("create local temporary table temp(id int primary key) transactional");
stat.execute("insert into temp values(3)");
stat.execute("rollback");
rs = stat.executeQuery("select * from test");
assertTrue(rs.next());
assertFalse(rs.next());
stat.execute("drop table test");
stat.execute("drop table temp");
conn.close();
}
private void testDeleteGlobalTempTableWhenClosing() throws SQLException {
if (config.memory) {
return;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论