001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.sql;
030:
031: import com.caucho.db.index.BTree;
032: import com.caucho.db.table.Column;
033: import com.caucho.db.table.TableIterator;
034: import com.caucho.log.Log;
035: import com.caucho.sql.SQLExceptionWrapper;
036:
037: import java.io.IOException;
038: import java.sql.SQLException;
039: import java.util.logging.Logger;
040:
041: class IndexExpr extends RowIterateExpr {
042: private static final Logger log = Log.open(IndexExpr.class);
043:
044: private IdExpr _columnExpr;
045: private Column _column;
046: private BTree _index;
047:
048: private Expr _expr;
049:
050: IndexExpr(IdExpr index, Expr expr) {
051: _expr = expr;
052: _columnExpr = index;
053:
054: if (expr == null || index == null)
055: throw new NullPointerException();
056:
057: _column = index.getColumn();
058: _index = _column.getIndex();
059:
060: if (_index == null)
061: throw new IllegalArgumentException();
062: }
063:
064: /**
065: * Binds the expression.
066: */
067: protected Expr bind(Query query) throws SQLException {
068: _expr = _expr.bind(query);
069:
070: return this ;
071: }
072:
073: /**
074: * Returns true if shifing the child rows will make a difference.
075: */
076: boolean allowChildRowShift(QueryContext context,
077: TableIterator rowIter) {
078: return false;
079: }
080:
081: /**
082: * Sets the initial row.
083: */
084: boolean init(QueryContext context, TableIterator rowIter)
085: throws SQLException, IOException {
086: rowIter.init(context);
087:
088: // the Query will call initRow immediately after, so the following
089: // call isn't necessary
090: //return initRow(context, rowIter);
091:
092: return true;
093: }
094:
095: /**
096: * Sets the initial row.
097: */
098: boolean initRow(QueryContext context, TableIterator tableIter)
099: throws SQLException, IOException {
100: long rowAddr = evalIndex(context);
101:
102: if (rowAddr == 0)
103: return false;
104:
105: context.unlock();
106:
107: tableIter.setRow(rowAddr);
108:
109: context.lock();
110:
111: return true;
112: }
113:
114: /**
115: * Returns the next row.
116: */
117: boolean nextRow(QueryContext context, TableIterator table) {
118: return false;
119: }
120:
121: long evalIndex(QueryContext context) throws SQLException {
122: byte[] buffer = context.getBuffer();
123:
124: int length = _expr.evalToBuffer(context, buffer, _column
125: .getTypeCode());
126:
127: if (length <= 0)
128: return 0;
129:
130: try {
131: long index = _index.lookup(buffer, 0, length, context
132: .getTransaction());
133:
134: //System.out.println("UPDATE: " + index + " " + _expr.evalString(context));
135:
136: return index;
137: } catch (IOException e) {
138: throw new SQLExceptionWrapper(e);
139: }
140: }
141:
142: /**
143: * Returns the next row.
144: */
145: boolean nextBlock(QueryContext context, TableIterator rowIter)
146: throws IOException {
147: return false;
148: }
149:
150: public String toString() {
151: return "(" + _columnExpr + " = " + _expr + ")";
152: }
153: }
|