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

Reading input streams from the classpath is now supported.

上级 b9e57e4d
......@@ -124,13 +124,16 @@ statements; each statement must end with ';'. This command can be used to
restore a database from a backup. The password must be in single quotes; it is
case sensitive and can contain spaces.
Instead of a file name, an URL may be used.
To read a stream from the classpath, use the prefix 'classpath:'.
The compression algorithm must match the one used when creating the script. When
using encryption, only DEFLATE and LZF are supported (LZF is faster but uses more space).
Instead of a file, an URL may be used.
Admin rights are required to execute this command.
","
RUNSCRIPT FROM 'backup'
RUNSCRIPT FROM 'backup.sql'
"
"Commands (DML)","SCRIPT","
......@@ -3310,8 +3313,10 @@ parsed as NULL. All columns of type VARCHAR.
The BOM (the byte-order-mark) character 0xfeff at the beginning of the file is ignored.
This function can be used like a table: ""SELECT * FROM CSVREAD(...)"".
Instead of a file, an URL may be used, for example
""jar:file:///c:/temp/example.zip!/org/example/nested.zip"".
""jar:file:///c:/temp/example.zip!/org/example/nested.csv"".
To read a stream from the classpath, use the prefix ""classpath:"".
Admin rights are required to execute this command.
","
......@@ -3322,6 +3327,7 @@ CALL CSVREAD('test2.csv', 'ID|NAME', 'UTF-8', '|');
-- Read a semicolon-separated file
SELECT * FROM CSVREAD('data/test.csv', NULL, NULL, ';');
SELECT ""Last Name"" FROM CSVREAD('address.csv');
SELECT ""Last Name"" FROM CSVREAD('classpath:/org/acme/data/address.csv');
"
"Functions (System)","CSVWRITE","
......@@ -3369,7 +3375,11 @@ FILE_READ(fileNameString [,encodingString])
Returns the contents of a file. If only one parameter is supplied, the data are
returned as a BLOB. If two parameters are used, the data is returned as a CLOB
(text). The second parameter is the character set to use, NULL meaning the
default character set for this system. File names and URLs are supported.
default character set for this system.
File names and URLs are supported.
To read a stream from the classpath, use the prefix ""classpath:"".
Admin rights are required to execute this command.
","
SELECT LENGTH(FILE_READ('~/.h2.server.properties')) LEN;
......
......@@ -1428,6 +1428,12 @@ As an example, to use the the <code>nio</code> file system, use the following da
To register a new file system, extend the classes <code>org.h2.store.fs.FileSystem, FileObject</code>,
and call the method <code>FileSystem.register</code> before using it.
</p>
<p>
For input streams (but not for random access files), URLs may be used in addition to the registered file systems.
Example: <code>jar:file:///c:/temp/example.zip!/org/example/nested.csv</code>.
To read a stream from the classpath, use the prefix <code>classpath:</code>, as in
<code>classpath:org/h2/samples/newsfeed.sql</code>.
</p>
<h2 id="database_upgrade">Database Upgrade</h2>
<p>
......
......@@ -34,6 +34,8 @@ public class FileSystemDisk extends FileSystem {
// could maybe implemented using some other means
private static final boolean IS_FILE_SYSTEM_CASE_INSENSITIVE = File.separatorChar == '\\';
private static final String CLASSPATH_PREFIX = "classpath:";
protected FileSystemDisk() {
// nothing to do
}
......@@ -394,6 +396,14 @@ public class FileSystemDisk extends FileSystem {
public InputStream openFileInputStream(String fileName) throws IOException {
if (fileName.indexOf(':') > 1) {
// if the : is in position 1, a windows file access is assumed: C:.. or D:
if (fileName.startsWith(CLASSPATH_PREFIX)) {
fileName = fileName.substring(CLASSPATH_PREFIX.length());
InputStream in = getClass().getClassLoader().getResourceAsStream(fileName);
if (in == null) {
throw new FileNotFoundException("resource " + fileName);
}
return in;
}
// otherwise an URL is assumed
URL url = new URL(fileName);
InputStream in = url.openStream();
......
......@@ -34,6 +34,7 @@ public class TestRunscript extends TestBase implements Trigger {
}
public void test() throws Exception {
testRunscriptFromClasspath();
testCancelScript();
testEncoding();
testClobPrimaryKey();
......@@ -42,6 +43,16 @@ public class TestRunscript extends TestBase implements Trigger {
deleteDb("runscript");
}
private void testRunscriptFromClasspath() throws Exception {
deleteDb("runscript");
Connection conn;
conn = getConnection("runscript");
Statement stat = conn.createStatement();
stat.execute("runscript from 'classpath:org/h2/samples/newsfeed.sql'");
stat.execute("select * from version");
conn.close();
}
private void testCancelScript() throws Exception {
deleteDb("runscript");
Connection conn;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论