001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedInputStream;
042: import java.io.BufferedOutputStream;
043: import java.io.ByteArrayInputStream;
044: import java.io.ByteArrayOutputStream;
045: import java.io.Externalizable;
046: import java.io.InputStream;
047: import java.io.IOException;
048: import java.io.ObjectInput;
049: import java.io.ObjectOutput;
050: import java.io.OutputStream;
051:
052: import java.util.Enumeration;
053: import java.util.Vector;
054:
055: import java.sql.SQLException;
056:
057: import com.quadcap.sql.file.ByteUtil;
058:
059: import com.quadcap.util.Debug;
060: import com.quadcap.util.Util;
061:
062: /**
063: * Cursor implementing the <b>ORDER BY</b> clause, uses a temporary table
064: * with the order-by columns as the key, then iterates the table's index.
065: *
066: * @author Stan Bailes
067: */
068: public class OrderByCursor extends BC_Cursor {
069: TempTable tempTable;
070: int[] kcols;
071: int rowCount = 0;
072:
073: public OrderByCursor(Session session, Cursor c, Vector v)
074: throws SQLException, IOException {
075: super (session, c.getName(), null);
076: this .session = session;
077: addColumns(session, c);
078:
079: // key columns
080: this .kcols = new int[v.size()];
081:
082: int kcnt = 0;
083: boolean[] asc = new boolean[v.size() + 1];
084: for (int i = 0; i < v.size(); i++) {
085: OrderElement elem = (OrderElement) v.elementAt(i);
086: asc[i] = elem.isAscending();
087: int num = elem.getColumn();
088: if (num < 0) {
089: String name = elem.getColName();
090: Column col = c.getColumn(name);
091: if (col == null) {
092: //#ifdef DEBUG
093: Debug.println("bad col: " + name + ", c = " + c);
094: //#endif
095: throw new SQLException("Bad column name: " + name);
096: }
097: num = col.getColumn();
098: elem.setColumn(num);
099: }
100: kcols[kcnt++] = num;
101: }
102: asc[v.size()] = true;
103: Key compare = new Key(asc);
104:
105: this .tempTable = new TempTable(session, compare, kcols);
106: tempTable.addRows(c);
107: c.close();
108: this .row = new LazyRow(c.getColumnCount());
109: beforeFirst();
110: }
111:
112: public void checkCursor() throws SQLException, IOException {
113: if (bc == null) {
114: bc = tempTable.getCursor();
115: }
116: }
117:
118: public long getCurrentRowId() throws SQLException {
119: return ByteUtil.getLong(bc.getValBuf(), 1);
120: }
121:
122: public void fetchCurrentRow() throws SQLException, IOException {
123: tempTable.getRow(bc.getValBuf(), row);
124: rowValid = true;
125: }
126:
127: public void close() throws SQLException {
128: try {
129: super .close();
130: } finally {
131: try {
132: if (tempTable != null)
133: tempTable.release();
134: } catch (IOException ex) {
135: throw DbException.wrapThrowable(ex);
136: } finally {
137: tempTable = null;
138: }
139: }
140: }
141: }
|