KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > LiteralExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.expr;
30
31 import com.caucho.amber.query.QueryParser;
32 import com.caucho.util.CharBuffer;
33
34 /**
35  * Literal expression for Amber.
36  */

37 public class LiteralExpr extends AbstractAmberExpr {
38   private QueryParser _parser;
39
40   // literal value
41
private String JavaDoc _value;
42
43   private Class JavaDoc _javaType;
44
45   /**
46    * Creates a new literal expression.
47    *
48    * @param value the string value of the literal
49    * @param type the java type of the literal
50    */

51   public LiteralExpr(QueryParser parser,
52                      String JavaDoc value,
53                      Class JavaDoc javaType)
54   {
55     _parser = parser;
56     _value = value;
57     _javaType = javaType;
58   }
59
60   /**
61    * Returns the java type
62    */

63   public Class JavaDoc getJavaType()
64   {
65     return _javaType;
66   }
67
68   /**
69    * Returns the literal value
70    */

71   public String JavaDoc getValue()
72   {
73     return _value;
74   }
75
76   /**
77    * Binds the expression as a select item.
78    */

79   public AmberExpr bindSelect(QueryParser parser)
80   {
81     return this;
82   }
83
84   /**
85    * Returns true if the expression must exist
86    */

87   @Override JavaDoc
88   public boolean exists()
89   {
90     return true;
91   }
92
93   /**
94    * Generates the literal.
95    */

96   public void generateWhere(CharBuffer cb)
97   {
98     if ((_javaType != null) && _javaType.equals(boolean.class)) {
99       if (! _parser.isPostgresDBMS()) {
100
101         // Derby and MySql.
102

103         if (_value.equalsIgnoreCase("false"))
104           cb.append("0");
105         else
106           cb.append("1");
107
108         return;
109       }
110     }
111
112     cb.append(_value);
113   }
114
115   /**
116    * Generates the (update) literal.
117    */

118   public void generateUpdateWhere(CharBuffer cb)
119   {
120     generateWhere(cb);
121   }
122
123   /**
124    * Generates the having expression.
125    */

126   public void generateHaving(CharBuffer cb)
127   {
128     generateWhere(cb);
129   }
130
131   public String JavaDoc toString()
132   {
133     return _value;
134   }
135 }
136
Popular Tags