提交 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,8 +60,8 @@ 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.
There was no unit test with the other value. So changing them was potentially dangerous
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).
</li><li>Disabling the page store is no longer supported. The old storage mechanism
......
......@@ -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());
......
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import org.h2.test.TestBase;
import org.h2.engine.ConnectionInfo;
import java.util.Properties;
/**
*/
public class TestConnectionInfo extends TestBase {
public static void main(String[] a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
Properties info = new Properties();
ConnectionInfo connectionInfo = new ConnectionInfo(
"jdbc:h2:mem:testdb" +
";LOG=2" +
";ACCESS_MODE_DATA=rws" +
";INIT=INSERT this...\\;UPDATE that..." +
";IFEXISTS=TRUE",
info);
assertEquals("jdbc:h2:mem:testdb", connectionInfo.getURL());
assertEquals("2", connectionInfo.getProperty("LOG", ""));
assertEquals("rws", connectionInfo.getProperty("ACCESS_MODE_DATA", ""));
assertEquals("INSERT this...;UPDATE 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,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import org.h2.test.TestBase;
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();
}
public void test() throws Exception {
Properties info = new Properties();
ConnectionInfo connectionInfo = new ConnectionInfo(
"jdbc:h2:mem:testdb" +
";LOG=2" +
";ACCESS_MODE_DATA=rws" +
";INIT=CREATE this...\\;INSERT that..." +
";IFEXISTS=TRUE",
info);
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,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
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;
/**
* Tests INIT command within embedded/server mode.
*/
public class TestInit extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String[] a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
// Create two scripts that we will run via "INIT"
FileWriter fw = new FileWriter(baseDir + "/test-init-1.sql");
PrintWriter writer = new PrintWriter(fw);
writer.println("create table test(id int identity, name varchar);");
writer.println("insert into test(name) values('cat');");
writer.close();
fw = new FileWriter(baseDir + "/test-init-2.sql");
writer = new PrintWriter(fw);
writer.println("insert into test(name) values('dog');");
writer.close();
// Make the database connection, and run the two scripts
deleteDb("initDb");
Connection conn = getConnection("initDb;" +
"INIT=" +
"RUNSCRIPT FROM '" + baseDir + "/test-init-1.sql'\\;" +
"RUNSCRIPT FROM '" + baseDir + "/test-init-2.sql'");
Statement stat = conn.createStatement();
// Confirm our scripts have run by loading the data they inserted
ResultSet rs = stat.executeQuery("select name from test order by name");
assertTrue(rs.next());
assertEquals("cat", rs.getString(1));
assertTrue(rs.next());
assertEquals("dog", rs.getString(1));
assertFalse(rs.next());
conn.close();
deleteDb("initDb");
IOUtils.delete(baseDir + "/test-init-1.sql");
IOUtils.delete(baseDir + "/test-init-2.sql");
}
}
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.server;
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.
*/
public class TestInit extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String[] a) throws Exception {
TestBase.createCaller().init().test();
}
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);
writer.println("create table test(id int identity, name varchar);");
writer.println("insert into test(name) values('cat');");
writer.close();
fw = new FileWriter(baseDir + "/test-init-2.sql");
writer = new PrintWriter(fw);
writer.println("insert into test(name) values('dog');");
writer.close();
// Make the database connection, and run the two scripts
deleteDb("initDb");
Connection conn = getConnection("initDb;" +
"INIT=" +
"RUNSCRIPT FROM '" + baseDir + "/test-init-1.sql'\\;" +
"RUNSCRIPT FROM '" + baseDir + "/test-init-2.sql'");
Statement stat = conn.createStatement();
// Confirm our scripts have run by loading the data they inserted
ResultSet rs = stat.executeQuery("select name from test order by name");
assertTrue(rs.next());
assertEquals("cat", rs.getString(1));
assertTrue(rs.next());
assertEquals("dog", rs.getString(1));
assertFalse(rs.next());
conn.close();
deleteDb("initDb");
IOUtils.delete(baseDir + "/test-init-1.sql");
IOUtils.delete(baseDir + "/test-init-2.sql");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论