KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.expr;
31
32 import com.caucho.amber.manager.AmberPersistenceUnit;
33 import com.caucho.amber.query.FromItem;
34 import com.caucho.amber.query.QueryParser;
35 import com.caucho.amber.table.Column;
36 import com.caucho.amber.table.LinkColumns;
37 import com.caucho.jdbc.JdbcMetaData;
38 import com.caucho.util.CharBuffer;
39
40 /**
41  * Column expression returning a boolean
42  */

43 public class BooleanColumnExpr extends AbstractAmberExpr {
44   protected PathExpr _parent;
45   // identifier name value
46
private Column _column;
47
48   protected FromItem _fromItem;
49
50   /**
51    * Creates a new unbound id expression.
52    */

53   public BooleanColumnExpr(PathExpr parent, Column column)
54   {
55     _parent = parent;
56     _column = column;
57   }
58
59   /**
60    * Returns the parent.
61    */

62   PathExpr getParent()
63   {
64     return _parent;
65   }
66
67   /**
68    * Returns the name.
69    */

70   Column getColumn()
71   {
72     return _column;
73   }
74
75   /**
76    * Returns true for a boolean.
77    */

78   public boolean isBoolean()
79   {
80     return true;
81   }
82
83   /**
84    * Binds the expression as a select item.
85    */

86   public AmberExpr bindSelect(QueryParser parser)
87   {
88     FromItem parentFromItem = _parent.bindSubPath(parser);
89
90     if (parentFromItem.getTable() == getColumn().getTable()) {
91       _fromItem = parentFromItem;
92
93       return this;
94     }
95
96     LinkColumns link = getColumn().getTable().getDependentIdLink();
97     _fromItem = parser.createDependentFromItem(parentFromItem, link);
98
99     return this;
100   }
101
102   /**
103    * Returns true if the expression uses the from item.
104    */

105   public boolean usesFrom(FromItem from, int type, boolean isNot)
106   {
107     if (_fromItem == from)
108       return true;
109     else if (_fromItem == null && _parent.getChildFromItem() == from)
110       return true;
111     else
112       return false;
113   }
114
115   /**
116    * Replaces linked join to eliminate a table.
117    */

118   public AmberExpr replaceJoin(JoinExpr join)
119   {
120     _parent = (PathExpr) _parent.replaceJoin(join);
121
122     return this;
123   }
124
125   /**
126    * Generates the where expression.
127    */

128   public void generateWhere(CharBuffer cb)
129   {
130     generateInternalWhere(cb, true);
131   }
132
133   /**
134    * Generates the (update) where expression.
135    */

136   public void generateUpdateWhere(CharBuffer cb)
137   {
138     generateInternalWhere(cb, false);
139   }
140
141   /**
142    * Generates the having expression.
143    */

144   public void generateHaving(CharBuffer cb)
145   {
146     generateWhere(cb);
147   }
148
149   public String JavaDoc toString()
150   {
151     return _parent + "." + _column.getName();
152   }
153
154   //
155
// private
156

157   private void generateInternalWhere(CharBuffer cb,
158                                      boolean select)
159   {
160     CharBuffer term = new CharBuffer();
161
162     if (_fromItem != null) {
163
164       if (select) {
165         term.append(_fromItem.getName());
166         term.append('.');
167       }
168
169       term.append(_column.getName());
170     }
171     else {
172
173       if (select) {
174         term.append(_parent.getChildFromItem().getName());
175         term.append('.');
176       }
177
178       term.append(_column.getName());
179     }
180
181     AmberPersistenceUnit manager = _column.getTable().getAmberManager();
182
183     JdbcMetaData metaData = manager.getMetaData();
184
185     cb.append(metaData.generateBoolean(term.toString()));
186   }
187 }
188
Popular Tags