提交 5fb0c18e authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 140: the Script tool now supports writing to a stream.

上级 8c61fb9d
...@@ -6,14 +6,13 @@ ...@@ -6,14 +6,13 @@
*/ */
package org.h2.tools; package org.h2.tools;
import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Writer;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import org.h2.util.FileUtils; import org.h2.util.FileUtils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
...@@ -130,6 +129,18 @@ public class Script extends Tool { ...@@ -130,6 +129,18 @@ public class Script extends Tool {
new Script().process(url, user, password, fileName); new Script().process(url, user, password, fileName);
} }
/**
* Backs up a database to a stream. The stream is not closed.
*
* @param url the database URL
* @param user the user name
* @param password the password
* @param out the output stream
*/
public static void execute(String url, String user, String password, OutputStream out) throws SQLException {
new Script().process(url, user, password, out);
}
/** /**
* Backs up a database to a SQL script file. * Backs up a database to a SQL script file.
* *
...@@ -139,25 +150,40 @@ public class Script extends Tool { ...@@ -139,25 +150,40 @@ public class Script extends Tool {
* @param fileName the script file * @param fileName the script file
*/ */
void process(String url, String user, String password, String fileName) throws SQLException { void process(String url, String user, String password, String fileName) throws SQLException {
OutputStream out = null;
try {
out = FileUtils.openFileOutputStream(fileName, false);
process(url, user, password, out);
} finally {
IOUtils.closeSilently(out);
}
}
/**
* Backs up a database to a stream. The stream is not closed.
*
* @param url the database URL
* @param user the user name
* @param password the password
* @param out the output stream
*/
void process(String url, String user, String password, OutputStream out) throws SQLException {
Connection conn = null; Connection conn = null;
Statement stat = null; Statement stat = null;
Writer fileWriter = null;
try { try {
org.h2.Driver.load(); org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password); conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
fileWriter = IOUtils.getWriter(FileUtils.openFileOutputStream(fileName, false)); PrintWriter writer = new PrintWriter(IOUtils.getWriter(out));
PrintWriter writer = new PrintWriter(fileWriter);
ResultSet rs = stat.executeQuery("SCRIPT"); ResultSet rs = stat.executeQuery("SCRIPT");
while (rs.next()) { while (rs.next()) {
String s = rs.getString(1); String s = rs.getString(1);
writer.println(s); writer.println(s);
} }
writer.close(); writer.flush();
} finally { } finally {
JdbcUtils.closeSilently(stat); JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(conn); JdbcUtils.closeSilently(conn);
IOUtils.closeSilently(fileWriter);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论