提交 fbb45d90 authored 作者: christian.peter.io's avatar christian.peter.io

Allow writing to linked tables in readonly databases

I wrote the code, it belongs to Docware GmbH, and Docware GmbH is contributing it to H2 for distribution.
Multiple-licensed under the H2 License, version 1.0, and under the
Eclipse Public License, version 1.0 (http://h2database.com/html/license.html).
上级 1b38a128
...@@ -22,6 +22,7 @@ Change Log ...@@ -22,6 +22,7 @@ Change Log
</li><li>Bugfixes in the page store. </li><li>Bugfixes in the page store.
</li><li>Possibility to set a vendor id in Constants.java, so that unofficial builds are distinguishable </li><li>Possibility to set a vendor id in Constants.java, so that unofficial builds are distinguishable
from official releases. from official releases.
</li><li>Allow writing to linked tables in readonly databases.
</li></ul> </li></ul>
<h2>Version 1.1.119 (2009-09-26)</h2> <h2>Version 1.1.119 (2009-09-26)</h2>
......
...@@ -91,7 +91,7 @@ public class User extends RightOwner { ...@@ -91,7 +91,7 @@ public class User extends RightOwner {
*/ */
public void checkRight(Table table, int rightMask) throws SQLException { public void checkRight(Table table, int rightMask) throws SQLException {
if (rightMask != Right.SELECT && !systemUser) { if (rightMask != Right.SELECT && !systemUser) {
database.checkWritingAllowed(); table.checkWritingAllowed();
} }
if (admin) { if (admin) {
return; return;
......
...@@ -937,6 +937,15 @@ public abstract class Table extends SchemaObjectBase { ...@@ -937,6 +937,15 @@ public abstract class Table extends SchemaObjectBase {
return compareMode; return compareMode;
} }
/**
* Tests if the table can be written. Usually, this depends on the
* database.checkWritingAllowed method, but some tables (eg. TableLink)
* overwrite this default behaviour.
*/
public void checkWritingAllowed() throws SQLException {
database.checkWritingAllowed();
}
/** /**
* Get or generate a default value for the given column. * Get or generate a default value for the given column.
* *
......
...@@ -543,6 +543,13 @@ public class TableLink extends Table { ...@@ -543,6 +543,13 @@ public class TableLink extends Table {
return false; return false;
} }
/**
* Linked tables don't know if they are readonly. This overwrites
* the default handling.
*/
public void checkWritingAllowed() throws SQLException {
}
/** /**
* Convert the values if required. Default values are not set (kept as * Convert the values if required. Default values are not set (kept as
* null). * null).
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.test.db; package org.h2.test.db;
import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -47,6 +48,7 @@ public class TestLinkedTable extends TestBase { ...@@ -47,6 +48,7 @@ public class TestLinkedTable extends TestBase {
testLinkTable(); testLinkTable();
testLinkTwoTables(); testLinkTwoTables();
testCachingResults(); testCachingResults();
testLinkedTableInReadOnlyDb();
deleteDb("linkedTable"); deleteDb("linkedTable");
} }
...@@ -582,4 +584,46 @@ public class TestLinkedTable extends TestBase { ...@@ -582,4 +584,46 @@ public class TestLinkedTable extends TestBase {
} }
assertEquals(expected, counter); assertEquals(expected, counter);
} }
private void testLinkedTableInReadOnlyDb() throws SQLException {
if (config.memory || config.networked) {
return;
}
deleteDb("testLinkedTableInReadOnlyDb");
org.h2.Driver.load();
Connection memConn = DriverManager.getConnection("jdbc:h2:mem:one", "sa", "sa");
Statement memStat = memConn.createStatement();
memStat.execute("CREATE TABLE TEST(ID VARCHAR)");
String url1 = getURL("testLinkedTableInReadOnlyDb", true);
Connection conn = DriverManager.getConnection(url1, "sa1", "abc abc");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY)");
conn.close();
File[] files = new File(baseDir).listFiles();
for (File file : files) {
if (file.getName().startsWith("testLinkedTableInReadOnlyDb")) {
boolean isReadOnly = file.setReadOnly();
if (!isReadOnly) {
fail("File " + file.getAbsolutePath() + " is not read only. Can't test it");
}
}
}
// Now it's read only
conn = DriverManager.getConnection(url1, "sa1", "abc abc");
stat = conn.createStatement();
stat.execute("CREATE LOCAL TEMPORARY LINKED TABLE T(NULL, 'jdbc:h2:mem:one', 'sa', 'sa', 'TEST')");
// This is valid because it's a linked table
stat.execute("INSERT INTO T VALUES('abc')");
conn.close();
memConn.close();
deleteDb("testLinkedTableInReadOnlyDb");
}
} }
...@@ -613,4 +613,4 @@ modern slight boost characteristics significantly gae vfs centrally ten ...@@ -613,4 +613,4 @@ modern slight boost characteristics significantly gae vfs centrally ten
approach risky getters suxxess gmb delegate delegating delegates collisions approach risky getters suxxess gmb delegate delegating delegates collisions
linkage superfluous disallow scoop moebius inputs copilot dmoebius leod jenkov linkage superfluous disallow scoop moebius inputs copilot dmoebius leod jenkov
jakob poker docware peter unstable measurable scramble reissued recreation jakob poker docware peter unstable measurable scramble reissued recreation
scrambling distinguish official unofficial distinguishable scrambling distinguish official unofficial distinguishable overwrites
\ No newline at end of file \ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论