提交 601c59ba authored 作者: Thomas Mueller's avatar Thomas Mueller

Script tool: Now, SCRIPT ... TO is always used (for higher speed and lower disk space usage).

上级 31f7cb05
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
*/ */
package org.h2.tools; package org.h2.tools;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -14,9 +12,7 @@ import java.sql.Statement; ...@@ -14,9 +12,7 @@ import java.sql.Statement;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.DbException;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.Tool; import org.h2.util.Tool;
...@@ -148,15 +144,7 @@ public class CreateCluster extends Tool { ...@@ -148,15 +144,7 @@ public class CreateCluster extends Tool {
// backup // backup
Script script = new Script(); Script script = new Script();
script.setOut(out); script.setOut(out);
OutputStream scriptOut = null; Script.process(connSource, scriptFile, "", "");
try {
scriptOut = FileUtils.newOutputStream(scriptFile, false);
Script.process(connSource, scriptOut);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
IOUtils.closeSilently(scriptOut);
}
// delete the target database and then restore // delete the target database and then restore
connTarget = DriverManager.getConnection( connTarget = DriverManager.getConnection(
......
...@@ -5,17 +5,11 @@ ...@@ -5,17 +5,11 @@
*/ */
package org.h2.tools; package org.h2.tools;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import org.h2.message.DbException;
import org.h2.store.fs.FileUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Tool; import org.h2.util.Tool;
...@@ -58,7 +52,8 @@ public class Script extends Tool { ...@@ -58,7 +52,8 @@ public class Script extends Tool {
String user = ""; String user = "";
String password = ""; String password = "";
String file = "backup.sql"; String file = "backup.sql";
String options1 = null, options2 = null; String options1 = "";
String options2 = "";
for (int i = 0; args != null && i < args.length; i++) { for (int i = 0; args != null && i < args.length; i++) {
String arg = args[i]; String arg = args[i];
if (arg.equals("-url")) { if (arg.equals("-url")) {
...@@ -103,90 +98,47 @@ public class Script extends Tool { ...@@ -103,90 +98,47 @@ public class Script extends Tool {
showUsage(); showUsage();
throw new SQLException("URL not set"); throw new SQLException("URL not set");
} }
if (options1 != null) { process(url, user, password, file, options1, options2);
processScript(url, user, password, file, options1, options2);
} else {
execute(url, user, password, file);
}
}
private static void processScript(String url, String user, String password,
String fileName, String options1, String options2) throws SQLException {
Connection conn = null;
Statement stat = null;
try {
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
String sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
stat.execute(sql);
} finally {
JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(conn);
}
}
/**
* Backs up a database to a SQL script file.
*
* @param url the database URL
* @param user the user name
* @param password the password
* @param fileName the script file
*/
public static void execute(String url, String user, String password,
String fileName) throws SQLException {
OutputStream o = null;
try {
o = FileUtils.newOutputStream(fileName, false);
execute(url, user, password, o);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
IOUtils.closeSilently(o);
}
} }
/** /**
* Backs up a database to a stream. The stream is not closed. * Backs up a database to a stream.
* *
* @param url the database URL * @param url the database URL
* @param user the user name * @param user the user name
* @param password the password * @param password the password
* @param out the output stream * @param fileName the target file name
* @param options1 the options before the file name (may be an empty string)
* @param options2 the options after the file name (may be an empty string)
*/ */
public static void execute(String url, String user, String password, public static void process(String url, String user, String password,
OutputStream out) throws SQLException { String fileName, String options1, String options2) throws SQLException {
Connection conn = null; Connection conn = null;
try { try {
org.h2.Driver.load(); org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password); conn = DriverManager.getConnection(url, user, password);
process(conn, out); process(conn, fileName, options1, options2);
} finally { } finally {
JdbcUtils.closeSilently(conn); JdbcUtils.closeSilently(conn);
} }
} }
/** /**
* Backs up a database to a stream. The stream is not closed. * Backs up a database to a stream. The stream is not closed.
* The connection is not closed. * The connection is not closed.
* *
* @param conn the connection * @param conn the connection
* @param out the output stream * @param fileName the target file name
* @param options1 the options before the file name
* @param options2 the options after the file name
*/ */
static void process(Connection conn, OutputStream out) throws SQLException { public static void process(Connection conn,
String fileName, String options1, String options2) throws SQLException {
Statement stat = null; Statement stat = null;
try { try {
stat = conn.createStatement(); stat = conn.createStatement();
PrintWriter writer = new PrintWriter(IOUtils.getBufferedWriter(out)); String sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
ResultSet rs = stat.executeQuery("SCRIPT"); stat.execute(sql);
while (rs.next()) {
String s = rs.getString(1);
writer.println(s);
}
writer.flush();
} finally { } finally {
JdbcUtils.closeSilently(stat); JdbcUtils.closeSilently(stat);
} }
......
...@@ -54,7 +54,7 @@ public class Compact { ...@@ -54,7 +54,7 @@ public class Compact {
String user, String password) throws SQLException { String user, String password) throws SQLException {
String url = "jdbc:h2:" + dir + "/" + dbName; String url = "jdbc:h2:" + dir + "/" + dbName;
String file = "data/test.sql"; String file = "data/test.sql";
Script.execute(url, user, password, file); Script.process(url, user, password, file, "", "");
DeleteDbFiles.execute(dir, dbName, true); DeleteDbFiles.execute(dir, dbName, true);
RunScript.execute(url, user, password, file, null, false); RunScript.execute(url, user, password, file, null, false);
FileUtils.delete(file); FileUtils.delete(file);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论