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

INIT

上级 11ecd55b
...@@ -23,7 +23,7 @@ Change Log ...@@ -23,7 +23,7 @@ Change Log
</li><li>Creating a database was delayed about 2 seconds if the directory didn't exist. </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 </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' 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> </li></ul>
<h2>Version 1.2.129 (2010-02-19)</h2> <h2>Version 1.2.129 (2010-02-19)</h2>
...@@ -60,8 +60,8 @@ Change Log ...@@ -60,8 +60,8 @@ Change Log
</li><li>The following system properties are no longer supported: </li><li>The following system properties are no longer supported:
h2.overflowExceptions, h2.optimizeDropDependencies, h2.optimizeGroupSorted, h2.overflowExceptions, h2.optimizeDropDependencies, h2.optimizeGroupSorted,
h2.optimizeMinMax, h2.optimizeNot, h2.optimizeIn, h2.optimizeInJoin, h2.reuseSpace*. 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 There was no unit test with the other value. So changing them was potentially dangerous
(not a lot, but still). (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). </li><li>The setting LOG has currently no effect (it only had an effect when the page store was disabled).
</li><li>Disabling the page store is no longer supported. The old storage mechanism </li><li>Disabling the page store is no longer supported. The old storage mechanism
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
*/ */
package org.h2.engine; package org.h2.engine;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap; import java.util.HashMap;
import org.h2.command.CommandInterface; import org.h2.command.CommandInterface;
import org.h2.command.Parser; import org.h2.command.Parser;
...@@ -119,19 +117,11 @@ public class Engine { ...@@ -119,19 +117,11 @@ public class Engine {
throw DbException.convert(e); throw DbException.convert(e);
} }
} }
String init = ci.removeProperty("INIT", null);
Session session = openSession(ci); Session session = openSession(ci);
validateUserAndPassword(true); validateUserAndPassword(true);
if (backup != null) { if (backup != null) {
session.setConnectionInfo(backup); session.setConnectionInfo(backup);
} }
if (init != null) {
runInitScripts(session, init);
}
return session; return session;
} catch (DbException e) { } catch (DbException e) {
if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) { if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
...@@ -141,28 +131,11 @@ public class Engine { ...@@ -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) { private synchronized Session openSession(ConnectionInfo ci) {
boolean ifExists = ci.removeProperty("IFEXISTS", false); boolean ifExists = ci.removeProperty("IFEXISTS", false);
boolean ignoreUnknownSetting = ci.removeProperty("IGNORE_UNKNOWN_SETTINGS", false); boolean ignoreUnknownSetting = ci.removeProperty("IGNORE_UNKNOWN_SETTINGS", false);
String cipher = ci.removeProperty("CIPHER", null); String cipher = ci.removeProperty("CIPHER", null);
String init = ci.removeProperty("INIT", null);
Session session; Session session;
while (true) { while (true) {
session = openSession(ci, ifExists, cipher); session = openSession(ci, ifExists, cipher);
...@@ -191,6 +164,17 @@ public class Engine { ...@@ -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.setAllowLiterals(false);
session.commit(true); session.commit(true);
session.getDatabase().getTrace(Trace.SESSION).info("connected #" + session.getId()); session.getDatabase().getTrace(Trace.SESSION).info("connected #" + session.getId());
......
/* /*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: H2 Group * Initial Developer: H2 Group
*/ */
package org.h2.test.db; package org.h2.test.db;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.engine.ConnectionInfo; import org.h2.engine.ConnectionInfo;
import java.util.Properties; import java.util.Properties;
/** /**
*/ * Test the ConnectionInfo class.
public class TestConnectionInfo extends TestBase { *
* @author Kerry Sainsbury
*/
public static void main(String[] a) throws Exception { public class TestConnectionInfo extends TestBase {
TestBase.createCaller().init().test();
} /**
* Run just this test.
public void test() throws Exception { *
Properties info = new Properties(); * @param a ignored
ConnectionInfo connectionInfo = new ConnectionInfo( */
"jdbc:h2:mem:testdb" + public static void main(String[] a) throws Exception {
";LOG=2" + TestBase.createCaller().init().test();
";ACCESS_MODE_DATA=rws" + }
";INIT=INSERT this...\\;UPDATE that..." +
";IFEXISTS=TRUE", public void test() throws Exception {
info); Properties info = new Properties();
ConnectionInfo connectionInfo = new ConnectionInfo(
assertEquals("jdbc:h2:mem:testdb", connectionInfo.getURL()); "jdbc:h2:mem:testdb" +
";LOG=2" +
assertEquals("2", connectionInfo.getProperty("LOG", "")); ";ACCESS_MODE_DATA=rws" +
assertEquals("rws", connectionInfo.getProperty("ACCESS_MODE_DATA", "")); ";INIT=CREATE this...\\;INSERT that..." +
assertEquals("INSERT this...;UPDATE that...", connectionInfo.getProperty("INIT", "")); ";IFEXISTS=TRUE",
assertEquals("TRUE", connectionInfo.getProperty("IFEXISTS", "")); info);
assertEquals("undefined", connectionInfo.getProperty("CACHE_TYPE", "undefined"));
} assertEquals("jdbc:h2:mem:testdb", connectionInfo.getURL());
}
assertEquals("2", connectionInfo.getProperty("LOG", ""));
assertEquals("rws", connectionInfo.getProperty("ACCESS_MODE_DATA", ""));
assertEquals("CREATE this...;INSERT that...", connectionInfo.getProperty("INIT", ""));
assertEquals("TRUE", connectionInfo.getProperty("IFEXISTS", ""));
assertEquals("undefined", connectionInfo.getProperty("CACHE_TYPE", "undefined"));
}
}
/* /*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: H2 Group * Initial Developer: H2 Group
*/ */
package org.h2.test.server; package org.h2.test.server;
import org.h2.test.TestBase; import java.io.FileWriter;
import org.h2.util.IOUtils; import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.*; import java.sql.ResultSet;
import java.io.FileWriter; import java.sql.Statement;
import java.io.PrintWriter; import org.h2.test.TestBase;
import org.h2.util.IOUtils;
/**
* Tests INIT command within embedded/server mode. /**
*/ * Tests INIT command within embedded/server mode.
public class TestInit extends TestBase { */
public class TestInit extends TestBase {
/**
* Run just this test. /**
* * Run just this test.
* @param a ignored *
*/ * @param a ignored
public static void main(String[] a) throws Exception { */
TestBase.createCaller().init().test(); public static void main(String[] a) throws Exception {
} TestBase.createCaller().init().test();
}
public void test() throws Exception {
public void test() throws Exception {
// Create two scripts that we will run via "INIT"
FileWriter fw = new FileWriter(baseDir + "/test-init-1.sql"); // Create two scripts that we will run via "INIT"
IOUtils.createDirs(baseDir + "/test-init-1.sql");
PrintWriter writer = new PrintWriter(fw); FileWriter fw = new FileWriter(baseDir + "/test-init-1.sql");
writer.println("create table test(id int identity, name varchar);");
writer.println("insert into test(name) values('cat');"); PrintWriter writer = new PrintWriter(fw);
writer.close(); writer.println("create table test(id int identity, name varchar);");
writer.println("insert into test(name) values('cat');");
fw = new FileWriter(baseDir + "/test-init-2.sql"); writer.close();
writer = new PrintWriter(fw);
writer.println("insert into test(name) values('dog');"); fw = new FileWriter(baseDir + "/test-init-2.sql");
writer.close(); writer = new PrintWriter(fw);
writer.println("insert into test(name) values('dog');");
// Make the database connection, and run the two scripts writer.close();
deleteDb("initDb");
Connection conn = getConnection("initDb;" + // Make the database connection, and run the two scripts
"INIT=" + deleteDb("initDb");
"RUNSCRIPT FROM '" + baseDir + "/test-init-1.sql'\\;" + Connection conn = getConnection("initDb;" +
"RUNSCRIPT FROM '" + baseDir + "/test-init-2.sql'"); "INIT=" +
"RUNSCRIPT FROM '" + baseDir + "/test-init-1.sql'\\;" +
Statement stat = conn.createStatement(); "RUNSCRIPT FROM '" + baseDir + "/test-init-2.sql'");
// Confirm our scripts have run by loading the data they inserted Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select name from test order by name");
// Confirm our scripts have run by loading the data they inserted
assertTrue(rs.next()); ResultSet rs = stat.executeQuery("select name from test order by name");
assertEquals("cat", rs.getString(1));
assertTrue(rs.next());
assertTrue(rs.next()); assertEquals("cat", rs.getString(1));
assertEquals("dog", rs.getString(1));
assertTrue(rs.next());
assertFalse(rs.next()); assertEquals("dog", rs.getString(1));
conn.close(); assertFalse(rs.next());
deleteDb("initDb");
conn.close();
IOUtils.delete(baseDir + "/test-init-1.sql"); deleteDb("initDb");
IOUtils.delete(baseDir + "/test-init-2.sql");
IOUtils.delete(baseDir + "/test-init-1.sql");
} IOUtils.delete(baseDir + "/test-init-2.sql");
} }
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论