提交 7fed46d6 authored 作者: Stéphane Eintrazi's avatar Stéphane Eintrazi

add epoch in extract function

上级 bcd56eb9
...@@ -106,7 +106,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -106,7 +106,7 @@ public class Function extends Expression implements FunctionCall {
SECOND = 114, WEEK = 115, YEAR = 116, CURRENT_DATE = 117, SECOND = 114, WEEK = 115, YEAR = 116, CURRENT_DATE = 117,
CURRENT_TIME = 118, CURRENT_TIMESTAMP = 119, EXTRACT = 120, CURRENT_TIME = 118, CURRENT_TIMESTAMP = 119, EXTRACT = 120,
FORMATDATETIME = 121, PARSEDATETIME = 122, ISO_YEAR = 123, FORMATDATETIME = 121, PARSEDATETIME = 122, ISO_YEAR = 123,
ISO_WEEK = 124, ISO_DAY_OF_WEEK = 125; ISO_WEEK = 124, ISO_DAY_OF_WEEK = 125, EPOCH = 126;
public static final int DATABASE = 150, USER = 151, CURRENT_USER = 152, public static final int DATABASE = 150, USER = 151, CURRENT_USER = 152,
IDENTITY = 153, SCOPE_IDENTITY = 154, AUTOCOMMIT = 155, IDENTITY = 153, SCOPE_IDENTITY = 154, AUTOCOMMIT = 155,
...@@ -191,6 +191,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -191,6 +191,7 @@ public class Function extends Expression implements FunctionCall {
DATE_PART.put("S", Calendar.SECOND); DATE_PART.put("S", Calendar.SECOND);
DATE_PART.put("MILLISECOND", Calendar.MILLISECOND); DATE_PART.put("MILLISECOND", Calendar.MILLISECOND);
DATE_PART.put("MS", Calendar.MILLISECOND); DATE_PART.put("MS", Calendar.MILLISECOND);
DATE_PART.put("EPOCH", EPOCH);
// SOUNDEX_INDEX // SOUNDEX_INDEX
String index = "7AEIOUY8HW1BFPV2CGJKQSXZ3DT4L5MN6R"; String index = "7AEIOUY8HW1BFPV2CGJKQSXZ3DT4L5MN6R";
...@@ -837,22 +838,22 @@ public class Function extends Expression implements FunctionCall { ...@@ -837,22 +838,22 @@ public class Function extends Expression implements FunctionCall {
break; break;
} }
case DAY_OF_MONTH: case DAY_OF_MONTH:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.DAY_OF_MONTH)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.DAY_OF_MONTH));
break; break;
case DAY_OF_WEEK: case DAY_OF_WEEK:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.DAY_OF_WEEK)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.DAY_OF_WEEK));
break; break;
case DAY_OF_YEAR: case DAY_OF_YEAR:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.DAY_OF_YEAR)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.DAY_OF_YEAR));
break; break;
case HOUR: case HOUR:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.HOUR_OF_DAY)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.HOUR_OF_DAY));
break; break;
case MINUTE: case MINUTE:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.MINUTE)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.MINUTE));
break; break;
case MONTH: case MONTH:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.MONTH)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.MONTH));
break; break;
case MONTH_NAME: { case MONTH_NAME: {
SimpleDateFormat monthName = new SimpleDateFormat("MMMM", SimpleDateFormat monthName = new SimpleDateFormat("MMMM",
...@@ -862,16 +863,16 @@ public class Function extends Expression implements FunctionCall { ...@@ -862,16 +863,16 @@ public class Function extends Expression implements FunctionCall {
break; break;
} }
case QUARTER: case QUARTER:
result = ValueInt.get((DateTimeUtils.getDatePart(v0, Calendar.MONTH) - 1) / 3 + 1); result = ValueLong.get((DateTimeUtils.getDatePart(v0, Calendar.MONTH) - 1) / 3 + 1);
break; break;
case SECOND: case SECOND:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.SECOND)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.SECOND));
break; break;
case WEEK: case WEEK:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.WEEK_OF_YEAR)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.WEEK_OF_YEAR));
break; break;
case YEAR: case YEAR:
result = ValueInt.get(DateTimeUtils.getDatePart(v0, Calendar.YEAR)); result = ValueLong.get(DateTimeUtils.getDatePart(v0, Calendar.YEAR));
break; break;
case ISO_YEAR: case ISO_YEAR:
result = ValueInt.get(DateTimeUtils.getIsoYear(v0)); result = ValueInt.get(DateTimeUtils.getIsoYear(v0));
...@@ -1513,7 +1514,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -1513,7 +1514,7 @@ public class Function extends Expression implements FunctionCall {
break; break;
case EXTRACT: { case EXTRACT: {
int field = getDatePart(v0.getString()); int field = getDatePart(v0.getString());
result = ValueInt.get(DateTimeUtils.getDatePart(v1, field)); result = ValueLong.get(DateTimeUtils.getDatePart(v1, field));
break; break;
} }
case FORMATDATETIME: { case FORMATDATETIME: {
......
...@@ -16,6 +16,7 @@ import java.util.Locale; ...@@ -16,6 +16,7 @@ import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.expression.Function;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
...@@ -594,8 +595,11 @@ public class DateTimeUtils { ...@@ -594,8 +595,11 @@ public class DateTimeUtils {
* @param field the field type * @param field the field type
* @return the value * @return the value
*/ */
public static int getDatePart(Value date, int field) { public static long getDatePart(Value date, int field) {
Calendar c = valueToCalendar(date); Calendar c = valueToCalendar(date);
if(field == Function.EPOCH) {
return c.getTime().getTime() / 1000;
}
if (field == Calendar.YEAR) { if (field == Calendar.YEAR) {
return getYear(c); return getYear(c);
} }
......
...@@ -374,6 +374,8 @@ select hour('2001-02-03 14:15:16'); ...@@ -374,6 +374,8 @@ select hour('2001-02-03 14:15:16');
> 14; > 14;
select extract(week from timestamp '2001-02-03 14:15:16'); select extract(week from timestamp '2001-02-03 14:15:16');
> 5; > 5;
select extract(epoch from '2001-02-03 14:15:16');
> 981209716;
CREATE TABLE TEST(A int NOT NULL, B int NOT NULL, C int) ; CREATE TABLE TEST(A int NOT NULL, B int NOT NULL, C int) ;
ALTER TABLE TEST ADD CONSTRAINT CON UNIQUE(A,B); ALTER TABLE TEST ADD CONSTRAINT CON UNIQUE(A,B);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论