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 Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.expr;
030:
031: import com.caucho.amber.query.QueryParser;
032: import com.caucho.util.CharBuffer;
033:
034: /**
035: * Literal expression for Amber.
036: */
037: public class LiteralExpr extends AbstractAmberExpr {
038: private QueryParser _parser;
039:
040: // literal value
041: private String _value;
042:
043: private Class _javaType;
044:
045: /**
046: * Creates a new literal expression.
047: *
048: * @param value the string value of the literal
049: * @param type the java type of the literal
050: */
051: public LiteralExpr(QueryParser parser, String value, Class javaType) {
052: _parser = parser;
053: _value = value;
054: _javaType = javaType;
055: }
056:
057: /**
058: * Returns the java type
059: */
060: public Class getJavaType() {
061: return _javaType;
062: }
063:
064: /**
065: * Returns the literal value
066: */
067: public String getValue() {
068: return _value;
069: }
070:
071: /**
072: * Binds the expression as a select item.
073: */
074: public AmberExpr bindSelect(QueryParser parser) {
075: return this ;
076: }
077:
078: /**
079: * Returns true if the expression must exist
080: */
081: @Override
082: public boolean exists() {
083: return true;
084: }
085:
086: /**
087: * Generates the literal.
088: */
089: public void generateWhere(CharBuffer cb) {
090: if ((_javaType != null) && _javaType.equals(boolean.class)) {
091: if (!_parser.isPostgresDBMS()) {
092:
093: // Derby and MySql.
094:
095: if (_value.equalsIgnoreCase("false"))
096: cb.append("0");
097: else
098: cb.append("1");
099:
100: return;
101: }
102: }
103:
104: cb.append(_value);
105: }
106:
107: /**
108: * Generates the (update) literal.
109: */
110: public void generateUpdateWhere(CharBuffer cb) {
111: generateWhere(cb);
112: }
113:
114: /**
115: * Generates the having expression.
116: */
117: public void generateHaving(CharBuffer cb) {
118: generateWhere(cb);
119: }
120:
121: public String toString() {
122: return _value;
123: }
124: }
|