提交 d515a1c1 authored 作者: sam's avatar sam

Added the simple Oracle function ORA_HASH (+ tests) to make H2 more Oracle complaint.

To see what it does see http://docs.oracle.com/cd/B12037_01/server.101/b10759/functions097.htm
上级 ce473e15
......@@ -92,7 +92,7 @@ public class Function extends Expression implements FunctionCall {
STRINGDECODE = 80, STRINGTOUTF8 = 81, UTF8TOSTRING = 82,
XMLATTR = 83, XMLNODE = 84, XMLCOMMENT = 85, XMLCDATA = 86,
XMLSTARTDOC = 87, XMLTEXT = 88, REGEXP_REPLACE = 89, RPAD = 90,
LPAD = 91, CONCAT_WS = 92, TO_CHAR = 93, TRANSLATE = 94;
LPAD = 91, CONCAT_WS = 92, TO_CHAR = 93, TRANSLATE = 94, ORA_HASH = 95;
public static final int CURDATE = 100, CURTIME = 101, DATE_ADD = 102,
DATE_DIFF = 103, DAY_NAME = 104, DAY_OF_MONTH = 105,
......@@ -297,6 +297,7 @@ public class Function extends Expression implements FunctionCall {
addFunction("RPAD", RPAD, VAR_ARGS, Value.STRING);
addFunction("LPAD", LPAD, VAR_ARGS, Value.STRING);
addFunction("TO_CHAR", TO_CHAR, VAR_ARGS, Value.STRING);
addFunction("ORA_HASH", ORA_HASH, VAR_ARGS, Value.INT);
addFunction("TRANSLATE", TRANSLATE, 3, Value.STRING);
// date
......@@ -1394,6 +1395,11 @@ public class Function extends Expression implements FunctionCall {
v1.getInt(), v2 == null ? null : v2.getString(), false),
database.getMode().treatEmptyStringsAsNull);
break;
case ORA_HASH:
result = ValueLong.get(oraHash(v0.getString(),
v1 == null ? null : v1.getInt(),
v2 == null ? null : v2.getInt()));
break;
case TO_CHAR:
switch(v0.getType()){
case Value.TIME:
......@@ -2039,6 +2045,20 @@ public class Function extends Expression implements FunctionCall {
return new String(chars);
}
private static Integer oraHash(String s, Integer bucket, Integer seed) {
int hc = s.hashCode();
if (seed != null && seed.intValue() != 0) {
hc *= seed.intValue() * 17;
}
if (bucket == null || bucket.intValue() <= 0) {
// do nothing
} else {
hc %= bucket.intValue();
}
return hc;
}
@Override
public int getType() {
return dataType;
......@@ -2088,6 +2108,10 @@ public class Function extends Expression implements FunctionCall {
min = 1;
max = 3;
break;
case ORA_HASH:
min = 1;
max = 3;
break;
case REPLACE:
case LOCATE:
case INSTR:
......
......@@ -90,6 +90,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testNvl2();
testConcatWs();
testTruncate();
testOraHash();
testToCharFromDateTime();
testToCharFromNumber();
testToCharFromText();
......@@ -1221,6 +1222,18 @@ public class TestFunctions extends TestBase implements AggregateFunction {
conn.close();
}
private void testOraHash() throws SQLException {
deleteDb("functions");
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
String testStr = "foo";
assertResult(String.valueOf("foo".hashCode()), stat, String.format("SELECT ORA_HASH('%s') FROM DUAL", testStr));
assertResult(String.valueOf("foo".hashCode()), stat,
String.format("SELECT ORA_HASH('%s', 0) FROM DUAL", testStr));
assertResult(String.valueOf("foo".hashCode()), stat,
String.format("SELECT ORA_HASH('%s', 0, 0) FROM DUAL", testStr));
}
private void testToCharFromDateTime() throws SQLException {
deleteDb("functions");
Connection conn = getConnection("functions");
......@@ -1658,7 +1671,6 @@ public class TestFunctions extends TestBase implements AggregateFunction {
conn.close();
}
private void testGenerateSeries() throws SQLException {
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论