KickJava   Java API By Example, From Geeks To Geeks.

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


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.index.BTree;
32 import com.caucho.db.table.Column;
33 import com.caucho.db.table.TableIterator;
34 import com.caucho.log.Log;
35 import com.caucho.sql.SQLExceptionWrapper;
36
37 import java.io.IOException JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 class IndexExpr extends RowIterateExpr {
42   private static final Logger JavaDoc log = Log.open(IndexExpr.class);
43
44   private IdExpr _columnExpr;
45   private Column _column;
46   private BTree _index;
47   
48   private Expr _expr;
49
50   IndexExpr(IdExpr index, Expr expr)
51   {
52     _expr = expr;
53     _columnExpr = index;
54
55     if (expr == null || index == null)
56       throw new NullPointerException JavaDoc();
57
58     _column = index.getColumn();
59     _index = _column.getIndex();
60
61     if (_index == null)
62       throw new IllegalArgumentException JavaDoc();
63   }
64
65   /**
66    * Binds the expression.
67    */

68   protected Expr bind(Query query)
69     throws SQLException JavaDoc
70   {
71     _expr = _expr.bind(query);
72     
73     return this;
74   }
75
76   /**
77    * Returns true if shifing the child rows will make a difference.
78    */

79   boolean allowChildRowShift(QueryContext context, TableIterator rowIter)
80   {
81     return false;
82   }
83
84   /**
85    * Sets the initial row.
86    */

87   boolean init(QueryContext context, TableIterator rowIter)
88     throws SQLException JavaDoc, IOException JavaDoc
89   {
90     rowIter.init(context);
91
92     // the Query will call initRow immediately after, so the following
93
// call isn't necessary
94
//return initRow(context, rowIter);
95

96     return true;
97   }
98
99   /**
100    * Sets the initial row.
101    */

102   boolean initRow(QueryContext context, TableIterator tableIter)
103     throws SQLException JavaDoc, IOException JavaDoc
104   {
105     long rowAddr = evalIndex(context);
106
107     if (rowAddr == 0)
108       return false;
109
110     context.unlock();
111
112     tableIter.setRow(rowAddr);
113     
114     context.lock();
115
116     return true;
117   }
118
119   /**
120    * Returns the next row.
121    */

122   boolean nextRow(QueryContext context, TableIterator table)
123   {
124     return false;
125   }
126   
127   long evalIndex(QueryContext context)
128     throws SQLException JavaDoc
129   {
130     byte []buffer = context.getBuffer();
131
132     int length = _expr.evalToBuffer(context, buffer, _column.getTypeCode());
133
134     if (length <= 0)
135       return 0;
136
137     try {
138       long index = _index.lookup(buffer, 0, length, context.getTransaction());
139
140       return index;
141     } catch (IOException JavaDoc e) {
142       throw new SQLExceptionWrapper(e);
143     }
144   }
145
146   /**
147    * Returns the next row.
148    */

149   boolean nextBlock(QueryContext context, TableIterator rowIter)
150     throws IOException JavaDoc
151   {
152     return false;
153   }
154
155   public String JavaDoc toString()
156   {
157     return "(" + _columnExpr + " = " + _expr + ")";
158   }
159 }
160
Popular Tags