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

INIT

上级 11ecd55b
......@@ -23,7 +23,7 @@ Change Log
</li><li>Creating a database was delayed about 2 seconds if the directory didn't exist.
</li><li>Implemented INIT feature. If the database URL contains ";INIT=...;" then the DDL or DML commands
following are executed on startup. Example URL: jdbc:h2:mem:test;INIT=RUNSCRIPT FROM '~/create.sql'
(patch from Kerry Sainsbury)
(patch from Kerry Sainsbury).
</li></ul>
<h2>Version 1.2.129 (2010-02-19)</h2>
......@@ -60,7 +60,7 @@ Change Log
</li><li>The following system properties are no longer supported:
h2.overflowExceptions, h2.optimizeDropDependencies, h2.optimizeGroupSorted,
h2.optimizeMinMax, h2.optimizeNot, h2.optimizeIn, h2.optimizeInJoin, h2.reuseSpace*.
Most of then were there fore a long time, but always with the same value.
Most of then were there for a long time, but always with the same value.
There was no unit test with the other value. So changing them was potentially dangerous
(not a lot, but still).
</li><li>The setting LOG has currently no effect (it only had an effect when the page store was disabled).
......
......@@ -6,8 +6,6 @@
*/
package org.h2.engine;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import org.h2.command.CommandInterface;
import org.h2.command.Parser;
......@@ -119,19 +117,11 @@ public class Engine {
throw DbException.convert(e);
}
}
String init = ci.removeProperty("INIT", null);
Session session = openSession(ci);
validateUserAndPassword(true);
if (backup != null) {
session.setConnectionInfo(backup);
}
if (init != null) {
runInitScripts(session, init);
}
return session;
} catch (DbException e) {
if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
......@@ -141,28 +131,11 @@ public class Engine {
}
}
private void runInitScripts(Session session, String init) throws DbException {
Statement stat = null;
try {
stat = session.createConnection(false).createStatement();
stat.execute(init);
} catch (SQLException e) {
throw DbException.convert(e);
} finally {
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
throw DbException.convert(e);
}
}
}
}
private synchronized Session openSession(ConnectionInfo ci) {
boolean ifExists = ci.removeProperty("IFEXISTS", false);
boolean ignoreUnknownSetting = ci.removeProperty("IGNORE_UNKNOWN_SETTINGS", false);
String cipher = ci.removeProperty("CIPHER", null);
String init = ci.removeProperty("INIT", null);
Session session;
while (true) {
session = openSession(ci, ifExists, cipher);
......@@ -191,6 +164,17 @@ public class Engine {
}
}
}
if (init != null) {
try {
CommandInterface command = session.prepareCommand(init, Integer.MAX_VALUE);
command.executeUpdate();
} catch (DbException e) {
if (!ignoreUnknownSetting) {
session.close();
throw e;
}
}
}
session.setAllowLiterals(false);
session.commit(true);
session.getDatabase().getTrace(Trace.SESSION).info("connected #" + session.getId());
......
......@@ -12,10 +12,17 @@ import org.h2.engine.ConnectionInfo;
import java.util.Properties;
/**
* Test the ConnectionInfo class.
*
* @author Kerry Sainsbury
*/
public class TestConnectionInfo extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String[] a) throws Exception {
TestBase.createCaller().init().test();
}
......@@ -26,7 +33,7 @@ public class TestConnectionInfo extends TestBase {
"jdbc:h2:mem:testdb" +
";LOG=2" +
";ACCESS_MODE_DATA=rws" +
";INIT=INSERT this...\\;UPDATE that..." +
";INIT=CREATE this...\\;INSERT that..." +
";IFEXISTS=TRUE",
info);
......@@ -34,8 +41,9 @@ public class TestConnectionInfo extends TestBase {
assertEquals("2", connectionInfo.getProperty("LOG", ""));
assertEquals("rws", connectionInfo.getProperty("ACCESS_MODE_DATA", ""));
assertEquals("INSERT this...;UPDATE that...", connectionInfo.getProperty("INIT", ""));
assertEquals("CREATE this...;INSERT that...", connectionInfo.getProperty("INIT", ""));
assertEquals("TRUE", connectionInfo.getProperty("IFEXISTS", ""));
assertEquals("undefined", connectionInfo.getProperty("CACHE_TYPE", "undefined"));
}
}
......@@ -6,12 +6,13 @@
*/
package org.h2.test.server;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
import java.sql.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
/**
* Tests INIT command within embedded/server mode.
......@@ -30,6 +31,7 @@ public class TestInit extends TestBase {
public void test() throws Exception {
// Create two scripts that we will run via "INIT"
IOUtils.createDirs(baseDir + "/test-init-1.sql");
FileWriter fw = new FileWriter(baseDir + "/test-init-1.sql");
PrintWriter writer = new PrintWriter(fw);
......@@ -67,6 +69,6 @@ public class TestInit extends TestBase {
IOUtils.delete(baseDir + "/test-init-1.sql");
IOUtils.delete(baseDir + "/test-init-2.sql");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论