KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > 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  * 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
36 import java.sql.SQLException JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 class ColumnExpr extends Expr {
40   private static final Logger JavaDoc log = Log.open(ColumnExpr.class);
41
42   private Table _table;
43   
44   private int _tableIndex;
45   private Column _column;
46   private int _columnIndex;
47
48   private String JavaDoc _name;
49   private Class JavaDoc _type;
50
51   ColumnExpr(String JavaDoc name, Table table, int tableIndex,
52          int columnIndex, Class JavaDoc type)
53   {
54     _name = name;
55     _table = table;
56     _tableIndex = tableIndex;
57     _columnIndex = columnIndex;
58     _column = table.getColumns()[_columnIndex];
59     _type = type;
60   }
61
62   public Class JavaDoc getType()
63   {
64     return _type;
65   }
66
67   /**
68    * Returns any column name.
69    */

70   public String JavaDoc getName()
71   {
72     return _name;
73   }
74
75   /**
76    * Returns the column.
77    */

78   public Column getColumn()
79   {
80     return _column;
81   }
82
83   /**
84    * Returns the column's table.
85    */

86   public Table getTable()
87   {
88     return _table;
89   }
90
91   /**
92    * Returns true if the expression is null.
93    */

94   public boolean isNull(QueryContext context)
95     throws SQLException JavaDoc
96   {
97     TableIterator []rows = context.getTableIterators();
98     TableIterator row = rows[_tableIndex];
99
100     return row.isNull(_column);
101   }
102
103   /**
104    * Evaluates the expression as a string.
105    */

106   public String JavaDoc evalString(QueryContext context)
107     throws SQLException JavaDoc
108   {
109     TableIterator []rows = context.getTableIterators();
110     TableIterator row = rows[_tableIndex];
111
112     return row.getString(_column);
113   }
114
115   public int evalInt(QueryContext context)
116     throws SQLException JavaDoc
117   {
118     TableIterator []rows = context.getTableIterators();
119     TableIterator row = rows[_tableIndex];
120
121     return row.getInteger(_column);
122   }
123
124   public long evalLong(QueryContext context)
125     throws SQLException JavaDoc
126   {
127     TableIterator []rows = context.getTableIterators();
128     TableIterator row = rows[_tableIndex];
129
130     return row.getLong(_column);
131   }
132
133   public double evalDouble(QueryContext context)
134     throws SQLException JavaDoc
135   {
136     TableIterator []rows = context.getTableIterators();
137     TableIterator row = rows[_tableIndex];
138
139     return row.getDouble(_column);
140   }
141
142   /**
143    * Evaluates the expression, writing to the result stream.
144    *
145    * @param context the query context
146    * @param result the output result
147    */

148   public void evalToResult(QueryContext context, SelectResult result)
149     throws SQLException JavaDoc
150   {
151     TableIterator []rows = context.getTableIterators();
152     TableIterator row = rows[_tableIndex];
153
154     row.evalToResult(_column, result);
155   }
156
157   public boolean evalEqual(QueryContext context, byte []matchBuffer)
158     throws SQLException JavaDoc
159   {
160     TableIterator []rows = context.getTableIterators();
161     TableIterator row = rows[_tableIndex];
162
163     return row.isEqual(_column, matchBuffer);
164   }
165
166   public boolean evalEqual(QueryContext context, String JavaDoc string)
167     throws SQLException JavaDoc
168   {
169     TableIterator []rows = context.getTableIterators();
170     TableIterator row = rows[_tableIndex];
171
172     return row.isEqual(_column, string);
173   }
174
175   public boolean equals(Object JavaDoc o)
176   {
177     if (o == null || ! ColumnExpr.class.equals(o.getClass()))
178       return false;
179
180     ColumnExpr expr = (ColumnExpr) o;
181
182     return (_tableIndex == expr._tableIndex &&
183         _column == expr._column);
184   }
185
186   public String JavaDoc toString()
187   {
188     return "ColumnExpr[" + _tableIndex + "," + _columnIndex + "]";
189   }
190 }
191
Popular Tags