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 Rodrigo Westrupp
027: */
028:
029: package com.caucho.amber.expr;
030:
031: import com.caucho.amber.query.QueryParser;
032: import com.caucho.amber.type.PrimitiveIntType;
033: import com.caucho.amber.type.Type;
034: import com.caucho.util.CharBuffer;
035:
036: /**
037: * Enum expression for Amber.
038: */
039: public class EnumExpr extends AbstractAmberExpr {
040: private Class _javaType;
041:
042: // enum string value
043: private String _name;
044:
045: // enum integer value
046: private int _ordinal;
047:
048: private boolean _isOrdinal = true;
049:
050: /**
051: * Creates a new enum expression.
052: *
053: * @param javaType the java type of the enum
054: * @param name the string name of the enum value
055: * @param ordinal the integer value of the enum
056: */
057: public EnumExpr(Class javaType, String name, int ordinal) {
058: _javaType = javaType;
059: _name = name;
060: _ordinal = ordinal;
061: }
062:
063: /**
064: * Returns the expr type.
065: */
066: public Type getType() {
067: return PrimitiveIntType.create();
068: }
069:
070: /**
071: * Returns the java type
072: */
073: public Class getJavaType() {
074: return _javaType;
075: }
076:
077: /**
078: * Returns the enum value
079: */
080: public int getOrdinal() {
081: return _ordinal;
082: }
083:
084: /**
085: * Returns true for ordinal
086: */
087: public boolean isOrdinal() {
088: return _isOrdinal;
089: }
090:
091: /**
092: * Sets true for ordinal
093: */
094: public void setOrdinal(boolean isOrdinal) {
095: _isOrdinal = isOrdinal;
096: }
097:
098: /**
099: * Binds the expression as a select item.
100: */
101: public AmberExpr bindSelect(QueryParser parser) {
102: return this ;
103: }
104:
105: /**
106: * Generates the enum.
107: */
108: public void generateWhere(CharBuffer cb) {
109: if (_isOrdinal)
110: cb.append(_ordinal);
111: else {
112: cb.append('\'');
113: cb.append(_name);
114: cb.append('\'');
115: }
116: }
117:
118: /**
119: * Generates the (update) enum.
120: */
121: public void generateUpdateWhere(CharBuffer cb) {
122: generateWhere(cb);
123: }
124:
125: /**
126: * Generates the having expression.
127: */
128: public void generateHaving(CharBuffer cb) {
129: generateWhere(cb);
130: }
131:
132: public String toString() {
133: return "EnumExpr[" + _javaType.getName() + "." + _name + "("
134: + _ordinal + ")]";
135: }
136: }
|