KickJava   Java API By Example, From Geeks To Geeks.

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


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.query.FromItem;
33 import com.caucho.amber.query.QueryParseException;
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.amber.type.Type;
38 import com.caucho.util.CharBuffer;
39
40
41 /**
42  * Bound identifier expression.
43  */

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

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

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

71   public Column getColumn()
72   {
73     return _column;
74   }
75
76   /**
77    * Returns the expr type.
78    */

79   public Type getType()
80   {
81     return getColumn().getType();
82   }
83
84   /**
85    * Returns a boolean expression.
86    */

87   public AmberExpr createBoolean()
88     throws QueryParseException
89   {
90     if (getColumn().getType().isBoolean())
91       return new BooleanColumnExpr(_parent, _column);
92     else
93       return super.createBoolean();
94   }
95
96   /**
97    * Binds the expression as a select item.
98    */

99   public AmberExpr bindSelect(QueryParser parser)
100   {
101     FromItem parentFromItem = _parent.bindSubPath(parser);
102
103     if (parentFromItem.getTable() == getColumn().getTable()) {
104       _fromItem = parentFromItem;
105
106       return this;
107     }
108
109     LinkColumns link = getColumn().getTable().getDependentIdLink();
110
111     if (link == null)
112       return this;
113
114     _fromItem = parser.createDependentFromItem(parentFromItem, link);
115
116     return this;
117   }
118
119   /**
120    * Returns true if the expression uses the from item.
121    */

122   public boolean usesFrom(FromItem from, int type, boolean isNot)
123   {
124     if (_fromItem == from)
125       return true;
126     else if (_fromItem == null && _parent.getChildFromItem() == from)
127       return true;
128     else
129       return false;
130   }
131
132   /**
133    * Replaces linked join to eliminate a table.
134    */

135   public AmberExpr replaceJoin(JoinExpr join)
136   {
137     _parent = (PathExpr) _parent.replaceJoin(join);
138
139     return this;
140   }
141
142   /**
143    * Generates the where expression.
144    */

145   public void generateWhere(CharBuffer cb)
146   {
147     generateInternalWhere(cb, true);
148   }
149
150   /**
151    * Generates the (update) where expression.
152    */

153   public void generateUpdateWhere(CharBuffer cb)
154   {
155     generateInternalWhere(cb, false);
156   }
157
158   /**
159    * Generates the having expression.
160    */

161   public void generateHaving(CharBuffer cb)
162   {
163     generateWhere(cb);
164   }
165
166   public String JavaDoc toString()
167   {
168     return _parent + "." + _column.getName();
169   }
170
171   //
172
// private
173

174   private void generateInternalWhere(CharBuffer cb,
175                                      boolean select)
176   {
177     if (_fromItem != null) {
178
179       if (select) {
180         cb.append(_fromItem.getName());
181         cb.append('.');
182       }
183
184       cb.append(_column.getName());
185     }
186     else {
187
188       if (select) {
189         cb.append(_parent.getChildFromItem().getName());
190         cb.append('.');
191       }
192
193       cb.append(_column.getName());
194     }
195   }
196 }
197
Popular Tags