oracle.toplink.essentials.internal.parsing.DateFunctionNode Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2007, Oracle. All rights reserved.
package oracle.toplink.essentials.internal.parsing;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
import oracle.toplink.essentials.queryframework.ReportQuery;
import oracle.toplink.essentials.expressions.*;
/**
* INTERNAL
* Purpose: Represent a date function: CURRENT_DATE, CURRENT_TIME,
* CURRENT_TIMESTAMP.
*
Responsibilities:
* - Generate the correct expression for the date function
*
*/
public class DateFunctionNode extends FunctionalExpressionNode {
private Class type;
/**
* DateFunctionNode constructor.
*/
public DateFunctionNode() {
super();
}
/**
* INTERNAL
* Apply this node to the passed query
*/
public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) {
if (theQuery.isReportQuery()){
ReportQuery reportQuery = (ReportQuery)theQuery;
reportQuery.addAttribute("date", generateExpression(context), type);
}
}
/**
* INTERNAL
* Validate node and calculate its type.
*/
public void validate(ParseTreeContext context) {
setType(type);
}
/**
* INTERNAL
* Generate the TopLink expression for this node
*/
public Expression generateExpression(GenerationContext context) {
Expression expr = context.getBaseExpression();
if (expr == null) {
expr = new ExpressionBuilder();
}
Expression result = null;
if (type == Date.class) {
result = expr.currentDateDate();
} else if (type == Time.class) {
result = expr.currentTime();
} else if (type == Timestamp.class) {
result = expr.currentDate();
}
return result;
}
/** */
public void useCurrentDate() {
type = Date.class;
}
/** */
public void useCurrentTime() {
type = Time.class;
}
/** */
public void useCurrentTimestamp() {
type = Timestamp.class;
}
}