提交 16894c19 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 291: Allow org.h2.value.Value as FUNCTION ALIAS params and return value.

上级 446add93
...@@ -18,7 +18,8 @@ Change Log ...@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Issue 265: Linked tables: auto-reconnect if the backside connection is lost <ul><li>Issue 291: Allow org.h2.value.Value as FUNCTION ALIAS params and return value.
</li><li>Issue 265: Linked tables: auto-reconnect if the backside connection is lost
(workaround for the MySQL problem that disconnects after 8 hours of inactivity). (workaround for the MySQL problem that disconnects after 8 hours of inactivity).
</li><li>Linked tables: the index conditions was sometimes not used when querying the remote database. </li><li>Linked tables: the index conditions was sometimes not used when querying the remote database.
</li><li>Issue 294: OSGi: the versions were missing in manifest package exports. </li><li>Issue 294: OSGi: the versions were missing in manifest package exports.
......
...@@ -1475,11 +1475,12 @@ to use <code>java.lang.Integer</code> instead. ...@@ -1475,11 +1475,12 @@ to use <code>java.lang.Integer</code> instead.
</p> </p>
<p> <p>
SQL types are mapped to Java classes and vice-versa as in the JDBC API. For details, see <a href="datatypes.html">Data Types</a>. SQL types are mapped to Java classes and vice-versa as in the JDBC API. For details, see <a href="datatypes.html">Data Types</a>.
There are two special cases: <code>java.lang.Object</code> is mapped to There are a few special cases: <code>java.lang.Object</code> is mapped to
<code>OTHER</code> (a serialized object). Therefore, <code>OTHER</code> (a serialized object). Therefore,
<code>java.lang.Object</code> can not be used <code>java.lang.Object</code> can not be used
to match all SQL types (matching all SQL types is not supported). The second special case is <code>Object[]</code>: to match all SQL types (matching all SQL types is not supported). The second special case is <code>Object[]</code>:
arrays of any class are mapped to <code>ARRAY</code>. arrays of any class are mapped to <code>ARRAY</code>.
Objects of type <code>org.h2.value.Value</code> (the internal value class) are passed through without conversion.
</p> </p>
<h3>Functions That Require a Connection</h3> <h3>Functions That Require a Connection</h3>
......
...@@ -349,8 +349,13 @@ public class FunctionAlias extends SchemaObjectBase { ...@@ -349,8 +349,13 @@ public class FunctionAlias extends SchemaObjectBase {
} }
int type = DataType.getTypeFromClass(paramClass); int type = DataType.getTypeFromClass(paramClass);
Value v = args[a].getValue(session); Value v = args[a].getValue(session);
Object o;
if (Value.class.isAssignableFrom(paramClass)) {
o = v;
} else {
v = v.convertTo(type); v = v.convertTo(type);
Object o = v.getObject(); o = v.getObject();
}
if (o == null) { if (o == null) {
if (paramClass.isPrimitive()) { if (paramClass.isPrimitive()) {
if (columnList) { if (columnList) {
...@@ -401,6 +406,9 @@ public class FunctionAlias extends SchemaObjectBase { ...@@ -401,6 +406,9 @@ public class FunctionAlias extends SchemaObjectBase {
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
if (Value.class.isAssignableFrom(method.getReturnType())) {
return (Value) returnValue;
}
Value ret = DataType.convertToValue(session, returnValue, dataType); Value ret = DataType.convertToValue(session, returnValue, dataType);
return ret.convertTo(dataType); return ret.convertTo(dataType);
} finally { } finally {
......
...@@ -29,6 +29,7 @@ import org.h2.test.TestBase; ...@@ -29,6 +29,7 @@ import org.h2.test.TestBase;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.value.Value;
/** /**
* Tests for user defined functions and aggregates. * Tests for user defined functions and aggregates.
...@@ -63,10 +64,30 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -63,10 +64,30 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testAggregate(); testAggregate();
testFunctions(); testFunctions();
testFileRead(); testFileRead();
testValue();
deleteDb("functions"); deleteDb("functions");
IOUtils.deleteRecursive(TEMP_DIR, true); IOUtils.deleteRecursive(TEMP_DIR, true);
} }
private void testValue() throws SQLException {
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
ResultSet rs;
stat.execute("create alias TO_CHAR for \"" + getClass().getName() + ".toChar\"");
rs = stat.executeQuery("call TO_CHAR(TIMESTAMP '2001-02-03 04:05:06', 'format')");
rs.next();
assertEquals("2001-02-03 04:05:06.0", rs.getString(1));
stat.execute("drop alias TO_CHAR");
conn.close();
}
public static Value toChar(Value... args) {
if (args.length == 0) {
return null;
}
return args[0].convertTo(Value.STRING);
}
private void testDefaultConnection() throws SQLException { private void testDefaultConnection() throws SQLException {
Connection conn = getConnection("functions;DEFAULT_CONNECTION=TRUE"); Connection conn = getConnection("functions;DEFAULT_CONNECTION=TRUE");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论