KickJava   Java API By Example, From Geeks To Geeks.

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


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.FromItem;
32 import com.caucho.amber.query.QueryParser;
33 import com.caucho.amber.table.ForeignColumn;
34 import com.caucho.util.CharBuffer;
35
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * Unary expression.
40  */

41 public class UnaryExpr extends AbstractAmberExpr {
42   private AmberExpr _expr;
43   private int _token;
44
45   /**
46    * Creates a new unary expression
47    */

48   public UnaryExpr(int token, AmberExpr expr)
49   {
50     _token = token;
51     _expr = expr;
52   }
53
54   /**
55    * Returns true for a boolean expr.
56    */

57   public boolean isBoolean()
58   {
59     return (_token == QueryParser.NOT ||
60             _token == QueryParser.NULL ||
61             _token == QueryParser.NOT_NULL);
62   }
63
64   /**
65    * Returns the java type.
66    */

67   public Class JavaDoc getJavaType()
68   {
69     switch (_token) {
70     case QueryParser.NOT:
71     case QueryParser.NULL:
72       return boolean.class;
73     default:
74       return double.class;
75     }
76   }
77
78   /**
79    * Binds the expression as a select item.
80    */

81   public AmberExpr bindSelect(QueryParser parser)
82   {
83     _expr = _expr.bindSelect(parser);
84
85     return this;
86   }
87
88   /**
89    * Returns true if the expression uses the from item.
90    */

91   public boolean usesFrom(FromItem from, int type, boolean isNot)
92   {
93     switch (_token) {
94     case '-':
95     case '+':
96     case QueryParser.NULL:
97     case QueryParser.NOT_NULL:
98       return _expr.usesFrom(from, type);
99
100     case QueryParser.NOT:
101       return _expr.usesFrom(from, type, ! isNot);
102
103     default:
104       return false;
105     }
106   }
107
108   /**
109    * Generates the where expression.
110    */

111   public void generateWhere(CharBuffer cb)
112   {
113     generateInternalWhere(cb, true);
114   }
115
116   /**
117    * Generates the (update) where expression.
118    */

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

127   public void generateHaving(CharBuffer cb)
128   {
129     generateWhere(cb);
130   }
131
132   public String JavaDoc toString()
133   {
134     String JavaDoc str = null;
135
136     switch (_token) {
137     case '-':
138       str = "-";
139       break;
140     case '+':
141       str = "+";
142       break;
143     case QueryParser.NOT:
144       str = "not";
145       break;
146     case QueryParser.NULL:
147       return _expr + " is null";
148     case QueryParser.NOT_NULL:
149       return _expr + " is not null";
150     }
151
152     return str + " " + _expr;
153   }
154
155   //
156
// private
157

158   private void generateInternalWhere(CharBuffer cb,
159                                      boolean select)
160   {
161     switch (_token) {
162     case '-':
163       cb.append(" -");
164       break;
165     case '+':
166       cb.append(" +");
167       break;
168     case QueryParser.NOT:
169       cb.append("NOT ");
170       break;
171     case QueryParser.NULL:
172     case QueryParser.NOT_NULL:
173       if (_expr instanceof ManyToOneExpr) {
174         ManyToOneExpr path = (ManyToOneExpr) _expr;
175         cb.append('(');
176
177         ArrayList JavaDoc<ForeignColumn> keys = path.getLinkColumns().getColumns();
178         for (int i = 0; i < keys.size(); i++) {
179           if (i != 0)
180             cb.append(" and ");
181
182           cb.append(path.getFromItem().getName());
183           cb.append(".");
184           cb.append(keys.get(i).getName());
185
186           if (_token == QueryParser.NULL)
187             cb.append(" is null");
188           else
189             cb.append(" is not null");
190         }
191         cb.append(')');
192       }
193       else {
194
195         if (select)
196           _expr.generateWhere(cb);
197         else
198           _expr.generateUpdateWhere(cb);
199
200         if (_token == QueryParser.NULL)
201           cb.append(" is null");
202         else
203           cb.append(" is not null");
204       }
205       return;
206     }
207
208     if (select)
209       _expr.generateWhere(cb);
210     else
211       _expr.generateUpdateWhere(cb);
212   }
213 }
214
Popular Tags