提交 cdeb93e4 authored 作者: Patrick Brielmayer's avatar Patrick Brielmayer

Added ADD_MONTHS functionality

上级 3d253279
...@@ -85,7 +85,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -85,7 +85,7 @@ public class Function extends Expression implements FunctionCall {
XMLATTR = 83, XMLNODE = 84, XMLCOMMENT = 85, XMLCDATA = 86, XMLATTR = 83, XMLNODE = 84, XMLCOMMENT = 85, XMLCDATA = 86,
XMLSTARTDOC = 87, XMLTEXT = 88, REGEXP_REPLACE = 89, RPAD = 90, XMLSTARTDOC = 87, XMLTEXT = 88, REGEXP_REPLACE = 89, RPAD = 90,
LPAD = 91, CONCAT_WS = 92, TO_CHAR = 93, TRANSLATE = 94, ORA_HASH = 95, LPAD = 91, CONCAT_WS = 92, TO_CHAR = 93, TRANSLATE = 94, ORA_HASH = 95,
TO_DATE = 96, TO_TIMESTAMP = 97; TO_DATE = 96, TO_TIMESTAMP = 97, ADD_MONTHS = 98;
public static final int CURDATE = 100, CURTIME = 101, DATE_ADD = 102, public static final int CURDATE = 100, CURTIME = 101, DATE_ADD = 102,
DATE_DIFF = 103, DAY_NAME = 104, DAY_OF_MONTH = 105, DATE_DIFF = 103, DAY_NAME = 104, DAY_OF_MONTH = 105,
...@@ -300,6 +300,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -300,6 +300,7 @@ public class Function extends Expression implements FunctionCall {
0, Value.DATE); 0, Value.DATE);
addFunction("TO_DATE", TO_DATE, VAR_ARGS, Value.STRING); addFunction("TO_DATE", TO_DATE, VAR_ARGS, Value.STRING);
addFunction("TO_TIMESTAMP", TO_TIMESTAMP, VAR_ARGS, Value.STRING); addFunction("TO_TIMESTAMP", TO_TIMESTAMP, VAR_ARGS, Value.STRING);
addFunction("ADD_MONTHS", ADD_MONTHS, VAR_ARGS, Value.TIMESTAMP);
// alias for MSSQLServer // alias for MSSQLServer
addFunctionNotDeterministic("GETDATE", CURDATE, addFunctionNotDeterministic("GETDATE", CURDATE,
0, Value.DATE); 0, Value.DATE);
...@@ -1431,6 +1432,9 @@ public class Function extends Expression implements FunctionCall { ...@@ -1431,6 +1432,9 @@ public class Function extends Expression implements FunctionCall {
result = ValueTimestamp.get(ToDate.TO_TIMESTAMP(v0.getString(), result = ValueTimestamp.get(ToDate.TO_TIMESTAMP(v0.getString(),
v1 == null ? null : v1.getString())); v1 == null ? null : v1.getString()));
break; break;
case ADD_MONTHS:
result = ValueTimestamp.get(DateTimeUtils.addMonths(v0.getTimestamp(), v1.getInt()));
break;
case TRANSLATE: { case TRANSLATE: {
String matching = v1.getString(); String matching = v1.getString();
String replacement = v2.getString(); String replacement = v2.getString();
...@@ -2106,6 +2110,10 @@ public class Function extends Expression implements FunctionCall { ...@@ -2106,6 +2110,10 @@ public class Function extends Expression implements FunctionCall {
protected void checkParameterCount(int len) { protected void checkParameterCount(int len) {
int min = 0, max = Integer.MAX_VALUE; int min = 0, max = Integer.MAX_VALUE;
switch (info.type) { switch (info.type) {
case ADD_MONTHS:
min = 2;
max = 2;
break;
case COALESCE: case COALESCE:
case CSVREAD: case CSVREAD:
case LEAST: case LEAST:
......
...@@ -902,4 +902,9 @@ public class DateTimeUtils { ...@@ -902,4 +902,9 @@ public class DateTimeUtils {
return dateValue(y, m + 3, (int) d); return dateValue(y, m + 3, (int) d);
} }
public static Timestamp addMonths(final Timestamp timestamp, final int numberMonths) {
timestamp.setMonth(timestamp.getMonth() + numberMonths);
return timestamp;
}
} }
...@@ -38,7 +38,7 @@ public class ToDate { ...@@ -38,7 +38,7 @@ public class ToDate {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table ToDateTest(id int primary key, start_date datetime, end_date datetime)"); stat.execute("create table ToDateTest(id int primary key, start_date datetime, end_date datetime)");
stat.execute("insert into ToDateTest values(1, TO_DATE('2015-11-13', 'yyyy-MM-DD'), TO_DATE('2015-12-15', 'YYYY-MM-DD'))"); stat.execute("insert into ToDateTest values(1, ADD_MONTHS(TO_DATE('2015-11-13', 'yyyy-MM-DD'), 1), TO_DATE('2015-12-15', 'YYYY-MM-DD'))");
stat.execute("insert into ToDateTest values(2, TO_DATE('2015-12-12 00:00:00', 'yyyy-MM-DD HH24:MI:ss'), TO_DATE('2015-12-16 15:00:00', 'YYYY-MM-DD HH24:MI:ss'))"); stat.execute("insert into ToDateTest values(2, TO_DATE('2015-12-12 00:00:00', 'yyyy-MM-DD HH24:MI:ss'), TO_DATE('2015-12-16 15:00:00', 'YYYY-MM-DD HH24:MI:ss'))");
stat.execute("insert into ToDateTest values(3, TO_DATE('2015-12-12 08:00 A.M.', 'yyyy-MM-DD HH:MI AM'), TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))"); stat.execute("insert into ToDateTest values(3, TO_DATE('2015-12-12 08:00 A.M.', 'yyyy-MM-DD HH:MI AM'), TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))");
stat.execute("insert into ToDateTest values(4, TO_DATE(substr('2015-12-12 08:00 A.M.', 1, 10), 'yyyy-MM-DD'), TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))"); stat.execute("insert into ToDateTest values(4, TO_DATE(substr('2015-12-12 08:00 A.M.', 1, 10), 'yyyy-MM-DD'), TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))");
......
...@@ -49,10 +49,7 @@ import org.h2.store.fs.FileUtils; ...@@ -49,10 +49,7 @@ import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.ap.TestAnnotationProcessor; import org.h2.test.ap.TestAnnotationProcessor;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.IOUtils; import org.h2.util.*;
import org.h2.util.New;
import org.h2.util.StringUtils;
import org.h2.util.ToDate;
import org.h2.value.Value; import org.h2.value.Value;
/** /**
...@@ -78,6 +75,7 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -78,6 +75,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
deleteDb("functions"); deleteDb("functions");
testToDate(); testToDate();
testToDateException(); testToDateException();
testAddMonths();
testDataType(); testDataType();
testVersion(); testVersion();
testFunctionTable(); testFunctionTable();
...@@ -1386,6 +1384,7 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -1386,6 +1384,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
assertEquals(date, ToDate.TO_DATE("02:04 PM", "HH:MI PM")); assertEquals(date, ToDate.TO_DATE("02:04 PM", "HH:MI PM"));
date = new SimpleDateFormat("yyyy-MM-dd").parse("1970-12-12"); date = new SimpleDateFormat("yyyy-MM-dd").parse("1970-12-12");
date.setMonth(curMonth);
assertEquals(date, ToDate.TO_DATE("12", "DD")); assertEquals(date, ToDate.TO_DATE("12", "DD"));
date = new SimpleDateFormat("yyyy-MM-dd").parse("1970-11-12"); date = new SimpleDateFormat("yyyy-MM-dd").parse("1970-11-12");
...@@ -1398,6 +1397,22 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -1398,6 +1397,22 @@ public class TestFunctions extends TestBase implements AggregateFunction {
assertEquals(date, ToDate.TO_DATE("113029", "J")); assertEquals(date, ToDate.TO_DATE("113029", "J"));
} }
private void testAddMonths() throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-01-29");
Date expected = new SimpleDateFormat("yyyy-MM-dd").parse("2013-02-29");
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 1));
expected = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-29");
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 12));
expected = new SimpleDateFormat("yyyy-MM-dd").parse("2012-11-29");
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), -2));
expected = new SimpleDateFormat("yyyy-MM-dd").parse("2012-01-29");
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), -12));
}
private void testToCharFromDateTime() throws SQLException { private void testToCharFromDateTime() throws SQLException {
deleteDb("functions"); deleteDb("functions");
Connection conn = getConnection("functions"); Connection conn = getConnection("functions");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论