KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.amber.AmberManager;
38
39 import com.caucho.jdbc.JdbcMetaData;
40
41 /**
42  * Column expression returning a boolean
43  */

44 public class BooleanColumnExpr 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 BooleanColumnExpr(PathExpr parent, Column column)
55   {
56     _parent = parent;
57     _column = column;
58   }
59
60   /**
61    * Returns the parent.
62    */

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

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

79   public boolean isBoolean()
80   {
81     return true;
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     CharBuffer term = new CharBuffer();
132     
133     if (_fromItem != null) {
134       term.append(_fromItem.getName());
135       term.append('.');
136       term.append(_column.getName());
137     }
138     else {
139       term.append(_parent.getChildFromItem().getName());
140       term.append('.');
141       term.append(_column.getName());
142     }
143
144     AmberManager manager = _column.getTable().getAmberManager();
145
146     JdbcMetaData metaData = manager.getMetaData();
147
148     cb.append(metaData.generateBoolean(term.toString()));
149   }
150
151   public String toString()
152   {
153     return _parent + "." + _column.getName();
154   }
155 }
156
Popular Tags