KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > IdExpr


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

28
29 package com.caucho.db.sql;
30
31 import com.caucho.db.table.Column;
32 import com.caucho.db.table.Table;
33 import com.caucho.db.table.TableIterator;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import java.sql.SQLException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 class IdExpr extends Expr {
42   private static final L10N L = new L10N(IdExpr.class);
43   private static final Logger JavaDoc log = Log.open(IdExpr.class);
44
45   private FromItem _fromItem;
46   private Column _column;
47
48   private int _tableIndex = -1;
49
50   /**
51    * Creates an unbound identifier with just a column name.
52    */

53   IdExpr(FromItem fromItem, Column column)
54   {
55     _fromItem = fromItem;
56     _column = column;
57   }
58
59   public Class JavaDoc getType()
60   {
61     return _column.getJavaType();
62   }
63
64   /**
65    * Returns the column.
66    */

67   public Column getColumn()
68   {
69     return _column;
70   }
71
72   /**
73    * Returns any column name.
74    */

75   public String JavaDoc getName()
76   {
77     return _column.getName();
78   }
79
80   /**
81    * Returns the from item
82    */

83   public FromItem getFromItem()
84   {
85     return _fromItem;
86   }
87
88   /**
89    * Returns the column's table.
90    */

91   public Table getTable()
92   {
93     return _fromItem.getTable();
94   }
95
96   /**
97    * The cost of a match of the expr.
98    */

99   protected long lookupCost(ArrayList JavaDoc<FromItem> fromList)
100   {
101     if (! fromList.contains(_fromItem))
102       return COST_NO_TABLE;
103
104     if (fromList.indexOf(_fromItem) < fromList.size() - 1)
105       return 0;
106       
107     if (_column.isPrimaryKey())
108       return COST_INDEX;
109     else if (_column.isUnique())
110       return COST_UNIQUE;
111     else
112       return COST_SCAN;
113   }
114
115   /**
116    * The cost of a match of the expr.
117    */

118   public long subCost(ArrayList JavaDoc<FromItem> fromList)
119   {
120     if (! fromList.contains(_fromItem))
121       return Integer.MAX_VALUE;
122     
123       
124     return 10 * 100 * 100 * 100;
125   }
126                 
127   protected Expr bind(Query query)
128     throws SQLException JavaDoc
129   {
130     FromItem []fromItems = query.getFromItems();
131
132     for (int i = 0; i < fromItems.length; i++) {
133       if (fromItems[i] == _fromItem)
134     _tableIndex = i;
135     }
136
137     return this;
138   }
139
140   /**
141    * Returns true if the expression is null.
142    */

143   public boolean isNull(QueryContext context)
144     throws SQLException JavaDoc
145   {
146     TableIterator []rows = context.getTableIterators();
147     TableIterator row = rows[_tableIndex];
148
149     return row.isNull(_column);
150   }
151
152   /**
153    * Evaluates the expression as a string.
154    */

155   public String JavaDoc evalString(QueryContext context)
156     throws SQLException JavaDoc
157   {
158     TableIterator []rows = context.getTableIterators();
159     TableIterator row = rows[_tableIndex];
160
161     return row.getString(_column);
162   }
163
164   /**
165    * Evaluates the expression as a boolean.
166    */

167   public int evalBoolean(QueryContext context)
168     throws SQLException JavaDoc
169   {
170     TableIterator []rows = context.getTableIterators();
171     TableIterator row = rows[_tableIndex];
172
173     String JavaDoc value = row.getString(_column);
174
175     if (value == null)
176       return UNKNOWN;
177     else if (value.equals("1"))
178       return TRUE;
179     else if (value.equalsIgnoreCase("t"))
180       return TRUE;
181     else if (value.equalsIgnoreCase("y"))
182       return TRUE;
183     else
184       return FALSE;
185   }
186
187   public int evalInt(QueryContext context)
188     throws SQLException JavaDoc
189   {
190     TableIterator []rows = context.getTableIterators();
191     TableIterator row = rows[_tableIndex];
192
193     return row.getInteger(_column);
194   }
195
196   public long evalLong(QueryContext context)
197     throws SQLException JavaDoc
198   {
199     TableIterator []rows = context.getTableIterators();
200     TableIterator row = rows[_tableIndex];
201
202     return row.getLong(_column);
203   }
204
205   public double evalDouble(QueryContext context)
206     throws SQLException JavaDoc
207   {
208     TableIterator []rows = context.getTableIterators();
209     TableIterator row = rows[_tableIndex];
210
211     return row.getDouble(_column);
212   }
213
214   /**
215    * Evaluates the expression, writing to the result stream.
216    *
217    * @param context the query context
218    * @param result the output result
219    */

220   public void evalToResult(QueryContext context, SelectResult result)
221     throws SQLException JavaDoc
222   {
223     TableIterator []rows = context.getTableIterators();
224     TableIterator row = rows[_tableIndex];
225
226     row.evalToResult(_column, result);
227   }
228
229   public boolean evalEqual(QueryContext context, byte []matchBuffer)
230     throws SQLException JavaDoc
231   {
232     TableIterator []rows = context.getTableIterators();
233     TableIterator row = rows[_tableIndex];
234
235     return row.isEqual(_column, matchBuffer);
236   }
237
238   public boolean evalEqual(QueryContext context, String JavaDoc string)
239     throws SQLException JavaDoc
240   {
241     TableIterator []rows = context.getTableIterators();
242     TableIterator row = rows[_tableIndex];
243
244     return row.isEqual(_column, string);
245   }
246
247   public boolean equals(Object JavaDoc o)
248   {
249     if (o == null || ! IdExpr.class.equals(o.getClass()))
250       return false;
251
252     IdExpr expr = (IdExpr) o;
253
254     return (_fromItem == expr._fromItem &&
255         _column == expr._column);
256   }
257
258   public String JavaDoc toString()
259   {
260     return _fromItem.getName() + "." + _column.getName();
261   }
262 }
263
Popular Tags