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