提交 40e873c7 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 1c06fd6a
......@@ -1407,7 +1407,7 @@ public class Parser {
private void parseSelectSimpleSelectPart(Select command) throws SQLException {
Select temp = currentSelect;
// make sure aggregate functions will not work in TOP and LIMIT clauses
// make sure aggregate functions will not work in TOP and LIMIT
currentSelect = null;
if (readIf("TOP")) {
// can't read more complex expressions here because
......
......@@ -301,7 +301,6 @@ public class SysProperties {
* System property <code>h2.reuseSpaceQuickly</code> (default: true).<br />
* Reuse space in database files quickly.
*/
private int test;
public static final boolean REUSE_SPACE_QUICKLY = getBooleanSetting("h2.reuseSpaceQuickly", true);
/**
......
......@@ -4,6 +4,10 @@
*/
package org.h2.expression;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
......@@ -33,6 +37,8 @@ import org.h2.table.LinkSchema;
import org.h2.table.TableFilter;
import org.h2.tools.CompressTool;
import org.h2.tools.Csv;
import org.h2.util.AutoCloseInputStream;
import org.h2.util.FileUtils;
import org.h2.util.MathUtils;
import org.h2.util.MemoryUtils;
import org.h2.util.ObjectArray;
......@@ -46,6 +52,7 @@ import org.h2.value.ValueBytes;
import org.h2.value.ValueDate;
import org.h2.value.ValueDouble;
import org.h2.value.ValueInt;
import org.h2.value.ValueLob;
import org.h2.value.ValueLong;
import org.h2.value.ValueNull;
import org.h2.value.ValueResultSet;
......@@ -84,7 +91,8 @@ public class Function extends Expression implements FunctionCall {
public static final int IFNULL = 200, CASEWHEN = 201, CONVERT = 202, CAST = 203, COALESCE = 204, NULLIF = 205,
CASE = 206, NEXTVAL = 207, CURRVAL = 208, ARRAY_GET = 209, CSVREAD = 210, CSVWRITE = 211,
MEMORY_FREE = 212, MEMORY_USED = 213, LOCK_MODE = 214, SCHEMA = 215, SESSION_ID = 216, ARRAY_LENGTH = 217,
LINK_SCHEMA = 218, GREATEST = 219, LEAST = 220, CANCEL_SESSION = 221, SET = 222, TABLE = 223, TABLE_DISTINCT = 224;
LINK_SCHEMA = 218, GREATEST = 219, LEAST = 220, CANCEL_SESSION = 221, SET = 222, TABLE = 223, TABLE_DISTINCT = 224,
FILE_READ = 225;
private static final int VAR_ARGS = -1;
......@@ -281,6 +289,7 @@ public class Function extends Expression implements FunctionCall {
addFunctionWithNull("GREATEST", GREATEST, VAR_ARGS, Value.NULL);
addFunction("CANCEL_SESSION", CANCEL_SESSION, 1, Value.BOOLEAN);
addFunction("SET", SET, 2, Value.NULL, false, false);
addFunction("FILE_READ", FILE_READ, VAR_ARGS, Value.NULL, false, true);
// TableFunction
addFunctionWithNull("TABLE", TABLE, VAR_ARGS, Value.RESULT_SET);
......@@ -1024,6 +1033,28 @@ public class Function extends Expression implements FunctionCall {
result = v1;
break;
}
case FILE_READ: {
session.getUser().checkAdmin();
String fileName = v0.getString();
boolean blob = args.length == 1;
try {
InputStream in = new AutoCloseInputStream(FileUtils.openFileInputStream(fileName));
if (blob) {
result = ValueLob.createBlob(in, -1, database);
} else {
Reader reader;
if (v1 == ValueNull.INSTANCE) {
reader = new InputStreamReader(in);
} else {
reader = new InputStreamReader(in, v1.getString());
}
result = ValueLob.createClob(reader, -1, database);
}
} catch (IOException e) {
throw Message.convertIOException(e, fileName);
}
break;
}
default:
throw Message.getInternalError("type=" + info.type);
}
......@@ -1459,6 +1490,7 @@ public class Function extends Expression implements FunctionCall {
case LTRIM:
case RTRIM:
case TRIM:
case FILE_READ:
min = 1;
max = 2;
break;
......@@ -1607,6 +1639,17 @@ public class Function extends Expression implements FunctionCall {
}
break;
}
case FILE_READ: {
if (args.length == 1) {
dataType = Value.BLOB;
} else {
dataType = Value.CLOB;
}
precision = Integer.MAX_VALUE;
scale = 0;
displaySize = Integer.MAX_VALUE;
break;
}
default:
dataType = info.dataType;
precision = 0;
......
......@@ -2694,6 +2694,20 @@ Returns NULL otherwise.
DATABASE_PATH()
"
"Functions (System)","FILE_READ","
FILE_READ(fileNameString [,encodingString]): value
","
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.
Admin rights are required to execute this command.
","
SELECT LENGTH(FILE_READ('~/.h2.server.properties')) LEN;
SELECT FILE_READ('http://localhost:8182/stylesheet.css', NULL) CSS;
"
"Functions (System)","GREATEST","
GREATEST(aValue, bValue [,...]): value
","
......
......@@ -161,6 +161,8 @@ link to new changelog and roadmap, remove pages from google groups
Can sometimes not delete log file? need test case
History:
New function FILE_READ to read a file or from an URL.
Both binary and text data is supported.
Roadmap:
......
......@@ -5,6 +5,9 @@
package org.h2.test.db;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
......@@ -15,10 +18,12 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Properties;
import org.h2.api.AggregateFunction;
import org.h2.test.TestBase;
import org.h2.tools.SimpleResultSet;
import org.h2.util.IOUtils;
/**
* Tests for user defined functions and aggregates.
......@@ -30,6 +35,33 @@ public class TestFunctions extends TestBase {
public void test() throws Exception {
testAggregate();
testFunctions();
testFileRead();
}
public void testFileRead() throws Exception {
Connection conn = getConnection("functions");
stat = conn.createStatement();
File f = new File(baseDir + "/test.txt");
Properties prop = System.getProperties();
FileOutputStream out = new FileOutputStream(f);
prop.store(out, "");
out.close();
ResultSet rs = stat.executeQuery("SELECT LENGTH(FILE_READ('" + baseDir + "/test.txt')) LEN");
rs.next();
check(f.length(), rs.getInt(1));
rs = stat.executeQuery("SELECT FILE_READ('" + baseDir + "/test.txt') PROP");
rs.next();
Properties p2 = new Properties();
p2.load(rs.getBinaryStream(1));
check(prop.size(), p2.size());
rs = stat.executeQuery("SELECT FILE_READ('" + baseDir + "/test.txt', NULL) PROP");
rs.next();
String ps = rs.getString(1);
FileReader r = new FileReader(f);
String ps2 = IOUtils.readStringAndClose(r, -1);
check(ps, ps2);
f.delete();
conn.close();
}
public static class MedianString implements AggregateFunction {
......
......@@ -4,6 +4,8 @@
*/
package org.h2.test.unit;
import java.sql.Timestamp;
/**
* This is a self-destructor class to kill a long running process automatically after
* a pre-defined time. The class reads the number of minutes
......@@ -32,7 +34,8 @@ public class SelfDestructor extends Thread {
// ignore
}
}
System.out.println("Killing the process after " + minutes + " minutes");
String time = new Timestamp(System.currentTimeMillis()).toString();
System.out.println(time + " Killing the process after " + minutes + " minutes");
Runtime.getRuntime().halt(1);
}
};
......
......@@ -527,4 +527,5 @@ violate verysmallint eremainder iee cgi adjust estimation consumption occupy ikv
upgrading consecutive acting
simulates dispatcher servlets chf destruction separating consulting reached
unreferenced longest enum jira jackcess track unreleased processors nearest fits shadow
cmu cosh tanh sinh contrib bzip contiguous huffman bitwise des
\ No newline at end of file
cmu cosh tanh sinh contrib bzip contiguous huffman bitwise des
nederlands italy berlini destructor destruct elisabetta
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论