KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2004 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.query;
31
32 import com.caucho.util.CharBuffer;
33
34 import com.caucho.amber.table.Column;
35 import com.caucho.amber.table.LinkColumns;
36
37 /**
38  * Bound identifier expression.
39  */

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

50   public ColumnExpr(PathExpr parent, Column column)
51   {
52     _parent = parent;
53     _column = column;
54   }
55
56   /**
57    * Returns the parent.
58    */

59   PathExpr getParent()
60   {
61     return _parent;
62   }
63
64   /**
65    * Returns the name.
66    */

67   Column getColumn()
68   {
69     return _column;
70   }
71
72   /**
73    * Returns a boolean expression.
74    */

75   public AmberExpr createBoolean()
76     throws QueryParseException
77   {
78     if (getColumn().getType().isBoolean())
79       return new BooleanColumnExpr(_parent, _column);
80     else
81       return super.createBoolean();
82   }
83
84   /**
85    * Binds the expression as a select item.
86    */

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

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

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

129   public void generateWhere(CharBuffer cb)
130   {
131     if (_fromItem != null) {
132       cb.append(_fromItem.getName());
133       cb.append('.');
134       cb.append(_column.getName());
135     }
136     else {
137       cb.append(_parent.getChildFromItem().getName());
138       cb.append('.');
139       cb.append(_column.getName());
140     }
141   }
142
143   public String toString()
144   {
145     return _parent + "." + _column.getName();
146   }
147 }
148
Popular Tags