return null;
}
int part = ((Number) nodes[0].valueData).intValue();
long units = ((Number) data[1]).longValue();
TimestampData source = (TimestampData) data[2];
IntervalType t;
Object o;
switch (part) {
case Tokens.SQL_TSI_FRAC_SECOND : {
long seconds = units / DTIType.limitNanoseconds;
int nanos = (int) (units % DTIType.limitNanoseconds);
t = Type.SQL_INTERVAL_SECOND_MAX_FRACTION;
o = new IntervalSecondData(seconds, nanos, t);
return dataType.add(source, o, t);
}
case Tokens.SQL_TSI_SECOND :
t = Type.SQL_INTERVAL_SECOND;
o = IntervalSecondData.newIntervalSeconds(units, t);
return dataType.add(source, o, t);
case Tokens.SQL_TSI_MINUTE :
t = Type.SQL_INTERVAL_MINUTE;
o = IntervalSecondData.newIntervalMinute(units, t);
return dataType.add(source, o, t);
case Tokens.SQL_TSI_HOUR :
t = Type.SQL_INTERVAL_HOUR;
o = IntervalSecondData.newIntervalHour(units, t);
return dataType.add(source, o, t);
case Tokens.SQL_TSI_DAY :
t = Type.SQL_INTERVAL_DAY;
o = IntervalSecondData.newIntervalDay(units, t);
return dataType.add(source, o, t);
case Tokens.SQL_TSI_WEEK :
t = Type.SQL_INTERVAL_DAY;
o = IntervalSecondData.newIntervalDay(units * 7, t);
return dataType.add(source, o, t);
case Tokens.SQL_TSI_MONTH :
t = Type.SQL_INTERVAL_MONTH;
o = IntervalMonthData.newIntervalMonth(units, t);
return dataType.add(source, o, t);
case Tokens.SQL_TSI_QUARTER :
t = Type.SQL_INTERVAL_MONTH;
o = IntervalMonthData.newIntervalMonth(units * 3, t);
return dataType.add(source, o, t);
case Tokens.SQL_TSI_YEAR :
t = Type.SQL_INTERVAL_YEAR;
o = IntervalMonthData.newIntervalMonth(units, t);
return dataType.add(source, o, t);
default :
throw Error.runtimeError(ErrorCode.U_S0500,
"FunctionCustom");
}
}
case FUNC_TIMESTAMPDIFF : {
if (data[1] == null || data[2] == null) {
return null;
}
int part = ((Number) nodes[0].valueData).intValue();
TimestampData a = (TimestampData) data[2];
TimestampData b = (TimestampData) data[1];
if (nodes[2].dataType.isDateTimeTypeWithZone()) {
a = (TimestampData) Type.SQL_TIMESTAMP.convertToType(
session, a, Type.SQL_TIMESTAMP_WITH_TIME_ZONE);
}
if (nodes[1].dataType.isDateTimeTypeWithZone()) {
b = (TimestampData) Type.SQL_TIMESTAMP.convertToType(
session, b, Type.SQL_TIMESTAMP_WITH_TIME_ZONE);
}
IntervalType t;
switch (part) {
case Tokens.SQL_TSI_FRAC_SECOND :
t = Type.SQL_INTERVAL_SECOND_MAX_PRECISION;
IntervalSecondData interval =
(IntervalSecondData) t.subtract(a, b, null);
return new Long(
DTIType.limitNanoseconds * interval.getSeconds()
+ interval.getNanos());
case Tokens.SQL_TSI_SECOND :
t = Type.SQL_INTERVAL_SECOND_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b,
null)));
case Tokens.SQL_TSI_MINUTE :
t = Type.SQL_INTERVAL_MINUTE_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b,
null)));
case Tokens.SQL_TSI_HOUR :
t = Type.SQL_INTERVAL_HOUR_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b,
null)));
case Tokens.SQL_TSI_DAY :
t = Type.SQL_INTERVAL_DAY_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b,
null)));
case Tokens.SQL_TSI_WEEK :
t = Type.SQL_INTERVAL_DAY_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b, null))
/ 7);
case Tokens.SQL_TSI_MONTH :
t = Type.SQL_INTERVAL_MONTH_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b,
null)));
case Tokens.SQL_TSI_QUARTER :
t = Type.SQL_INTERVAL_MONTH_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b, null))
/ 3);
case Tokens.SQL_TSI_YEAR :
t = Type.SQL_INTERVAL_YEAR_MAX_PRECISION;
return new Long(t.convertToLong(t.subtract(a, b,
null)));
default :
throw Error.runtimeError(ErrorCode.U_S0500,
"FunctionCustom");
}
}
case FUNC_SECONDS_MIDNIGHT : {
if (data[0] == null) {
return null;
}
}
// $FALL-THROUGH$
case FUNC_TRUNCATE : {
if (data[0] == null || data[1] == null) {
return null;
}
return ((NumberType) dataType).truncate(data[0],
((Number) data[1]).intValue());
}
case FUNC_TO_CHAR : {
if (data[0] == null || data[1] == null) {
return null;
}
SimpleDateFormat format = session.getSimpleDateFormatGMT();
String javaPattern =
HsqlDateTime.toJavaDatePattern((String) data[1]);
try {
format.applyPattern(javaPattern);
} catch (Exception e) {
throw Error.error(ErrorCode.X_22511);
}
Date date =
(Date) ((DateTimeType) nodes[0].dataType)
.convertSQLToJavaGMT(session, data[0]);
return format.format(date);
}
case FUNC_TIMESTAMP : {
boolean unary = nodes[1] == null;
if (data[0] == null) {
return null;
}
if (unary) {
return Type.SQL_TIMESTAMP.convertToType(session, data[0],
nodes[0].dataType);
}
if (data[1] == null) {
return null;
}
TimestampData date =
(TimestampData) Type.SQL_DATE.convertToType(session,
data[0], nodes[0].dataType);
TimeData time = (TimeData) Type.SQL_TIME.convertToType(session,
data[1], nodes[1].dataType);
return new TimestampData(date.getSeconds()
+ time.getSeconds(), time.getNanos());
}
case FUNC_PI :
return Double.valueOf(Math.PI);