001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.fun;
030:
031: import com.caucho.db.sql.Expr;
032: import com.caucho.db.sql.FunExpr;
033: import com.caucho.db.sql.QueryContext;
034: import com.caucho.log.Log;
035: import com.caucho.util.Alarm;
036: import com.caucho.util.L10N;
037: import com.caucho.util.QDate;
038:
039: import java.sql.SQLException;
040: import java.util.Date;
041: import java.util.logging.Logger;
042:
043: public class NowExpr extends FunExpr {
044: protected static final L10N L = new L10N(NowExpr.class);
045: private static final Logger log = Log.open(NowExpr.class);
046:
047: protected void addArg(Expr expr) throws SQLException {
048: throw new SQLException(L.l("now() has no arguments"));
049: }
050:
051: /**
052: * Returns the expected result type of the expression.
053: */
054: public Class getType() {
055: return Date.class;
056: }
057:
058: /**
059: * Returns true for a null value.
060: *
061: * @param rows the current tuple being evaluated
062: *
063: * @return true if null
064: */
065: public boolean isNull(QueryContext context) throws SQLException {
066: return false;
067: }
068:
069: /**
070: * Evaluates the expression as a double.
071: *
072: * @param rows the current tuple being evaluated
073: *
074: * @return the double value
075: */
076: public double evalDouble(QueryContext context) throws SQLException {
077: return evalLong(context);
078: }
079:
080: /**
081: * Evaluates the expression as a long.
082: *
083: * @param rows the current tuple being evaluated
084: *
085: * @return the long value
086: */
087: public long evalLong(QueryContext context) throws SQLException {
088: return Alarm.getCurrentTime();
089: }
090:
091: /**
092: * Evaluates the expression as a string.
093: *
094: * @param rows the current tuple being evaluated
095: *
096: * @return the string value
097: */
098: public String evalString(QueryContext context) throws SQLException {
099: return QDate.formatLocal(evalLong(context));
100: }
101: }
|